1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.seasar.cubby.filter;
18
19 import static org.easymock.EasyMock.createMock;
20 import static org.easymock.EasyMock.expect;
21 import static org.easymock.EasyMock.replay;
22 import static org.easymock.EasyMock.verify;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.assertSame;
26
27 import java.util.Arrays;
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.regex.Pattern;
32
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.seasar.cubby.CubbyConstants;
40 import org.seasar.cubby.action.ActionResult;
41 import org.seasar.cubby.action.Path;
42 import org.seasar.cubby.internal.util.RequestUtils;
43 import org.seasar.cubby.mock.MockPathResolverProvider;
44 import org.seasar.cubby.plugin.PluginRegistry;
45 import org.seasar.cubby.plugins.BinderPlugin;
46 import org.seasar.cubby.routing.PathInfo;
47 import org.seasar.cubby.routing.PathResolver;
48 import org.seasar.cubby.routing.Routing;
49 import org.seasar.cubby.routing.impl.PathResolverImpl;
50 import org.seasar.cubby.routing.impl.PathTemplateParserImpl;
51 import org.seasar.cubby.spi.PathResolverProvider;
52
53 public class CubbyFlterRoutingTest {
54
55 private final PluginRegistry pluginRegistry = PluginRegistry.getInstance();
56
57
58 private CubbyFilter cubbyFilter = new CubbyFilter();
59
60 @Before
61 public void setupProvider() {
62 final BinderPlugin binderPlugin = new BinderPlugin();
63 final PathResolver pathResolver = new PathResolverImpl(
64 new PathTemplateParserImpl());
65 pathResolver.add(FooAction.class);
66 binderPlugin.bind(PathResolverProvider.class).toInstance(
67 new MockPathResolverProvider(pathResolver));
68 pluginRegistry.register(binderPlugin);
69 }
70
71 @After
72 public void teardownProvider() {
73 pluginRegistry.clear();
74 }
75
76 @Test
77 public void routing() {
78 HttpServletRequest request = createMock(HttpServletRequest.class);
79 expect(request.getServletPath()).andStubReturn("/foo/");
80 expect(request.getPathInfo()).andStubReturn("");
81 expect(request.getMethod()).andStubReturn("GET");
82 expect(request.getAttribute(CubbyConstants.ATTR_ROUTING)).andReturn(
83 null);
84 expect(request.getCharacterEncoding()).andReturn("UTF-8");
85 HttpServletResponse response = createMock(HttpServletResponse.class);
86 replay(request, response);
87
88 String path = RequestUtils.getPath(request);
89 List<Pattern> emptyList = Collections.emptyList();
90 PathInfo pathInfo = cubbyFilter.findPathInfo(request, response, path,
91 emptyList);
92 Map<String, Object[]> parameterMap = Collections.emptyMap();
93 Routing routing = pathInfo.dispatch(parameterMap);
94 assertNotNull(routing);
95
96 verify(request, response);
97 }
98
99 @Test
100 public void routingWithInternalForward() {
101 Routing routing = createMock(Routing.class);
102 HttpServletRequest request = createMock(HttpServletRequest.class);
103 expect(request.getServletPath()).andStubReturn("/foo/");
104 expect(request.getPathInfo()).andStubReturn("");
105 expect(request.getMethod()).andStubReturn("GET");
106 expect(request.getAttribute(CubbyConstants.ATTR_ROUTING)).andReturn(
107 routing);
108 request.removeAttribute(CubbyConstants.ATTR_ROUTING);
109 HttpServletResponse response = createMock(HttpServletResponse.class);
110 replay(routing, request, response);
111
112 String path = RequestUtils.getPath(request);
113 List<Pattern> emptyList = Collections.emptyList();
114 PathInfo pathInfo = cubbyFilter.findPathInfo(request, response, path,
115 emptyList);
116 Map<String, Object[]> parameterMap = Collections.emptyMap();
117 assertSame(routing, pathInfo.dispatch(parameterMap));
118
119 verify(routing, request, response);
120 }
121
122 @Test
123 public void ignorePath() {
124 HttpServletRequest request = createMock(HttpServletRequest.class);
125 expect(request.getServletPath()).andStubReturn("/js/ignore");
126 expect(request.getPathInfo()).andStubReturn("");
127 expect(request.getMethod()).andStubReturn("GET");
128 expect(request.getAttribute(CubbyConstants.ATTR_ROUTING)).andReturn(
129 null);
130 HttpServletResponse response = createMock(HttpServletResponse.class);
131 replay(request, response);
132
133 String path = RequestUtils.getPath(request);
134 PathInfo pathInfo = cubbyFilter.findPathInfo(request, response, path,
135 Arrays.asList(new Pattern[] { Pattern.compile("/js/.*") }));
136 assertNull(pathInfo);
137
138 verify(request, response);
139 }
140
141 public static class FooAction {
142
143 public ActionResult index() {
144 return null;
145 }
146
147 @Path("/js/ignore")
148 public ActionResult ignore() {
149 return null;
150 }
151 }
152
153 }