1 16 17 package org.springframework.web.servlet.handler.metadata; 18 19 import java.util.HashMap ; 20 21 import junit.framework.TestCase; 22 23 import org.springframework.beans.MutablePropertyValues; 24 import org.springframework.beans.TestBean; 25 import org.springframework.beans.factory.UnsatisfiedDependencyException; 26 import org.springframework.beans.factory.config.AutowireCapableBeanFactory; 27 import org.springframework.mock.web.MockHttpServletRequest; 28 import org.springframework.web.context.support.StaticWebApplicationContext; 29 import org.springframework.web.servlet.HandlerExecutionChain; 30 31 34 public class PathMapHandlerMappingTests extends TestCase { 35 36 public void testSatisfiedConstructorDependency() throws Exception { 37 String path = "/Constructor.htm"; 38 StaticWebApplicationContext wac = new StaticWebApplicationContext(); 39 wac.registerSingleton("test", TestBean.class, new MutablePropertyValues()); 40 41 HashUrlMapHandlerMapping hm = new HashUrlMapHandlerMapping(); 42 hm.register(ConstructorController.class, new PathMap(path)); 43 hm.setApplicationContext(wac); 44 45 ConstructorController cc = (ConstructorController) wac.getBean(ConstructorController.class.getName()); 46 assertSame(wac.getBean("test"), cc.testBean); 47 HandlerExecutionChain chain = hm.getHandler(new MockHttpServletRequest("GET", path)); 48 assertNotNull(chain); 49 assertEquals("Path is mapped correctly based on attribute", cc, chain.getHandler()); 50 chain = hm.getHandler(new MockHttpServletRequest("GET", "completeRubbish.html")); 51 assertNull("Don't know anything about this path", chain); 52 } 53 54 public void testUnsatisfiedConstructorDependency() throws Exception { 55 String path = "/Constructor.htm"; 56 StaticWebApplicationContext wac = new StaticWebApplicationContext(); 57 60 HashUrlMapHandlerMapping hm = new HashUrlMapHandlerMapping(); 61 hm.register(ConstructorController.class, new PathMap(path)); 62 try { 63 hm.setApplicationContext(wac); 64 fail("DependencyCheck should have failed"); 65 } 66 catch (UnsatisfiedDependencyException ex) { 67 } 69 } 70 71 public void testSatisfiedBeanPropertyDependency() throws Exception { 72 String path = "/BeanProperty.htm"; 73 StaticWebApplicationContext wac = new StaticWebApplicationContext(); 74 wac.registerSingleton("test", TestBean.class, new MutablePropertyValues()); 75 76 HashUrlMapHandlerMapping hm = new HashUrlMapHandlerMapping(); 77 hm.register(BeanPropertyController.class, new PathMap(path)); 78 hm.setApplicationContext(wac); 79 80 BeanPropertyController bpc = (BeanPropertyController) wac.getBean(BeanPropertyController.class.getName()); 81 assertSame(wac.getBean("test"), bpc.testBean); 82 HandlerExecutionChain chain = hm.getHandler(new MockHttpServletRequest("GET", path)); 83 assertNotNull(chain); 84 assertEquals("Path is mapped correctly based on attribute", bpc, chain.getHandler()); 85 chain = hm.getHandler(new MockHttpServletRequest("GET", "completeRubbish.html")); 86 assertNull("Don't know anything about this path", chain); 87 } 88 89 public void testSatisfiedBeanPropertyDependencyWithAutowireByType() throws Exception { 90 String path = "/BeanProperty.htm"; 91 StaticWebApplicationContext wac = new StaticWebApplicationContext(); 92 wac.registerSingleton("test", TestBean.class, new MutablePropertyValues()); 93 94 HashUrlMapHandlerMapping hm = new HashUrlMapHandlerMapping(); 95 hm.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE); 96 hm.register(BeanPropertyController.class, new PathMap(path)); 97 hm.setApplicationContext(wac); 98 99 BeanPropertyController bpc = (BeanPropertyController) wac.getBean(BeanPropertyController.class.getName()); 100 assertSame(wac.getBean("test"), bpc.testBean); 101 HandlerExecutionChain chain = hm.getHandler(new MockHttpServletRequest("GET", path)); 102 assertNotNull(chain); 103 assertEquals("Path is mapped correctly based on attribute", bpc, chain.getHandler()); 104 chain = hm.getHandler(new MockHttpServletRequest("GET", "completeRubbish.html")); 105 assertNull("Don't know anything about this path", chain); 106 } 107 108 public void testUnsatisfiedBeanPropertyDependencyWithAutowireByType() throws Exception { 109 String path = "/BeanProperty.htm"; 110 StaticWebApplicationContext wac = new StaticWebApplicationContext(); 111 112 HashUrlMapHandlerMapping hm = new HashUrlMapHandlerMapping(); 113 hm.setAutowireModeName("AUTOWIRE_BY_TYPE"); 114 hm.register(BeanPropertyController.class, new PathMap(path)); 115 try { 116 hm.setApplicationContext(wac); 117 fail("DependencyCheck should have failed"); 118 } 119 catch (UnsatisfiedDependencyException ex) { 120 } 122 } 123 124 public void testSatisfiedBeanPropertyDependencyWithAutowireByName() throws Exception { 125 String path = "/BeanProperty.htm"; 126 StaticWebApplicationContext wac = new StaticWebApplicationContext(); 127 wac.registerSingleton("testBean", TestBean.class, new MutablePropertyValues()); 128 129 HashUrlMapHandlerMapping hm = new HashUrlMapHandlerMapping(); 130 hm.setAutowireModeName("AUTOWIRE_BY_NAME"); 131 hm.register(BeanPropertyController.class, new PathMap(path)); 132 hm.setApplicationContext(wac); 133 134 BeanPropertyController bpc = (BeanPropertyController) wac.getBean(BeanPropertyController.class.getName()); 135 assertSame(wac.getBean("testBean"), bpc.testBean); 136 HandlerExecutionChain chain = hm.getHandler(new MockHttpServletRequest("GET", path)); 137 assertNotNull(chain); 138 assertEquals("Path is mapped correctly based on attribute", bpc, chain.getHandler()); 139 chain = hm.getHandler(new MockHttpServletRequest("GET", "completeRubbish.html")); 140 assertNull("Don't know anything about this path", chain); 141 } 142 143 public void testUnsatisfiedBeanPropertyDependencyWithAutowireByName() throws Exception { 144 String path = "/BeanProperty.htm"; 145 StaticWebApplicationContext wac = new StaticWebApplicationContext(); 146 wac.registerSingleton("test", TestBean.class, new MutablePropertyValues()); 147 148 HashUrlMapHandlerMapping hm = new HashUrlMapHandlerMapping(); 149 hm.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME); 150 hm.register(BeanPropertyController.class, new PathMap(path)); 151 try { 152 hm.setApplicationContext(wac); 153 fail("DependencyCheck should have failed"); 154 } 155 catch (UnsatisfiedDependencyException ex) { 156 } 158 } 159 160 public void testUnsatisfiedBeanPropertyDependencyWithNoDependencyCheck() throws Exception { 161 String path = "/BeanProperty.htm"; 162 StaticWebApplicationContext wac = new StaticWebApplicationContext(); 163 164 HashUrlMapHandlerMapping hm = new HashUrlMapHandlerMapping(); 165 hm.setAutowireModeName("AUTOWIRE_BY_NAME"); 166 hm.setDependencyCheck(false); 167 hm.register(BeanPropertyController.class, new PathMap(path)); 168 hm.setApplicationContext(wac); 169 170 BeanPropertyController bpc = (BeanPropertyController) wac.getBean(BeanPropertyController.class.getName()); 171 assertNull("Not autowired but no dependency check", bpc.testBean); 172 HandlerExecutionChain chain = hm.getHandler(new MockHttpServletRequest("GET", path)); 173 assertNotNull(chain); 174 assertEquals("Path is mapped correctly based on attribute", bpc, chain.getHandler()); 175 chain = hm.getHandler(new MockHttpServletRequest("GET", "completeRubbish.html")); 176 assertNull("Don't know anything about this path", chain); 177 } 178 179 public void testMultiplePaths() throws Exception { 180 String path1 = "/Constructor.htm"; 181 String path2 = "path2.cgi"; 182 StaticWebApplicationContext wac = new StaticWebApplicationContext(); 183 wac.registerSingleton("test", TestBean.class, new MutablePropertyValues()); 184 185 HashUrlMapHandlerMapping hm = new HashUrlMapHandlerMapping(); 186 hm.register(ConstructorController.class, new PathMap[] { new PathMap(path1), new PathMap(path2) }); 187 hm.setApplicationContext(wac); 188 ConstructorController cc = (ConstructorController) wac.getBean(ConstructorController.class.getName()); 189 assertSame(wac.getBean("test"), cc.testBean); 190 HandlerExecutionChain chain = hm.getHandler(new MockHttpServletRequest("GET", path1)); 191 assertNotNull(chain); 192 assertEquals("Path is mapped correctly based on attribute 1", cc, chain.getHandler()); 193 chain = hm.getHandler(new MockHttpServletRequest(null, "GET", "/" + path2)); 194 assertEquals("Path is mapped correctly based on attribute 2", cc, chain.getHandler()); 195 chain = hm.getHandler(new MockHttpServletRequest(null, "GET", "completeRubbish.html")); 196 assertNull("Don't know anything about this path", chain); 197 } 198 199 200 private static class HashUrlMapHandlerMapping extends AbstractPathMapHandlerMapping { 201 202 private HashMap classToPathMaps = new HashMap (); 203 204 public void register(Class clazz, PathMap pm) { 205 classToPathMaps.put(clazz, new PathMap[] { pm }); 206 } 207 208 public void register(Class clazz, PathMap[] pms) { 209 classToPathMaps.put(clazz, pms); 210 } 211 212 protected Class [] getClassesWithPathMapAttributes() { 213 return (Class []) classToPathMaps.keySet().toArray(new Class [classToPathMaps.size()]); 214 } 215 216 protected PathMap[] getPathMapAttributes(Class handlerClass) { 217 return (PathMap[]) classToPathMaps.get(handlerClass); 218 } 219 } 220 221 } 222 | Popular Tags |