2
* Copyright 2005-2012 Restlet S.A.S.
4
* The contents of this file are subject to the terms of one of the following
5
* open source licenses: Apache 2.0 or LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL
6
* 1.0 (the "Licenses"). You can select the license that you prefer but you may
7
* not use this file except in compliance with one of these Licenses.
9
* You can obtain a copy of the Apache 2.0 license at
10
* http://www.opensource.org/licenses/apache-2.0
12
* You can obtain a copy of the LGPL 3.0 license at
13
* http://www.opensource.org/licenses/lgpl-3.0
15
* You can obtain a copy of the LGPL 2.1 license at
16
* http://www.opensource.org/licenses/lgpl-2.1
18
* You can obtain a copy of the CDDL 1.0 license at
19
* http://www.opensource.org/licenses/cddl1
21
* You can obtain a copy of the EPL 1.0 license at
22
* http://www.opensource.org/licenses/eclipse-1.0
24
* See the Licenses for the specific language governing permissions and
25
* limitations under the Licenses.
27
* Alternatively, you can obtain a royalty free commercial license with less
28
* limitations, transferable or non-transferable, directly at
29
* http://www.restlet.com/products/restlet-framework
31
* Restlet is a registered trademark of Restlet S.A.S.
34
package org.restlet.test.jaxrs.util;
36
import java.lang.reflect.Constructor;
38
import javax.ws.rs.Path;
40
import junit.framework.TestCase;
42
import org.restlet.ext.jaxrs.internal.util.MatchingResult;
43
import org.restlet.ext.jaxrs.internal.util.PathRegExp;
44
import org.restlet.ext.jaxrs.internal.util.RemainingPath;
47
* @author Stephan Koops
50
public class PathRegExpTests extends TestCase {
52
static final String ID1 = "id1";
54
static final String ID2 = "id2";
56
/** as {@link #PATH_PATTERN_2} but without "/" at end */
57
static final String PATH_PATTERN_1 = "/abc/{" + ID1 + "}/shf/{" + ID2
60
/** as {@link #PATH_PATTERN_1} but with "/" at end */
61
static final String PATH_PATTERN_2 = PATH_PATTERN_1 + "/";
63
/** as {@link #VALID_PATH_2} but without "/" at end */
64
static final String VALID_PATH_1 = "/abc/25478/shf/12345/xyz";
66
/** as {@link #VALID_PATH_2} but without "/" at end */
67
static final RemainingPath VALID_PATH_1_RP = new RemainingPath(VALID_PATH_1);
69
/** as {@link #VALID_PATH_1} but with "/" at end */
70
public static final String VALID_PATH_2 = VALID_PATH_1 + "/";
72
/** as {@link #VALID_PATH_1} but with "/" at end */
73
public static final RemainingPath VALID_PATH_2_RP = new RemainingPath(
76
private static final PathRegExp newPathRegExp(String pathPattern) {
78
final Constructor<PathRegExp> constructor;
79
final Class<PathRegExp> pathRegExpClass = PathRegExp.class;
80
constructor = pathRegExpClass.getDeclaredConstructor(String.class,
82
constructor.setAccessible(true);
83
return constructor.newInstance(pathPattern, null);
84
} catch (RuntimeException e) {
86
} catch (Exception e) {
87
throw new RuntimeException(e);
91
private final PathRegExp regExpMultipleSegments1 = newPathRegExp(PATH_PATTERN_1);
93
private final PathRegExp regExpMultipleSegments2 = newPathRegExp(PATH_PATTERN_2);
95
private final PathRegExp regExpOneSegment1 = newPathRegExp(PATH_PATTERN_1);
97
private final PathRegExp regExpOneSegment2 = newPathRegExp(PATH_PATTERN_2);
101
* {@link org.restlet.ext.jaxrs.internal.util.PathRegExp#match(java.lang.String)} .
103
public void testMatchM1() {
104
MatchingResult matchingResult = this.regExpMultipleSegments1
105
.match(VALID_PATH_1_RP);
106
assertNotNull(matchingResult);
107
assertEquals("25478", matchingResult.getVariables().get(ID1));
108
assertEquals("12345", matchingResult.getVariables().get(ID2));
109
assertEquals(new RemainingPath(""), matchingResult
110
.getFinalCapturingGroup());
112
matchingResult = this.regExpMultipleSegments1.match(VALID_PATH_2_RP);
113
assertNotNull(matchingResult);
114
assertEquals("25478", matchingResult.getVariables().get(ID1));
115
assertEquals("12345", matchingResult.getVariables().get(ID2));
116
assertEquals(new RemainingPath(""), matchingResult
117
.getFinalCapturingGroup());
122
* {@link org.restlet.ext.jaxrs.internal.util.PathRegExp#match(java.lang.String)} .
124
public void testMatchM2() {
125
MatchingResult matchingResult = this.regExpMultipleSegments2
126
.match(VALID_PATH_1_RP);
127
assertNotNull(matchingResult);
128
assertEquals("25478", matchingResult.getVariables().get(ID1));
129
assertEquals("12345", matchingResult.getVariables().get(ID2));
131
matchingResult = this.regExpMultipleSegments2.match(VALID_PATH_2_RP);
132
assertNotNull(matchingResult);
133
assertEquals("25478", matchingResult.getVariables().get(ID1));
134
assertEquals("12345", matchingResult.getVariables().get(ID2));
135
assertEquals(new RemainingPath(""), matchingResult
136
.getFinalCapturingGroup());
141
* {@link org.restlet.ext.jaxrs.internal.util.PathRegExp#match(java.lang.String)} .
143
public void testMatchM3() {
144
final String rest = "/jkgjg";
150
* {@link org.restlet.ext.jaxrs.internal.util.PathRegExp#match(java.lang.String)} .
152
public void testMatchM4() {
153
final String rest = "/qarear/iuguz/izu/";
159
* {@link org.restlet.ext.jaxrs.internal.util.PathRegExp#match(java.lang.String)} .
161
public void testMatchO1() {
162
final MatchingResult matchingResult = this.regExpOneSegment1
163
.match(VALID_PATH_1_RP);
164
assertNotNull(matchingResult);
165
assertEquals("25478", matchingResult.getVariables().get(ID1));
166
assertEquals("12345", matchingResult.getVariables().get(ID2));
167
assertEquals(new RemainingPath(""), matchingResult
168
.getFinalCapturingGroup());
170
this.regExpOneSegment1.match(VALID_PATH_2_RP);
171
assertNotNull(matchingResult);
172
assertEquals("25478", matchingResult.getVariables().get(ID1));
173
assertEquals("12345", matchingResult.getVariables().get(ID2));
174
assertEquals(new RemainingPath(""), matchingResult
175
.getFinalCapturingGroup());
180
* {@link org.restlet.ext.jaxrs.internal.util.PathRegExp#match(java.lang.String)} .
182
public void testMatchO21() {
183
MatchingResult matchingResult = this.regExpOneSegment2
184
.match(VALID_PATH_1_RP);
185
assertNotNull(matchingResult);
186
assertEquals("25478", matchingResult.getVariables().get(ID1));
187
assertEquals("12345", matchingResult.getVariables().get(ID2));
189
matchingResult = this.regExpOneSegment2.match(VALID_PATH_2_RP);
190
assertNotNull(matchingResult);
191
assertEquals("25478", matchingResult.getVariables().get(ID1));
192
assertEquals("12345", matchingResult.getVariables().get(ID2));
193
assertEquals(new RemainingPath(""), matchingResult
194
.getFinalCapturingGroup());
200
private void tryWithRest(final String rest) {
201
final MatchingResult matchingResult = this.regExpMultipleSegments2
202
.match(new RemainingPath(VALID_PATH_2 + rest));
203
assertNotNull(matchingResult);
204
assertEquals("25478", matchingResult.getVariables().get(ID1));
205
assertEquals("12345", matchingResult.getVariables().get(ID2));
206
assertEquals(new RemainingPath(rest), matchingResult
207
.getFinalCapturingGroup());