Search in sources :

Example 71 with EqualsTester

use of com.google.common.testing.EqualsTester in project guava by google.

the class SubscriberTest method testEquals.

public void testEquals() throws Exception {
    Method charAt = String.class.getMethod("charAt", int.class);
    Method concat = String.class.getMethod("concat", String.class);
    new EqualsTester().addEqualityGroup(Subscriber.create(bus, "foo", charAt), Subscriber.create(bus, "foo", charAt)).addEqualityGroup(Subscriber.create(bus, "bar", charAt)).addEqualityGroup(Subscriber.create(bus, "foo", concat)).testEquals();
}
Also used : EqualsTester(com.google.common.testing.EqualsTester) Method(java.lang.reflect.Method)

Example 72 with EqualsTester

use of com.google.common.testing.EqualsTester in project core-java by SpineEventEngine.

the class QueryParameterShould method support_equality.

@Test
public void support_equality() {
    final String param1 = "param1";
    final String param2 = "param2";
    final String foobar = "foobar";
    final String baz = "baz";
    final StringValue foobarValue = StringValue.newBuilder().setValue(foobar).build();
    final QueryParameter parameter1 = eq(param1, foobar);
    final QueryParameter parameter2 = eq(param1, foobarValue);
    final QueryParameter parameter3 = eq(param1, baz);
    final QueryParameter parameter4 = eq(param2, foobar);
    new EqualsTester().addEqualityGroup(parameter1, parameter2).addEqualityGroup(parameter3).addEqualityGroup(parameter4).testEquals();
}
Also used : QueryParameter(io.spine.client.QueryParameter) EqualsTester(com.google.common.testing.EqualsTester) String(java.lang.String) Matchers.containsString(org.hamcrest.Matchers.containsString) StringValue(com.google.protobuf.StringValue) Test(org.junit.Test)

Example 73 with EqualsTester

use of com.google.common.testing.EqualsTester in project core-java by SpineEventEngine.

the class AbstractVersionableEntityShould method have_equals.

@SuppressWarnings("MagicNumber")
@Test
public void have_equals() throws Exception {
    final AvEntity entity = new AvEntity(88L);
    final AvEntity another = new AvEntity(88L);
    another.updateState(entity.getState(), entity.getVersion());
    new EqualsTester().addEqualityGroup(entity, another).addEqualityGroup(new AvEntity(42L)).testEquals();
}
Also used : EqualsTester(com.google.common.testing.EqualsTester) Test(org.junit.Test)

Example 74 with EqualsTester

use of com.google.common.testing.EqualsTester in project alluxio by Alluxio.

the class CommonTestUtils method testEquals.

/**
   * Uses reflection to test the equals and hashCode methods for the given simple java object.
   *
   * It is required that the given class has a no-arg constructor.
   *
   * Note: To use this method to test a class which contains a final non-enum class as a field, the
   * class must either have a no-arg constructor, or you must prepare the final class for testing.
   * See the top of {@link CommonTestUtilsTest} for an example.
   *
   * @param clazz the class to test the equals and hashCode methods for
   * @param excludedFields names of fields which should not impact equality
   */
public static <T> void testEquals(Class<T> clazz, String... excludedFields) {
    Set<String> excludedFieldsSet = new HashSet<>(Arrays.asList(excludedFields));
    EqualsTester equalsTester = new EqualsTester();
    equalsTester.addEqualityGroup(createBaseObject(clazz), createBaseObject(clazz));
    // For each non-excluded field, create an object of the class with only that field changed.
    for (Field field : getNonStaticFields(clazz)) {
        if (excludedFieldsSet.contains(field.getName())) {
            continue;
        }
        field.setAccessible(true);
        T instance = createBaseObject(clazz);
        try {
            field.set(instance, getValuesForFieldType(field.getType()).get(1));
        } catch (Exception e) {
            throw Throwables.propagate(e);
        }
        equalsTester.addEqualityGroup(instance);
    }
    equalsTester.testEquals();
}
Also used : Field(java.lang.reflect.Field) EqualsTester(com.google.common.testing.EqualsTester) HashSet(java.util.HashSet)

Example 75 with EqualsTester

use of com.google.common.testing.EqualsTester in project beam by apache.

the class PCollectionTupleTest method testEquals.

@Test
public void testEquals() {
    TestPipeline p = TestPipeline.create();
    TupleTag<Long> longTag = new TupleTag<>();
    PCollection<Long> longs = p.apply(GenerateSequence.from(0));
    TupleTag<String> strTag = new TupleTag<>();
    PCollection<String> strs = p.apply(Create.of("foo", "bar"));
    EqualsTester tester = new EqualsTester();
    // Empty tuples in the same pipeline are equal
    tester.addEqualityGroup(PCollectionTuple.empty(p), PCollectionTuple.empty(p));
    tester.addEqualityGroup(PCollectionTuple.of(longTag, longs).and(strTag, strs), PCollectionTuple.of(longTag, longs).and(strTag, strs));
    tester.addEqualityGroup(PCollectionTuple.of(longTag, longs));
    tester.addEqualityGroup(PCollectionTuple.of(strTag, strs));
    TestPipeline otherPipeline = TestPipeline.create();
    // Empty tuples in different pipelines are not equal
    tester.addEqualityGroup(PCollectionTuple.empty(otherPipeline));
    tester.testEquals();
}
Also used : TestPipeline(org.apache.beam.sdk.testing.TestPipeline) EqualsTester(com.google.common.testing.EqualsTester) Test(org.junit.Test)

Aggregations

EqualsTester (com.google.common.testing.EqualsTester)177 Test (org.junit.Test)83 GwtIncompatible (com.google.common.annotations.GwtIncompatible)10 ParameterizedType (java.lang.reflect.ParameterizedType)10 WildcardType (java.lang.reflect.WildcardType)8 Method (java.lang.reflect.Method)7 Entry (java.util.Map.Entry)7 GenericArrayType (java.lang.reflect.GenericArrayType)6 Type (java.lang.reflect.Type)6 ImmutableList (com.google.common.collect.ImmutableList)5 Path (com.google.devtools.build.lib.vfs.Path)5 List (java.util.List)5 Set (java.util.Set)5 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)4 HashMap (java.util.HashMap)4 ImmutableSet (com.google.common.collect.ImmutableSet)3 Label (com.google.devtools.build.lib.cmdline.Label)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3