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