~ubuntu-branches/ubuntu/saucy/restlet/saucy

« back to all changes in this revision

Viewing changes to org.restlet.test/src/org/restlet/test/jaxrs/util/PathRegExpTests.java

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-06-11 16:25:45 UTC
  • Revision ID: package-import@ubuntu.com-20120611162545-5w2o0resi5y3pybc
Tags: upstream-2.0.14
ImportĀ upstreamĀ versionĀ 2.0.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright 2005-2012 Restlet S.A.S.
 
3
 * 
 
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.
 
8
 * 
 
9
 * You can obtain a copy of the Apache 2.0 license at
 
10
 * http://www.opensource.org/licenses/apache-2.0
 
11
 * 
 
12
 * You can obtain a copy of the LGPL 3.0 license at
 
13
 * http://www.opensource.org/licenses/lgpl-3.0
 
14
 * 
 
15
 * You can obtain a copy of the LGPL 2.1 license at
 
16
 * http://www.opensource.org/licenses/lgpl-2.1
 
17
 * 
 
18
 * You can obtain a copy of the CDDL 1.0 license at
 
19
 * http://www.opensource.org/licenses/cddl1
 
20
 * 
 
21
 * You can obtain a copy of the EPL 1.0 license at
 
22
 * http://www.opensource.org/licenses/eclipse-1.0
 
23
 * 
 
24
 * See the Licenses for the specific language governing permissions and
 
25
 * limitations under the Licenses.
 
26
 * 
 
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
 
30
 * 
 
31
 * Restlet is a registered trademark of Restlet S.A.S.
 
32
 */
 
33
 
 
34
package org.restlet.test.jaxrs.util;
 
35
 
 
36
import java.lang.reflect.Constructor;
 
37
 
 
38
import javax.ws.rs.Path;
 
39
 
 
40
import junit.framework.TestCase;
 
41
 
 
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;
 
45
 
 
46
/**
 
47
 * @author Stephan Koops
 
48
 * @see PathRegExp
 
49
 */
 
50
public class PathRegExpTests extends TestCase {
 
51
 
 
52
    static final String ID1 = "id1";
 
53
 
 
54
    static final String ID2 = "id2";
 
55
 
 
56
    /** as {@link #PATH_PATTERN_2} but without "/" at end */
 
57
    static final String PATH_PATTERN_1 = "/abc/{" + ID1 + "}/shf/{" + ID2
 
58
            + "}/xyz";
 
59
 
 
60
    /** as {@link #PATH_PATTERN_1} but with "/" at end */
 
61
    static final String PATH_PATTERN_2 = PATH_PATTERN_1 + "/";
 
62
 
 
63
    /** as {@link #VALID_PATH_2} but without "/" at end */
 
64
    static final String VALID_PATH_1 = "/abc/25478/shf/12345/xyz";
 
65
 
 
66
    /** as {@link #VALID_PATH_2} but without "/" at end */
 
67
    static final RemainingPath VALID_PATH_1_RP = new RemainingPath(VALID_PATH_1);
 
68
 
 
69
    /** as {@link #VALID_PATH_1} but with "/" at end */
 
70
    public static final String VALID_PATH_2 = VALID_PATH_1 + "/";
 
71
 
 
72
    /** as {@link #VALID_PATH_1} but with "/" at end */
 
73
    public static final RemainingPath VALID_PATH_2_RP = new RemainingPath(
 
74
            VALID_PATH_2);
 
75
 
 
76
    private static final PathRegExp newPathRegExp(String pathPattern) {
 
77
        try {
 
78
            final Constructor<PathRegExp> constructor;
 
79
            final Class<PathRegExp> pathRegExpClass = PathRegExp.class;
 
80
            constructor = pathRegExpClass.getDeclaredConstructor(String.class,
 
81
                    Path.class);
 
82
            constructor.setAccessible(true);
 
83
            return constructor.newInstance(pathPattern, null);
 
84
        } catch (RuntimeException e) {
 
85
            throw e;
 
86
        } catch (Exception e) {
 
87
            throw new RuntimeException(e);
 
88
        }
 
89
    }
 
90
 
 
91
    private final PathRegExp regExpMultipleSegments1 = newPathRegExp(PATH_PATTERN_1);
 
92
 
 
93
    private final PathRegExp regExpMultipleSegments2 = newPathRegExp(PATH_PATTERN_2);
 
94
 
 
95
    private final PathRegExp regExpOneSegment1 = newPathRegExp(PATH_PATTERN_1);
 
96
 
 
97
    private final PathRegExp regExpOneSegment2 = newPathRegExp(PATH_PATTERN_2);
 
98
 
 
99
    /**
 
100
     * Test method for
 
101
     * {@link org.restlet.ext.jaxrs.internal.util.PathRegExp#match(java.lang.String)} .
 
102
     */
 
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());
 
111
 
 
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());
 
118
    }
 
119
 
 
120
    /**
 
121
     * Test method for
 
122
     * {@link org.restlet.ext.jaxrs.internal.util.PathRegExp#match(java.lang.String)} .
 
123
     */
 
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));
 
130
 
 
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());
 
137
    }
 
138
 
 
139
    /**
 
140
     * Test method for
 
141
     * {@link org.restlet.ext.jaxrs.internal.util.PathRegExp#match(java.lang.String)} .
 
142
     */
 
143
    public void testMatchM3() {
 
144
        final String rest = "/jkgjg";
 
145
        tryWithRest(rest);
 
146
    }
 
147
 
 
148
    /**
 
149
     * Test method for
 
150
     * {@link org.restlet.ext.jaxrs.internal.util.PathRegExp#match(java.lang.String)} .
 
151
     */
 
152
    public void testMatchM4() {
 
153
        final String rest = "/qarear/iuguz/izu/";
 
154
        tryWithRest(rest);
 
155
    }
 
156
 
 
157
    /**
 
158
     * Test method for
 
159
     * {@link org.restlet.ext.jaxrs.internal.util.PathRegExp#match(java.lang.String)} .
 
160
     */
 
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());
 
169
 
 
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());
 
176
    }
 
177
 
 
178
    /**
 
179
     * Test method for
 
180
     * {@link org.restlet.ext.jaxrs.internal.util.PathRegExp#match(java.lang.String)} .
 
181
     */
 
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));
 
188
 
 
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());
 
195
    }
 
196
 
 
197
    /**
 
198
     * @param rest
 
199
     */
 
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());
 
208
    }
 
209
}