Example usage for android.test MoreAsserts checkEqualsAndHashCodeMethods

List of usage examples for android.test MoreAsserts checkEqualsAndHashCodeMethods

Introduction

In this page you can find the example usage for android.test MoreAsserts checkEqualsAndHashCodeMethods.

Prototype

public static void checkEqualsAndHashCodeMethods(Object lhs, Object rhs, boolean expectedResult) 

Source Link

Document

Variant of checkEqualsAndHashCodeMethods(String,Object,Object,boolean...)} using a generic message.

Usage

From source file:com.scvngr.levelup.core.model.OrderTest.java

/**
 * Tests {@link #equals(Object)} and {@link #hashCode()} methods.
 *
 * @throws org.json.JSONException for parsing errors.
 *//* ww w  .  jav  a 2 s  .  c o  m*/
@SmallTest
public void testEqualsAndHashCode() throws JSONException {
    // Test identical objects
    final Order order1 = new OrderJsonFactory().from(OrderFixture.getFullJsonObject());
    final Order order2 = new OrderJsonFactory().from(OrderFixture.getFullJsonObject());
    MoreAsserts.checkEqualsAndHashCodeMethods(order1, order2, true);

    JsonTestUtil.checkEqualsAndHashCodeOnJsonVariants(OrderJsonFactory.JsonKeys.class, new OrderJsonFactory(),
            OrderFixture.getFullJsonObject(), new String[] { "MODEL_ROOT" });
}

From source file:com.scvngr.levelup.core.model.PaymentTokenTest.java

/**
 * Tests {@link #equals(Object)} and {@link #hashCode()} methods.
 *
 * @throws org.json.JSONException for parsing errors.
 *///from   w w w  .  j  a va 2  s . c om
@SmallTest
public void testEqualsAndHashCode() throws JSONException {
    // Test similarities
    final PaymentToken paymentToken1 = new PaymentTokenJsonFactory()
            .from(PaymentTokenFixture.getFullJsonObject());
    final PaymentToken paymentToken2 = new PaymentTokenJsonFactory()
            .from(PaymentTokenFixture.getFullJsonObject());
    MoreAsserts.checkEqualsAndHashCodeMethods(paymentToken1, paymentToken2, true);

    // Test differences across variations based on all JSON keys
    JsonTestUtil.checkEqualsAndHashCodeOnJsonVariants(PaymentTokenJsonFactory.JsonKeys.class,
            new PaymentTokenJsonFactory(), PaymentTokenFixture.getFullJsonObject(),
            new String[] { "MODEL_ROOT" });
}

From source file:com.scvngr.levelup.core.model.WebLinkTest.java

@SmallTest
public void testEqualsAndHashCode() throws JSONException {
    // Test differences across variations based on all JSON keys
    JsonTestUtil.checkEqualsAndHashCodeOnJsonVariants(WebLinkJsonFactory.JsonKeys.class,
            new WebLinkJsonFactory(), WebLinkFixture.getFullJsonObject(1), new String[] { "MODEL_ROOT" });

    {/* w  w w .  jav  a  2s  .c om*/
        final JSONObject object = WebLinkFixture.getFullJsonObject(1);
        final WebLink webLink1 = new WebLinkJsonFactory().from(object);
        object.put(WebLinkJsonFactory.JsonKeys.WEB_URL, "AAAA");
        final WebLink webLink2 = new WebLinkJsonFactory().from(object);
        MoreAsserts.checkEqualsAndHashCodeMethods(webLink1, webLink2, false);
    }

    {
        final JSONObject object = WebLinkFixture.getFullJsonObject(1);
        final WebLink webLink1 = new WebLinkJsonFactory().from(object);
        object.put(WebLinkJsonFactory.JsonKeys.TITLE, "AAA");
        final WebLink webLink2 = new WebLinkJsonFactory().from(object);
        MoreAsserts.checkEqualsAndHashCodeMethods(webLink1, webLink2, false);
    }

    {
        final JSONObject object = WebLinkFixture.getFullJsonObject(1);
        final WebLink webLink1 = new WebLinkJsonFactory().from(object);
        object.put(WebLinkJsonFactory.JsonKeys.WEB_LINK_TYPE_ID, "234");
        final WebLink webLink2 = new WebLinkJsonFactory().from(object);
        MoreAsserts.checkEqualsAndHashCodeMethods(webLink1, webLink2, false);
    }
}

From source file:com.scvngr.levelup.core.model.LoyaltyTest.java

@SmallTest
public void testEqualsAndHashCode() throws JSONException {
    // Test differences across variations based on all JSON keys
    JsonTestUtil.checkEqualsAndHashCodeOnJsonVariants(LoyaltyJsonFactory.JsonKeys.class,
            new LoyaltyJsonFactory(), LoyaltyFixture.getFullJsonObject(1),
            new String[] { "MODEL_ROOT", "ORDERS_COUNT", "POTENTIAL_CREDIT", "SAVINGS", "SHOULD_SPEND",
                    "SPEND_REMAINING", "TOTAL_VOLUME", "WILL_EARN" });

    {//from w  ww  .  j av a  2s  . c o  m
        final JSONObject object = LoyaltyFixture.getFullJsonObject(1);
        final Loyalty loyalty1 = new LoyaltyJsonFactory().from(object);
        object.remove(LoyaltyJsonFactory.JsonKeys.ORDERS_COUNT);
        final Loyalty loyalty2 = new LoyaltyJsonFactory().from(object);
        MoreAsserts.checkEqualsAndHashCodeMethods(loyalty1, loyalty2, false);
    }

    {
        final JSONObject object = LoyaltyFixture.getFullJsonObject(1);
        final Loyalty loyalty1 = new LoyaltyJsonFactory().from(object);
        object.remove(LoyaltyJsonFactory.JsonKeys.POTENTIAL_CREDIT);
        final Loyalty loyalty2 = new LoyaltyJsonFactory().from(object);
        MoreAsserts.checkEqualsAndHashCodeMethods(loyalty1, loyalty2, false);
    }

    {
        final JSONObject object = LoyaltyFixture.getFullJsonObject(1);
        final Loyalty loyalty1 = new LoyaltyJsonFactory().from(object);
        object.remove(LoyaltyJsonFactory.JsonKeys.SAVINGS);
        final Loyalty loyalty2 = new LoyaltyJsonFactory().from(object);
        MoreAsserts.checkEqualsAndHashCodeMethods(loyalty1, loyalty2, false);
    }

    {
        final JSONObject object = LoyaltyFixture.getFullJsonObject(1);
        final Loyalty loyalty1 = new LoyaltyJsonFactory().from(object);
        object.remove(LoyaltyJsonFactory.JsonKeys.SHOULD_SPEND);
        final Loyalty loyalty2 = new LoyaltyJsonFactory().from(object);
        MoreAsserts.checkEqualsAndHashCodeMethods(loyalty1, loyalty2, false);
    }

    {
        final JSONObject object = LoyaltyFixture.getFullJsonObject(1);
        final Loyalty loyalty1 = new LoyaltyJsonFactory().from(object);
        object.remove(LoyaltyJsonFactory.JsonKeys.SPEND_REMAINING);
        final Loyalty loyalty2 = new LoyaltyJsonFactory().from(object);
        MoreAsserts.checkEqualsAndHashCodeMethods(loyalty1, loyalty2, false);
    }

    {
        final JSONObject object = LoyaltyFixture.getFullJsonObject(1);
        final Loyalty loyalty1 = new LoyaltyJsonFactory().from(object);
        object.remove(LoyaltyJsonFactory.JsonKeys.TOTAL_VOLUME);
        final Loyalty loyalty2 = new LoyaltyJsonFactory().from(object);
        MoreAsserts.checkEqualsAndHashCodeMethods(loyalty1, loyalty2, false);
    }

    {
        final JSONObject object = LoyaltyFixture.getFullJsonObject(1);
        final Loyalty loyalty1 = new LoyaltyJsonFactory().from(object);
        object.remove(LoyaltyJsonFactory.JsonKeys.WILL_EARN);
        final Loyalty loyalty2 = new LoyaltyJsonFactory().from(object);
        MoreAsserts.checkEqualsAndHashCodeMethods(loyalty1, loyalty2, false);
    }

    {
        final Loyalty loyalty1 = LoyaltyFixture.getMinimalModel();
        final Loyalty loyalty2 = LoyaltyFixture.getMinimalModel();
        MoreAsserts.checkEqualsAndHashCodeMethods(loyalty1, loyalty2, true);
    }
}

From source file:com.scvngr.levelup.core.test.JsonTestUtil.java

/**
 * <p>//from  w w  w  . j  a v a 2 s .c  om
 * Checks that modifying individual fields in a model will result in its equals/hashCode methods
 * failing. Uses reflection on {@link JsonValueType} annotations on fields of a passed class to
 * figure out how to modify the JSON representation of the model in different ways, then parses
 * the JSON with a {@link AbstractJsonModelFactory} subclass before checking equals/hashcode on
 * both the original and a modified object.
 * </p>
 * <p>
 * This effectively checks that equals/hashcode works across any value changes from fields we
 * read from JSON, but also checks some other potential issues. We're implicitly checking that
 * the JSON typing declared in annotations for the fields matches what we actually use when
 * parsing our JSON (since if it doesn't, we'll get JSON errors when reading the data during the
 * clone/modify). We're also checking for fields that may have been added to the JSON keys and
 * the model without updating equals/hashcode to reflect them (as long as they're declared in
 * the JSONKeys class used here).
 * </p>
 * <p>
 * Note that this is only intended for test use and will turn all checked exceptions it might
 * throw into unchecked ones.
 * </p>
 *
 * @param jsonKeysClass Class of the underlying keys class to test all fields (except
 *        blacklistFields) from. Must have visible fields to read from.
 * @param jsonFactory Factory object to construct model instances from out of the base and
 *        generated-variant JSON objects before checking equals/hashcode.
 * @param baseJsonObject Fully-populated JSON object for the model to use for comparison with
 *        modified copies.
 * @param blacklistFields Fields to exclude from variant testing (either because we need to test
 *        them manually or because they don't reflect fields that are used for parsing into the
 *        model). Note that this is the jsonKeysClass's field name as a string, not the JSON key
 *        value (eg "ID", not "id").
 */
public static void checkEqualsAndHashCodeOnJsonVariants(@NonNull final Class<?> jsonKeysClass,
        @NonNull final AbstractJsonModelFactory<?> jsonFactory, @NonNull final JSONObject baseJsonObject,
        @NonNull final String[] blacklistFields) {
    Object originalModel;
    Object differentModel;
    Object differentModelReparse;

    try {
        originalModel = jsonFactory.from(baseJsonObject);
    } catch (final JSONException e1) {
        throw new RuntimeException(e1);
    }

    MoreAsserts.checkEqualsAndHashCodeMethods(originalModel, null, false);

    final Field[] jsonKeyFields = jsonKeysClass.getFields();
    final List<String> blacklisted = Arrays.asList(blacklistFields);
    final String key = null;

    MoreAsserts.assertNotEmpty("JSON keys class visible fields", Arrays.asList(jsonKeyFields));

    for (final Field field : jsonKeyFields) {
        if (!blacklisted.contains(field.getName())) {
            JSONObject copiedDifferingObject;
            String fieldString;
            // Don't check exceptions, just let tests fail.
            try {
                fieldString = NullUtils.nonNullContract((String) field.get(key));
                copiedDifferingObject = cloneObjectDifferingOnParam(baseJsonObject, fieldString,
                        reflectJsonType(field));
                differentModel = jsonFactory.from(copiedDifferingObject);
                differentModelReparse = jsonFactory.from(copiedDifferingObject);
            } catch (final IllegalArgumentException e) {
                throw new RuntimeException(e);
            } catch (final IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (final JSONException e) {
                throw new RuntimeException(e);
            }

            MoreAsserts.checkEqualsAndHashCodeMethods(
                    String.format(Locale.US, "Modified %s and checked equals and hash", fieldString),
                    originalModel, differentModel, false);
            MoreAsserts.checkEqualsAndHashCodeMethods(
                    String.format(Locale.US, "Modified %s and checked equals and hash", fieldString),
                    differentModel, differentModel, true);
            MoreAsserts.checkEqualsAndHashCodeMethods(
                    String.format(Locale.US, "Modified %s and checked equals and hash", fieldString),
                    differentModel, differentModelReparse, true);
        }
    }
}

From source file:com.scvngr.levelup.core.model.UserTest.java

/**
 * Tests {@link #equals(Object)} and {@link #hashCode()} methods.
 *
 * @throws org.json.JSONException for parsing errors.
 *//*www  . jav  a 2 s  .c  om*/
@SmallTest
public void testEqualsAndHashCode() throws JSONException {
    // Test identical objects
    final User user1 = new UserJsonFactory().from(UserFixture.getFullJsonObject());
    User user2 = new UserJsonFactory().from(UserFixture.getFullJsonObject());
    MoreAsserts.checkEqualsAndHashCodeMethods(user1, user2, true);

    /*
     * Test standard variations on all JsonKeys values (and check for any keys we may have added
     * that didn't make their way into equals/hashcode and the tests explicitly).
     *
     * CUSTOM_ATTRIBUTES needs to be tested by hand since it is deserialized into a dictionary.
     * MODEL_ROOT is only used for automatically un-nesting and isn't used for an attribute.
     */
    JsonTestUtil.checkEqualsAndHashCodeOnJsonVariants(UserJsonFactory.JsonKeys.class, new UserJsonFactory(),
            UserFixture.getFullJsonObject(), new String[] { "CUSTOM_ATTRIBUTES", "MODEL_ROOT" });

    // Modifying a single custom attribute should be unequal/give different hashcode
    final HashMap<String, String> customAttributesMap = new HashMap<String, String>();
    // Changed value
    customAttributesMap.put("test_attr", "1");
    customAttributesMap.put("test_attr2", "1");
    final JSONObject customAttributesObject = new JSONObject(customAttributesMap);
    user2 = new UserJsonFactory().from(UserFixture.getFullJsonObject()
            .put(UserJsonFactory.JsonKeys.CUSTOM_ATTRIBUTES, customAttributesObject));
    MoreAsserts.checkEqualsAndHashCodeMethods(user1, user2, false);
}

From source file:com.scvngr.levelup.core.model.UserTest.java

@SmallTest
public void testEqualsAndHashCode_no_total_savings() throws JSONException {
    final User userModelWithNoTotalSavings = new UserJsonFactory().from(UserFixture.getMinimalJsonObject());

    final JSONObject userJsonWithTotalSavings = UserFixture.getMinimalJsonObject();
    userJsonWithTotalSavings.put(UserJsonFactory.JsonKeys.TOTAL_SAVINGS_AMOUNT, 2000);
    final User userModelWithTotalSavings = new UserJsonFactory().from(userJsonWithTotalSavings);

    MoreAsserts.checkEqualsAndHashCodeMethods(userModelWithNoTotalSavings, userModelWithNoTotalSavings, true);
    MoreAsserts.checkEqualsAndHashCodeMethods(userModelWithTotalSavings, userModelWithTotalSavings, true);

    MoreAsserts.checkEqualsAndHashCodeMethods(userModelWithNoTotalSavings, userModelWithTotalSavings, false);
}

From source file:com.scvngr.levelup.core.net.LevelUpRequestTest.java

@SmallTest
public void testEqualsAndHashCode() {
    LevelUpRequest request1 = new LevelUpRequest(getContext(), HttpMethod.GET, TEST_API_VERSION, TEST_ENDPOINT,
            null, null);/*from  www.  ja  v  a 2s .c om*/
    LevelUpRequest request2 = new LevelUpRequest(getContext(), HttpMethod.GET, TEST_API_VERSION, TEST_ENDPOINT,
            null, null);
    final Map<String, String> hash = new HashMap<String, String>();
    hash.put("test", "test");

    MoreAsserts.checkEqualsAndHashCodeMethods(request1, request2, true);

    // Test equalities
    request1 = new LevelUpRequest(getContext(), HttpMethod.POST, TEST_API_VERSION, TEST_ENDPOINT, null, null);
    request2 = new LevelUpRequest(getContext(), HttpMethod.POST, TEST_API_VERSION, TEST_ENDPOINT, null, null);
    MoreAsserts.checkEqualsAndHashCodeMethods(request1, request2, true);

    request1 = new LevelUpRequest(getContext(), HttpMethod.GET, TEST_API_VERSION + "version", TEST_ENDPOINT,
            null, null);
    request2 = new LevelUpRequest(getContext(), HttpMethod.GET, TEST_API_VERSION + "version", TEST_ENDPOINT,
            null, null);
    MoreAsserts.checkEqualsAndHashCodeMethods(request1, request2, true);

    request1 = new LevelUpRequest(getContext(), HttpMethod.GET, TEST_API_VERSION, TEST_ENDPOINT + "endpoint",
            null, null);
    request2 = new LevelUpRequest(getContext(), HttpMethod.GET, TEST_API_VERSION, TEST_ENDPOINT + "endpoint",
            null, null);
    MoreAsserts.checkEqualsAndHashCodeMethods(request1, request2, true);

    request1 = new LevelUpRequest(getContext(), HttpMethod.GET, TEST_API_VERSION, TEST_ENDPOINT, hash, null);
    request2 = new LevelUpRequest(getContext(), HttpMethod.GET, TEST_API_VERSION, TEST_ENDPOINT, hash, null);
    MoreAsserts.checkEqualsAndHashCodeMethods(request1, request2, true);

    request1 = new LevelUpRequest(getContext(), HttpMethod.GET, TEST_API_VERSION, TEST_ENDPOINT, null,
            new MockRequestBody());
    request2 = new LevelUpRequest(getContext(), HttpMethod.GET, TEST_API_VERSION, TEST_ENDPOINT, null,
            new MockRequestBody());
    MoreAsserts.checkEqualsAndHashCodeMethods(request1, request2, true);

    // Test differences
    request1 = new LevelUpRequest(getContext(), HttpMethod.GET, TEST_API_VERSION, TEST_ENDPOINT, null, null);
    request2 = new LevelUpRequest(getContext(), HttpMethod.POST, TEST_API_VERSION, TEST_ENDPOINT, null, null);
    MoreAsserts.checkEqualsAndHashCodeMethods(request1, request2, false);

    request2 = new LevelUpRequest(getContext(), HttpMethod.GET, TEST_API_VERSION + "version", TEST_ENDPOINT,
            null, null);
    MoreAsserts.checkEqualsAndHashCodeMethods(request1, request2, false);

    request2 = new LevelUpRequest(getContext(), HttpMethod.GET, TEST_API_VERSION, TEST_ENDPOINT + "endpoint",
            null, null);
    MoreAsserts.checkEqualsAndHashCodeMethods(request1, request2, false);

    request2 = new LevelUpRequest(getContext(), HttpMethod.GET, TEST_API_VERSION, TEST_ENDPOINT, hash, null);
    MoreAsserts.checkEqualsAndHashCodeMethods(request1, request2, false);

    request2 = new LevelUpRequest(getContext(), HttpMethod.GET, TEST_API_VERSION, TEST_ENDPOINT, null,
            new MockRequestBody());
    MoreAsserts.checkEqualsAndHashCodeMethods(request1, request2, false);

    request2 = null;
    MoreAsserts.checkEqualsAndHashCodeMethods(request1, request2, false);
}

From source file:com.scvngr.levelup.core.net.LevelUpResponseTest.java

/**
 * Tests the {@link com.scvngr.levelup.core.net.LevelUpResponse#equals(Object)} and
 * {@link com.scvngr.levelup.core.net.LevelUpResponse#hashCode()} methods.
 *
 * @throws Exception if {@link com.scvngr.levelup.core.net.LevelUpResponseTest.MockHttpUrlConnection}
 * throws/*from  www. jav  a 2 s .c  o m*/
 */
@SmallTest
public void testEqualsAndHashCode() throws Exception {
    final StreamingResponse baseResponse = new StreamingResponse();
    LevelUpResponse response1 = new LevelUpResponse(baseResponse);
    LevelUpResponse response2 = new LevelUpResponse(baseResponse);

    // Same base response should be the same
    MoreAsserts.checkEqualsAndHashCodeMethods(response1, response2, true);

    // New object should be the same.
    response2 = new LevelUpResponse(new StreamingResponse());
    MoreAsserts.checkEqualsAndHashCodeMethods(response1, response2, true);

    // Null shouldn't be the same
    MoreAsserts.checkEqualsAndHashCodeMethods(response1, null, false);

    // Differing data
    response2 = new LevelUpResponse(new StreamingResponse(new MockHttpUrlConnection()));
    MoreAsserts.checkEqualsAndHashCodeMethods(response1, response2, false);

    // Now have the same response data.
    response1 = new LevelUpResponse(new StreamingResponse(new MockHttpUrlConnection()));
    MoreAsserts.checkEqualsAndHashCodeMethods(response1, response2, true);

    // Reset to initial
    response1 = new LevelUpResponse(baseResponse);

    // Build response 2 from error
    response2 = new LevelUpResponse(new StreamingResponse(new ResponseTooLargeException()));
    MoreAsserts.checkEqualsAndHashCodeMethods(response1, response2, false);

    // Match it with response 1
    response1 = new LevelUpResponse(new StreamingResponse(new ResponseTooLargeException()));
    MoreAsserts.checkEqualsAndHashCodeMethods(response1, response2, true);

    // Build response 2 from error
    response2 = new LevelUpResponse(new StreamingResponse(new IOException()));
    MoreAsserts.checkEqualsAndHashCodeMethods(response1, response2, false);

    // Match it with response 1
    response1 = new LevelUpResponse(new StreamingResponse(new IOException()));
    MoreAsserts.checkEqualsAndHashCodeMethods(response1, response2, true);

    response2 = null;
    MoreAsserts.checkEqualsAndHashCodeMethods(response1, response2, false);

    // With original response
    final LevelUpResponse originalResponse = new LevelUpResponse("data", LevelUpStatus.OK);

    response2 = new LevelUpResponse(LevelUpStatus.ERROR_PARSING, originalResponse);

    MoreAsserts.checkEqualsAndHashCodeMethods(response1, response2, false);

    response1 = new LevelUpResponse(LevelUpStatus.ERROR_PARSING, originalResponse);

    MoreAsserts.checkEqualsAndHashCodeMethods(response1, response2, true);

}