KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > test > web > AbstractModelAndViewTests


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.test.web;
18
19 import java.util.Collections JavaDoc;
20 import java.util.Comparator JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import junit.framework.TestCase;
28
29 import org.springframework.web.servlet.ModelAndView;
30
31 /**
32  * Convenient base class for tests dealing with Spring web MVC
33  * {@link org.springframework.web.servlet.ModelAndView} objects.
34  *
35  * @author Alef Arendsen
36  * @author Bram Smeets
37  * @since 2.0
38  * @see org.springframework.web.servlet.ModelAndView
39  */

40 public abstract class AbstractModelAndViewTests extends TestCase {
41     
42     /**
43      * Assert whether or not a model attribute is available.
44      */

45     protected void assertModelAttributeAvailable(ModelAndView mav, Object JavaDoc key) {
46         assertNotNull("Model is null", mav.getModel());
47         assertTrue("Model attribute with name '" + key + "' is not available",
48                 mav.getModel().containsKey(key));
49     }
50     
51     /**
52      * Compare each individual entry in a list, not sorting the lists first.
53      */

54     protected void assertCompareListModelAttribute(ModelAndView mav, Object JavaDoc key, List JavaDoc assertionList) {
55         List JavaDoc modelList = (List JavaDoc)assertAndReturnModelAttributeOfType(mav, key, List JavaDoc.class);
56         assertEquals("Size of model list is '" + modelList.size() +
57                 "' while size of assertion list is '" + assertionList.size() + "'",
58                 assertionList.size(), modelList.size());
59         assertEquals("List in model under key '" + key + "' is not equals to given list", assertionList, modelList);
60     }
61     
62     /**
63      * Compare each individual entry in a list after having sorted both lists
64      * (optionally using a comparator).
65      * @param comp the comparator to use. If not specifying the comparator,
66      * both lists will be sorted not using any comparator.
67      */

68     protected void assertSortAndCompareListModelAttribute(
69             ModelAndView mav, Object JavaDoc key, List JavaDoc assertionList, Comparator JavaDoc comp) {
70
71         List JavaDoc modelList = (List JavaDoc)assertAndReturnModelAttributeOfType(mav, key, List JavaDoc.class);
72         assertEquals("Size of model list is '" + modelList.size() +
73                 "' while size of assertion list is '" + assertionList.size() + "'",
74                 assertionList.size(), modelList.size());
75
76         if (comp != null) {
77             Collections.sort(modelList, comp);
78             Collections.sort(assertionList, comp);
79         }
80         else {
81             Collections.sort(modelList);
82             Collections.sort(assertionList);
83         }
84         assertEquals("List in model under key '" + key + "' is not equals to given list", assertionList, modelList);
85     }
86     
87     /**
88      * Checks whether the given model key exists and checks it type, based
89      * on the given type. If the model entry exists and the type matches,
90      * the given model value is returned.
91      */

92     protected Object JavaDoc assertAndReturnModelAttributeOfType(ModelAndView mav, Object JavaDoc key, Class JavaDoc type) {
93         assertNotNull("Model is null", mav.getModel());
94         assertNotNull("Model attribute with key '" + key + "' is null", mav.getModel().get(key));
95         Object JavaDoc obj = mav.getModel().get(key);
96         assertTrue("Model attribute is not of type '" + type.getName() + "' but is a '" +
97                 obj.getClass().getName() + "'", type.isAssignableFrom(obj.getClass()));
98         return obj;
99     }
100     
101     /**
102      * Check to see if the view name in the ModelAndView matches the given String.
103      */

104     protected void assertViewName(ModelAndView mav, String JavaDoc name) {
105         assertEquals("View name is not equal to '" + name + "' but was '" + mav.getViewName() + "'",
106                 name, mav.getViewName());
107     }
108     
109     /**
110      * Compare a given Object to the value from the model bound under the given key.
111      */

112     protected void assertModelAttributeValue(ModelAndView mav, Object JavaDoc key, Object JavaDoc value) {
113         Object JavaDoc modelValue = assertAndReturnModelAttributeOfType(mav, key, Object JavaDoc.class);
114         assertEquals("Model value with key '" + key + "' is not the same as given value which was '" + value + "'",
115                 value, modelValue);
116     }
117     
118     /**
119      * Inspect the given model to see if all elements in the model appear and are equal
120      */

121     protected void assertModelAttributeValues(ModelAndView mav, Map JavaDoc assertionModel) {
122         assertNotNull(mav.getModel());
123         
124         if (!mav.getModel().keySet().equals(assertionModel.keySet())) {
125             StringBuffer JavaDoc buf = new StringBuffer JavaDoc("Keyset of given model does not match.\n");
126             appendNonMatchingSetsErrorMessage(assertionModel.keySet(), mav.getModel().keySet(), buf);
127             fail(buf.toString());
128         }
129         
130         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
131         Iterator JavaDoc it = mav.getModel().keySet().iterator();
132         while (it.hasNext()) {
133             Object JavaDoc key = (Object JavaDoc) it.next();
134             Object JavaDoc assertionValue = assertionModel.get(key);
135             Object JavaDoc mavValue = mav.getModel().get(key);
136             if (!assertionValue.equals(mavValue)) {
137                 buf.append("Value under key '" + key + "' differs, should have been '" +
138                         assertionValue + "' but was '" + mavValue +"'\n");
139             }
140         }
141
142         if (buf.length() != 0) {
143              buf.insert(0, "Values of given model do not match.\n");
144              fail(buf.toString());
145         }
146     }
147
148     private void appendNonMatchingSetsErrorMessage(Set JavaDoc assertionSet, Set JavaDoc incorrectSet, StringBuffer JavaDoc buf) {
149         Set JavaDoc tempSet = new HashSet JavaDoc();
150         tempSet.addAll(incorrectSet);
151         tempSet.removeAll(assertionSet);
152         
153         if (tempSet.size() > 0) {
154             buf.append("Set has too many elements:\n");
155             Iterator JavaDoc it = tempSet.iterator();
156             while (it.hasNext()) {
157                 Object JavaDoc o = (Object JavaDoc) it.next();
158                 buf.append('-');
159                 buf.append(o.toString());
160                 buf.append('\n');
161             }
162         }
163         
164         tempSet = new HashSet JavaDoc();
165         tempSet.addAll(assertionSet);
166         tempSet.removeAll(incorrectSet);
167         
168         if (tempSet.size() > 0) {
169             buf.append("Set is missing elements:\n");
170             Iterator JavaDoc it = tempSet.iterator();
171             while (it.hasNext()) {
172                 Object JavaDoc o = (Object JavaDoc) it.next();
173                 buf.append('-');
174                 buf.append(o.toString());
175                 buf.append('\n');
176             }
177         }
178     }
179     
180 }
181
Popular Tags