From 000d5fbe0b2d3fb59b08bf5992c88a23fc5611ed Mon Sep 17 00:00:00 2001 From: Jan Ouwens Date: Mon, 23 Sep 2024 16:05:19 +0200 Subject: [PATCH] Makes Objenesis dependency explicit --- .../reflection/RecordInstantiatorTest.java | 3 +- .../RecordInstanceCreatorTest.java | 3 +- .../RecordObjectAccessorCopyingTest.java | 8 +++- .../RecordObjectAccessorScramblingTest.java | 3 +- .../vintage/RecordObjectAccessorTest.java | 22 ++++++--- .../ConfiguredEqualsVerifier.java | 6 ++- .../api/SingleTypeEqualsVerifierApi.java | 29 +++++++++--- .../internal/reflection/Instantiator.java | 19 ++++---- .../instantiation/InstanceCreator.java | 45 ++++++++++++++----- .../instantiation/SubjectCreator.java | 42 ++++++----------- .../instantiation/VintageValueProvider.java | 7 ++- .../reflection/vintage/ClassAccessor.java | 16 +++++-- .../vintage/InPlaceObjectAccessor.java | 5 ++- .../reflection/vintage/ObjectAccessor.java | 4 +- .../vintage/RecordObjectAccessor.java | 3 +- .../factories/FallbackFactory.java | 9 +++- .../equalsverifier/internal/util/Context.java | 9 ++-- .../internal/util/ObjenesisWrapper.java | 29 ------------ .../internal/util/PrefabValuesApi.java | 7 ++- .../operational/WithResetCachesTest.java | 20 --------- .../internal/reflection/InstantiatorTest.java | 28 +++++++----- .../AnnotationCacheBuilderTest.java | 3 +- .../instantiation/InstanceCreatorTest.java | 38 +++++++++++++--- .../instantiation/SubjectCreatorTest.java | 10 ++++- .../VintageValueProviderCreatorTest.java | 14 +++--- .../VintageValueProviderTest.java | 13 +++--- .../reflection/vintage/ClassAccessorTest.java | 34 +++++++++----- .../InPlaceObjectAccessorCopyingTest.java | 8 +++- .../InPlaceObjectAccessorScramblingTest.java | 8 +++- .../factories/FallbackFactoryTest.java | 7 ++- .../factories/MapFactoryTest.java | 3 +- .../factories/SimpleGenericFactoryTest.java | 3 +- .../JavaFxFactoryProviderTest.java | 3 +- .../internal/util/FormatterTest.java | 19 +++++--- 34 files changed, 296 insertions(+), 184 deletions(-) delete mode 100644 equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/ObjenesisWrapper.java delete mode 100644 equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/operational/WithResetCachesTest.java diff --git a/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/RecordInstantiatorTest.java b/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/RecordInstantiatorTest.java index 63b3f010a..be0c04dcf 100644 --- a/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/RecordInstantiatorTest.java +++ b/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/RecordInstantiatorTest.java @@ -3,12 +3,13 @@ import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; +import org.objenesis.ObjenesisStd; public class RecordInstantiatorTest { @Test public void instantiateRecord() { - Instantiator instantiator = Instantiator.of(SimpleRecord.class); + Instantiator instantiator = Instantiator.of(SimpleRecord.class, new ObjenesisStd()); Object simpleRecord = instantiator.instantiate(); assertEquals(SimpleRecord.class, simpleRecord.getClass()); } diff --git a/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/RecordInstanceCreatorTest.java b/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/RecordInstanceCreatorTest.java index 1eeac75ad..8bced9fd4 100644 --- a/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/RecordInstanceCreatorTest.java +++ b/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/RecordInstanceCreatorTest.java @@ -7,13 +7,14 @@ import java.util.Map; import nl.jqno.equalsverifier.internal.reflection.ClassProbe; import org.junit.jupiter.api.Test; +import org.objenesis.ObjenesisStd; public class RecordInstanceCreatorTest { @Test public void instanceCreator() throws NoSuchFieldException { ClassProbe probe = new ClassProbe<>(SomeRecord.class); - InstanceCreator sut = new InstanceCreator<>(probe); + InstanceCreator sut = new InstanceCreator<>(probe, new ObjenesisStd()); Field x = SomeRecord.class.getDeclaredField("x"); Field z = SomeRecord.class.getDeclaredField("z"); diff --git a/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordObjectAccessorCopyingTest.java b/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordObjectAccessorCopyingTest.java index bec1a8f20..25c904414 100644 --- a/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordObjectAccessorCopyingTest.java +++ b/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordObjectAccessorCopyingTest.java @@ -8,9 +8,13 @@ import nl.jqno.equalsverifier.internal.reflection.FieldIterable; import nl.jqno.equalsverifier.internal.reflection.Instantiator; import org.junit.jupiter.api.Test; +import org.objenesis.Objenesis; +import org.objenesis.ObjenesisStd; public class RecordObjectAccessorCopyingTest { + private Objenesis objenesis = new ObjenesisStd(); + @Test public void copyHappyPath() { Object original = instantiate(SimpleRecord.class); @@ -41,11 +45,11 @@ private RecordObjectAccessor create(T object) { } private T instantiate(Class type) { - return Instantiator.of(type).instantiate(); + return Instantiator.of(type, objenesis).instantiate(); } private T copyOf(T from) { - return create(from).copy(); + return create(from).copy(objenesis); } record SimpleRecord(int i, String s) {} diff --git a/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordObjectAccessorScramblingTest.java b/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordObjectAccessorScramblingTest.java index 80afbd8e8..830186672 100644 --- a/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordObjectAccessorScramblingTest.java +++ b/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordObjectAccessorScramblingTest.java @@ -12,6 +12,7 @@ import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.objenesis.ObjenesisStd; public class RecordObjectAccessorScramblingTest { @@ -22,7 +23,7 @@ public class RecordObjectAccessorScramblingTest { @BeforeEach public void setup() throws Exception { factoryCache = JavaApiPrefabValues.build(); - valueProvider = new VintageValueProvider(factoryCache); + valueProvider = new VintageValueProvider(factoryCache, new ObjenesisStd()); } @Test diff --git a/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordObjectAccessorTest.java b/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordObjectAccessorTest.java index b09f96b92..dbcbb904f 100644 --- a/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordObjectAccessorTest.java +++ b/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordObjectAccessorTest.java @@ -14,10 +14,13 @@ import nl.jqno.equalsverifier.internal.testhelpers.ExpectedException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.objenesis.Objenesis; +import org.objenesis.ObjenesisStd; public class RecordObjectAccessorTest { private static final LinkedHashSet EMPTY_TYPE_STACK = new LinkedHashSet<>(); + private Objenesis objenesis; private Object recordInstance; @BeforeEach @@ -25,6 +28,7 @@ public void setUp() throws Exception { Constructor constructor = SimpleRecord.class.getDeclaredConstructor(int.class, String.class); constructor.setAccessible(true); + objenesis = new ObjenesisStd(); recordInstance = constructor.newInstance(42, "hello"); } @@ -42,29 +46,35 @@ public void get() { @Test public void fail_whenConstructorThrowsNpe() { - Object instance = Instantiator.of(NpeThrowingConstructorRecord.class).instantiate(); + Object instance = Instantiator + .of(NpeThrowingConstructorRecord.class, objenesis) + .instantiate(); ExpectedException - .when(() -> accessorFor(instance).copy()) + .when(() -> accessorFor(instance).copy(objenesis)) .assertThrows(ReflectionException.class) .assertMessageContains("Record:", "failed to run constructor", "Warning.NULL_FIELDS"); } @Test public void fail_whenConstructorThrowsOnZero() { - Object instance = Instantiator.of(ZeroThrowingConstructorRecord.class).instantiate(); + Object instance = Instantiator + .of(ZeroThrowingConstructorRecord.class, objenesis) + .instantiate(); ExpectedException - .when(() -> accessorFor(instance).copy()) + .when(() -> accessorFor(instance).copy(objenesis)) .assertThrows(ReflectionException.class) .assertMessageContains("Record:", "failed to run constructor", "Warning.ZERO_FIELDS"); } @Test public void fail_whenConstructorThrowsOnSomethingElse() { - Object instance = Instantiator.of(OtherThrowingConstructorRecord.class).instantiate(); + Object instance = Instantiator + .of(OtherThrowingConstructorRecord.class, objenesis) + .instantiate(); - VintageValueProvider vp = new VintageValueProvider(JavaApiPrefabValues.build()); + VintageValueProvider vp = new VintageValueProvider(JavaApiPrefabValues.build(), objenesis); ExpectedException .when(() -> accessorFor(instance).scramble(vp, TypeTag.NULL, EMPTY_TYPE_STACK)) .assertThrows(ReflectionException.class) diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/ConfiguredEqualsVerifier.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/ConfiguredEqualsVerifier.java index fb500d9ff..5574cfb43 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/ConfiguredEqualsVerifier.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/ConfiguredEqualsVerifier.java @@ -14,6 +14,8 @@ import nl.jqno.equalsverifier.internal.util.ListBuilders; import nl.jqno.equalsverifier.internal.util.PrefabValuesApi; import nl.jqno.equalsverifier.internal.util.Validations; +import org.objenesis.Objenesis; +import org.objenesis.ObjenesisStd; public final class ConfiguredEqualsVerifier implements EqualsVerifierApi { @@ -21,6 +23,7 @@ public final class ConfiguredEqualsVerifier implements EqualsVerifierApi { private final FactoryCache factoryCache; private boolean usingGetClass; private Function fieldnameToGetter; + private final Objenesis objenesis = new ObjenesisStd(); /** Constructor. */ public ConfiguredEqualsVerifier() { @@ -64,7 +67,7 @@ public ConfiguredEqualsVerifier suppress(Warning... warnings) { /** {@inheritDoc} */ @Override public ConfiguredEqualsVerifier withPrefabValues(Class otherType, S red, S blue) { - PrefabValuesApi.addPrefabValues(factoryCache, otherType, red, blue); + PrefabValuesApi.addPrefabValues(factoryCache, objenesis, otherType, red, blue); return this; } @@ -125,6 +128,7 @@ public SingleTypeEqualsVerifierApi forClass(Class type) { type, EnumSet.copyOf(warningsToSuppress), factoryCache, + objenesis, usingGetClass, fieldnameToGetter ); diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/api/SingleTypeEqualsVerifierApi.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/api/SingleTypeEqualsVerifierApi.java index cd28c31cd..c09b94d68 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/api/SingleTypeEqualsVerifierApi.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/api/SingleTypeEqualsVerifierApi.java @@ -13,6 +13,8 @@ import nl.jqno.equalsverifier.internal.reflection.FieldCache; import nl.jqno.equalsverifier.internal.util.*; import nl.jqno.equalsverifier.internal.util.Formatter; +import org.objenesis.Objenesis; +import org.objenesis.ObjenesisStd; /** * Helps to construct an {@link EqualsVerifier} test with a fluent API. @@ -39,6 +41,7 @@ public class SingleTypeEqualsVerifierApi implements EqualsVerifierApi { private Set ignoredAnnotationClassNames = new HashSet<>(); private List equalExamples = new ArrayList<>(); private List unequalExamples = new ArrayList<>(); + private final Objenesis objenesis; /** * Constructor. @@ -46,8 +49,19 @@ public class SingleTypeEqualsVerifierApi implements EqualsVerifierApi { * @param type The class for which the {@code equals} method should be tested. */ public SingleTypeEqualsVerifierApi(Class type) { + this(type, new ObjenesisStd()); + } + + /** + * Constructor. + * + * @param type The class for which the {@code equals} method should be tested. + * @param objenesis To instantiate non-record classes. + */ + public SingleTypeEqualsVerifierApi(Class type, Objenesis objenesis) { this.type = type; - actualFields = FieldNameExtractor.extractFieldNames(type); + this.actualFields = FieldNameExtractor.extractFieldNames(type); + this.objenesis = objenesis; } /** @@ -56,6 +70,7 @@ public SingleTypeEqualsVerifierApi(Class type) { * @param type The class for which the {@code equals} method should be tested. * @param warningsToSuppress A list of warnings to suppress in {@code EqualsVerifier}. * @param factoryCache Factories that can be used to create values. + * @param objenesis To instantiate non-record classes. * @param usingGetClass Whether {@code getClass} is used in the implementation of the {@code * equals} method, instead of an {@code instanceof} check. * @param converter A function that converts from field name to getter name. @@ -64,10 +79,11 @@ public SingleTypeEqualsVerifierApi( Class type, EnumSet warningsToSuppress, FactoryCache factoryCache, + Objenesis objenesis, boolean usingGetClass, Function converter ) { - this(type); + this(type, objenesis); this.warningsToSuppress = EnumSet.copyOf(warningsToSuppress); this.factoryCache = this.factoryCache.merge(factoryCache); this.usingGetClass = usingGetClass; @@ -83,7 +99,7 @@ public SingleTypeEqualsVerifierApi( List equalExamples, List unequalExamples ) { - this(type); + this(type, new ObjenesisStd()); this.equalExamples = equalExamples; this.unequalExamples = unequalExamples; } @@ -105,7 +121,7 @@ public SingleTypeEqualsVerifierApi suppress(Warning... warnings) { /** {@inheritDoc} */ @Override public SingleTypeEqualsVerifierApi withPrefabValues(Class otherType, S red, S blue) { - PrefabValuesApi.addPrefabValues(factoryCache, otherType, red, blue); + PrefabValuesApi.addPrefabValues(factoryCache, objenesis, otherType, red, blue); return this; } @@ -127,7 +143,7 @@ public SingleTypeEqualsVerifierApi withPrefabValuesForField( S red, S blue ) { - PrefabValuesApi.addPrefabValuesForField(fieldCache, type, fieldName, red, blue); + PrefabValuesApi.addPrefabValuesForField(fieldCache, objenesis, type, fieldName, red, blue); withNonnullFields(fieldName); return this; } @@ -409,14 +425,13 @@ private String buildErrorMessage(String description, boolean showUrl) { } private void performVerification() { - ObjenesisWrapper.reset(); if (type.isEnum() || type.isInterface()) { return; } Validations.validateClassCanBeVerified(type); Configuration config = buildConfig(); - Context context = new Context<>(config, factoryCache, fieldCache); + Context context = new Context<>(config, factoryCache, fieldCache, objenesis); Validations.validateProcessedAnnotations( type, config.getAnnotationCache(), diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/Instantiator.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/Instantiator.java index bd89aa77d..894fd53c2 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/Instantiator.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/Instantiator.java @@ -13,7 +13,7 @@ import net.bytebuddy.dynamic.DynamicType; import net.bytebuddy.dynamic.loading.ClassLoadingStrategy; import net.bytebuddy.dynamic.scaffold.TypeValidation; -import nl.jqno.equalsverifier.internal.util.ObjenesisWrapper; +import org.objenesis.Objenesis; /** * Instantiates objects of a given class. @@ -33,10 +33,12 @@ public final class Instantiator { private static final String FALLBACK_PACKAGE_NAME = getPackageName(Instantiator.class); private final Class type; + private final Objenesis objenesis; /** Private constructor. Call {@link #of(Class)} to instantiate. */ - private Instantiator(Class type) { + private Instantiator(Class type, Objenesis objenesis) { this.type = type; + this.objenesis = objenesis; } /** @@ -44,17 +46,18 @@ private Instantiator(Class type) { * * @param The class on which {@link Instantiator} operates. * @param type The class on which {@link Instantiator} operates. Should be the same as T. + * @param objenesis To instantiate non-record classes. * @return An {@link Instantiator} for {@link #type}. */ - public static Instantiator of(Class type) { + public static Instantiator of(Class type, Objenesis objenesis) { if (SealedTypesHelper.isSealed(type)) { Class concrete = SealedTypesHelper.findInstantiableSubclass(type).get(); - return Instantiator.of(concrete); + return Instantiator.of(concrete, objenesis); } if (Modifier.isAbstract(type.getModifiers())) { - return new Instantiator<>(giveDynamicSubclass(type, "", b -> b)); + return new Instantiator<>(giveDynamicSubclass(type, "", b -> b), objenesis); } - return new Instantiator<>(type); + return new Instantiator<>(type, objenesis); } /** @@ -66,7 +69,7 @@ public static Instantiator of(Class type) { * @return An object of type T. */ public T instantiate() { - return ObjenesisWrapper.getObjenesis().newInstance(type); + return objenesis.newInstance(type); } /** @@ -76,7 +79,7 @@ public T instantiate() { */ public T instantiateAnonymousSubclass() { Class proxyClass = giveDynamicSubclass(type); - return ObjenesisWrapper.getObjenesis().newInstance(proxyClass); + return objenesis.newInstance(proxyClass); } public static Class giveDynamicSubclass(Class superclass) { diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/InstanceCreator.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/InstanceCreator.java index 3b3b8223f..b26966398 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/InstanceCreator.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/InstanceCreator.java @@ -1,36 +1,61 @@ package nl.jqno.equalsverifier.internal.reflection.instantiation; import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.function.BiConsumer; import nl.jqno.equalsverifier.internal.reflection.*; import nl.jqno.equalsverifier.internal.util.PrimitiveMappers; +import org.objenesis.Objenesis; /** * Creates an instance of a class or record. */ -class InstanceCreator { +public class InstanceCreator { private final Class type; private final ClassProbe probe; + private final Objenesis objenesis; - /** Constructor. */ - public InstanceCreator(ClassProbe probe) { + /** + * Constructor. + * + * @param probe Represents the class to instantiate. + * @param objenesis To instantiate non-record classes. + */ + public InstanceCreator(ClassProbe probe, Objenesis objenesis) { this.type = probe.getType(); this.probe = probe; + this.objenesis = objenesis; } /** * Creates an instance of the given type, with its field set to the given values. If no value * is given for a specific field, the field will be set to its default value: null for object * references, 0 for numbers, false for booleans. + * + * @param values Values to assign to the instance's fields. + * @return An instance with assigned values. */ public T instantiate(Map values) { return probe.isRecord() ? createRecordInstance(values) : createClassInstance(values); } + /** + * Creates a new instance with all fields set to the same value as their counterparts from + * {@code original}. + * + * @param original The instance to copy. + * @return A copy of the given original. + */ + public T copy(Object original) { + Map values = new HashMap<>(); + for (Field f : fields(original.getClass())) { + Object value = FieldProbe.of(f).getValue(original); + values.put(f, value); + } + return instantiate(values); + } + private T createRecordInstance(Map values) { List params = new ArrayList<>(); traverseFields(values, (f, v) -> params.add(v)); @@ -39,7 +64,7 @@ private T createRecordInstance(Map values) { } private T createClassInstance(Map values) { - T instance = Instantiator.of(type).instantiate(); + T instance = Instantiator.of(type, objenesis).instantiate(); traverseFields( values, (f, v) -> new FieldMutator(FieldProbe.of(f)).setNewValue(instance, v) @@ -48,7 +73,7 @@ private T createClassInstance(Map values) { } private void traverseFields(Map values, BiConsumer setValue) { - for (Field f : fields()) { + for (Field f : fields(type)) { Object value = values.get(f); if (value == null) { value = PrimitiveMappers.DEFAULT_VALUE_MAPPER.get(f.getType()); @@ -57,7 +82,7 @@ private void traverseFields(Map values, BiConsumer } } - private FieldIterable fields() { - return FieldIterable.ofIgnoringStatic(type); + private FieldIterable fields(Class typeWithFields) { + return FieldIterable.ofIgnoringStatic(typeWithFields); } } diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/SubjectCreator.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/SubjectCreator.java index c239077b2..f77ca14a0 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/SubjectCreator.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/SubjectCreator.java @@ -8,6 +8,7 @@ import nl.jqno.equalsverifier.internal.reflection.*; import nl.jqno.equalsverifier.internal.util.Configuration; import nl.jqno.equalsverifier.internal.util.Rethrow; +import org.objenesis.Objenesis; /** * Creates a subject, i.e. an instance of the class that is currently being tested by @@ -21,6 +22,7 @@ public class SubjectCreator { private final ValueProvider valueProvider; private final ClassProbe classProbe; private final FieldCache fieldCache; + private final Objenesis objenesis; /** * Constructor. @@ -28,12 +30,14 @@ public class SubjectCreator { * @param config A configuration object. * @param valueProvider To provide values for the fields of the subject. * @param fieldCache Prepared values for the fields of the subject. + * @param objenesis Needed by InstanceCreator to instantiate non-record classes. */ @SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "A cache is inherently mutable") public SubjectCreator( Configuration config, ValueProvider valueProvider, - FieldCache fieldCache + FieldCache fieldCache, + Objenesis objenesis ) { this.typeTag = config.getTypeTag(); this.type = typeTag.getType(); @@ -41,6 +45,7 @@ public SubjectCreator( this.valueProvider = valueProvider; this.classProbe = new ClassProbe<>(type); this.fieldCache = fieldCache; + this.objenesis = objenesis; } /** @@ -160,12 +165,8 @@ public T withAllFieldsShallowlyChanged() { * @return A copy of the given original. */ public T copy(T original) { - Map values = empty(); - for (Field f : fields()) { - Object value = FieldProbe.of(f).getValue(original); - values.put(f, value); - } - return createInstance(values); + InstanceCreator instanceCreator = new InstanceCreator<>(classProbe, objenesis); + return Rethrow.rethrow(() -> instanceCreator.copy(original)); } /** @@ -176,16 +177,11 @@ public T copy(T original) { * @return An instance of the givenoriginal's superclass, but otherwise a copy of the original. */ public Object copyIntoSuperclass(T original) { - Map values = empty(); - for (Field f : superFields()) { - Object value = FieldProbe.of(f).getValue(original); - values.put(f, value); - } - InstanceCreator superCreator = new InstanceCreator<>( - new ClassProbe<>(type.getSuperclass()) + new ClassProbe<>(type.getSuperclass()), + objenesis ); - return superCreator.instantiate(values); + return superCreator.copy(original); } /** @@ -200,19 +196,13 @@ public Object copyIntoSuperclass(T original) { * @return An instance of the given subType, but otherwise a copy of the given original. */ public S copyIntoSubclass(T original, Class subType) { - Map values = empty(); - for (Field f : fields()) { - Object value = FieldProbe.of(f).getValue(original); - values.put(f, value); - } - - InstanceCreator subCreator = new InstanceCreator<>(new ClassProbe<>(subType)); - return subCreator.instantiate(values); + InstanceCreator subCreator = new InstanceCreator<>(new ClassProbe<>(subType), objenesis); + return subCreator.copy(original); } private T createInstance(Map givens) { Map values = determineValues(givens); - InstanceCreator instanceCreator = new InstanceCreator<>(classProbe); + InstanceCreator instanceCreator = new InstanceCreator<>(classProbe, objenesis); return Rethrow.rethrow(() -> instanceCreator.instantiate(values)); } @@ -244,10 +234,6 @@ private FieldIterable fields() { return FieldIterable.ofIgnoringStatic(type); } - private FieldIterable superFields() { - return FieldIterable.ofIgnoringStatic(type.getSuperclass()); - } - private FieldIterable nonSuperFields() { return FieldIterable.ofIgnoringSuperAndStatic(type); } diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/VintageValueProvider.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/VintageValueProvider.java index c1dea1ac9..4af2fbd1b 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/VintageValueProvider.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/instantiation/VintageValueProvider.java @@ -11,6 +11,7 @@ import nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.PrefabValueFactory; import nl.jqno.equalsverifier.internal.util.PrimitiveMappers; import nl.jqno.equalsverifier.internal.util.Rethrow; +import org.objenesis.Objenesis; /** * Creator of prefabricated instances of classes, using a "vintage" strategy for doing so. @@ -25,16 +26,18 @@ public class VintageValueProvider implements ValueProvider { private final Map> valueCache = new HashMap<>(); private final FactoryCache factoryCache; - private final PrefabValueFactory fallbackFactory = new FallbackFactory<>(); + private final PrefabValueFactory fallbackFactory; /** * Constructor. * * @param factoryCache The factories that can be used to create values. + * @param objenesis To instantiate non-record classes. */ @SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "A cache is inherently mutable.") - public VintageValueProvider(FactoryCache factoryCache) { + public VintageValueProvider(FactoryCache factoryCache, Objenesis objenesis) { this.factoryCache = factoryCache; + this.fallbackFactory = new FallbackFactory<>(objenesis); } /** {@inheritDoc} */ diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/ClassAccessor.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/ClassAccessor.java index 387f8fe7c..e19fde95d 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/ClassAccessor.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/ClassAccessor.java @@ -4,6 +4,7 @@ import nl.jqno.equalsverifier.internal.reflection.Instantiator; import nl.jqno.equalsverifier.internal.reflection.TypeTag; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; +import org.objenesis.Objenesis; /** * Instantiates and populates objects of a given class. {@link ClassAccessor} can create two @@ -16,11 +17,13 @@ public class ClassAccessor { private final Class type; private final VintageValueProvider valueProvider; + private final Objenesis objenesis; /** Private constructor. Call {@link #of(Class, PrefabValues)} instead. */ - ClassAccessor(Class type, VintageValueProvider valueProvider) { + ClassAccessor(Class type, VintageValueProvider valueProvider, Objenesis objenesis) { this.type = type; this.valueProvider = valueProvider; + this.objenesis = objenesis; } /** @@ -29,10 +32,15 @@ public class ClassAccessor { * @param The class on which {@link ClassAccessor} operates. * @param type The class on which {@link ClassAccessor} operates. Should be the same as T. * @param valueProvider Prefabricated values with which to fill instantiated objects. + * @param objenesis To instantiate non-record classes. * @return A {@link ClassAccessor} for T. */ - public static ClassAccessor of(Class type, VintageValueProvider valueProvider) { - return new ClassAccessor<>(type, valueProvider); + public static ClassAccessor of( + Class type, + VintageValueProvider valueProvider, + Objenesis objenesis + ) { + return new ClassAccessor<>(type, valueProvider, objenesis); } /** @@ -94,7 +102,7 @@ public ObjectAccessor getBlueAccessor( } private ObjectAccessor buildObjectAccessor() { - T object = Instantiator.of(type).instantiate(); + T object = Instantiator.of(type, objenesis).instantiate(); return ObjectAccessor.of(object); } } diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/InPlaceObjectAccessor.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/InPlaceObjectAccessor.java index b991efc3f..c9cf1c573 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/InPlaceObjectAccessor.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/InPlaceObjectAccessor.java @@ -6,6 +6,7 @@ import nl.jqno.equalsverifier.internal.reflection.Instantiator; import nl.jqno.equalsverifier.internal.reflection.TypeTag; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; +import org.objenesis.Objenesis; /** * Implementation of ObjectAccessor that modifies its wrapped object in-place through reflection. @@ -21,8 +22,8 @@ final class InPlaceObjectAccessor extends ObjectAccessor { /** {@inheritDoc} */ @Override - public T copy() { - T copy = Instantiator.of(type()).instantiate(); + public T copy(Objenesis objenesis) { + T copy = Instantiator.of(type(), objenesis).instantiate(); for (Field field : FieldIterable.of(type())) { fieldModifierFor(field).copyTo(copy); } diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/ObjectAccessor.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/ObjectAccessor.java index 72b031683..480e30224 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/ObjectAccessor.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/ObjectAccessor.java @@ -4,6 +4,7 @@ import nl.jqno.equalsverifier.internal.reflection.RecordsHelper; import nl.jqno.equalsverifier.internal.reflection.TypeTag; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; +import org.objenesis.Objenesis; /** * Wraps an object to provide access to it. ObjectAccessor can copy and scramble the wrapped object. @@ -73,9 +74,10 @@ public Class type() { * *

Note: it does a "shallow" copy. Reference fields are not copied recursively. * + * @param objenesis Needed to instantiate the copy. * @return A shallow copy. */ - public abstract T copy(); + public abstract T copy(Objenesis objenesis); /** * Modifies all fields of the wrapped object that are declared in T and in its superclasses. It diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordObjectAccessor.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordObjectAccessor.java index 459f50b66..c1f63ea44 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordObjectAccessor.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordObjectAccessor.java @@ -9,6 +9,7 @@ import nl.jqno.equalsverifier.internal.reflection.RecordProbe; import nl.jqno.equalsverifier.internal.reflection.TypeTag; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; +import org.objenesis.Objenesis; /** * Implementation of ObjectAccessor that returns modified copies of its wrapped object, through @@ -29,7 +30,7 @@ final class RecordObjectAccessor extends ObjectAccessor { /** {@inheritDoc} */ @Override - public T copy() { + public T copy(Objenesis objenesis) { List params = probe.fields().map(this::getField).collect(Collectors.toList()); return callRecordConstructor(params); } diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/FallbackFactory.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/FallbackFactory.java index 3c673dc91..eb69ea6b3 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/FallbackFactory.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/FallbackFactory.java @@ -9,6 +9,7 @@ import nl.jqno.equalsverifier.internal.reflection.TypeTag; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; import nl.jqno.equalsverifier.internal.reflection.vintage.ClassAccessor; +import org.objenesis.Objenesis; /** * Implementation of {@link PrefabValueFactory} that instantiates types "by force". @@ -18,6 +19,12 @@ */ public class FallbackFactory implements PrefabValueFactory { + private final Objenesis objenesis; + + public FallbackFactory(Objenesis objenesis) { + this.objenesis = objenesis; + } + @Override public Tuple createValues( TypeTag tag, @@ -95,7 +102,7 @@ private Tuple giveInstances( VintageValueProvider valueProvider, LinkedHashSet typeStack ) { - ClassAccessor accessor = ClassAccessor.of(tag.getType(), valueProvider); + ClassAccessor accessor = ClassAccessor.of(tag.getType(), valueProvider, objenesis); T red = accessor.getRedObject(tag, typeStack); T blue = accessor.getBlueObject(tag, typeStack); T redCopy = accessor.getRedObject(tag, typeStack); diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/Context.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/Context.java index 456b45704..e70f1620d 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/Context.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/Context.java @@ -5,6 +5,7 @@ import nl.jqno.equalsverifier.internal.reflection.instantiation.SubjectCreator; import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider; import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; +import org.objenesis.Objenesis; public final class Context { @@ -23,7 +24,8 @@ public final class Context { public Context( Configuration configuration, FactoryCache factoryCache, - FieldCache fieldCache + FieldCache fieldCache, + Objenesis objenesis ) { this.type = configuration.getType(); this.configuration = configuration; @@ -31,8 +33,9 @@ public Context( this.fieldCache = fieldCache; FactoryCache cache = JavaApiPrefabValues.build().merge(factoryCache); - this.valueProvider = new VintageValueProvider(cache); - this.subjectCreator = new SubjectCreator<>(configuration, valueProvider, fieldCache); + this.valueProvider = new VintageValueProvider(cache, objenesis); + this.subjectCreator = + new SubjectCreator<>(configuration, valueProvider, fieldCache, objenesis); } public Class getType() { diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/ObjenesisWrapper.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/ObjenesisWrapper.java deleted file mode 100644 index 9b4e55942..000000000 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/ObjenesisWrapper.java +++ /dev/null @@ -1,29 +0,0 @@ -package nl.jqno.equalsverifier.internal.util; - -import org.objenesis.Objenesis; -import org.objenesis.ObjenesisStd; - -/** - * A wrapper around Objenesis. Objenesis keeps caches of objects it has instantiated, so we want a - * way to easily re-use the same instance of `Objenesis`. This class reflects the usage in - * {@link org.objenesis.ObjenesisHelper}, but with the added benefit that now we can reset the - * caches if needed (for instance if the test framework used does some "clever" tricks with - * ClassLoaders) by re-initializing the Objenesis instance. - * - * Note: I realise that a wrapper around a static reference is not very architecturally sound; - * however, doing it properly would require major re-writes. Maybe some other time. - */ -public final class ObjenesisWrapper { - - private static Objenesis objenesis = new ObjenesisStd(); - - private ObjenesisWrapper() {} - - public static Objenesis getObjenesis() { - return objenesis; - } - - public static void reset() { - objenesis = new ObjenesisStd(); - } -} diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/PrefabValuesApi.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/PrefabValuesApi.java index 8493682e6..8bc5c9c0c 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/PrefabValuesApi.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/PrefabValuesApi.java @@ -10,6 +10,7 @@ import nl.jqno.equalsverifier.internal.reflection.Tuple; import nl.jqno.equalsverifier.internal.reflection.vintage.ObjectAccessor; import nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.PrefabValueFactory; +import org.objenesis.Objenesis; public final class PrefabValuesApi { @@ -17,6 +18,7 @@ private PrefabValuesApi() {} public static void addPrefabValues( FactoryCache factoryCache, + Objenesis objenesis, Class otherType, T red, T blue @@ -27,7 +29,7 @@ public static void addPrefabValues( factoryCache.put(otherType, values(red, blue, red)); } else { try { - T redCopy = ObjectAccessor.of(red).copy(); + T redCopy = ObjectAccessor.of(red).copy(objenesis); factoryCache.put(otherType, values(red, blue, redCopy)); } catch (RuntimeException ignored) { /* specifically, on Java 9+: InacessibleObjectException */ @@ -39,6 +41,7 @@ public static void addPrefabValues( @SuppressWarnings("unchecked") public static void addPrefabValuesForField( FieldCache fieldCache, + Objenesis objenesis, Class type, String fieldName, T red, @@ -51,7 +54,7 @@ public static void addPrefabValuesForField( fieldCache.put(fieldName, new Tuple<>(red, blue, red)); } else { try { - T redCopy = ObjectAccessor.of(red).copy(); + T redCopy = ObjectAccessor.of(red).copy(objenesis); fieldCache.put(fieldName, new Tuple<>(red, blue, redCopy)); } catch (RuntimeException ignored) { /* specifically, on Java 9+: InacessibleObjectException */ diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/operational/WithResetCachesTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/operational/WithResetCachesTest.java deleted file mode 100644 index 8803c0a40..000000000 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/operational/WithResetCachesTest.java +++ /dev/null @@ -1,20 +0,0 @@ -package nl.jqno.equalsverifier.integration.operational; - -import static org.junit.jupiter.api.Assertions.assertNotEquals; - -import nl.jqno.equalsverifier.EqualsVerifier; -import nl.jqno.equalsverifier.internal.util.ObjenesisWrapper; -import nl.jqno.equalsverifier.testhelpers.packages.correct.A; -import org.junit.jupiter.api.Test; -import org.objenesis.Objenesis; - -public class WithResetCachesTest { - - @Test - public void resetObjenesisCacheOnEachRun() { - Objenesis original = ObjenesisWrapper.getObjenesis(); - EqualsVerifier.forClass(A.class).verify(); - Objenesis reset = ObjenesisWrapper.getObjenesis(); - assertNotEquals(original, reset); - } -} diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/InstantiatorTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/InstantiatorTest.java index 95c1f5376..be7a5357a 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/InstantiatorTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/InstantiatorTest.java @@ -17,51 +17,57 @@ import nl.jqno.equalsverifier.testhelpers.types.TypeHelper.ArrayContainer; import nl.jqno.equalsverifier.testhelpers.types.TypeHelper.Interface; import org.junit.jupiter.api.Test; +import org.objenesis.Objenesis; +import org.objenesis.ObjenesisStd; import org.w3c.dom.Element; public class InstantiatorTest { + private Objenesis objenesis = new ObjenesisStd(); + @Test public void instantiateClass() { - Instantiator instantiator = Instantiator.of(Point.class); + Instantiator instantiator = Instantiator.of(Point.class, objenesis); Point p = instantiator.instantiate(); assertEquals(Point.class, p.getClass()); } @Test public void fieldsOfInstantiatedObjectHaveDefaultValues() { - ColorBlindColorPoint p = Instantiator.of(ColorBlindColorPoint.class).instantiate(); + ColorBlindColorPoint p = Instantiator + .of(ColorBlindColorPoint.class, objenesis) + .instantiate(); assertEquals(0, p.x); assertEquals(null, p.color); } @Test public void instantiateInterface() { - Instantiator instantiator = Instantiator.of(Interface.class); + Instantiator instantiator = Instantiator.of(Interface.class, objenesis); Interface i = instantiator.instantiate(); assertTrue(Interface.class.isAssignableFrom(i.getClass())); } @Test public void instantiateFinalClass() { - Instantiator.of(FinalPoint.class); + Instantiator.of(FinalPoint.class, objenesis); } @Test public void instantiateArrayContainer() { - Instantiator.of(ArrayContainer.class); + Instantiator.of(ArrayContainer.class, objenesis); } @Test public void instantiateAbstractClass() { - Instantiator instantiator = Instantiator.of(AbstractClass.class); + Instantiator instantiator = Instantiator.of(AbstractClass.class, objenesis); AbstractClass ac = instantiator.instantiate(); assertTrue(AbstractClass.class.isAssignableFrom(ac.getClass())); } @Test public void instantiateSubclass() { - Instantiator instantiator = Instantiator.of(Point.class); + Instantiator instantiator = Instantiator.of(Point.class, objenesis); Point p = instantiator.instantiateAnonymousSubclass(); assertFalse(p.getClass() == Point.class); assertTrue(Point.class.isAssignableFrom(p.getClass())); @@ -70,26 +76,26 @@ public void instantiateSubclass() { @Test public void instantiateAnNonToplevelClass() { class Something {} - Instantiator instantiator = Instantiator.of(Something.class); + Instantiator instantiator = Instantiator.of(Something.class, objenesis); instantiator.instantiateAnonymousSubclass(); } @Test @SuppressWarnings("rawtypes") public void instantiateJavaApiClassWhichHasBootstrapClassLoader() { - Instantiator instantiator = Instantiator.of(List.class); + Instantiator instantiator = Instantiator.of(List.class, objenesis); instantiator.instantiateAnonymousSubclass(); } @Test public void instantiateOrgW3cDomClassWhichHasBootstrapClassLoader() { - Instantiator instantiator = Instantiator.of(Element.class); + Instantiator instantiator = Instantiator.of(Element.class, objenesis); instantiator.instantiateAnonymousSubclass(); } @Test public void instantiateTheSameSubclass() { - Instantiator instantiator = Instantiator.of(Point.class); + Instantiator instantiator = Instantiator.of(Point.class, objenesis); Class expected = instantiator.instantiateAnonymousSubclass().getClass(); Class actual = instantiator.instantiateAnonymousSubclass().getClass(); assertEquals(expected, actual); diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/annotations/AnnotationCacheBuilderTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/annotations/AnnotationCacheBuilderTest.java index 20d227c10..6eb8fcce7 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/annotations/AnnotationCacheBuilderTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/annotations/AnnotationCacheBuilderTest.java @@ -20,6 +20,7 @@ import nl.jqno.equalsverifier.testhelpers.types.TypeHelper.*; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.objenesis.ObjenesisStd; public class AnnotationCacheBuilderTest { @@ -407,7 +408,7 @@ public void loadedBySystemClassLoaderDoesNotThrowNullPointerException() { @Test public void dynamicClassDoesntGetProcessed_butDoesntThrowEither() { Class type = Instantiator - .of(AnnotatedWithRuntime.class) + .of(AnnotatedWithRuntime.class, new ObjenesisStd()) .instantiateAnonymousSubclass() .getClass(); build(type); diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/InstanceCreatorTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/InstanceCreatorTest.java index ba7144957..4cd563b6f 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/InstanceCreatorTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/InstanceCreatorTest.java @@ -7,13 +7,16 @@ import java.util.Map; import nl.jqno.equalsverifier.internal.reflection.ClassProbe; import org.junit.jupiter.api.Test; +import org.objenesis.Objenesis; +import org.objenesis.ObjenesisStd; public class InstanceCreatorTest { @Test - public void instanceCreator() throws NoSuchFieldException { + public void instantiate() throws NoSuchFieldException { ClassProbe probe = new ClassProbe<>(SomeClass.class); - InstanceCreator sut = new InstanceCreator<>(probe); + Objenesis objenesis = new ObjenesisStd(); + InstanceCreator sut = new InstanceCreator<>(probe, objenesis); Field x = SomeClass.class.getDeclaredField("x"); Field z = SomeClass.class.getDeclaredField("z"); @@ -28,11 +31,26 @@ public void instanceCreator() throws NoSuchFieldException { assertEquals("42", actual.z); } + @Test + public void copy() throws NoSuchFieldException { + ClassProbe probe = new ClassProbe<>(SomeSubClass.class); + Objenesis objenesis = new ObjenesisStd(); + InstanceCreator sut = new InstanceCreator<>(probe, objenesis); + + SomeClass original = new SomeClass(42, 1337, "yeah"); + SomeSubClass copy = sut.copy(original); + + assertEquals(original.x, copy.x); + assertEquals(original.y, copy.y); + assertEquals(original.z, copy.z); + assertEquals(0, copy.a); + } + static class SomeClass { - private final int x; - private final int y; - private final String z; + final int x; + final int y; + final String z; public SomeClass(int x, int y, String z) { this.x = x; @@ -40,4 +58,14 @@ public SomeClass(int x, int y, String z) { this.z = z; } } + + static class SomeSubClass extends SomeClass { + + final int a; + + public SomeSubClass(int x, int y, String z, int a) { + super(x, y, z); + this.a = a; + } + } } diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/SubjectCreatorTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/SubjectCreatorTest.java index d98d287e7..d9e9ba8cd 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/SubjectCreatorTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/SubjectCreatorTest.java @@ -12,6 +12,8 @@ import nl.jqno.equalsverifier.internal.util.ConfigurationHelper; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.objenesis.Objenesis; +import org.objenesis.ObjenesisStd; public class SubjectCreatorTest { @@ -25,7 +27,13 @@ public class SubjectCreatorTest { ); private ValueProvider valueProvider = new SubjectCreatorTestValueProvider(); private FieldCache fieldCache = new FieldCache(); - private SubjectCreator sut = new SubjectCreator<>(config, valueProvider, fieldCache); + private Objenesis objenesis = new ObjenesisStd(); + private SubjectCreator sut = new SubjectCreator<>( + config, + valueProvider, + fieldCache, + objenesis + ); private Field fieldX; private Field fieldI; diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/VintageValueProviderCreatorTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/VintageValueProviderCreatorTest.java index d95680d10..45bba76bc 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/VintageValueProviderCreatorTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/VintageValueProviderCreatorTest.java @@ -15,6 +15,8 @@ import nl.jqno.equalsverifier.testhelpers.types.TypeHelper.OneElementEnum; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.objenesis.Objenesis; +import org.objenesis.ObjenesisStd; public class VintageValueProviderCreatorTest { @@ -27,13 +29,15 @@ public class VintageValueProviderCreatorTest { private static final TypeTag TWOSTEP_NODE_A_TAG = new TypeTag(TwoStepNodeA.class); private static final TypeTag TWOSTEP_NODE_ARRAY_A_TAG = new TypeTag(TwoStepNodeArrayA.class); + private Objenesis objenesis; private FactoryCache factoryCache; private VintageValueProvider valueProvider; @BeforeEach public void setup() { + objenesis = new ObjenesisStd(); factoryCache = FactoryCacheFactory.withPrimitiveFactories(); - valueProvider = new VintageValueProvider(factoryCache); + valueProvider = new VintageValueProvider(factoryCache, objenesis); } @Test @@ -73,7 +77,7 @@ public void createEmptyEnum() { @Test public void oneStepRecursiveType() { factoryCache.put(Node.class, values(new Node(), new Node(), new Node())); - valueProvider = new VintageValueProvider(factoryCache); + valueProvider = new VintageValueProvider(factoryCache, objenesis); valueProvider.giveRed(NODE_TAG); } @@ -90,7 +94,7 @@ public void oneStepRecursiveArrayType() { NodeArray.class, values(new NodeArray(), new NodeArray(), new NodeArray()) ); - valueProvider = new VintageValueProvider(factoryCache); + valueProvider = new VintageValueProvider(factoryCache, objenesis); valueProvider.giveRed(NODE_ARRAY_TAG); } @@ -107,7 +111,7 @@ public void addTwoStepRecursiveType() { TwoStepNodeB.class, values(new TwoStepNodeB(), new TwoStepNodeB(), new TwoStepNodeB()) ); - valueProvider = new VintageValueProvider(factoryCache); + valueProvider = new VintageValueProvider(factoryCache, objenesis); valueProvider.giveRed(TWOSTEP_NODE_A_TAG); } @@ -124,7 +128,7 @@ public void twoStepRecursiveArrayType() { TwoStepNodeArrayB.class, values(new TwoStepNodeArrayB(), new TwoStepNodeArrayB(), new TwoStepNodeArrayB()) ); - valueProvider = new VintageValueProvider(factoryCache); + valueProvider = new VintageValueProvider(factoryCache, objenesis); valueProvider.giveRed(TWOSTEP_NODE_ARRAY_A_TAG); } diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/VintageValueProviderTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/VintageValueProviderTest.java index 0fb687430..2fa15353e 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/VintageValueProviderTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/VintageValueProviderTest.java @@ -18,6 +18,8 @@ import nl.jqno.equalsverifier.testhelpers.types.ThrowingInitializer; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.objenesis.Objenesis; +import org.objenesis.ObjenesisStd; public class VintageValueProviderTest { @@ -25,6 +27,7 @@ public class VintageValueProviderTest { private static final TypeTag POINT_TAG = new TypeTag(Point.class); private static final TypeTag INT_TAG = new TypeTag(int.class); + private Objenesis objenesis = new ObjenesisStd(); private FactoryCache factoryCache = new FactoryCache(); private VintageValueProvider vp; @@ -32,7 +35,7 @@ public class VintageValueProviderTest { public void setUp() { factoryCache.put(String.class, new AppendingStringTestFactory()); factoryCache.put(int.class, values(42, 1337, 42)); - vp = new VintageValueProvider(factoryCache); + vp = new VintageValueProvider(factoryCache, objenesis); } @Test @@ -113,7 +116,7 @@ public void fallbackDoesNotAffectStaticFields() { @Test public void stringListIsSeparateFromIntegerList() { factoryCache.put(List.class, new ListTestFactory()); - vp = new VintageValueProvider(factoryCache); + vp = new VintageValueProvider(factoryCache, objenesis); List strings = vp.giveRed(new TypeTag(List.class, STRING_TAG)); List ints = vp.giveRed(new TypeTag(List.class, INT_TAG)); @@ -130,7 +133,7 @@ public void addingNullDoesntBreakAnything() { @Test public void addingATypeTwiceOverrulesTheExistingOne() { factoryCache.put(int.class, values(-1, -2, -1)); - vp = new VintageValueProvider(factoryCache); + vp = new VintageValueProvider(factoryCache, objenesis); assertEquals(-1, (int) vp.giveRed(INT_TAG)); assertEquals(-2, (int) vp.giveBlue(INT_TAG)); } @@ -139,7 +142,7 @@ public void addingATypeTwiceOverrulesTheExistingOne() { public void addLazyFactoryWorks() { TypeTag lazyTag = new TypeTag(Lazy.class); factoryCache.put(Lazy.class.getName(), values(Lazy.X, Lazy.Y, Lazy.X)); - vp = new VintageValueProvider(factoryCache); + vp = new VintageValueProvider(factoryCache, objenesis); assertEquals(Lazy.X, vp.giveRed(lazyTag)); assertEquals(Lazy.Y, vp.giveBlue(lazyTag)); assertEquals(Lazy.X, vp.giveRedCopy(lazyTag)); @@ -155,7 +158,7 @@ public void addLazyFactoryIsLazy() { (t, p, ts) -> Tuple.of(ThrowingInitializer.X, ThrowingInitializer.Y, ThrowingInitializer.X) ); - vp = new VintageValueProvider(factoryCache); + vp = new VintageValueProvider(factoryCache, objenesis); // Should throw, because `giveRed` does instantiate objects: try { diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/ClassAccessorTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/ClassAccessorTest.java index 5c588b924..3fbfcb6cb 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/ClassAccessorTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/ClassAccessorTest.java @@ -16,10 +16,13 @@ import nl.jqno.equalsverifier.testhelpers.types.TypeHelper.*; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.objenesis.Objenesis; +import org.objenesis.ObjenesisStd; public class ClassAccessorTest { private LinkedHashSet empty; + private Objenesis objenesis; private FactoryCache factoryCache; private VintageValueProvider valueProvider; private ClassAccessor pointContainerAccessor; @@ -27,9 +30,10 @@ public class ClassAccessorTest { @BeforeEach public void setup() { empty = new LinkedHashSet<>(); + objenesis = new ObjenesisStd(); factoryCache = JavaApiPrefabValues.build(); - valueProvider = new VintageValueProvider(factoryCache); - pointContainerAccessor = ClassAccessor.of(PointContainer.class, valueProvider); + valueProvider = new VintageValueProvider(factoryCache, objenesis); + pointContainerAccessor = ClassAccessor.of(PointContainer.class, valueProvider, objenesis); } @Test @@ -42,7 +46,8 @@ public void getRedObject() { public void getRedObjectGeneric() { ClassAccessor accessor = ClassAccessor.of( GenericTypeVariableListContainer.class, - valueProvider + valueProvider, + objenesis ); GenericTypeVariableListContainer foo = accessor.getRedObject( new TypeTag(GenericTypeVariableListContainer.class, new TypeTag(String.class)), @@ -71,7 +76,8 @@ public void getBlueObject() { public void getBlueObjectGeneric() { ClassAccessor accessor = ClassAccessor.of( GenericTypeVariableListContainer.class, - valueProvider + valueProvider, + objenesis ); GenericTypeVariableListContainer foo = accessor.getBlueObject( new TypeTag(GenericTypeVariableListContainer.class, new TypeTag(String.class)), @@ -99,13 +105,15 @@ public void redAndBlueNotEqual() { @Test public void instantiateAllTypes() { - ClassAccessor.of(AllTypesContainer.class, valueProvider).getRedObject(TypeTag.NULL, empty); + ClassAccessor + .of(AllTypesContainer.class, valueProvider, objenesis) + .getRedObject(TypeTag.NULL, empty); } @Test public void instantiateArrayTypes() { ClassAccessor - .of(AllArrayTypesContainer.class, valueProvider) + .of(AllArrayTypesContainer.class, valueProvider, objenesis) .getRedObject(TypeTag.NULL, empty); } @@ -115,25 +123,29 @@ public void instantiateRecursiveTypeUsingPrefabValue() { TwoStepNodeB.class, values(new TwoStepNodeB(), new TwoStepNodeB(), new TwoStepNodeB()) ); - valueProvider = new VintageValueProvider(factoryCache); - ClassAccessor.of(TwoStepNodeA.class, valueProvider).getRedObject(TypeTag.NULL, empty); + valueProvider = new VintageValueProvider(factoryCache, objenesis); + ClassAccessor + .of(TwoStepNodeA.class, valueProvider, objenesis) + .getRedObject(TypeTag.NULL, empty); } @Test public void instantiateInterfaceField() { - ClassAccessor.of(InterfaceContainer.class, valueProvider).getRedObject(TypeTag.NULL, empty); + ClassAccessor + .of(InterfaceContainer.class, valueProvider, objenesis) + .getRedObject(TypeTag.NULL, empty); } @Test public void instantiateAbstractClassField() { ClassAccessor - .of(AbstractClassContainer.class, valueProvider) + .of(AbstractClassContainer.class, valueProvider, objenesis) .getRedObject(TypeTag.NULL, empty); } @Test public void anInvalidTypeShouldNotThrowAnExceptionUponCreation() { - ClassAccessor.of(null, valueProvider); + ClassAccessor.of(null, valueProvider, objenesis); } private void assertObjectHasNoNullFields(PointContainer foo) { diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/InPlaceObjectAccessorCopyingTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/InPlaceObjectAccessorCopyingTest.java index 60b6c7b1f..a224280df 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/InPlaceObjectAccessorCopyingTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/InPlaceObjectAccessorCopyingTest.java @@ -11,9 +11,13 @@ import nl.jqno.equalsverifier.testhelpers.types.PointContainer; import nl.jqno.equalsverifier.testhelpers.types.TypeHelper.StaticFinalContainer; import org.junit.jupiter.api.Test; +import org.objenesis.Objenesis; +import org.objenesis.ObjenesisStd; public class InPlaceObjectAccessorCopyingTest { + private Objenesis objenesis = new ObjenesisStd(); + @Test public void copyHappyPath() { Point original = new Point(2, 3); @@ -61,11 +65,11 @@ private InPlaceObjectAccessor create(T object) { } private T copyOf(T from) { - return create(from).copy(); + return create(from).copy(objenesis); } private T copyOf(T from, Class type) { - return new InPlaceObjectAccessor(from, type).copy(); + return new InPlaceObjectAccessor(from, type).copy(objenesis); } private static void assertAllFieldsEqual(T original, T copy, Class type) { diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/InPlaceObjectAccessorScramblingTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/InPlaceObjectAccessorScramblingTest.java index 04e7668a0..a6775ab59 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/InPlaceObjectAccessorScramblingTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/InPlaceObjectAccessorScramblingTest.java @@ -20,17 +20,21 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledForJreRange; import org.junit.jupiter.api.condition.JRE; +import org.objenesis.Objenesis; +import org.objenesis.ObjenesisStd; public class InPlaceObjectAccessorScramblingTest { private static final LinkedHashSet EMPTY_TYPE_STACK = new LinkedHashSet<>(); + private Objenesis objenesis; private VintageValueProvider valueProviderTest; @BeforeEach public void setup() { FactoryCache factoryCache = JavaApiPrefabValues.build(); factoryCache.put(Point.class, values(new Point(1, 2), new Point(2, 3), new Point(1, 2))); - valueProviderTest = new VintageValueProvider(factoryCache); + objenesis = new ObjenesisStd(); + valueProviderTest = new VintageValueProvider(factoryCache, objenesis); } @Test @@ -146,7 +150,7 @@ private InPlaceObjectAccessor create(T object) { } private T copy(T object) { - return create(object).copy(); + return create(object).copy(objenesis); } private ObjectAccessor doScramble(Object object) { diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/FallbackFactoryTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/FallbackFactoryTest.java index 2af38cf09..ff91709c4 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/FallbackFactoryTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/FallbackFactoryTest.java @@ -21,6 +21,8 @@ import nl.jqno.equalsverifier.testhelpers.types.TypeHelper.TwoElementEnum; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.objenesis.Objenesis; +import org.objenesis.ObjenesisStd; public class FallbackFactoryTest { @@ -30,10 +32,11 @@ public class FallbackFactoryTest { @BeforeEach public void setUp() { - factory = new FallbackFactory<>(); + Objenesis objenesis = new ObjenesisStd(); + factory = new FallbackFactory<>(objenesis); FactoryCache factoryCache = new FactoryCache(); factoryCache.put(int.class, values(42, 1337, 42)); - valueProvider = new VintageValueProvider(factoryCache); + valueProvider = new VintageValueProvider(factoryCache, objenesis); typeStack = new LinkedHashSet<>(); } diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/MapFactoryTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/MapFactoryTest.java index 5b67f1674..afd23e9d7 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/MapFactoryTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/MapFactoryTest.java @@ -12,6 +12,7 @@ import nl.jqno.equalsverifier.testhelpers.types.TypeHelper.OneElementEnum; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.objenesis.ObjenesisStd; @SuppressWarnings("rawtypes") public class MapFactoryTest { @@ -48,7 +49,7 @@ public class MapFactoryTest { @BeforeEach public void setUp() { - valueProvider = new VintageValueProvider(JavaApiPrefabValues.build()); + valueProvider = new VintageValueProvider(JavaApiPrefabValues.build(), new ObjenesisStd()); red = valueProvider.giveRed(STRING_TYPETAG); blue = valueProvider.giveBlue(STRING_TYPETAG); redObject = valueProvider.giveRed(OBJECT_TYPETAG); diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/SimpleGenericFactoryTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/SimpleGenericFactoryTest.java index e7d69dc49..ff5f32d6e 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/SimpleGenericFactoryTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factories/SimpleGenericFactoryTest.java @@ -11,6 +11,7 @@ import nl.jqno.equalsverifier.testhelpers.types.Pair; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.objenesis.ObjenesisStd; @SuppressWarnings("rawtypes") public class SimpleGenericFactoryTest { @@ -50,7 +51,7 @@ public class SimpleGenericFactoryTest { @BeforeEach public void setUp() { - valueProvider = new VintageValueProvider(JavaApiPrefabValues.build()); + valueProvider = new VintageValueProvider(JavaApiPrefabValues.build(), new ObjenesisStd()); redString = valueProvider.giveRed(STRING_TYPETAG); blueString = valueProvider.giveBlue(STRING_TYPETAG); redInt = valueProvider.giveRed(INTEGER_TYPETAG); diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/JavaFxFactoryProviderTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/JavaFxFactoryProviderTest.java index f3612ccf7..06681caeb 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/JavaFxFactoryProviderTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/prefabvalues/factoryproviders/JavaFxFactoryProviderTest.java @@ -14,6 +14,7 @@ import nl.jqno.equalsverifier.testhelpers.types.Point; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.objenesis.ObjenesisStd; @SuppressWarnings("rawtypes") public class JavaFxFactoryProviderTest { @@ -22,7 +23,7 @@ public class JavaFxFactoryProviderTest { @BeforeEach public void setUp() { - valueProvider = new VintageValueProvider(JavaApiPrefabValues.build()); + valueProvider = new VintageValueProvider(JavaApiPrefabValues.build(), new ObjenesisStd()); } @Test diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/util/FormatterTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/util/FormatterTest.java index 8570afd0b..ac26e1721 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/util/FormatterTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/util/FormatterTest.java @@ -8,9 +8,13 @@ import nl.jqno.equalsverifier.internal.reflection.Instantiator; import nl.jqno.equalsverifier.internal.testhelpers.ExpectedException; import org.junit.jupiter.api.Test; +import org.objenesis.Objenesis; +import org.objenesis.ObjenesisStd; public class FormatterTest { + private final Objenesis objenesis = new ObjenesisStd(); + @Test public void noParameters() { Formatter f = Formatter.of("No parameters"); @@ -69,35 +73,38 @@ public void oneParameterWithNoFieldsAndThrowsWithNullMessage() { @Test public void oneAbstractParameter() { - Instantiator i = Instantiator.of(Abstract.class); + Instantiator i = Instantiator.of(Abstract.class, objenesis); Formatter f = Formatter.of("Abstract: %%", i.instantiate()); assertThat(f.format(), containsString("Abstract: [Abstract x=0]")); } @Test public void oneConcreteSubclassParameter() { - Instantiator i = Instantiator.of(AbstractImpl.class); + Instantiator i = Instantiator.of(AbstractImpl.class, objenesis); Formatter f = Formatter.of("Concrete: %%", i.instantiate()); assertThat(f.format(), containsString("Concrete: something concrete")); } @Test public void oneDelegatedAbstractParameter() { - Instantiator i = Instantiator.of(AbstractDelegation.class); + Instantiator i = Instantiator.of(AbstractDelegation.class, objenesis); Formatter f = Formatter.of("Abstract: %%", i.instantiate()); assertThat(f.format(), containsString("Abstract: [AbstractDelegation y=0]")); } @Test public void oneDelegatedConcreteSubclassParameter() { - Instantiator i = Instantiator.of(AbstractDelegationImpl.class); + Instantiator i = Instantiator.of( + AbstractDelegationImpl.class, + objenesis + ); Formatter f = Formatter.of("Concrete: %%", i.instantiate()); assertThat(f.format(), containsString("Concrete: something concrete")); } @Test public void oneThrowingContainerParameter() { - Instantiator i = Instantiator.of(Throwing.class); + Instantiator i = Instantiator.of(Throwing.class, objenesis); ThrowingContainer tc = new ThrowingContainer(i.instantiate()); Formatter f = Formatter.of("TC: %%", tc); String expected = @@ -107,7 +114,7 @@ public void oneThrowingContainerParameter() { @Test public void oneAbstractContainerParameter() { - Instantiator i = Instantiator.of(AbstractDelegation.class); + Instantiator i = Instantiator.of(AbstractDelegation.class, objenesis); AbstractContainer ac = new AbstractContainer(i.instantiate()); Formatter f = Formatter.of("AC: %%", ac);