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