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