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.action.RequestMethod;
30  import org.seasar.cubby.routing.impl.PathResolverImpl.Routing;
31  import org.seasar.cubby.routing.impl.PathResolverImpl.RoutingComparator;
32  
33  public class RoutingComparatorTest extends TestCase {
34  
35  	private RoutingComparator comparator = new RoutingComparator();
36  	private Routing routing1;
37  	private Routing routing1d;
38  	private Routing routing2;
39  	private Routing routing3;
40  	private Routing routing4;
41  	private Routing routing5;
42  	private Routing routing5d;
43  
44  	protected void setUp() throws Exception {
45  		super.setUp();
46  		routing1 = new Routing(null, null, asList(new String[0]), Pattern
47  				.compile("/foo/bar"), new RequestMethod[] { GET }, false);
48  		routing1d = new Routing(null, null, asList(new String[0]), Pattern
49  				.compile("/foo/bar"), new RequestMethod[] { GET }, false);
50  		routing2 = new Routing(null, null, asList(new String[] { "p1" }),
51  				Pattern.compile("/foo/bar/a"), new RequestMethod[] { GET },
52  				false);
53  		routing3 = new Routing(null, null, asList(new String[] { "p1", "p2" }),
54  				Pattern.compile("/foo/bar/bbb"), new RequestMethod[] { GET },
55  				false);
56  		routing4 = new Routing(null, null, asList(new String[] { "p1", "p2" }),
57  				Pattern.compile("/foo/bar/cc"), new RequestMethod[] { GET },
58  				false);
59  		routing5 = new Routing(null, null, asList(new String[] { "p1", "p2" }),
60  				Pattern.compile("/foo/bar/cc"), new RequestMethod[] { POST },
61  				false);
62  		routing5d = new Routing(null, null, asList(new String[] { "p1", "p2" }),
63  				Pattern.compile("/foo/bar/cc"), new RequestMethod[] { POST, GET },
64  				false);
65  	}
66  
67  	public void testDuplicate() {
68  		assertEquals(0, comparator.compare(routing1, routing1d));
69  		assertEquals(0, comparator.compare(routing5, routing5d));
70  	}
71  
72  	public void testSort() {
73  		List<Routing> routings = new ArrayList<Routing>(asList(new Routing[] {
74  				routing3, routing5, routing1, routing4, routing2 }));
75  		Collections.sort(routings, comparator);
76  		System.out.println(routings);
77  		assertSame("1", routing1, routings.get(0));
78  		assertSame("2", routing2, routings.get(1));
79  		assertSame("3", routing3, routings.get(2));
80  		assertSame("4", routing4, routings.get(3));
81  		assertSame("5", routing5, routings.get(4));
82  	}
83  
84  }