Skip to content

Commit

Permalink
Merge pull request #1021 from jqno/add-cached-valueprovider
Browse files Browse the repository at this point in the history
Add cached valueprovider
  • Loading branch information
jqno authored Nov 29, 2024
2 parents bf849eb + 486c144 commit ae91f71
Show file tree
Hide file tree
Showing 63 changed files with 906 additions and 628 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void succeed_whenRecordValidatesInput_givenValidPrefabValues() {
}

@Test
public void succeed_whenRecord() {
public void succeed_whenRecordHasBoundedWildcardGeneric() {
EqualsVerifier.forClass(WildcardGenericRecordContainer.class).verify();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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 nl.jqno.equalsverifier.internal.reflection.Tuple;
import nl.jqno.equalsverifier.internal.reflection.TypeTag;
import nl.jqno.equalsverifier.internal.reflection.instantiation.CachedValueProvider;
import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes;
import nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.FallbackFactory;
import nl.jqno.equalsverifier.internal.testhelpers.TestValueProviders;
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 Attributes attributes;

@BeforeEach
public void setUp() {
Objenesis objenesis = new ObjenesisStd();
factory = new FallbackFactory<>(objenesis);
CachedValueProvider cache = new CachedValueProvider();
FactoryCache factoryCache = new FactoryCache();
factoryCache.put(int.class, values(42, 1337, 42));
valueProvider =
new VintageValueProvider(TestValueProviders.empty(), cache, factoryCache, objenesis);
attributes = Attributes.unlabeled();
}

@Test
public void redCopyHasTheSameValuesAsRed_whenSutContainsGenericValueThatNeedsToBeIdenticalInRedAndRedCopy() {
Tuple<?> tuple = factory.createValues(
new TypeTag(GenericRecordContainer.class),
valueProvider,
attributes
);

assertEquals(tuple.getRed(), tuple.getRedCopy());
assertNotSame(tuple.getRed(), tuple.getRedCopy());
}

record GenericRecord<T>(T t) {}

record GenericRecordContainer(GenericRecord<?> bgr) {}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nl.jqno.equalsverifier.internal.reflection.vintage;
package nl.jqno.equalsverifier.internal.reflection.vintage.mutation;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
package nl.jqno.equalsverifier.internal.reflection.vintage;
package nl.jqno.equalsverifier.internal.reflection.vintage.mutation;

import static nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.Factories.values;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotSame;

import java.lang.reflect.Constructor;
import java.util.LinkedHashSet;
import nl.jqno.equalsverifier.internal.reflection.JavaApiPrefabValues;
import nl.jqno.equalsverifier.internal.reflection.TypeTag;
import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider;
import nl.jqno.equalsverifier.internal.reflection.instantiation.CachedValueProvider;
import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes;
import nl.jqno.equalsverifier.internal.reflection.vintage.FactoryCache;
import nl.jqno.equalsverifier.internal.reflection.vintage.VintageValueProvider;
import nl.jqno.equalsverifier.internal.testhelpers.TestValueProviders;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.objenesis.ObjenesisStd;

public class RecordObjectAccessorScramblingTest {

private static final LinkedHashSet<TypeTag> EMPTY_TYPE_STACK = new LinkedHashSet<>();
private static final Attributes EMPTY_ATTRIBUTES = Attributes.unlabeled();
private CachedValueProvider cache;
private FactoryCache factoryCache;
private VintageValueProvider valueProvider;

@BeforeEach
public void setup() throws Exception {
cache = new CachedValueProvider();
factoryCache = JavaApiPrefabValues.build();
valueProvider =
new VintageValueProvider(TestValueProviders.empty(), factoryCache, new ObjenesisStd());
new VintageValueProvider(
TestValueProviders.empty(),
cache,
factoryCache,
new ObjenesisStd()
);
}

@Test
Expand Down Expand Up @@ -58,7 +67,7 @@ private <T> RecordObjectAccessor<T> create(T object) {
}

private ObjectAccessor<Object> doScramble(Object object) {
return create(object).scramble(valueProvider, TypeTag.NULL, EMPTY_TYPE_STACK);
return create(object).scramble(valueProvider, TypeTag.NULL, EMPTY_ATTRIBUTES);
}

record Point(int x, int y) {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package nl.jqno.equalsverifier.internal.reflection.vintage;
package nl.jqno.equalsverifier.internal.reflection.vintage.mutation;

import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.lang.reflect.Constructor;
import java.util.LinkedHashSet;
import java.util.Objects;
import nl.jqno.equalsverifier.internal.exceptions.ReflectionException;
import nl.jqno.equalsverifier.internal.reflection.Instantiator;
import nl.jqno.equalsverifier.internal.reflection.JavaApiPrefabValues;
import nl.jqno.equalsverifier.internal.reflection.TypeTag;
import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider;
import nl.jqno.equalsverifier.internal.reflection.instantiation.CachedValueProvider;
import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes;
import nl.jqno.equalsverifier.internal.reflection.vintage.VintageValueProvider;
import nl.jqno.equalsverifier.internal.testhelpers.ExpectedException;
import nl.jqno.equalsverifier.internal.testhelpers.TestValueProviders;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -20,7 +21,7 @@

public class RecordObjectAccessorTest {

private static final LinkedHashSet<TypeTag> EMPTY_TYPE_STACK = new LinkedHashSet<>();
private static final Attributes EMPTY_ATTRIBUTES = Attributes.unlabeled();
private Objenesis objenesis;
private Object recordInstance;

Expand Down Expand Up @@ -77,11 +78,12 @@ public void fail_whenConstructorThrowsOnSomethingElse() {

VintageValueProvider vp = new VintageValueProvider(
TestValueProviders.empty(),
new CachedValueProvider(),
JavaApiPrefabValues.build(),
objenesis
);
ExpectedException
.when(() -> accessorFor(instance).scramble(vp, TypeTag.NULL, EMPTY_TYPE_STACK))
.when(() -> accessorFor(instance).scramble(vp, TypeTag.NULL, EMPTY_ATTRIBUTES))
.assertThrows(ReflectionException.class)
.assertMessageContains("Record:", "failed to run constructor", "prefab values");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import org.junit.jupiter.api.Test;

public class ClassAccessorSealedTest {
public class ClassProbeSealedTest {

@Test
public void isNotSealed() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
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.Objects;
import nl.jqno.equalsverifier.internal.reflection.Tuple;
import nl.jqno.equalsverifier.internal.reflection.TypeTag;
import nl.jqno.equalsverifier.internal.reflection.instantiation.CachedValueProvider;
import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes;
import nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.FallbackFactory;
import nl.jqno.equalsverifier.internal.testhelpers.TestValueProviders;
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 Attributes attributes;

@BeforeEach
public void setUp() {
Objenesis objenesis = new ObjenesisStd();
factory = new FallbackFactory<>(objenesis);
CachedValueProvider cache = new CachedValueProvider();
FactoryCache factoryCache = new FactoryCache();
factoryCache.put(int.class, values(42, 1337, 42));
valueProvider =
new VintageValueProvider(TestValueProviders.empty(), cache, factoryCache, objenesis);
attributes = Attributes.unlabeled();
}

@Test
public void redCopyHasTheSameValuesAsRed_whenSutIsAbstractSealedAndPermittedTypeAddsField() {
Tuple<?> tuple = factory.createValues(
new TypeTag(SealedParentWithFinalChild.class),
valueProvider,
attributes
);

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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import nl.jqno.equalsverifier.api.SingleTypeEqualsVerifierApi;
import nl.jqno.equalsverifier.internal.reflection.PackageScanner;
import nl.jqno.equalsverifier.internal.reflection.vintage.FactoryCache;
import nl.jqno.equalsverifier.internal.reflection.vintage.PrefabValuesApi;
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import nl.jqno.equalsverifier.internal.checkers.*;
import nl.jqno.equalsverifier.internal.exceptions.MessagingException;
import nl.jqno.equalsverifier.internal.reflection.vintage.FactoryCache;
import nl.jqno.equalsverifier.internal.reflection.vintage.PrefabValuesApi;
import nl.jqno.equalsverifier.internal.util.*;
import nl.jqno.equalsverifier.internal.util.Formatter;
import org.objenesis.Objenesis;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import nl.jqno.equalsverifier.internal.reflection.TypeTag;
import nl.jqno.equalsverifier.internal.reflection.instantiation.SubjectCreator;
import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider;
import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes;
import nl.jqno.equalsverifier.internal.util.*;

public class AbstractDelegationChecker<T> implements Checker {
Expand Down Expand Up @@ -74,7 +75,7 @@ private void checkAbstractDelegationInFields() {

private <U> Tuple<U> safelyGetTuple(TypeTag tag, String fieldName) {
try {
return valueProvider.provideOrThrow(tag, fieldName);
return valueProvider.provideOrThrow(tag, Attributes.labeled(fieldName));
} catch (Exception ignored) {
// If it fails for some reason, any reason, just return null so we can skip the test.
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Map;
import java.util.Objects;
import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider;
import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes;
import nl.jqno.equalsverifier.internal.util.Configuration;
import nl.jqno.equalsverifier.internal.util.Context;
import nl.jqno.equalsverifier.internal.util.Formatter;
Expand All @@ -23,7 +24,7 @@ public MapEntryHashCodeRequirementChecker(Context<T> context) {
public void check() {
if (Map.Entry.class.isAssignableFrom(config.getType())) {
Map.Entry<?, ?> e = valueProvider
.<Map.Entry<?, ?>>provideOrThrow(config.getTypeTag(), null)
.<Map.Entry<?, ?>>provideOrThrow(config.getTypeTag(), Attributes.unlabeled())
.getRed();

int expectedHashCode = Objects.hashCode(e.getKey()) ^ Objects.hashCode(e.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import nl.jqno.equalsverifier.internal.reflection.annotations.SupportedAnnotations;
import nl.jqno.equalsverifier.internal.reflection.instantiation.SubjectCreator;
import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider;
import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes;
import nl.jqno.equalsverifier.internal.util.Configuration;
import nl.jqno.equalsverifier.internal.util.Context;
import nl.jqno.equalsverifier.internal.util.Formatter;
Expand Down Expand Up @@ -64,7 +65,7 @@ public void execute(FieldProbe fieldProbe) {
);

TypeTag sub = new TypeTag(throwingGetterCreator(getterName));
Tuple<T> tuple = valueProvider.provideOrThrow(sub, fieldName);
Tuple<T> tuple = valueProvider.provideOrThrow(sub, Attributes.labeled(fieldName));
T red1 = tuple.getRed();
T red2 = tuple.getRedCopy();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import nl.jqno.equalsverifier.internal.reflection.annotations.SupportedAnnotations;
import nl.jqno.equalsverifier.internal.reflection.instantiation.SubjectCreator;
import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider;
import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes;
import nl.jqno.equalsverifier.internal.util.Configuration;
import nl.jqno.equalsverifier.internal.util.Context;
import nl.jqno.equalsverifier.internal.util.Formatter;
Expand Down Expand Up @@ -80,7 +81,7 @@ private void checkValueReflexivity(FieldProbe probe) {
String fieldName = field.getName();
TypeTag tag = TypeTag.of(field, typeTag);
Tuple<?> tuple = valueProvider
.provide(tag, fieldName)
.provide(tag, Attributes.labeled(fieldName))
.orElseThrow(() -> new NoValueException(tag, fieldName));

Object left = subjectCreator.withFieldSetTo(field, tuple.getRed());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import nl.jqno.equalsverifier.internal.reflection.TypeTag;
import nl.jqno.equalsverifier.internal.reflection.instantiation.SubjectCreator;
import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider;
import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider.Attributes;
import nl.jqno.equalsverifier.internal.util.CachedHashCodeInitializer;
import nl.jqno.equalsverifier.internal.util.Formatter;

Expand Down Expand Up @@ -42,7 +43,7 @@ public void execute(FieldProbe fieldProbe) {
if (String.class.equals(fieldProbe.getType()) && !fieldProbe.isStatic()) {
TypeTag string = new TypeTag(String.class);
String red = valueProvider
.<String>provideOrThrow(string, fieldProbe.getName())
.<String>provideOrThrow(string, Attributes.labeled(fieldProbe.getName()))
.getRed();

final T reference;
Expand Down
Loading

0 comments on commit ae91f71

Please sign in to comment.