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