List of usage examples for android.test MoreAsserts assertNotEmpty
public static void assertNotEmpty(String message, Map<?, ?> map)
From source file:com.scvngr.levelup.core.test.JsonTestUtil.java
/** * <p>/*w ww. ja 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); } } }