diff --git a/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordFallbackFactoryTest.java b/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordFallbackFactoryTest.java new file mode 100644 index 000000000..e1f4ed91e --- /dev/null +++ b/equalsverifier-16/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/RecordFallbackFactoryTest.java @@ -0,0 +1,49 @@ +package nl.jqno.equalsverifier.internal.reflection.vintage; + +import static nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.Factories.values; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotSame; + +import java.util.LinkedHashSet; +import nl.jqno.equalsverifier.internal.reflection.FactoryCache; +import nl.jqno.equalsverifier.internal.reflection.Tuple; +import nl.jqno.equalsverifier.internal.reflection.TypeTag; +import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; +import nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.FallbackFactory; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.objenesis.Objenesis; +import org.objenesis.ObjenesisStd; + +public class RecordFallbackFactoryTest { + + private FallbackFactory factory; + private VintageValueProvider valueProvider; + private LinkedHashSet typeStack; + + @BeforeEach + public void setUp() { + Objenesis objenesis = new ObjenesisStd(); + factory = new FallbackFactory<>(objenesis); + FactoryCache factoryCache = new FactoryCache(); + factoryCache.put(int.class, values(42, 1337, 42)); + valueProvider = new VintageValueProvider(factoryCache, objenesis); + typeStack = new LinkedHashSet<>(); + } + + @Test + public void redCopyHasTheSameValuesAsRed_whenSutContainsGenericValueThatNeedsToBeIdenticalInRedAndRedCopy() { + Tuple tuple = factory.createValues( + new TypeTag(GenericRecordContainer.class), + valueProvider, + typeStack + ); + + assertEquals(tuple.getRed(), tuple.getRedCopy()); + assertNotSame(tuple.getRed(), tuple.getRedCopy()); + } + + record GenericRecord(T t) {} + + record GenericRecordContainer(GenericRecord bgr) {} +} diff --git a/equalsverifier-17/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/SealedTypesFallbackFactoryTest.java b/equalsverifier-17/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/SealedTypesFallbackFactoryTest.java new file mode 100644 index 000000000..0babdd4c1 --- /dev/null +++ b/equalsverifier-17/src/test/java/nl/jqno/equalsverifier/internal/reflection/vintage/SealedTypesFallbackFactoryTest.java @@ -0,0 +1,85 @@ +package nl.jqno.equalsverifier.internal.reflection.vintage; + +import static nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.Factories.values; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotSame; + +import java.util.LinkedHashSet; +import java.util.Objects; +import nl.jqno.equalsverifier.internal.reflection.FactoryCache; +import nl.jqno.equalsverifier.internal.reflection.Tuple; +import nl.jqno.equalsverifier.internal.reflection.TypeTag; +import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider; +import nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.FallbackFactory; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.objenesis.Objenesis; +import org.objenesis.ObjenesisStd; + +public class SealedTypesFallbackFactoryTest { + + private FallbackFactory factory; + private VintageValueProvider valueProvider; + private LinkedHashSet typeStack; + + @BeforeEach + public void setUp() { + Objenesis objenesis = new ObjenesisStd(); + factory = new FallbackFactory<>(objenesis); + FactoryCache factoryCache = new FactoryCache(); + factoryCache.put(int.class, values(42, 1337, 42)); + valueProvider = new VintageValueProvider(factoryCache, objenesis); + typeStack = new LinkedHashSet<>(); + } + + @Test + public void redCopyHasTheSameValuesAsRed_whenSutIsAbstractSealedAndPermittedTypeAddsField() { + Tuple tuple = factory.createValues( + new TypeTag(SealedParentWithFinalChild.class), + valueProvider, + typeStack + ); + + assertEquals(tuple.getRed(), tuple.getRedCopy()); + assertNotSame(tuple.getRed(), tuple.getRedCopy()); + } + + public abstract static sealed class SealedParentWithFinalChild permits FinalSealedChild { + + private final int i; + + public SealedParentWithFinalChild(int i) { + this.i = i; + } + + @Override + public boolean equals(Object obj) { + return obj instanceof SealedParentWithFinalChild other && i == other.i; + } + + @Override + public int hashCode() { + return Objects.hash(i); + } + } + + public static final class FinalSealedChild extends SealedParentWithFinalChild { + + private final int j; + + public FinalSealedChild(int i, int j) { + super(i); + this.j = j; + } + + @Override + public boolean equals(Object obj) { + return obj instanceof FinalSealedChild other && super.equals(obj) && j == other.j; + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), j); + } + } +}