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 org.seasar.cubby.exception.PathTemplateParseException;
19  import org.seasar.cubby.routing.PathTemplateParser;
20  
21  import junit.framework.TestCase;
22  
23  public class PathTemplateParserImplTest extends TestCase {
24  
25  	private PathTemplateParser parser = new PathTemplateParserImpl();
26  
27  	public void testParseFail() {
28  		assertIllegalTemplate("/foo/bar/{}");
29  		assertIllegalTemplate("/foo/bar/{");
30  		assertIllegalTemplate("/foo/bar/{a");
31  		assertIllegalTemplate("/foo/bar/{a,");
32  		assertIllegalTemplate("/foo/bar/{a,b");
33  		assertIllegalTemplate("/foo/bar/{a,}");
34  	}
35  
36  	private void assertIllegalTemplate(String template) {
37  		try {
38  			parser.parse(template, new PathTemplateParser.Handler() {
39  
40  				public String handle(String name, String regex) {
41  					fail();
42  					return regex;
43  				}
44  
45  			});
46  			fail();
47  		} catch (PathTemplateParseException e) {
48  			// ok
49  			e.printStackTrace();
50  		}
51  	}
52  
53  	public void testParse1() {
54  		String regex = parser.parse("/foo/{abc}",
55  				new PathTemplateParser.Handler() {
56  
57  					private int count = 0;
58  
59  					public String handle(String name, String regex) {
60  						switch (count) {
61  						case 0:
62  							assertEquals("abc", name);
63  							assertEquals("[a-zA-Z0-9]+", regex);
64  							break;
65  						default:
66  							fail();
67  						}
68  						count++;
69  						return "(" + regex + ")";
70  					}
71  
72  				});
73  		assertEquals("/foo/([a-zA-Z0-9]+)", regex);
74  	}
75  
76  	public void testParse2() {
77  		String regex = parser.parse("/foo/{abc}/bar/{def}",
78  				new PathTemplateParser.Handler() {
79  
80  					private int count = 0;
81  
82  					public String handle(String name, String regex) {
83  						switch (count) {
84  						case 0:
85  							assertEquals("abc", name);
86  							assertEquals("[a-zA-Z0-9]+", regex);
87  							break;
88  						case 1:
89  							assertEquals("def", name);
90  							assertEquals("[a-zA-Z0-9]+", regex);
91  							break;
92  						default:
93  							fail();
94  						}
95  						count++;
96  						return "(" + regex + ")";
97  					}
98  
99  				});
100 		assertEquals("/foo/([a-zA-Z0-9]+)/bar/([a-zA-Z0-9]+)", regex);
101 	}
102 
103 	public void testParse3() {
104 		String regex = parser.parse("/foo/bar/{abc,{3}a}",
105 				new PathTemplateParser.Handler() {
106 
107 					private int count = 0;
108 
109 					public String handle(String name, String regex) {
110 						switch (count) {
111 						case 0:
112 							assertEquals("abc", name);
113 							assertEquals("{3}a", regex);
114 							break;
115 						default:
116 							fail();
117 						}
118 						count++;
119 						return "(" + regex + ")";
120 					}
121 
122 				});
123 		assertEquals("/foo/bar/({3}a)", regex);
124 	}
125 
126 	public void testParse4() {
127 		String regex = parser.parse("/foo/bar/{abc,\\{aa}",
128 				new PathTemplateParser.Handler() {
129 
130 					private int count = 0;
131 
132 					public String handle(String name, String regex) {
133 						switch (count) {
134 						case 0:
135 							assertEquals("abc", name);
136 							assertEquals("\\{aa", regex);
137 							break;
138 						default:
139 							fail();
140 						}
141 						count++;
142 						return "(" + regex + ")";
143 					}
144 
145 				});
146 		assertEquals("/foo/bar/(\\{aa)", regex);
147 	}
148 
149 	public void testParse5() {
150 		String regex = parser.parse("/foo/bar/{abc,\\{aa}/{def,a\\}a}",
151 				new PathTemplateParser.Handler() {
152 
153 					private int count = 0;
154 
155 					public String handle(String name, String regex) {
156 						switch (count) {
157 						case 0:
158 							assertEquals("abc", name);
159 							assertEquals("\\{aa", regex);
160 							break;
161 						case 1:
162 							assertEquals("def", name);
163 							assertEquals("a\\}a", regex);
164 							break;
165 						default:
166 							fail();
167 						}
168 						count++;
169 						return "(" + regex + ")";
170 					}
171 
172 				});
173 		assertEquals("/foo/bar/(\\{aa)/(a\\}a)", regex);
174 	}
175 
176 	public void testParse6() {
177 		String regex = parser.parse("/foo/bar/{abc,aa\\\\}",
178 				new PathTemplateParser.Handler() {
179 
180 					private int count = 0;
181 
182 					public String handle(String name, String regex) {
183 						switch (count) {
184 						case 0:
185 							assertEquals("abc", name);
186 							assertEquals("aa\\\\", regex);
187 							break;
188 						default:
189 							fail();
190 						}
191 						count++;
192 						return "(" + regex + ")";
193 					}
194 
195 				});
196 		assertEquals("/foo/bar/(aa\\\\)", regex);
197 	}
198 
199 }