1 16 17 package org.springframework.web.servlet.view; 18 19 import java.io.IOException ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 import java.util.Properties ; 24 import java.util.Set ; 25 26 import javax.servlet.ServletException ; 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 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 44 public class BaseViewTests extends TestCase { 45 46 public void testRenderWithoutStaticAttributes() throws Exception { 47 MockControl mc = MockControl.createControl(WebApplicationContext.class); 48 WebApplicationContext wac = (WebApplicationContext) mc.getMock(); 49 mc.replay(); 50 HttpServletRequest request = new MockHttpServletRequest(); 51 HttpServletResponse response = new MockHttpServletResponse(); 52 TestView tv = new TestView(request, response, wac); 53 54 tv.setApplicationContext(wac); 56 tv.setApplicationContext(wac); 57 58 Map model = new HashMap (); 59 model.put("foo", "bar"); 60 model.put("something", new Object ()); 61 tv.render(model, request, response); 62 63 checkContainsAll(model, tv.model); 65 66 assertTrue(tv.inited); 67 mc.verify(); 68 } 69 70 73 public void testRenderWithStaticAttributesNoCollision() throws Exception { 74 MockControl mc = MockControl.createControl(WebApplicationContext.class); 75 WebApplicationContext wac = (WebApplicationContext) mc.getMock(); 76 mc.replay(); 77 HttpServletRequest request = new MockHttpServletRequest(); 78 HttpServletResponse response = new MockHttpServletResponse(); 79 TestView tv = new TestView(request, response, wac); 80 81 tv.setApplicationContext(wac); 82 Properties p = new Properties (); 83 p.setProperty("foo", "bar"); 84 p.setProperty("something", "else"); 85 tv.setAttributes(p); 86 87 Map model = new HashMap (); 88 model.put("one", new HashMap ()); 89 model.put("two", new Object ()); 90 tv.render(model, request, response); 91 92 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 { 101 MockControl mc = MockControl.createControl(WebApplicationContext.class); 102 WebApplicationContext wac = (WebApplicationContext) mc.getMock(); 103 mc.replay(); 104 HttpServletRequest request = new MockHttpServletRequest(); 105 HttpServletResponse response = new MockHttpServletResponse(); 106 TestView tv = new TestView(request, response, wac); 107 108 tv.setApplicationContext(wac); 109 Properties p = new Properties (); 110 p.setProperty("one", "bar"); 111 p.setProperty("something", "else"); 112 tv.setAttributes(p); 113 114 Map model = new HashMap (); 115 model.put("one", new HashMap ()); 116 model.put("two", new Object ()); 117 tv.render(model, request, response); 118 119 checkContainsAll(model, tv.model); 121 assertTrue(tv.model.size() == 3); 122 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 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 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 fooval = "owfie fue&3[][[[2 \n\n \r \t 8£3"; 164 String 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 v.setAttributesCSV("fweoiruiu"); 177 fail(); 178 } 179 catch (IllegalArgumentException ex) { 180 } 181 182 try { 183 v.setAttributesCSV("fweoiruiu="); 185 fail(); 186 } 187 catch (IllegalArgumentException ex) { 188 } 189 190 try { 191 v.setAttributesCSV("fweoiruiu=["); 193 fail(); 194 } 195 catch (IllegalArgumentException ex) { 196 } 197 try { 198 v.setAttributesCSV("fweoiruiu=[de],="); 200 fail(); 201 } 202 catch (IllegalArgumentException 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 217 private void checkContainsAll(Map expected, Map actual) { 218 Set keys = expected.keySet(); 219 for (Iterator iter = keys.iterator(); iter.hasNext();) { 220 String key = (String ) iter.next(); 221 assertTrue("Value for model key '" + key + "' must match", actual.get(key) == expected.get(key)); 223 } 224 } 225 226 230 private class ConcreteView extends AbstractView { 231 protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response) 233 throws ServletException , IOException { 234 throw new UnsupportedOperationException (); 235 } 236 } 237 238 242 private class TestView extends AbstractView { 243 private HttpServletRequest request; 244 private HttpServletResponse response; 245 private WebApplicationContext wac; 246 public boolean inited; 247 248 249 public Map model; 250 251 public TestView(HttpServletRequest request, HttpServletResponse response, WebApplicationContext wac) { 252 this.request = request; 253 this.response = response; 254 this.wac = wac; 255 256 } 257 protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response) 258 throws ServletException , IOException { 259 this.model = model; 261 } 262 265 protected void initApplicationContext() throws ApplicationContextException { 266 if (inited) 267 throw new RuntimeException ("Already initialized"); 268 this.inited = true; 269 assertTrue(getApplicationContext() == wac); 270 } 271 272 } 273 274 } 275 | Popular Tags |