1   /*
2    * Copyright 2004-2008 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.seasar.cubby.action.RequestMethod.GET;
20  import static org.seasar.cubby.action.RequestMethod.POST;
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  import java.util.regex.Pattern;
26  
27  import junit.framework.TestCase;
28  
29  import org.seasar.cubby.routing.impl.PathResolverImpl.RoutingComparator;
30  
31  public class RoutingComparatorTest extends TestCase {
32  
33  	private RoutingComparator comparator = new RoutingComparator();
34  	private RoutingImpl routing1;
35  	private RoutingImpl routing1d;
36  	private RoutingImpl routing2;
37  	private RoutingImpl routing3;
38  	private RoutingImpl routing4;
39  	private RoutingImpl routing5;
40  	private RoutingImpl routing5d;
41  	private RoutingImpl routing6;
42  	private RoutingImpl routing6d;
43  
44  	protected void setUp() throws Exception {
45  		super.setUp();
46  		routing1 = new RoutingImpl(null, null, null, asList(new String[0]),
47  				Pattern.compile("/foo/bar"), GET, null, Integer.MAX_VALUE, true);
48  		routing1d = new RoutingImpl(null, null, null, asList(new String[0]),
49  				Pattern.compile("/foo/bar"), GET, null, Integer.MAX_VALUE, true);
50  		routing2 = new RoutingImpl(null, null, null,
51  				asList(new String[] { "p1" }), Pattern.compile("/foo/bar/a"),
52  				GET, null, Integer.MAX_VALUE, true);
53  		routing3 = new RoutingImpl(null, null, null, asList(new String[] {
54  				"p1", "p2" }), Pattern.compile("/foo/bar/bbb"), GET, null,
55  				Integer.MAX_VALUE, true);
56  		routing4 = new RoutingImpl(null, null, null, asList(new String[] {
57  				"p1", "p2" }), Pattern.compile("/foo/bar/cc"), GET, null,
58  				Integer.MAX_VALUE, true);
59  		routing5 = new RoutingImpl(null, null, null, asList(new String[] {
60  				"p1", "p2" }), Pattern.compile("/foo/bar/cc"), POST, null,
61  				Integer.MAX_VALUE, true);
62  		routing5d = new RoutingImpl(null, null, null, asList(new String[] {
63  				"p1", "p2" }), Pattern.compile("/foo/bar/cc"), POST, null,
64  				Integer.MAX_VALUE, true);
65  		routing6 = new RoutingImpl(null, null, null, asList(new String[] {
66  				"p1", "p2" }), Pattern.compile("/foo/bar/cc/dd"), GET, null, 1,
67  				false);
68  		routing6d = new RoutingImpl(null, null, null, asList(new String[] {
69  				"p1", "p2" }), Pattern.compile("/foo/bar/cc/dd"), GET, null, 0,
70  				false);
71  	}
72  
73  	public void testDuplicate() {
74  		assertEquals(0, comparator.compare(routing1, routing1d));
75  		assertEquals(0, comparator.compare(routing5, routing5d));
76  	}
77  
78  	public void testSort() {
79  		List<RoutingImpl> routings = new ArrayList<RoutingImpl>(
80  				asList(new RoutingImpl[] { routing3, routing5, routing1,
81  						routing4, routing2 }));
82  		Collections.sort(routings, comparator);
83  		System.out.println(routings);
84  		assertSame("1", routing1, routings.get(0));
85  		assertSame("2", routing2, routings.get(1));
86  		assertSame("3", routing3, routings.get(2));
87  		assertSame("4", routing4, routings.get(3));
88  		assertSame("5", routing5, routings.get(4));
89  	}
90  
91  	public void testSort2() {
92  		List<RoutingImpl> routings = new ArrayList<RoutingImpl>(
93  				asList(new RoutingImpl[] { routing3, routing5, routing1,
94  						routing4, routing2, routing6, routing6d }));
95  		Collections.sort(routings, comparator);
96  		System.out.println(routings);
97  		assertSame("0", routing6d, routings.get(0));
98  		assertSame("1", routing6, routings.get(1));
99  		assertSame("2", routing1, routings.get(2));
100 		assertSame("3", routing2, routings.get(3));
101 		assertSame("4", routing3, routings.get(4));
102 		assertSame("5", routing4, routings.get(5));
103 		assertSame("6", routing5, routings.get(6));
104 	}
105 
106 }