Source Code Cross Referenced for AbstractModelAndViewTests.java in  » J2EE » spring-framework-2.0.6 » org » springframework » test » web » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » spring framework 2.0.6 » org.springframework.test.web 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.