Skip to content

Commit bd32f06

Browse files
committed
Wow... where("param").validatedBy(...) is working now
1 parent 0c5802e commit bd32f06

55 files changed

Lines changed: 1156 additions & 687 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎api-tests/src/test/java/org/ocpsoft/rewrite/bind/BindingsTest.java‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import org.junit.Assert;
1919
import org.junit.Before;
2020
import org.junit.Test;
21-
2221
import org.ocpsoft.rewrite.config.Condition;
22+
import org.ocpsoft.rewrite.context.EvaluationContext;
2323
import org.ocpsoft.rewrite.event.Rewrite;
2424
import org.ocpsoft.rewrite.mock.MockBinding;
2525
import org.ocpsoft.rewrite.mock.MockEvaluationContext;
@@ -39,6 +39,34 @@ public void before()
3939
rewrite = new MockRewrite();
4040
}
4141

42+
@Test
43+
@SuppressWarnings("rawtypes")
44+
public void testEnqueueSubmissionsExecutesEvaluationEagerly() throws Exception
45+
{
46+
MockEvaluationContext context = new MockEvaluationContext();
47+
48+
Assert.assertTrue(Bindings.enqueueSubmission(rewrite, context,
49+
new DefaultBindable().bindsTo(Evaluation.property("lincoln")), "baxter"));
50+
Assert.assertEquals("baxter", Evaluation.property("lincoln").retrieve(rewrite, context));
51+
}
52+
53+
@Test
54+
@SuppressWarnings("rawtypes")
55+
public void testEnqueueEvaluationProcessesConversionAndValidation() throws Exception
56+
{
57+
MockEvaluationContext context = new MockEvaluationContext();
58+
Validator validator = new Validator() {
59+
@Override
60+
public boolean validate(Rewrite event, EvaluationContext context, Object value)
61+
{
62+
return "baxter III".equals(value);
63+
}
64+
};
65+
66+
Assert.assertFalse(Bindings.enqueueSubmission(rewrite, context,
67+
new DefaultBindable().bindsTo(Evaluation.property("lincoln").validatedBy(validator)), "baxter"));
68+
}
69+
4270
/*
4371
* Equals
4472
*/
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package org.ocpsoft.rewrite.bind;
2+
3+
import javax.servlet.http.HttpServletRequest;
4+
5+
import junit.framework.Assert;
6+
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
import org.mockito.Mockito;
10+
import org.ocpsoft.rewrite.context.EvaluationContext;
11+
import org.ocpsoft.rewrite.event.Rewrite;
12+
import org.ocpsoft.rewrite.mock.MockEvaluationContext;
13+
import org.ocpsoft.rewrite.servlet.impl.HttpInboundRewriteImpl;
14+
import org.ocpsoft.rewrite.servlet.impl.HttpOutboundRewriteImpl;
15+
16+
public class EvaluationBindingTest
17+
{
18+
private Rewrite inbound;
19+
private Rewrite outbound;
20+
private HttpServletRequest request;
21+
22+
@Before
23+
public void before()
24+
{
25+
request = Mockito.mock(HttpServletRequest.class);
26+
Mockito.when(request.getServerName())
27+
.thenReturn("example.com");
28+
29+
inbound = new HttpInboundRewriteImpl(request, null);
30+
outbound = new HttpOutboundRewriteImpl(request, null, "http://example.com:8080/path?query=value");
31+
}
32+
33+
@Test(expected = IllegalArgumentException.class)
34+
public void testCannotAccessNonexistentEvaluationContextPropertyInbound() throws Exception
35+
{
36+
MockEvaluationContext context = new MockEvaluationContext();
37+
Evaluation.property("property").retrieve(inbound, context);
38+
}
39+
40+
@Test(expected = IllegalArgumentException.class)
41+
public void testCannotAccessNonexistentEvaluationContextPropertyOutbound() throws Exception
42+
{
43+
MockEvaluationContext context = new MockEvaluationContext();
44+
Evaluation.property("property").retrieve(outbound, context);
45+
}
46+
47+
@Test
48+
public void testCanAccessEvaluationContextPropertyInbound() throws Exception
49+
{
50+
MockEvaluationContext context = new MockEvaluationContext();
51+
try {
52+
Evaluation.property("property").retrieve(inbound, context);
53+
Assert.fail();
54+
}
55+
catch (IllegalArgumentException e) {}
56+
57+
Evaluation.property("property").submit(inbound, context, "Foo");
58+
Object value = Evaluation.property("property").retrieve(inbound, context);
59+
60+
Assert.assertEquals("Foo", value);
61+
}
62+
63+
@Test
64+
public void testCanAccessEvaluationContextPropertyOutbound() throws Exception
65+
{
66+
MockEvaluationContext context = new MockEvaluationContext();
67+
try {
68+
Evaluation.property("property").retrieve(outbound, context);
69+
Assert.fail();
70+
}
71+
catch (IllegalArgumentException e) {}
72+
73+
Evaluation.property("property").submit(outbound, context, "Foo");
74+
Object value = Evaluation.property("property").retrieve(outbound, context);
75+
76+
Assert.assertEquals("Foo", value);
77+
}
78+
79+
@Test
80+
public void testCanAccessEvaluationContextConvertedPropertyInbound() throws Exception
81+
{
82+
MockEvaluationContext context = new MockEvaluationContext();
83+
84+
Converter<String> converter = new Converter<String>() {
85+
@Override
86+
public String convert(Rewrite event, EvaluationContext context, Object value)
87+
{
88+
return "Bar";
89+
}
90+
};
91+
92+
Evaluation.property("property").convertedBy(converter).submit(inbound, context, "Foo");
93+
Assert.assertEquals("Foo", Evaluation.property("property").retrieve(inbound, context));
94+
Assert.assertEquals("Bar", Evaluation.property("property").retrieveConverted(inbound, context));
95+
}
96+
97+
@Test
98+
public void testCanAccessEvaluationContextConvertedPropertyOutbound() throws Exception
99+
{
100+
MockEvaluationContext context = new MockEvaluationContext();
101+
102+
Converter<String> converter = new Converter<String>() {
103+
@Override
104+
public String convert(Rewrite event, EvaluationContext context, Object value)
105+
{
106+
return "Bar";
107+
}
108+
};
109+
110+
Evaluation.property("property").convertedBy(converter).submit(outbound, context, "Foo");
111+
Assert.assertEquals("Foo", Evaluation.property("property").retrieve(outbound, context));
112+
Assert.assertEquals("Bar", Evaluation.property("property").retrieveConverted(outbound, context));
113+
}
114+
115+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.ocpsoft.rewrite.param;
2+
3+
4+
public class MockParameter extends ParameterBuilder<MockParameter, String>
5+
{
6+
private final String name;
7+
8+
public MockParameter(String name)
9+
{
10+
this.name = name;
11+
}
12+
13+
@Override
14+
public String getName()
15+
{
16+
return name;
17+
}
18+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.ocpsoft.rewrite.param;
2+
3+
import junit.framework.Assert;
4+
5+
import org.junit.Test;
6+
import org.ocpsoft.rewrite.bind.Converter;
7+
import org.ocpsoft.rewrite.bind.Validator;
8+
import org.ocpsoft.rewrite.context.EvaluationContext;
9+
import org.ocpsoft.rewrite.event.Rewrite;
10+
import org.ocpsoft.rewrite.mock.MockEvaluationContext;
11+
import org.ocpsoft.rewrite.mock.MockRewrite;
12+
13+
public class ParameterBuilderTest
14+
{
15+
16+
protected boolean validated;
17+
protected boolean converted;
18+
19+
@Test
20+
public void testCanValidateAndConvertWithoutBinding()
21+
{
22+
Validator<String> validator = new Validator<String>() {
23+
@Override
24+
public boolean validate(Rewrite event, EvaluationContext context, String value)
25+
{
26+
validated = true;
27+
return false;
28+
}
29+
};
30+
31+
Converter<String> converter = new Converter<String>() {
32+
33+
@Override
34+
public String convert(Rewrite event, EvaluationContext context, Object value)
35+
{
36+
converted = true;
37+
return null;
38+
}
39+
};
40+
41+
Parameter<?, ?> parameter = new MockParameter("foo").validatedBy(validator).convertedBy(converter);
42+
43+
Object value = parameter.convert(new MockRewrite(), new MockEvaluationContext(), new Object());
44+
boolean valid = parameter.validates(new MockRewrite(), new MockEvaluationContext(), value);
45+
46+
Assert.assertTrue(validated);
47+
Assert.assertFalse(valid);
48+
Assert.assertTrue(converted);
49+
Assert.assertNull(value);
50+
}
51+
52+
}

0 commit comments

Comments
 (0)