From c8e23290ca0feaf2a8cacd4443dfc2488d9b0306 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 27 Nov 2008 21:24:42 +0000 Subject: [PATCH] consistent use of model name Strings --- .../test/web/AbstractModelAndViewTests.java | 12 +++--- .../test/web/ModelAndViewAssert.java | 37 +++++++++---------- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/org.springframework.test/src/main/java/org/springframework/test/web/AbstractModelAndViewTests.java b/org.springframework.test/src/main/java/org/springframework/test/web/AbstractModelAndViewTests.java index c7d0b753aa..494a6374f2 100644 --- a/org.springframework.test/src/main/java/org/springframework/test/web/AbstractModelAndViewTests.java +++ b/org.springframework.test/src/main/java/org/springframework/test/web/AbstractModelAndViewTests.java @@ -55,7 +55,7 @@ public abstract class AbstractModelAndViewTests extends TestCase { * @param expectedType expected type of the model value * @return the model value */ - protected Object assertAndReturnModelAttributeOfType(ModelAndView mav, Object modelName, Class expectedType) { + protected T assertAndReturnModelAttributeOfType(ModelAndView mav, String modelName, Class expectedType) { try { return ModelAndViewAssert.assertAndReturnModelAttributeOfType(mav, modelName, expectedType); } @@ -71,7 +71,7 @@ public abstract class AbstractModelAndViewTests extends TestCase { * null) * @param expectedList the expected list */ - protected void assertCompareListModelAttribute(ModelAndView mav, Object modelName, List expectedList) { + protected void assertCompareListModelAttribute(ModelAndView mav, String modelName, List expectedList) { try { ModelAndViewAssert.assertCompareListModelAttribute(mav, modelName, expectedList); } @@ -86,7 +86,7 @@ public abstract class AbstractModelAndViewTests extends TestCase { * @param modelName name of the object to add to the model (never * null) */ - protected void assertModelAttributeAvailable(ModelAndView mav, Object modelName) { + protected void assertModelAttributeAvailable(ModelAndView mav, String modelName) { try { ModelAndViewAssert.assertModelAttributeAvailable(mav, modelName); } @@ -103,7 +103,7 @@ public abstract class AbstractModelAndViewTests extends TestCase { * null) * @param expectedValue the model value */ - protected void assertModelAttributeValue(ModelAndView mav, Object modelName, Object expectedValue) { + protected void assertModelAttributeValue(ModelAndView mav, String modelName, Object expectedValue) { try { ModelAndViewAssert.assertModelAttributeValue(mav, modelName, expectedValue); } @@ -118,7 +118,7 @@ public abstract class AbstractModelAndViewTests extends TestCase { * @param mav ModelAndView to test against (never null) * @param expectedModel the expected model */ - protected void assertModelAttributeValues(ModelAndView mav, Map expectedModel) { + protected void assertModelAttributeValues(ModelAndView mav, Map expectedModel) { try { ModelAndViewAssert.assertModelAttributeValues(mav, expectedModel); } @@ -139,7 +139,7 @@ public abstract class AbstractModelAndViewTests extends TestCase { * any comparator. */ protected void assertSortAndCompareListModelAttribute( - ModelAndView mav, Object modelName, List expectedList, Comparator comparator) { + ModelAndView mav, String modelName, List expectedList, Comparator comparator) { try { ModelAndViewAssert.assertSortAndCompareListModelAttribute(mav, modelName, expectedList, comparator); } diff --git a/org.springframework.test/src/main/java/org/springframework/test/web/ModelAndViewAssert.java b/org.springframework.test/src/main/java/org/springframework/test/web/ModelAndViewAssert.java index b4937604f1..a938146936 100644 --- a/org.springframework.test/src/main/java/org/springframework/test/web/ModelAndViewAssert.java +++ b/org.springframework.test/src/main/java/org/springframework/test/web/ModelAndViewAssert.java @@ -52,14 +52,11 @@ public abstract class ModelAndViewAssert { * @return the model value */ @SuppressWarnings("unchecked") - public static T assertAndReturnModelAttributeOfType(ModelAndView mav, Object modelName, Class expectedType) - throws AssertionError { - + public static T assertAndReturnModelAttributeOfType(ModelAndView mav, String modelName, Class expectedType) { assertCondition(mav != null, "ModelAndView is null"); assertCondition(mav.getModel() != null, "Model is null"); Object obj = mav.getModel().get(modelName); assertCondition(obj != null, "Model attribute with name '" + modelName + "' is null"); - assertCondition(expectedType.isAssignableFrom(obj.getClass()), "Model attribute is not of expected type '" + expectedType.getName() + "' but rather of type '" + obj.getClass().getName() + "'"); return (T) obj; @@ -72,11 +69,9 @@ public abstract class ModelAndViewAssert { * null) * @param expectedList the expected list */ - public static void assertCompareListModelAttribute(ModelAndView mav, Object modelName, List expectedList) - throws AssertionError { - + public static void assertCompareListModelAttribute(ModelAndView mav, String modelName, List expectedList) { assertCondition(mav != null, "ModelAndView is null"); - List modelList = (List) assertAndReturnModelAttributeOfType(mav, modelName, List.class); + List modelList = assertAndReturnModelAttributeOfType(mav, modelName, List.class); assertCondition(expectedList.size() == modelList.size(), "Size of model list is '" + modelList.size() + "' while size of expected list is '" + expectedList.size() + "'"); assertCondition(expectedList.equals(modelList), "List in model under name '" + modelName + @@ -89,7 +84,7 @@ public abstract class ModelAndViewAssert { * @param modelName name of the object to add to the model (never * null) */ - public static void assertModelAttributeAvailable(ModelAndView mav, Object modelName) throws AssertionError { + public static void assertModelAttributeAvailable(ModelAndView mav, String modelName) { assertCondition(mav != null, "ModelAndView is null"); assertCondition(mav.getModel() != null, "Model is null"); assertCondition(mav.getModel().containsKey(modelName), "Model attribute with name '" + modelName + @@ -104,7 +99,7 @@ public abstract class ModelAndViewAssert { * null) * @param expectedValue the model value */ - public static void assertModelAttributeValue(ModelAndView mav, Object modelName, Object expectedValue) { + public static void assertModelAttributeValue(ModelAndView mav, String modelName, Object expectedValue) { assertCondition(mav != null, "ModelAndView is null"); Object modelValue = assertAndReturnModelAttributeOfType(mav, modelName, Object.class); assertCondition(modelValue.equals(expectedValue), "Model value with name '" + modelName + @@ -156,7 +151,7 @@ public abstract class ModelAndViewAssert { */ @SuppressWarnings("unchecked") public static void assertSortAndCompareListModelAttribute( - ModelAndView mav, Object modelName, List expectedList, Comparator comparator) { + ModelAndView mav, String modelName, List expectedList, Comparator comparator) { assertCondition(mav != null, "ModelAndView is null"); List modelList = assertAndReturnModelAttributeOfType(mav, modelName, List.class); @@ -214,17 +209,19 @@ public abstract class ModelAndViewAssert { } } - private static void appendNonMatchingSetsErrorMessage(Set assertionSet, Set incorrectSet, StringBuilder buf) { + private static void appendNonMatchingSetsErrorMessage( + Set assertionSet, Set incorrectSet, StringBuilder sb) { + Set tempSet = new HashSet(); tempSet.addAll(incorrectSet); tempSet.removeAll(assertionSet); if (tempSet.size() > 0) { - buf.append("Set has too many elements:\n"); + sb.append("Set has too many elements:\n"); for (Object element : tempSet) { - buf.append('-'); - buf.append(element); - buf.append('\n'); + sb.append('-'); + sb.append(element); + sb.append('\n'); } } @@ -233,11 +230,11 @@ public abstract class ModelAndViewAssert { tempSet.removeAll(incorrectSet); if (tempSet.size() > 0) { - buf.append("Set is missing elements:\n"); + sb.append("Set is missing elements:\n"); for (Object element : tempSet) { - buf.append('-'); - buf.append(element); - buf.append('\n'); + sb.append('-'); + sb.append(element); + sb.append('\n'); } } }