KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > handler > metadata > PathMapHandlerMappingTests


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.handler.metadata;
18
19 import java.util.HashMap JavaDoc;
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 /**
32  * @author Rod Johnson
33  */

34 public class PathMapHandlerMappingTests extends TestCase {
35     
36     public void testSatisfiedConstructorDependency() throws Exception JavaDoc {
37         String JavaDoc 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 JavaDoc {
55         String JavaDoc path = "/Constructor.htm";
56         StaticWebApplicationContext wac = new StaticWebApplicationContext();
57         // No registration of a TestBean
58
//wac.registerSingleton("test", TestBean.class, new MutablePropertyValues());
59

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             // Ok
68
}
69     }
70
71     public void testSatisfiedBeanPropertyDependency() throws Exception JavaDoc {
72         String JavaDoc 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 JavaDoc {
90         String JavaDoc 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 JavaDoc {
109         String JavaDoc 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             // Ok
121
}
122     }
123
124     public void testSatisfiedBeanPropertyDependencyWithAutowireByName() throws Exception JavaDoc {
125         String JavaDoc 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 JavaDoc {
144         String JavaDoc 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             // Ok
157
}
158     }
159
160     public void testUnsatisfiedBeanPropertyDependencyWithNoDependencyCheck() throws Exception JavaDoc {
161         String JavaDoc 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 JavaDoc {
180         String JavaDoc path1 = "/Constructor.htm";
181         String JavaDoc 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 JavaDoc classToPathMaps = new HashMap JavaDoc();
203
204         public void register(Class JavaDoc clazz, PathMap pm) {
205             classToPathMaps.put(clazz, new PathMap[] { pm });
206         }
207
208         public void register(Class JavaDoc clazz, PathMap[] pms) {
209             classToPathMaps.put(clazz, pms);
210         }
211
212         protected Class JavaDoc[] getClassesWithPathMapAttributes() {
213             return (Class JavaDoc[]) classToPathMaps.keySet().toArray(new Class JavaDoc[classToPathMaps.size()]);
214         }
215
216         protected PathMap[] getPathMapAttributes(Class JavaDoc handlerClass) {
217             return (PathMap[]) classToPathMaps.get(handlerClass);
218         }
219     }
220
221 }
222
Popular Tags