KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > BaseViewTests


1 /*
2  * Copyright 2002-2005 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.web.servlet.view;
18
19 import java.io.IOException JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Properties JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import javax.servlet.ServletException JavaDoc;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29
30 import junit.framework.TestCase;
31 import org.easymock.MockControl;
32
33 import org.springframework.context.ApplicationContextException;
34 import org.springframework.mock.web.MockHttpServletResponse;
35 import org.springframework.mock.web.MockHttpServletRequest;
36 import org.springframework.web.context.WebApplicationContext;
37
38 /**
39  * Tests for AbstractView. Not called AbstractViewTests as
40  * would otherwise be excluded by Ant build script wildcard.
41  *
42  * @author Rod Johnson
43  */

44 public class BaseViewTests extends TestCase {
45
46     public void testRenderWithoutStaticAttributes() throws Exception JavaDoc {
47         MockControl mc = MockControl.createControl(WebApplicationContext.class);
48         WebApplicationContext wac = (WebApplicationContext) mc.getMock();
49         mc.replay();
50         HttpServletRequest JavaDoc request = new MockHttpServletRequest();
51         HttpServletResponse JavaDoc response = new MockHttpServletResponse();
52         TestView tv = new TestView(request, response, wac);
53         
54         // Check superclass handles duplicate init
55
tv.setApplicationContext(wac);
56         tv.setApplicationContext(wac);
57         
58         Map JavaDoc model = new HashMap JavaDoc();
59         model.put("foo", "bar");
60         model.put("something", new Object JavaDoc());
61         tv.render(model, request, response);
62
63         // check it contains all
64
checkContainsAll(model, tv.model);
65         
66         assertTrue(tv.inited);
67         mc.verify();
68     }
69     
70     /**
71      * Test attribute passing, NOT CSV parsing.
72      */

73     public void testRenderWithStaticAttributesNoCollision() throws Exception JavaDoc {
74         MockControl mc = MockControl.createControl(WebApplicationContext.class);
75         WebApplicationContext wac = (WebApplicationContext) mc.getMock();
76         mc.replay();
77         HttpServletRequest JavaDoc request = new MockHttpServletRequest();
78         HttpServletResponse JavaDoc response = new MockHttpServletResponse();
79         TestView tv = new TestView(request, response, wac);
80     
81         tv.setApplicationContext(wac);
82         Properties JavaDoc p = new Properties JavaDoc();
83         p.setProperty("foo", "bar");
84         p.setProperty("something", "else");
85         tv.setAttributes(p);
86     
87         Map JavaDoc model = new HashMap JavaDoc();
88         model.put("one", new HashMap JavaDoc());
89         model.put("two", new Object JavaDoc());
90         tv.render(model, request, response);
91
92         // Check it contains all
93
checkContainsAll(model, tv.model);
94         checkContainsAll(p, tv.model);
95     
96         assertTrue(tv.inited);
97         mc.verify();
98     }
99     
100     public void testDynamicModelOverridesStaticAttributesIfCollision() throws Exception JavaDoc {
101         MockControl mc = MockControl.createControl(WebApplicationContext.class);
102         WebApplicationContext wac = (WebApplicationContext) mc.getMock();
103         mc.replay();
104         HttpServletRequest JavaDoc request = new MockHttpServletRequest();
105         HttpServletResponse JavaDoc response = new MockHttpServletResponse();
106         TestView tv = new TestView(request, response, wac);
107
108         tv.setApplicationContext(wac);
109         Properties JavaDoc p = new Properties JavaDoc();
110         p.setProperty("one", "bar");
111         p.setProperty("something", "else");
112         tv.setAttributes(p);
113
114         Map JavaDoc model = new HashMap JavaDoc();
115         model.put("one", new HashMap JavaDoc());
116         model.put("two", new Object JavaDoc());
117         tv.render(model, request, response);
118
119         // Check it contains all
120
checkContainsAll(model, tv.model);
121         assertTrue(tv.model.size() == 3);
122         // will have old something from properties
123
assertTrue(tv.model.get("something").equals("else"));
124
125         assertTrue(tv.inited);
126         mc.verify();
127     }
128     
129     public void testIgnoresNullAttributes() {
130         AbstractView v = new ConcreteView();
131         v.setAttributes(null);
132         assertTrue(v.getStaticAttributes().size() == 0);
133     }
134     
135     /**
136      * Test only the CSV parsing implementation.
137      */

138     public void testAttributeCSVParsingIgnoresNull() {
139         AbstractView v = new ConcreteView();
140         v.setAttributesCSV(null);
141         assertTrue(v.getStaticAttributes().size() == 0);
142     }
143     
144     public void testAttributeCSVParsingIgnoresEmptyString() {
145         AbstractView v = new ConcreteView();
146         v.setAttributesCSV("");
147         assertTrue(v.getStaticAttributes().size() == 0);
148     }
149     
150     /**
151      * Format is attname0={value1},attname1={value1}
152      */

153     public void testAttributeCSVParsingValid() {
154         AbstractView v = new ConcreteView();
155         v.setAttributesCSV("foo=[bar],king=[kong]");
156         assertTrue(v.getStaticAttributes().size() == 2);
157         assertTrue(v.getStaticAttributes().get("foo").equals("bar"));
158         assertTrue(v.getStaticAttributes().get("king").equals("kong"));
159     }
160     
161     public void testAttributeCSVParsingValidWithWeirdCharacters() {
162         AbstractView v = new ConcreteView();
163         String JavaDoc fooval = "owfie fue&3[][[[2 \n\n \r \t 8£3";
164         // Also tests empty value
165
String JavaDoc kingval = "";
166         v.setAttributesCSV("foo=(" + fooval + "),king={" + kingval + "},f1=[we]");
167         assertTrue(v.getStaticAttributes().size() == 3);
168         assertTrue(v.getStaticAttributes().get("foo").equals(fooval));
169         assertTrue(v.getStaticAttributes().get("king").equals(kingval));
170     }
171     
172     public void testAttributeCSVParsingInvalid() {
173         AbstractView v = new ConcreteView();
174         try {
175             // No equals
176
v.setAttributesCSV("fweoiruiu");
177             fail();
178         }
179         catch (IllegalArgumentException JavaDoc ex) {
180         }
181         
182         try {
183             // No value
184
v.setAttributesCSV("fweoiruiu=");
185             fail();
186         }
187         catch (IllegalArgumentException JavaDoc ex) {
188         }
189         
190         try {
191             // No closing ]
192
v.setAttributesCSV("fweoiruiu=[");
193             fail();
194         }
195         catch (IllegalArgumentException JavaDoc ex) {
196         }
197         try {
198             // Second one is bogus
199
v.setAttributesCSV("fweoiruiu=[de],=");
200             fail();
201         }
202         catch (IllegalArgumentException JavaDoc ex) {
203         }
204     }
205     
206     public void testAttributeCSVParsingIgoresTrailingComma() {
207         AbstractView v = new ConcreteView();
208         v.setAttributesCSV("foo=[de],");
209         assertTrue(v.getStaticAttributes().size() == 1);
210     }
211     
212     /**
213      * Check that all keys in expected have same values in actual
214      * @param expected
215      * @param actual
216      */

217     private void checkContainsAll(Map JavaDoc expected, Map JavaDoc actual) {
218         Set JavaDoc keys = expected.keySet();
219         for (Iterator JavaDoc iter = keys.iterator(); iter.hasNext();) {
220             String JavaDoc key = (String JavaDoc) iter.next();
221             //System.out.println("Checking model key " + key);
222
assertTrue("Value for model key '" + key + "' must match", actual.get(key) == expected.get(key));
223         }
224     }
225     
226     /**
227      * Trivial concrete subclass we can use when we're interested only
228      * in CSV parsing, which doesn't require lifecycle management
229      */

230     private class ConcreteView extends AbstractView {
231         // Do-nothing concrete subclass
232
protected void renderMergedOutputModel(Map JavaDoc model, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
233             throws ServletException JavaDoc, IOException JavaDoc {
234             throw new UnsupportedOperationException JavaDoc();
235         }
236     }
237
238     /**
239      * Single threaded subclass of AbstractView to check superclass
240      * behaviour
241      */

242     private class TestView extends AbstractView {
243         private HttpServletRequest JavaDoc request;
244         private HttpServletResponse JavaDoc response;
245         private WebApplicationContext wac;
246         public boolean inited;
247         
248         /** Captured model in render */
249         public Map JavaDoc model;
250         
251         public TestView(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, WebApplicationContext wac) {
252             this.request = request;
253             this.response = response;
254             this.wac = wac;
255             
256         }
257         protected void renderMergedOutputModel(Map JavaDoc model, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
258             throws ServletException JavaDoc, IOException JavaDoc {
259                 // do nothing
260
this.model = model;
261             }
262         /**
263          * @see org.springframework.context.support.ApplicationObjectSupport#initApplicationContext()
264          */

265         protected void initApplicationContext() throws ApplicationContextException {
266             if (inited)
267                 throw new RuntimeException JavaDoc("Already initialized");
268             this.inited = true;
269             assertTrue(getApplicationContext() == wac);
270         }
271
272     }
273
274 }
275
Popular Tags