if ((expectedMap == null) || (actualMap == null)) {
AssertionUtils.jettAssertEquals("Checking LinkedHashMap: ", expectedMap, actualMap);
return;
}
DataTypeEnum keyType = null;
Class<?> keyClass = null;
if (actualMap.size() > 0) {
Set<Object> keySet = actualMap.keySet();
for(Object key: keySet) {
if (keyClass == null) {
keyClass = key.getClass();
} else {
if (key.getClass().isAssignableFrom(keyClass)) {
keyClass = key.getClass();
} else if (keyClass.isAssignableFrom(key.getClass())) {
//Ok
} else {
throw new AssertionError("JETT can only assert maps where keys have the same class");
}
}
}
keyType = ObjectUtil.resolveType(keyClass);
}
//Check keys order
List<Object> actKeys = new ArrayList<Object>();
for (Object actKey: actualMap.keySet()) {
actKeys.add(actKey);
}
Map<Object, Object> castedKeysMap = new LinkedHashMap<Object, Object>();
int actualKeyPreviousPos = Integer.MIN_VALUE;
Object previousKey = null;
for (Object expKey: expectedMap.keySet()) {
String compareMessage = "Checking LinkedHashMap ";
Object castedExpKey;
switch (keyType) {
case JAVA_DOT_LANG_OBJECT:
case STRING:
castedExpKey = Reflect.getWrappedValue(keyClass, expKey);
break;
default:
try {
castedExpKey = keyClass.cast(expKey);
} catch (ClassCastException e) {
AssertionUtils.actionFail(compareMessage + " - Unable to cast expected key '" + expKey.toString() + "' to the class of actual keys '" + keyClass + "'");
continue;
}
break;
}
castedKeysMap.put(castedExpKey, expectedMap.get(expKey));
if (actKeys.contains(castedExpKey)) {
int actualKeyPosition = actKeys.indexOf(castedExpKey);
if (actualKeyPreviousPos != Integer.MIN_VALUE) {
if (actualKeyPosition <= actualKeyPreviousPos) {
AssertionUtils.actionFail(compareMessage + "- Key order not as expected in LinkedHashMap - '" + castedExpKey + "' did not follow '" + previousKey + "'");
} else {
AssertionUtils.actionPass(compareMessage + "- Key order as expected in LinkedHashMap - '" + castedExpKey + "' followed '" + previousKey + "'");
}
}
actualKeyPreviousPos = actualKeyPosition;
previousKey = castedExpKey;
} else {
AssertionUtils.actionFail(compareMessage + "- Key not present in LinkedHashMap: '" + castedExpKey + "'");
}
}
for (Object castedExpKey : castedKeysMap.keySet()) {
String compareMessage = "Checking LinkedHashMap Entry for Key '" + castedExpKey.toString() + "' ";
if (!actualMap.containsKey(castedExpKey)) {
//Failure already reported above
continue;
}
Object expectedObj = castedKeysMap.get(castedExpKey);
Object actualObj = actualMap.get(castedExpKey);
if (expectedObj==null) {
AssertionUtils.jettAssertNull(compareMessage + "expected object is null, check if actual object is null.",actualObj);
} else if (actualObj==null) {
AssertionUtils.jettAssertNull(compareMessage + "actual is object is null, checking if expected object is null.",expectedObj);
} else {
DataTypeEnum type;
type = ObjectUtil.resolveType(actualObj);
switch(type) {
case JAVA_DOT_LANG_OBJECT:
case STRING:
Object castedExpectedObj = Reflect.getWrappedValue(actualObj.getClass(), expectedObj);