Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid cache breakage #2402

Merged
merged 4 commits into from Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion settings.gradle.kts
Expand Up @@ -9,7 +9,6 @@ include("deprecatedPluginsTest",
"kotlinTest",
"kotlinReleaseCoroutinesTest",
"android",
"androidTest",
"junit-jupiter",
"junitJupiterExtensionTest",
"junitJupiterInlineMockMakerExtensionTest",
Expand All @@ -19,6 +18,12 @@ include("deprecatedPluginsTest",
"junitJupiterParallelTest",
"osgi-test")

if (System.getenv("ANDROID_SDK_ROOT") != null || File(".local.properties").exists()) {
include("androidTest")
} else {
logger.info("Not including android test project due to missing SDK configuration")
}

rootProject.name = "mockito"

val koltinBuildScriptProject = hashSetOf("junitJupiterExtensionTest", "junitJupiterInlineMockMakerExtensionTest")
Expand Down
Expand Up @@ -44,7 +44,6 @@
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.implementation.attribute.MethodAttributeAppender;
import net.bytebuddy.matcher.ElementMatcher;
import org.mockito.Answers;
import org.mockito.codegen.InjectionBase;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.creation.bytebuddy.ByteBuddyCrossClassLoaderSerializationSupport.CrossClassLoaderSerializableMock;
Expand Down Expand Up @@ -241,21 +240,11 @@ public <T> Class<? extends T> mockClass(MockFeatures<T> features) {
.serialVersionUid(42L)
.defineField("mockitoInterceptor", MockMethodInterceptor.class, PRIVATE)
.implement(MockAccess.class)
.intercept(FieldAccessor.ofBeanProperty());

if (features.defaultAnswer != Answers.CALLS_REAL_METHODS) {
builder =
builder.method(isHashCode())
.intercept(hashCode)
.method(isEquals())
.intercept(equals);
} else {
builder =
builder.method(isHashCode())
.intercept(dispatcher)
.method(isEquals())
.intercept(dispatcher);
}
.intercept(FieldAccessor.ofBeanProperty())
.method(isHashCode())
.intercept(hashCode)
.method(isEquals())
.intercept(equals);
if (features.serializableMode == SerializableMode.ACROSS_CLASSLOADERS) {
builder =
builder.implement(CrossClassLoaderSerializableMock.class)
Expand Down
Expand Up @@ -5,14 +5,12 @@
package org.mockito.internal.creation.bytebuddy;

import java.lang.ref.ReferenceQueue;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import net.bytebuddy.TypeCache;
import org.mockito.mock.SerializableMode;
import org.mockito.stubbing.Answer;

class TypeCachingBytecodeGenerator extends ReferenceQueue<ClassLoader>
implements BytecodeGenerator {
Expand Down Expand Up @@ -45,8 +43,7 @@ public <T> Class<T> mockClass(final MockFeatures<T> params) {
params.mockedType,
params.interfaces,
params.serializableMode,
params.stripAnnotations,
params.defaultAnswer),
params.stripAnnotations),
() -> bytecodeGenerator.mockClass(params),
BOOTSTRAP_LOCK);
} catch (IllegalArgumentException exception) {
Expand Down Expand Up @@ -86,18 +83,15 @@ private static class MockitoMockKey extends TypeCache.SimpleKey {

private final SerializableMode serializableMode;
private final boolean stripAnnotations;
private final Answer defaultAnswer;

private MockitoMockKey(
Class<?> type,
Set<Class<?>> additionalType,
SerializableMode serializableMode,
boolean stripAnnotations,
Answer defaultAnswer) {
boolean stripAnnotations) {
super(type, additionalType);
this.serializableMode = serializableMode;
this.stripAnnotations = stripAnnotations;
this.defaultAnswer = defaultAnswer;
}

@Override
Expand All @@ -113,16 +107,14 @@ public boolean equals(Object object) {
}
MockitoMockKey that = (MockitoMockKey) object;
return stripAnnotations == that.stripAnnotations
&& serializableMode.equals(that.serializableMode)
&& Objects.equals(defaultAnswer, that.defaultAnswer);
&& serializableMode.equals(that.serializableMode);
}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (stripAnnotations ? 1 : 0);
result = 31 * result + serializableMode.hashCode();
result = 31 * result + Objects.hashCode(defaultAnswer);
return result;
}
}
Expand Down
Expand Up @@ -145,7 +145,7 @@ public void ensure_cache_returns_different_instance_serializableMode() throws Ex
}

@Test
public void ensure_cache_returns_different_instance_defaultAnswer() throws Exception {
public void ensure_cache_returns_same_instance_defaultAnswer() throws Exception {
// given
ClassLoader classloader_with_life_shorter_than_cache =
inMemoryClassLoader()
Expand Down Expand Up @@ -174,10 +174,10 @@ public void ensure_cache_returns_different_instance_defaultAnswer() throws Excep
SerializableMode.NONE,
false,
answer));
assertThat(classes.add(klass)).isTrue();
assertThat(classes.add(klass)).isFalse();
}

assertThat(classes).hasSize(answers.length + 1);
assertThat(classes).hasSize(1);
}

@Test
Expand Down

This file was deleted.

This file was deleted.

Expand Up @@ -9,7 +9,6 @@
import static org.junit.Assume.assumeTrue;
import static org.mockito.Mockito.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -60,7 +59,7 @@ public void shouldStub() {

@Test
public void shouldAllowOverridingStubs() {
when(spy.contains(anyObject())).thenReturn(true);
when(spy.contains(any())).thenReturn(true);
when(spy.contains("foo")).thenReturn(false);

assertTrue(spy.contains("bar"));
Expand Down Expand Up @@ -193,13 +192,4 @@ public void shouldSayNiceMessageWhenSpyingOnPrivateClass() throws Exception {
"Most likely it is due to mocking a private class that is not visible to Mockito");
}
}

@Test
public void spysHashCodeEqualsDelegatedToActualMethods() {
List<String> real = new ArrayList<>();
real.add("one");
List<String> spy = spy(real);
assertEquals(real.hashCode(), spy.hashCode());
assertTrue(spy.equals(real));
}
}
Expand Up @@ -5,8 +5,8 @@
package org.mockito.kotlin

import kotlinx.coroutines.runBlocking
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.core.IsEqual
import org.junit.Assert.assertThat
import org.junit.Test
import org.mockito.Mockito.*

Expand Down