Skip to content

Commit c8e2329

Browse files
committed
consistent use of model name Strings
1 parent e3cc923 commit c8e2329

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

org.springframework.test/src/main/java/org/springframework/test/web/AbstractModelAndViewTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public abstract class AbstractModelAndViewTests extends TestCase {
5555
* @param expectedType expected type of the model value
5656
* @return the model value
5757
*/
58-
protected Object assertAndReturnModelAttributeOfType(ModelAndView mav, Object modelName, Class expectedType) {
58+
protected <T> T assertAndReturnModelAttributeOfType(ModelAndView mav, String modelName, Class<T> expectedType) {
5959
try {
6060
return ModelAndViewAssert.assertAndReturnModelAttributeOfType(mav, modelName, expectedType);
6161
}
@@ -71,7 +71,7 @@ protected Object assertAndReturnModelAttributeOfType(ModelAndView mav, Object mo
7171
* <code>null</code>)
7272
* @param expectedList the expected list
7373
*/
74-
protected void assertCompareListModelAttribute(ModelAndView mav, Object modelName, List expectedList) {
74+
protected void assertCompareListModelAttribute(ModelAndView mav, String modelName, List expectedList) {
7575
try {
7676
ModelAndViewAssert.assertCompareListModelAttribute(mav, modelName, expectedList);
7777
}
@@ -86,7 +86,7 @@ protected void assertCompareListModelAttribute(ModelAndView mav, Object modelNam
8686
* @param modelName name of the object to add to the model (never
8787
* <code>null</code>)
8888
*/
89-
protected void assertModelAttributeAvailable(ModelAndView mav, Object modelName) {
89+
protected void assertModelAttributeAvailable(ModelAndView mav, String modelName) {
9090
try {
9191
ModelAndViewAssert.assertModelAttributeAvailable(mav, modelName);
9292
}
@@ -103,7 +103,7 @@ protected void assertModelAttributeAvailable(ModelAndView mav, Object modelName)
103103
* <code>null</code>)
104104
* @param expectedValue the model value
105105
*/
106-
protected void assertModelAttributeValue(ModelAndView mav, Object modelName, Object expectedValue) {
106+
protected void assertModelAttributeValue(ModelAndView mav, String modelName, Object expectedValue) {
107107
try {
108108
ModelAndViewAssert.assertModelAttributeValue(mav, modelName, expectedValue);
109109
}
@@ -118,7 +118,7 @@ protected void assertModelAttributeValue(ModelAndView mav, Object modelName, Obj
118118
* @param mav ModelAndView to test against (never <code>null</code>)
119119
* @param expectedModel the expected model
120120
*/
121-
protected void assertModelAttributeValues(ModelAndView mav, Map expectedModel) {
121+
protected void assertModelAttributeValues(ModelAndView mav, Map<String, Object> expectedModel) {
122122
try {
123123
ModelAndViewAssert.assertModelAttributeValues(mav, expectedModel);
124124
}
@@ -139,7 +139,7 @@ protected void assertModelAttributeValues(ModelAndView mav, Map expectedModel) {
139139
* any comparator.
140140
*/
141141
protected void assertSortAndCompareListModelAttribute(
142-
ModelAndView mav, Object modelName, List expectedList, Comparator comparator) {
142+
ModelAndView mav, String modelName, List expectedList, Comparator comparator) {
143143
try {
144144
ModelAndViewAssert.assertSortAndCompareListModelAttribute(mav, modelName, expectedList, comparator);
145145
}

org.springframework.test/src/main/java/org/springframework/test/web/ModelAndViewAssert.java

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,11 @@ public abstract class ModelAndViewAssert {
5252
* @return the model value
5353
*/
5454
@SuppressWarnings("unchecked")
55-
public static <T> T assertAndReturnModelAttributeOfType(ModelAndView mav, Object modelName, Class<T> expectedType)
56-
throws AssertionError {
57-
55+
public static <T> T assertAndReturnModelAttributeOfType(ModelAndView mav, String modelName, Class<T> expectedType) {
5856
assertCondition(mav != null, "ModelAndView is null");
5957
assertCondition(mav.getModel() != null, "Model is null");
6058
Object obj = mav.getModel().get(modelName);
6159
assertCondition(obj != null, "Model attribute with name '" + modelName + "' is null");
62-
6360
assertCondition(expectedType.isAssignableFrom(obj.getClass()), "Model attribute is not of expected type '" +
6461
expectedType.getName() + "' but rather of type '" + obj.getClass().getName() + "'");
6562
return (T) obj;
@@ -72,11 +69,9 @@ public static <T> T assertAndReturnModelAttributeOfType(ModelAndView mav, Object
7269
* <code>null</code>)
7370
* @param expectedList the expected list
7471
*/
75-
public static void assertCompareListModelAttribute(ModelAndView mav, Object modelName, List expectedList)
76-
throws AssertionError {
77-
72+
public static void assertCompareListModelAttribute(ModelAndView mav, String modelName, List expectedList) {
7873
assertCondition(mav != null, "ModelAndView is null");
79-
List modelList = (List) assertAndReturnModelAttributeOfType(mav, modelName, List.class);
74+
List modelList = assertAndReturnModelAttributeOfType(mav, modelName, List.class);
8075
assertCondition(expectedList.size() == modelList.size(), "Size of model list is '" + modelList.size() +
8176
"' while size of expected list is '" + expectedList.size() + "'");
8277
assertCondition(expectedList.equals(modelList), "List in model under name '" + modelName +
@@ -89,7 +84,7 @@ public static void assertCompareListModelAttribute(ModelAndView mav, Object mode
8984
* @param modelName name of the object to add to the model (never
9085
* <code>null</code>)
9186
*/
92-
public static void assertModelAttributeAvailable(ModelAndView mav, Object modelName) throws AssertionError {
87+
public static void assertModelAttributeAvailable(ModelAndView mav, String modelName) {
9388
assertCondition(mav != null, "ModelAndView is null");
9489
assertCondition(mav.getModel() != null, "Model is null");
9590
assertCondition(mav.getModel().containsKey(modelName), "Model attribute with name '" + modelName +
@@ -104,7 +99,7 @@ public static void assertModelAttributeAvailable(ModelAndView mav, Object modelN
10499
* <code>null</code>)
105100
* @param expectedValue the model value
106101
*/
107-
public static void assertModelAttributeValue(ModelAndView mav, Object modelName, Object expectedValue) {
102+
public static void assertModelAttributeValue(ModelAndView mav, String modelName, Object expectedValue) {
108103
assertCondition(mav != null, "ModelAndView is null");
109104
Object modelValue = assertAndReturnModelAttributeOfType(mav, modelName, Object.class);
110105
assertCondition(modelValue.equals(expectedValue), "Model value with name '" + modelName +
@@ -156,7 +151,7 @@ public static void assertModelAttributeValues(ModelAndView mav, Map<String, Obje
156151
*/
157152
@SuppressWarnings("unchecked")
158153
public static void assertSortAndCompareListModelAttribute(
159-
ModelAndView mav, Object modelName, List expectedList, Comparator comparator) {
154+
ModelAndView mav, String modelName, List expectedList, Comparator comparator) {
160155

161156
assertCondition(mav != null, "ModelAndView is null");
162157
List modelList = assertAndReturnModelAttributeOfType(mav, modelName, List.class);
@@ -214,17 +209,19 @@ private static void assertCondition(boolean condition, String message) {
214209
}
215210
}
216211

217-
private static void appendNonMatchingSetsErrorMessage(Set<String> assertionSet, Set<String> incorrectSet, StringBuilder buf) {
212+
private static void appendNonMatchingSetsErrorMessage(
213+
Set<String> assertionSet, Set<String> incorrectSet, StringBuilder sb) {
214+
218215
Set<String> tempSet = new HashSet<String>();
219216
tempSet.addAll(incorrectSet);
220217
tempSet.removeAll(assertionSet);
221218

222219
if (tempSet.size() > 0) {
223-
buf.append("Set has too many elements:\n");
220+
sb.append("Set has too many elements:\n");
224221
for (Object element : tempSet) {
225-
buf.append('-');
226-
buf.append(element);
227-
buf.append('\n');
222+
sb.append('-');
223+
sb.append(element);
224+
sb.append('\n');
228225
}
229226
}
230227

@@ -233,11 +230,11 @@ private static void appendNonMatchingSetsErrorMessage(Set<String> assertionSet,
233230
tempSet.removeAll(incorrectSet);
234231

235232
if (tempSet.size() > 0) {
236-
buf.append("Set is missing elements:\n");
233+
sb.append("Set is missing elements:\n");
237234
for (Object element : tempSet) {
238-
buf.append('-');
239-
buf.append(element);
240-
buf.append('\n');
235+
sb.append('-');
236+
sb.append(element);
237+
sb.append('\n');
241238
}
242239
}
243240
}

0 commit comments

Comments
 (0)