Skip to content

Commit

Permalink
Refactor ParameterizedPatternParser to have a slightly nicer API
Browse files Browse the repository at this point in the history
  • Loading branch information
lincolnthree committed Nov 13, 2014
1 parent 9728e16 commit a39a2d1
Show file tree
Hide file tree
Showing 32 changed files with 799 additions and 637 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@
import java.util.Map;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.ocpsoft.rewrite.mock.MockEvaluationContext;
import org.ocpsoft.rewrite.mock.MockRewrite;

/**
* This test case was intended to begin mocking out an i18n API.
Expand All @@ -32,24 +29,14 @@
*/
public class ParameterizedPatternInterceptorTest
{
private MockEvaluationContext context;
private MockRewrite rewrite;

@Before
public void before()
{
context = new MockEvaluationContext();
rewrite = new MockRewrite();
}

@Ignore
@Test
public void testMatchesWithParametersRespectsTrailingCharsWithWildcardParameter()
{
RegexParameterizedPatternParser path = new RegexParameterizedPatternParser("[^/]+",
"/{i18n:customer}/{customer}/orders/{id}");
Assert.assertTrue(path.matches(rewrite, context, "/cust/lincoln/orders/3"));
Assert.assertFalse(path.matches(rewrite, context, "/wrong/lincoln/orders/3"));
Assert.assertTrue(path.parse("/cust/lincoln/orders/3").matches());
Assert.assertFalse(path.parse("/wrong/lincoln/orders/3").matches());
}

@Ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.Set;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.ocpsoft.rewrite.mock.MockEvaluationContext;
import org.ocpsoft.rewrite.mock.MockRewrite;
Expand All @@ -34,24 +34,27 @@
*/
public class ParameterizedPatternTest
{
private MockEvaluationContext context = new MockEvaluationContext();
private MockRewrite rewrite = new MockRewrite();
private MockRewrite event;
private MockEvaluationContext context;

public static void initialize(ParameterStore store, Parameterized parameterized)
{
Set<String> names = parameterized.getRequiredParameterNames();
for (String name : names) {
for (String name : names)
{
store.get(name, new DefaultParameter(name));
}

parameterized.setParameterStore(store);
}

@After
public void after()
@Before
public void before()
{
event = new MockRewrite();
context = new MockEvaluationContext();
rewrite = new MockRewrite();
context.put(ParameterStore.class, new DefaultParameterStore());
context.put(ParameterValueStore.class, new DefaultParameterValueStore());
}

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

DefaultParameterStore store = new DefaultParameterStore();
ParameterStore store = DefaultParameterStore.getInstance(context);
initialize(store, path);

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

initialize(store, path);

Assert.assertTrue(path.matches(rewrite, new MockEvaluationContext(), url));
Assert.assertTrue(path.parse(url).matches());

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

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

Assert.assertEquals(0, path.getRequiredParameterNames().size());
Assert.assertFalse(path.matches(rewrite, context, "/omg/doesnt/matter"));
Assert.assertFalse(path.parse("/omg/doesnt/matter").matches());
}

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

Assert.assertEquals(0, path.getRequiredParameterNames().size());
Assert.assertTrue(path.matches(rewrite, context, ""));
Map<Parameter<?>, String> results = path.parse(rewrite, context, "");
Assert.assertTrue(path.parse("").matches());
Map<Parameter<?>, String> results = path.parse("").getParameters(context);
Assert.assertNotNull(results);
}

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

Assert.assertEquals(0, path.getRequiredParameterNames().size());
Assert.assertTrue(path.matches(rewrite, context, ""));
Map<Parameter<?>, String> results = path.parse("");
Assert.assertTrue(path.parse("").matches());
Map<Parameter<?>, String> results = path.parse("").getParameters(context);
Assert.assertNotNull(results);
}

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

Assert.assertEquals(0, path.getRequiredParameterNames().size());
Assert.assertTrue(path.matches(rewrite, context, "/"));
Assert.assertTrue(path.parse("/").matches());

Map<Parameter<?>, String> results = path.parse(rewrite, context, "/");
Map<Parameter<?>, String> results = path.parse("/").getParameters(context);
Assert.assertNotNull(results);
}

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

ParameterStore parameters = new DefaultParameterStore();
ParameterStore parameters = DefaultParameterStore.getInstance(context);

initialize(parameters, path);

Assert.assertEquals(2, parameters.size());
Assert.assertEquals("customer", parameters.get("customer").getName());
Assert.assertEquals("id", parameters.get("id").getName());

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

ParameterStore parameters = new DefaultParameterStore();
ParameterStore parameters = DefaultParameterStore.getInstance(context);
initialize(parameters, path);

Assert.assertEquals(2, parameters.size());
Assert.assertEquals("customer", parameters.get("customer").getName());
Assert.assertEquals("id", parameters.get("id").getName());

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

Assert.assertTrue(path.matches(rewrite, context, "/lincoln/"));
Assert.assertFalse(path.matches(rewrite, context, "/lincoln/foo"));
Assert.assertTrue(path.parse("/lincoln/").matches());
Assert.assertFalse(path.parse("/lincoln/foo").matches());
}

@Test
public void testRegularExpressionsAreDisabled()
{
ParameterizedPatternParser path = new RegexParameterizedPatternParser("[^/]+", ".*/{customer}/");
Assert.assertTrue(path.matches(rewrite, context, ".*/lincoln/"));
Assert.assertFalse(path.matches(rewrite, context, "foobar/lincoln/"));
Assert.assertTrue(path.parse(".*/lincoln/").matches());
Assert.assertFalse(path.parse("foobar/lincoln/").matches());
}

@Test
Expand Down Expand Up @@ -215,39 +218,58 @@ public void testParametersUsedMultipleRequireSingleConfiguration()

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

Assert.assertTrue(path.matches(rewrite, context, "/foo/foo/"));
Assert.assertFalse(path.matches(rewrite, context, "/foo/bar/"));
Assert.assertTrue(path.parse("/foo/foo/").matches());
Assert.assertFalse(path.parse("/foo/bar/").matches());
}

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

ParameterStore parameters = new DefaultParameterStore();
ParameterStore parameters = DefaultParameterStore.getInstance(context);
initialize(parameters, path);

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

Map<Parameter<?>, String> results = path.parse(rewrite, context, "/lincoln/");
Map<Parameter<?>, String> results = path.parse("/lincoln/").getParameters(context);
Assert.assertEquals("lincoln", results.get(parameters.get("customer")));
}

@Test
public void testParsesWithParametersEscapesTrailingChars()
{
ParameterizedPatternParser path = new RegexParameterizedPatternParser(".*", "/{customer}.");

ParameterStore parameters = DefaultParameterStore.getInstance(context);
initialize(parameters, path);

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

Assert.assertFalse(path.parse("/lincolnX").matches());
Assert.assertFalse(path.parse("/lincoln/").matches());
Assert.assertTrue(path.parse("/lincoln.").matches());
}

@Test
public void testMatchesWithParametersAndTrailingSlash()
{
ParameterizedPatternParser path = new RegexParameterizedPatternBuilder("[^/]+", "/{customer}/orders/{id}/")
.getParser();

ParameterStore parameters = new DefaultParameterStore();
ParameterStore parameters = DefaultParameterStore.getInstance(context);
initialize(parameters, path);

Assert.assertEquals(2, parameters.size());
Assert.assertEquals("customer", parameters.get("customer").getName());
Assert.assertEquals("id", parameters.get("id").getName());

Map<Parameter<?>, String> results = path.parse(rewrite, context, "/lincoln/orders/24/");
ParameterizedPatternResult result = path.parse("/lincoln/orders/24/");
Assert.assertTrue(result.matches());
Assert.assertTrue(result.submit(event, context));
Map<Parameter<?>, String> results = result.getParameters(context);
Assert.assertEquals("lincoln", results.get(parameters.get("customer")));
Assert.assertEquals("24", results.get(parameters.get("id")));
}
Expand Down
1 change: 1 addition & 0 deletions api/src/main/java/org/ocpsoft/rewrite/config/And.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public boolean evaluate(final Rewrite event, final EvaluationContext context)
if (!conditions[i].evaluate(event, context))
{
result = false;
break;
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public String toString()
};
}

public static abstract class DefaultConditionBuilderInternal extends DefaultConditionBuilder implements
static abstract class DefaultConditionBuilderInternal extends DefaultConditionBuilder implements
CompositeCondition
{
private List<Condition> conditions;
Expand Down
Loading

0 comments on commit a39a2d1

Please sign in to comment.