Skip to content

Commit 59dc1cd

Browse files
committed
Refactor ParameterizedPatternParser to have a slightly nicer API
1 parent 2f39e97 commit 59dc1cd

32 files changed

Lines changed: 799 additions & 637 deletions

File tree

‎api-tests/src/test/java/org/ocpsoft/rewrite/param/ParameterizedPatternInterceptorTest.java‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@
1919
import java.util.Map;
2020

2121
import org.junit.Assert;
22-
import org.junit.Before;
2322
import org.junit.Ignore;
2423
import org.junit.Test;
25-
import org.ocpsoft.rewrite.mock.MockEvaluationContext;
26-
import org.ocpsoft.rewrite.mock.MockRewrite;
2724

2825
/**
2926
* This test case was intended to begin mocking out an i18n API.
@@ -32,24 +29,14 @@
3229
*/
3330
public class ParameterizedPatternInterceptorTest
3431
{
35-
private MockEvaluationContext context;
36-
private MockRewrite rewrite;
37-
38-
@Before
39-
public void before()
40-
{
41-
context = new MockEvaluationContext();
42-
rewrite = new MockRewrite();
43-
}
44-
4532
@Ignore
4633
@Test
4734
public void testMatchesWithParametersRespectsTrailingCharsWithWildcardParameter()
4835
{
4936
RegexParameterizedPatternParser path = new RegexParameterizedPatternParser("[^/]+",
5037
"/{i18n:customer}/{customer}/orders/{id}");
51-
Assert.assertTrue(path.matches(rewrite, context, "/cust/lincoln/orders/3"));
52-
Assert.assertFalse(path.matches(rewrite, context, "/wrong/lincoln/orders/3"));
38+
Assert.assertTrue(path.parse("/cust/lincoln/orders/3").matches());
39+
Assert.assertFalse(path.parse("/wrong/lincoln/orders/3").matches());
5340
}
5441

5542
@Ignore

‎api-tests/src/test/java/org/ocpsoft/rewrite/param/ParameterizedPatternTest.java‎

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
import java.util.Arrays;
1919
import java.util.LinkedHashMap;
2020
import java.util.Map;
21-
import java.util.Set;
2221
import java.util.Map.Entry;
22+
import java.util.Set;
2323

24-
import org.junit.After;
2524
import org.junit.Assert;
25+
import org.junit.Before;
2626
import org.junit.Test;
2727
import org.ocpsoft.rewrite.mock.MockEvaluationContext;
2828
import org.ocpsoft.rewrite.mock.MockRewrite;
@@ -34,24 +34,27 @@
3434
*/
3535
public class ParameterizedPatternTest
3636
{
37-
private MockEvaluationContext context = new MockEvaluationContext();
38-
private MockRewrite rewrite = new MockRewrite();
37+
private MockRewrite event;
38+
private MockEvaluationContext context;
3939

4040
public static void initialize(ParameterStore store, Parameterized parameterized)
4141
{
4242
Set<String> names = parameterized.getRequiredParameterNames();
43-
for (String name : names) {
43+
for (String name : names)
44+
{
4445
store.get(name, new DefaultParameter(name));
4546
}
4647

4748
parameterized.setParameterStore(store);
4849
}
4950

50-
@After
51-
public void after()
51+
@Before
52+
public void before()
5253
{
54+
event = new MockRewrite();
5355
context = new MockEvaluationContext();
54-
rewrite = new MockRewrite();
56+
context.put(ParameterStore.class, new DefaultParameterStore());
57+
context.put(ParameterValueStore.class, new DefaultParameterValueStore());
5558
}
5659

5760
@Test
@@ -62,7 +65,7 @@ public void testComplexMatchingWithMutipleAdjacentParameters() throws Exception
6265
ParameterizedPatternParser path = new RegexParameterizedPatternParser(
6366
"{prefix}/application/{seg}{suffix}");
6467

65-
DefaultParameterStore store = new DefaultParameterStore();
68+
ParameterStore store = DefaultParameterStore.getInstance(context);
6669
initialize(store, path);
6770

6871
((ConfigurableParameter<?>) store.get("prefix")).constrainedBy(new RegexConstraint(".*"));
@@ -71,14 +74,14 @@ public void testComplexMatchingWithMutipleAdjacentParameters() throws Exception
7174

7275
initialize(store, path);
7376

74-
Assert.assertTrue(path.matches(rewrite, new MockEvaluationContext(), url));
77+
Assert.assertTrue(path.parse(url).matches());
7578

7679
String[] expected = new String[] { "http://domain.com:8080/context", "pathy", "?foo=bar&baz=bazaar" };
77-
Map<Parameter<?>, String> parsed = path.parse(rewrite,
78-
new MockEvaluationContext(), url);
80+
Map<Parameter<?>, String> parsed = path.parse(url).getParameters(context);
7981

8082
int index = 0;
81-
for (Entry<Parameter<?>, String> entry : parsed.entrySet()) {
83+
for (Entry<Parameter<?>, String> entry : parsed.entrySet())
84+
{
8285
String value = entry.getValue();
8386
Assert.assertEquals(expected[index++], value);
8487
}
@@ -90,7 +93,7 @@ public void testCannotUseRegularExpressionsWithoutParameter()
9093
ParameterizedPatternParser path = new RegexParameterizedPatternParser(CaptureType.BRACE, ".*");
9194

9295
Assert.assertEquals(0, path.getRequiredParameterNames().size());
93-
Assert.assertFalse(path.matches(rewrite, context, "/omg/doesnt/matter"));
96+
Assert.assertFalse(path.parse("/omg/doesnt/matter").matches());
9497
}
9598

9699
@Test
@@ -99,8 +102,8 @@ public void testMatchesEmptyPath()
99102
ParameterizedPatternParser path = new RegexParameterizedPatternParser(CaptureType.BRACE, "");
100103

101104
Assert.assertEquals(0, path.getRequiredParameterNames().size());
102-
Assert.assertTrue(path.matches(rewrite, context, ""));
103-
Map<Parameter<?>, String> results = path.parse(rewrite, context, "");
105+
Assert.assertTrue(path.parse("").matches());
106+
Map<Parameter<?>, String> results = path.parse("").getParameters(context);
104107
Assert.assertNotNull(results);
105108
}
106109

@@ -110,8 +113,8 @@ public void testMatchesEmptyPathNoTransforms()
110113
ParameterizedPatternParser path = new RegexParameterizedPatternParser(CaptureType.BRACE, "");
111114

112115
Assert.assertEquals(0, path.getRequiredParameterNames().size());
113-
Assert.assertTrue(path.matches(rewrite, context, ""));
114-
Map<Parameter<?>, String> results = path.parse("");
116+
Assert.assertTrue(path.parse("").matches());
117+
Map<Parameter<?>, String> results = path.parse("").getParameters(context);
115118
Assert.assertNotNull(results);
116119
}
117120

@@ -121,9 +124,9 @@ public void testMatchesBarePath()
121124
ParameterizedPatternParser path = new RegexParameterizedPatternParser("/");
122125

123126
Assert.assertEquals(0, path.getRequiredParameterNames().size());
124-
Assert.assertTrue(path.matches(rewrite, context, "/"));
127+
Assert.assertTrue(path.parse("/").matches());
125128

126-
Map<Parameter<?>, String> results = path.parse(rewrite, context, "/");
129+
Map<Parameter<?>, String> results = path.parse("/").getParameters(context);
127130
Assert.assertNotNull(results);
128131
}
129132

@@ -132,15 +135,15 @@ public void testMatchesWithParameters()
132135
{
133136
ParameterizedPatternParser path = new RegexParameterizedPatternParser("[^/]+", "/{customer}/orders/{id}");
134137

135-
ParameterStore parameters = new DefaultParameterStore();
138+
ParameterStore parameters = DefaultParameterStore.getInstance(context);
136139

137140
initialize(parameters, path);
138141

139142
Assert.assertEquals(2, parameters.size());
140143
Assert.assertEquals("customer", parameters.get("customer").getName());
141144
Assert.assertEquals("id", parameters.get("id").getName());
142145

143-
Map<Parameter<?>, String> results = path.parse(rewrite, context, "/lincoln/orders/24");
146+
Map<Parameter<?>, String> results = path.parse("/lincoln/orders/24").getParameters(context);
144147
Assert.assertEquals("lincoln", results.get(parameters.get("customer")));
145148
Assert.assertEquals("24", results.get(parameters.get("id")));
146149
}
@@ -150,14 +153,14 @@ public void testMatchesWithParametersNoTransforms()
150153
{
151154
ParameterizedPatternParser path = new RegexParameterizedPatternParser("[^/]+", "/{customer}/orders/{id}");
152155

153-
ParameterStore parameters = new DefaultParameterStore();
156+
ParameterStore parameters = DefaultParameterStore.getInstance(context);
154157
initialize(parameters, path);
155158

156159
Assert.assertEquals(2, parameters.size());
157160
Assert.assertEquals("customer", parameters.get("customer").getName());
158161
Assert.assertEquals("id", parameters.get("id").getName());
159162

160-
Map<Parameter<?>, String> results = path.parse("/lincoln/orders/24");
163+
Map<Parameter<?>, String> results = path.parse("/lincoln/orders/24").getParameters(context);
161164
Assert.assertEquals("lincoln", results.get(parameters.get("customer")));
162165
Assert.assertEquals("24", results.get(parameters.get("id")));
163166
}
@@ -170,16 +173,16 @@ public void testMatchesWithParametersRespectsTrailingCharsWithWildcardParameter(
170173
ParameterStore parameters = new DefaultParameterStore();
171174
initialize(parameters, path);
172175

173-
Assert.assertTrue(path.matches(rewrite, context, "/lincoln/"));
174-
Assert.assertFalse(path.matches(rewrite, context, "/lincoln/foo"));
176+
Assert.assertTrue(path.parse("/lincoln/").matches());
177+
Assert.assertFalse(path.parse("/lincoln/foo").matches());
175178
}
176179

177180
@Test
178181
public void testRegularExpressionsAreDisabled()
179182
{
180183
ParameterizedPatternParser path = new RegexParameterizedPatternParser("[^/]+", ".*/{customer}/");
181-
Assert.assertTrue(path.matches(rewrite, context, ".*/lincoln/"));
182-
Assert.assertFalse(path.matches(rewrite, context, "foobar/lincoln/"));
184+
Assert.assertTrue(path.parse(".*/lincoln/").matches());
185+
Assert.assertFalse(path.parse("foobar/lincoln/").matches());
183186
}
184187

185188
@Test
@@ -215,39 +218,58 @@ public void testParametersUsedMultipleRequireSingleConfiguration()
215218

216219
((ConfigurableParameter<?>) parameters.get("f")).constrainedBy(new RegexConstraint("foo"));
217220

218-
Assert.assertTrue(path.matches(rewrite, context, "/foo/foo/"));
219-
Assert.assertFalse(path.matches(rewrite, context, "/foo/bar/"));
221+
Assert.assertTrue(path.parse("/foo/foo/").matches());
222+
Assert.assertFalse(path.parse("/foo/bar/").matches());
220223
}
221224

222225
@Test
223226
public void testParsesWithParametersRespectsTrailingCharsWithWildcardParameter()
224227
{
225228
ParameterizedPatternParser path = new RegexParameterizedPatternParser(".*", "/{customer}/");
226229

227-
ParameterStore parameters = new DefaultParameterStore();
230+
ParameterStore parameters = DefaultParameterStore.getInstance(context);
228231
initialize(parameters, path);
229232

230233
Assert.assertEquals(1, parameters.size());
231234
Assert.assertEquals("customer", parameters.get("customer").getName());
232235

233-
Map<Parameter<?>, String> results = path.parse(rewrite, context, "/lincoln/");
236+
Map<Parameter<?>, String> results = path.parse("/lincoln/").getParameters(context);
234237
Assert.assertEquals("lincoln", results.get(parameters.get("customer")));
235238
}
236239

240+
@Test
241+
public void testParsesWithParametersEscapesTrailingChars()
242+
{
243+
ParameterizedPatternParser path = new RegexParameterizedPatternParser(".*", "/{customer}.");
244+
245+
ParameterStore parameters = DefaultParameterStore.getInstance(context);
246+
initialize(parameters, path);
247+
248+
Assert.assertEquals(1, parameters.size());
249+
Assert.assertEquals("customer", parameters.get("customer").getName());
250+
251+
Assert.assertFalse(path.parse("/lincolnX").matches());
252+
Assert.assertFalse(path.parse("/lincoln/").matches());
253+
Assert.assertTrue(path.parse("/lincoln.").matches());
254+
}
255+
237256
@Test
238257
public void testMatchesWithParametersAndTrailingSlash()
239258
{
240259
ParameterizedPatternParser path = new RegexParameterizedPatternBuilder("[^/]+", "/{customer}/orders/{id}/")
241260
.getParser();
242261

243-
ParameterStore parameters = new DefaultParameterStore();
262+
ParameterStore parameters = DefaultParameterStore.getInstance(context);
244263
initialize(parameters, path);
245264

246265
Assert.assertEquals(2, parameters.size());
247266
Assert.assertEquals("customer", parameters.get("customer").getName());
248267
Assert.assertEquals("id", parameters.get("id").getName());
249268

250-
Map<Parameter<?>, String> results = path.parse(rewrite, context, "/lincoln/orders/24/");
269+
ParameterizedPatternResult result = path.parse("/lincoln/orders/24/");
270+
Assert.assertTrue(result.matches());
271+
Assert.assertTrue(result.submit(event, context));
272+
Map<Parameter<?>, String> results = result.getParameters(context);
251273
Assert.assertEquals("lincoln", results.get(parameters.get("customer")));
252274
Assert.assertEquals("24", results.get(parameters.get("id")));
253275
}

‎api/src/main/java/org/ocpsoft/rewrite/config/And.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public boolean evaluate(final Rewrite event, final EvaluationContext context)
7878
if (!conditions[i].evaluate(event, context))
7979
{
8080
result = false;
81+
break;
8182
}
8283
}
8384
return result;

‎api/src/main/java/org/ocpsoft/rewrite/config/DefaultConditionBuilder.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public String toString()
125125
};
126126
}
127127

128-
public static abstract class DefaultConditionBuilderInternal extends DefaultConditionBuilder implements
128+
static abstract class DefaultConditionBuilderInternal extends DefaultConditionBuilder implements
129129
CompositeCondition
130130
{
131131
private List<Condition> conditions;

0 commit comments

Comments
 (0)