|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2012 the original author or authors. |
|
|
|
* Copyright 2002-2018 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -32,8 +32,8 @@ import static org.springframework.test.util.AssertionErrors.*; |
|
|
|
* A collection of assertions intended to simplify testing scenarios dealing |
|
|
|
* A collection of assertions intended to simplify testing scenarios dealing |
|
|
|
* with Spring Web MVC {@link org.springframework.web.servlet.ModelAndView |
|
|
|
* with Spring Web MVC {@link org.springframework.web.servlet.ModelAndView |
|
|
|
* ModelAndView} objects. |
|
|
|
* ModelAndView} objects. |
|
|
|
* <p> |
|
|
|
* |
|
|
|
* Intended for use with JUnit 4 and TestNG. All {@code assert*()} methods |
|
|
|
* <p>Intended for use with JUnit 4 and TestNG. All {@code assert*()} methods |
|
|
|
* throw {@link AssertionError}s. |
|
|
|
* throw {@link AssertionError}s. |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Sam Brannen |
|
|
|
* @author Sam Brannen |
|
|
|
@ -48,53 +48,44 @@ public abstract class ModelAndViewAssert { |
|
|
|
* Checks whether the model value under the given {@code modelName} |
|
|
|
* Checks whether the model value under the given {@code modelName} |
|
|
|
* exists and checks it type, based on the {@code expectedType}. If the |
|
|
|
* exists and checks it type, based on the {@code expectedType}. If the |
|
|
|
* model entry exists and the type matches, the model value is returned. |
|
|
|
* model entry exists and the type matches, the model value is returned. |
|
|
|
* |
|
|
|
|
|
|
|
* @param mav ModelAndView to test against (never {@code null}) |
|
|
|
* @param mav ModelAndView to test against (never {@code null}) |
|
|
|
* @param modelName name of the object to add to the model (never |
|
|
|
* @param modelName name of the object to add to the model (never {@code null}) |
|
|
|
* {@code null}) |
|
|
|
|
|
|
|
* @param expectedType expected type of the model value |
|
|
|
* @param expectedType expected type of the model value |
|
|
|
* @return the model value |
|
|
|
* @return the model value |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
public static <T> T assertAndReturnModelAttributeOfType(ModelAndView mav, String modelName, Class<T> expectedType) { |
|
|
|
public static <T> T assertAndReturnModelAttributeOfType(ModelAndView mav, String modelName, Class<T> expectedType) { |
|
|
|
assertTrue("ModelAndView is null", mav != null); |
|
|
|
assertTrue("ModelAndView is null", mav != null); |
|
|
|
assertTrue("Model is null", mav.getModel() != null); |
|
|
|
|
|
|
|
Object obj = mav.getModel().get(modelName); |
|
|
|
Object obj = mav.getModel().get(modelName); |
|
|
|
assertTrue("Model attribute with name '" + modelName + "' is null", obj != null); |
|
|
|
assertTrue("Model attribute with name '" + modelName + "' is null", obj != null); |
|
|
|
assertTrue("Model attribute is not of expected type '" + expectedType.getName() + "' but rather of type '" |
|
|
|
assertTrue("Model attribute is not of expected type '" + expectedType.getName() + "' but rather of type '" + |
|
|
|
+ obj.getClass().getName() + "'", expectedType.isAssignableFrom(obj.getClass())); |
|
|
|
obj.getClass().getName() + "'", expectedType.isAssignableFrom(obj.getClass())); |
|
|
|
return (T) obj; |
|
|
|
return (T) obj; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Compare each individual entry in a list, without first sorting the lists. |
|
|
|
* Compare each individual entry in a list, without first sorting the lists. |
|
|
|
* |
|
|
|
|
|
|
|
* @param mav ModelAndView to test against (never {@code null}) |
|
|
|
* @param mav ModelAndView to test against (never {@code null}) |
|
|
|
* @param modelName name of the object to add to the model (never |
|
|
|
* @param modelName name of the object to add to the model (never {@code null}) |
|
|
|
* {@code null}) |
|
|
|
|
|
|
|
* @param expectedList the expected list |
|
|
|
* @param expectedList the expected list |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@SuppressWarnings("rawtypes") |
|
|
|
@SuppressWarnings("rawtypes") |
|
|
|
public static void assertCompareListModelAttribute(ModelAndView mav, String modelName, List expectedList) { |
|
|
|
public static void assertCompareListModelAttribute(ModelAndView mav, String modelName, List expectedList) { |
|
|
|
assertTrue("ModelAndView is null", mav != null); |
|
|
|
assertTrue("ModelAndView is null", mav != null); |
|
|
|
List modelList = assertAndReturnModelAttributeOfType(mav, modelName, List.class); |
|
|
|
List modelList = assertAndReturnModelAttributeOfType(mav, modelName, List.class); |
|
|
|
assertTrue( |
|
|
|
assertTrue("Size of model list is '" + modelList.size() + "' while size of expected list is '" + |
|
|
|
"Size of model list is '" + modelList.size() + "' while size of expected list is '" + expectedList.size() |
|
|
|
expectedList.size() + "'", expectedList.size() == modelList.size()); |
|
|
|
+ "'", expectedList.size() == modelList.size()); |
|
|
|
|
|
|
|
assertTrue("List in model under name '" + modelName + "' is not equal to the expected list.", |
|
|
|
assertTrue("List in model under name '" + modelName + "' is not equal to the expected list.", |
|
|
|
expectedList.equals(modelList)); |
|
|
|
expectedList.equals(modelList)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Assert whether or not a model attribute is available. |
|
|
|
* Assert whether or not a model attribute is available. |
|
|
|
* |
|
|
|
|
|
|
|
* @param mav ModelAndView to test against (never {@code null}) |
|
|
|
* @param mav ModelAndView to test against (never {@code null}) |
|
|
|
* @param modelName name of the object to add to the model (never |
|
|
|
* @param modelName name of the object to add to the model (never {@code null}) |
|
|
|
* {@code null}) |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static void assertModelAttributeAvailable(ModelAndView mav, String modelName) { |
|
|
|
public static void assertModelAttributeAvailable(ModelAndView mav, String modelName) { |
|
|
|
assertTrue("ModelAndView is null", mav != null); |
|
|
|
assertTrue("ModelAndView is null", mav != null); |
|
|
|
assertTrue("Model is null", mav.getModel() != null); |
|
|
|
|
|
|
|
assertTrue("Model attribute with name '" + modelName + "' is not available", |
|
|
|
assertTrue("Model attribute with name '" + modelName + "' is not available", |
|
|
|
mav.getModel().containsKey(modelName)); |
|
|
|
mav.getModel().containsKey(modelName)); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -102,40 +93,37 @@ public abstract class ModelAndViewAssert { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Compare a given {@code expectedValue} to the value from the model |
|
|
|
* Compare a given {@code expectedValue} to the value from the model |
|
|
|
* bound under the given {@code modelName}. |
|
|
|
* bound under the given {@code modelName}. |
|
|
|
* |
|
|
|
|
|
|
|
* @param mav ModelAndView to test against (never {@code null}) |
|
|
|
* @param mav ModelAndView to test against (never {@code null}) |
|
|
|
* @param modelName name of the object to add to the model (never |
|
|
|
* @param modelName name of the object to add to the model (never {@code null}) |
|
|
|
* {@code null}) |
|
|
|
|
|
|
|
* @param expectedValue the model value |
|
|
|
* @param expectedValue the model value |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static void assertModelAttributeValue(ModelAndView mav, String modelName, Object expectedValue) { |
|
|
|
public static void assertModelAttributeValue(ModelAndView mav, String modelName, Object expectedValue) { |
|
|
|
assertTrue("ModelAndView is null", mav != null); |
|
|
|
assertTrue("ModelAndView is null", mav != null); |
|
|
|
Object modelValue = assertAndReturnModelAttributeOfType(mav, modelName, Object.class); |
|
|
|
Object modelValue = assertAndReturnModelAttributeOfType(mav, modelName, Object.class); |
|
|
|
assertTrue("Model value with name '" + modelName + "' is not the same as the expected value which was '" |
|
|
|
assertTrue("Model value with name '" + modelName + "' is not the same as the expected value which was '" + |
|
|
|
+ expectedValue + "'", modelValue.equals(expectedValue)); |
|
|
|
expectedValue + "'", modelValue.equals(expectedValue)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Inspect the {@code expectedModel} to see if all elements in the |
|
|
|
* Inspect the {@code expectedModel} to see if all elements in the |
|
|
|
* model appear and are equal. |
|
|
|
* model appear and are equal. |
|
|
|
* |
|
|
|
|
|
|
|
* @param mav ModelAndView to test against (never {@code null}) |
|
|
|
* @param mav ModelAndView to test against (never {@code null}) |
|
|
|
* @param expectedModel the expected model |
|
|
|
* @param expectedModel the expected model |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static void assertModelAttributeValues(ModelAndView mav, Map<String, Object> expectedModel) { |
|
|
|
public static void assertModelAttributeValues(ModelAndView mav, Map<String, Object> expectedModel) { |
|
|
|
assertTrue("ModelAndView is null", mav != null); |
|
|
|
assertTrue("ModelAndView is null", mav != null); |
|
|
|
assertTrue("Model is null", mav.getModel() != null); |
|
|
|
Map<String, Object> model = mav.getModel(); |
|
|
|
|
|
|
|
|
|
|
|
if (!mav.getModel().keySet().equals(expectedModel.keySet())) { |
|
|
|
if (!model.keySet().equals(expectedModel.keySet())) { |
|
|
|
StringBuilder sb = new StringBuilder("Keyset of expected model does not match.\n"); |
|
|
|
StringBuilder sb = new StringBuilder("Keyset of expected model does not match.\n"); |
|
|
|
appendNonMatchingSetsErrorMessage(expectedModel.keySet(), mav.getModel().keySet(), sb); |
|
|
|
appendNonMatchingSetsErrorMessage(expectedModel.keySet(), model.keySet(), sb); |
|
|
|
fail(sb.toString()); |
|
|
|
fail(sb.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
for (String modelName : mav.getModel().keySet()) { |
|
|
|
for (String modelName : model.keySet()) { |
|
|
|
Object assertionValue = expectedModel.get(modelName); |
|
|
|
Object assertionValue = expectedModel.get(modelName); |
|
|
|
Object mavValue = mav.getModel().get(modelName); |
|
|
|
Object mavValue = model.get(modelName); |
|
|
|
if (!assertionValue.equals(mavValue)) { |
|
|
|
if (!assertionValue.equals(mavValue)) { |
|
|
|
sb.append("Value under name '").append(modelName).append("' differs, should have been '").append( |
|
|
|
sb.append("Value under name '").append(modelName).append("' differs, should have been '").append( |
|
|
|
assertionValue).append("' but was '").append(mavValue).append("'\n"); |
|
|
|
assertionValue).append("' but was '").append(mavValue).append("'\n"); |
|
|
|
@ -151,25 +139,20 @@ public abstract class ModelAndViewAssert { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Compare each individual entry in a list after having sorted both lists |
|
|
|
* Compare each individual entry in a list after having sorted both lists |
|
|
|
* (optionally using a comparator). |
|
|
|
* (optionally using a comparator). |
|
|
|
* |
|
|
|
|
|
|
|
* @param mav ModelAndView to test against (never {@code null}) |
|
|
|
* @param mav ModelAndView to test against (never {@code null}) |
|
|
|
* @param modelName name of the object to add to the model (never |
|
|
|
* @param modelName name of the object to add to the model (never {@code null}) |
|
|
|
* {@code null}) |
|
|
|
|
|
|
|
* @param expectedList the expected list |
|
|
|
* @param expectedList the expected list |
|
|
|
* @param comparator the comparator to use (may be {@code null}). If |
|
|
|
* @param comparator the comparator to use (may be {@code null}). If not |
|
|
|
* not specifying the comparator, both lists will be sorted not using any |
|
|
|
* specifying the comparator, both lists will be sorted not using any comparator. |
|
|
|
* comparator. |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@SuppressWarnings({ "unchecked", "rawtypes" }) |
|
|
|
@SuppressWarnings({"unchecked", "rawtypes"}) |
|
|
|
public static void assertSortAndCompareListModelAttribute(ModelAndView mav, String modelName, List expectedList, |
|
|
|
public static void assertSortAndCompareListModelAttribute( |
|
|
|
Comparator comparator) { |
|
|
|
ModelAndView mav, String modelName, List expectedList, Comparator comparator) { |
|
|
|
|
|
|
|
|
|
|
|
assertTrue("ModelAndView is null", mav != null); |
|
|
|
assertTrue("ModelAndView is null", mav != null); |
|
|
|
List modelList = assertAndReturnModelAttributeOfType(mav, modelName, List.class); |
|
|
|
List modelList = assertAndReturnModelAttributeOfType(mav, modelName, List.class); |
|
|
|
|
|
|
|
assertTrue("Size of model list is '" + modelList.size() + "' while size of expected list is '" + |
|
|
|
assertTrue( |
|
|
|
expectedList.size() + "'", expectedList.size() == modelList.size()); |
|
|
|
"Size of model list is '" + modelList.size() + "' while size of expected list is '" + expectedList.size() |
|
|
|
|
|
|
|
+ "'", expectedList.size() == modelList.size()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (comparator != null) { |
|
|
|
if (comparator != null) { |
|
|
|
Collections.sort(modelList, comparator); |
|
|
|
Collections.sort(modelList, comparator); |
|
|
|
@ -187,7 +170,6 @@ public abstract class ModelAndViewAssert { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Check to see if the view name in the ModelAndView matches the given |
|
|
|
* Check to see if the view name in the ModelAndView matches the given |
|
|
|
* {@code expectedName}. |
|
|
|
* {@code expectedName}. |
|
|
|
* |
|
|
|
|
|
|
|
* @param mav ModelAndView to test against (never {@code null}) |
|
|
|
* @param mav ModelAndView to test against (never {@code null}) |
|
|
|
* @param expectedName the name of the model value |
|
|
|
* @param expectedName the name of the model value |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@ -197,14 +179,14 @@ public abstract class ModelAndViewAssert { |
|
|
|
ObjectUtils.nullSafeEquals(expectedName, mav.getViewName())); |
|
|
|
ObjectUtils.nullSafeEquals(expectedName, mav.getViewName())); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static void appendNonMatchingSetsErrorMessage(Set<String> assertionSet, Set<String> incorrectSet, |
|
|
|
|
|
|
|
StringBuilder sb) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Set<String> tempSet = new HashSet<String>(); |
|
|
|
private static void appendNonMatchingSetsErrorMessage( |
|
|
|
tempSet.addAll(incorrectSet); |
|
|
|
Set<String> assertionSet, Set<String> incorrectSet, StringBuilder sb) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Set<String> tempSet = new HashSet<String>(incorrectSet); |
|
|
|
tempSet.removeAll(assertionSet); |
|
|
|
tempSet.removeAll(assertionSet); |
|
|
|
|
|
|
|
|
|
|
|
if (tempSet.size() > 0) { |
|
|
|
if (!tempSet.isEmpty()) { |
|
|
|
sb.append("Set has too many elements:\n"); |
|
|
|
sb.append("Set has too many elements:\n"); |
|
|
|
for (Object element : tempSet) { |
|
|
|
for (Object element : tempSet) { |
|
|
|
sb.append('-'); |
|
|
|
sb.append('-'); |
|
|
|
@ -213,11 +195,10 @@ public abstract class ModelAndViewAssert { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
tempSet = new HashSet<String>(); |
|
|
|
tempSet = new HashSet<String>(assertionSet); |
|
|
|
tempSet.addAll(assertionSet); |
|
|
|
|
|
|
|
tempSet.removeAll(incorrectSet); |
|
|
|
tempSet.removeAll(incorrectSet); |
|
|
|
|
|
|
|
|
|
|
|
if (tempSet.size() > 0) { |
|
|
|
if (!tempSet.isEmpty()) { |
|
|
|
sb.append("Set is missing elements:\n"); |
|
|
|
sb.append("Set is missing elements:\n"); |
|
|
|
for (Object element : tempSet) { |
|
|
|
for (Object element : tempSet) { |
|
|
|
sb.append('-'); |
|
|
|
sb.append('-'); |
|
|
|
|