001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.test.web;
018:
019: import java.util.Collections;
020: import java.util.Comparator;
021: import java.util.HashSet;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.Set;
026:
027: import junit.framework.TestCase;
028:
029: import org.springframework.web.servlet.ModelAndView;
030:
031: /**
032: * Convenient base class for tests dealing with Spring web MVC
033: * {@link org.springframework.web.servlet.ModelAndView} objects.
034: *
035: * @author Alef Arendsen
036: * @author Bram Smeets
037: * @since 2.0
038: * @see org.springframework.web.servlet.ModelAndView
039: */
040: public abstract class AbstractModelAndViewTests extends TestCase {
041:
042: /**
043: * Assert whether or not a model attribute is available.
044: */
045: protected void assertModelAttributeAvailable(ModelAndView mav,
046: Object key) {
047: assertNotNull("Model is null", mav.getModel());
048: assertTrue("Model attribute with name '" + key
049: + "' is not available", mav.getModel().containsKey(key));
050: }
051:
052: /**
053: * Compare each individual entry in a list, not sorting the lists first.
054: */
055: protected void assertCompareListModelAttribute(ModelAndView mav,
056: Object key, List assertionList) {
057: List modelList = (List) assertAndReturnModelAttributeOfType(
058: mav, key, List.class);
059: assertEquals("Size of model list is '" + modelList.size()
060: + "' while size of assertion list is '"
061: + assertionList.size() + "'", assertionList.size(),
062: modelList.size());
063: assertEquals("List in model under key '" + key
064: + "' is not equals to given list", assertionList,
065: modelList);
066: }
067:
068: /**
069: * Compare each individual entry in a list after having sorted both lists
070: * (optionally using a comparator).
071: * @param comp the comparator to use. If not specifying the comparator,
072: * both lists will be sorted not using any comparator.
073: */
074: protected void assertSortAndCompareListModelAttribute(
075: ModelAndView mav, Object key, List assertionList,
076: Comparator comp) {
077:
078: List modelList = (List) assertAndReturnModelAttributeOfType(
079: mav, key, List.class);
080: assertEquals("Size of model list is '" + modelList.size()
081: + "' while size of assertion list is '"
082: + assertionList.size() + "'", assertionList.size(),
083: modelList.size());
084:
085: if (comp != null) {
086: Collections.sort(modelList, comp);
087: Collections.sort(assertionList, comp);
088: } else {
089: Collections.sort(modelList);
090: Collections.sort(assertionList);
091: }
092: assertEquals("List in model under key '" + key
093: + "' is not equals to given list", assertionList,
094: modelList);
095: }
096:
097: /**
098: * Checks whether the given model key exists and checks it type, based
099: * on the given type. If the model entry exists and the type matches,
100: * the given model value is returned.
101: */
102: protected Object assertAndReturnModelAttributeOfType(
103: ModelAndView mav, Object key, Class type) {
104: assertNotNull("Model is null", mav.getModel());
105: assertNotNull("Model attribute with key '" + key + "' is null",
106: mav.getModel().get(key));
107: Object obj = mav.getModel().get(key);
108: assertTrue("Model attribute is not of type '" + type.getName()
109: + "' but is a '" + obj.getClass().getName() + "'", type
110: .isAssignableFrom(obj.getClass()));
111: return obj;
112: }
113:
114: /**
115: * Check to see if the view name in the ModelAndView matches the given String.
116: */
117: protected void assertViewName(ModelAndView mav, String name) {
118: assertEquals("View name is not equal to '" + name
119: + "' but was '" + mav.getViewName() + "'", name, mav
120: .getViewName());
121: }
122:
123: /**
124: * Compare a given Object to the value from the model bound under the given key.
125: */
126: protected void assertModelAttributeValue(ModelAndView mav,
127: Object key, Object value) {
128: Object modelValue = assertAndReturnModelAttributeOfType(mav,
129: key, Object.class);
130: assertEquals("Model value with key '" + key
131: + "' is not the same as given value which was '"
132: + value + "'", value, modelValue);
133: }
134:
135: /**
136: * Inspect the given model to see if all elements in the model appear and are equal
137: */
138: protected void assertModelAttributeValues(ModelAndView mav,
139: Map assertionModel) {
140: assertNotNull(mav.getModel());
141:
142: if (!mav.getModel().keySet().equals(assertionModel.keySet())) {
143: StringBuffer buf = new StringBuffer(
144: "Keyset of given model does not match.\n");
145: appendNonMatchingSetsErrorMessage(assertionModel.keySet(),
146: mav.getModel().keySet(), buf);
147: fail(buf.toString());
148: }
149:
150: StringBuffer buf = new StringBuffer();
151: Iterator it = mav.getModel().keySet().iterator();
152: while (it.hasNext()) {
153: Object key = (Object) it.next();
154: Object assertionValue = assertionModel.get(key);
155: Object mavValue = mav.getModel().get(key);
156: if (!assertionValue.equals(mavValue)) {
157: buf.append("Value under key '" + key
158: + "' differs, should have been '"
159: + assertionValue + "' but was '" + mavValue
160: + "'\n");
161: }
162: }
163:
164: if (buf.length() != 0) {
165: buf.insert(0, "Values of given model do not match.\n");
166: fail(buf.toString());
167: }
168: }
169:
170: private void appendNonMatchingSetsErrorMessage(Set assertionSet,
171: Set incorrectSet, StringBuffer buf) {
172: Set tempSet = new HashSet();
173: tempSet.addAll(incorrectSet);
174: tempSet.removeAll(assertionSet);
175:
176: if (tempSet.size() > 0) {
177: buf.append("Set has too many elements:\n");
178: Iterator it = tempSet.iterator();
179: while (it.hasNext()) {
180: Object o = (Object) it.next();
181: buf.append('-');
182: buf.append(o.toString());
183: buf.append('\n');
184: }
185: }
186:
187: tempSet = new HashSet();
188: tempSet.addAll(assertionSet);
189: tempSet.removeAll(incorrectSet);
190:
191: if (tempSet.size() > 0) {
192: buf.append("Set is missing elements:\n");
193: Iterator it = tempSet.iterator();
194: while (it.hasNext()) {
195: Object o = (Object) it.next();
196: buf.append('-');
197: buf.append(o.toString());
198: buf.append('\n');
199: }
200: }
201: }
202:
203: }
|