1   /*
2    * Copyright 2004-2010 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  
17  package org.seasar.cubby.routing.impl;
18  
19  import static java.util.Arrays.asList;
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertSame;
22  import static org.seasar.cubby.action.RequestMethod.GET;
23  import static org.seasar.cubby.action.RequestMethod.POST;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.TreeMap;
29  import java.util.regex.Pattern;
30  
31  import org.junit.Before;
32  import org.junit.Test;
33  import org.seasar.cubby.routing.Routing;
34  import org.seasar.cubby.routing.impl.RoutingImpl;
35  import org.seasar.cubby.routing.impl.PathResolverImpl.RoutingKey;
36  
37  public class RoutingKeyTest {
38  
39  	private Routing routing1;
40  	private Routing routing1d;
41  	private Routing routing2;
42  	private Routing routing3;
43  	private Routing routing4;
44  	private Routing routing5;
45  	private Routing routing5d;
46  	private Routing routing6;
47  	private Routing routing6d;
48  
49  	@Before
50  	public void setupRoutings() throws Exception {
51  		routing1 = new RoutingImpl(null, null, null, asList(new String[0]),
52  				Pattern.compile("/foo/bar"), GET, null, Integer.MAX_VALUE);
53  		routing1d = new RoutingImpl(null, null, null, asList(new String[0]),
54  				Pattern.compile("/foo/bar"), GET, null, Integer.MAX_VALUE);
55  		routing2 = new RoutingImpl(null, null, null,
56  				asList(new String[] { "p1" }), Pattern.compile("/foo/bar/a"),
57  				GET, null, Integer.MAX_VALUE);
58  		routing3 = new RoutingImpl(null, null, null, asList(new String[] {
59  				"p1", "p2" }), Pattern.compile("/foo/bar/bbb"), GET, null,
60  				Integer.MAX_VALUE);
61  		routing4 = new RoutingImpl(null, null, null, asList(new String[] {
62  				"p1", "p2" }), Pattern.compile("/foo/bar/cc"), GET, null,
63  				Integer.MAX_VALUE);
64  		routing5 = new RoutingImpl(null, null, null, asList(new String[] {
65  				"p1", "p2" }), Pattern.compile("/foo/bar/cc"), POST, null,
66  				Integer.MAX_VALUE);
67  		routing5d = new RoutingImpl(null, null, null, asList(new String[] {
68  				"p1", "p2" }), Pattern.compile("/foo/bar/cc"), POST, null,
69  				Integer.MAX_VALUE);
70  		routing6 = new RoutingImpl(null, null, null, asList(new String[] {
71  				"p1", "p2" }), Pattern.compile("/foo/bar/cc/dd"), GET, null, 1);
72  		routing6d = new RoutingImpl(null, null, null, asList(new String[] {
73  				"p1", "p2" }), Pattern.compile("/foo/bar/cc/dd"), GET, null, 0);
74  	}
75  
76  	@Test
77  	public void duplicate() {
78  		assertEquals(0, new RoutingKey(routing1).compareTo(new RoutingKey(
79  				routing1d)));
80  		assertEquals(0, new RoutingKey(routing5).compareTo(new RoutingKey(
81  				routing5d)));
82  	}
83  
84  	@Test
85  	public void sort() {
86  		Map<RoutingKey, Routing> map = new TreeMap<RoutingKey, Routing>();
87  		map.put(new RoutingKey(routing3), routing3);
88  		map.put(new RoutingKey(routing5), routing5);
89  		map.put(new RoutingKey(routing1), routing1);
90  		map.put(new RoutingKey(routing4), routing4);
91  		map.put(new RoutingKey(routing2), routing2);
92  		List<Routing> actualList = new ArrayList<Routing>(asList(new Routing[] {
93  				routing1, routing2, routing3, routing4, routing5 }));
94  		for (Routing routing : map.values()) {
95  			assertSame(actualList.remove(0), routing);
96  		}
97  	}
98  
99  	@Test
100 	public void sort2() {
101 		Map<RoutingKey, Routing> map = new TreeMap<RoutingKey, Routing>();
102 		map.put(new RoutingKey(routing3), routing3);
103 		map.put(new RoutingKey(routing5), routing5);
104 		map.put(new RoutingKey(routing1), routing1);
105 		map.put(new RoutingKey(routing4), routing4);
106 		map.put(new RoutingKey(routing2), routing2);
107 		map.put(new RoutingKey(routing6), routing6);
108 		map.put(new RoutingKey(routing6d), routing6d);
109 		List<Routing> actualList = new ArrayList<Routing>(asList(new Routing[] {
110 				routing6d, routing6, routing1, routing2, routing3, routing4,
111 				routing5 }));
112 		for (Routing routing : map.values()) {
113 			assertSame(actualList.remove(0), routing);
114 		}
115 	}
116 
117 }