1 /*
2  * Copyright 2002-2011 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.config.annotation;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertNull;
22 
23 import java.util.Map;
24 
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
28 import org.springframework.web.servlet.mvc.ParameterizableViewController;
29 
30 /**
31  * Test fixture with a {@link ViewControllerRegistry}.
32  *
33  * @author Rossen Stoyanchev
34  */
35 public class ViewControllerRegistryTests {
36 
37 	private ViewControllerRegistry registry;
38 
39 	@Before
setUp()40 	public void setUp() {
41 		registry = new ViewControllerRegistry();
42 	}
43 
44 	@Test
noViewControllers()45 	public void noViewControllers() throws Exception {
46 		assertNull(registry.getHandlerMapping());
47 	}
48 
49 	@Test
addViewController()50 	public void addViewController() {
51 		registry.addViewController("/path");
52 		Map<String, ?> urlMap = getHandlerMapping().getUrlMap();
53 		ParameterizableViewController controller = (ParameterizableViewController) urlMap.get("/path");
54 		assertNotNull(controller);
55 		assertNull(controller.getViewName());
56 	}
57 
58 	@Test
addViewControllerWithViewName()59 	public void addViewControllerWithViewName() {
60 		registry.addViewController("/path").setViewName("viewName");
61 		Map<String, ?> urlMap = getHandlerMapping().getUrlMap();
62 		ParameterizableViewController controller = (ParameterizableViewController) urlMap.get("/path");
63 		assertNotNull(controller);
64 		assertEquals("viewName", controller.getViewName());
65 	}
66 
67 	@Test
order()68 	public void order() {
69 		registry.addViewController("/path");
70 		SimpleUrlHandlerMapping handlerMapping = getHandlerMapping();
71 		assertEquals(1, handlerMapping.getOrder());
72 
73 		registry.setOrder(2);
74 		handlerMapping = getHandlerMapping();
75 		assertEquals(2, handlerMapping.getOrder());
76 	}
77 
getHandlerMapping()78 	private SimpleUrlHandlerMapping getHandlerMapping() {
79 		return (SimpleUrlHandlerMapping) registry.getHandlerMapping();
80 	}
81 
82 }
83