Skip to content

Commit ffcead0

Browse files
author
mars
committed
Tests for issue ocpsoft#102
1 parent fc7a39c commit ffcead0

2 files changed

Lines changed: 150 additions & 0 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:fabmars@gmail.com">Fabien Marsaud</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.servlet.config;
17+
18+
import junit.framework.Assert;
19+
20+
import org.apache.http.client.methods.HttpGet;
21+
import org.jboss.arquillian.container.test.api.Deployment;
22+
import org.jboss.arquillian.junit.Arquillian;
23+
import org.jboss.shrinkwrap.api.spec.WebArchive;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
import org.ocpsoft.rewrite.config.ConfigurationProvider;
27+
import org.ocpsoft.rewrite.servlet.ServletRoot;
28+
import org.ocpsoft.rewrite.test.HttpAction;
29+
import org.ocpsoft.rewrite.test.RewriteTest;
30+
31+
/**
32+
* @author <a href="mailto:fabmars@gmail.com">Fabien Marsaud</a>
33+
*/
34+
@RunWith(Arquillian.class)
35+
public class RequestNullBindingTest extends RewriteTest
36+
{
37+
@Deployment(testable = false)
38+
public static WebArchive getDeployment()
39+
{
40+
WebArchive deployment = RewriteTest
41+
.getDeployment()
42+
.addPackages(true, ServletRoot.class.getPackage())
43+
.addAsServiceProvider(ConfigurationProvider.class, RequestNullBindingTestProvider.class);
44+
return deployment;
45+
}
46+
47+
//"123" should be converted to 123L, OK
48+
@Test
49+
public void testNotNullBinding() throws Exception
50+
{
51+
HttpAction<HttpGet> action = get("/foo/123");
52+
Assert.assertEquals(200, action.getResponse().getStatusLine().getStatusCode());
53+
}
54+
55+
//"abc" can't be converted to Long so we're expecting an error 500 here.
56+
@Test
57+
public void testNonLongBinding() throws Exception
58+
{
59+
HttpAction<HttpGet> action = get("/foo/abc");
60+
Assert.assertEquals(500, action.getResponse().getStatusLine().getStatusCode());
61+
}
62+
63+
// "" should be converted to null
64+
@Test
65+
public void testNullBinding1() throws Exception
66+
{
67+
HttpAction<HttpGet> action = get("/foo/");
68+
Assert.assertEquals(200, action.getResponse().getStatusLine().getStatusCode());
69+
}
70+
71+
// idem
72+
@Test
73+
public void testNullBinding2() throws Exception
74+
{
75+
HttpAction<HttpGet> action = get("/bar//");
76+
Assert.assertEquals(200, action.getResponse().getStatusLine().getStatusCode());
77+
}
78+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:fabmars@gmail.com">Fabien Marsaud</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.servlet.config;
17+
18+
import javax.servlet.ServletContext;
19+
20+
import org.ocpsoft.rewrite.bind.Evaluation;
21+
import org.ocpsoft.rewrite.config.Configuration;
22+
import org.ocpsoft.rewrite.config.ConfigurationBuilder;
23+
import org.ocpsoft.rewrite.config.Direction;
24+
import org.ocpsoft.rewrite.context.EvaluationContext;
25+
import org.ocpsoft.rewrite.event.Rewrite;
26+
import org.ocpsoft.rewrite.param.Converter;
27+
28+
/**
29+
* @author <a href="mailto:fabmars@gmail.com">Fabien Marsaud</a>
30+
*/
31+
public class RequestNullBindingTestProvider extends HttpConfigurationProvider
32+
{
33+
@Override
34+
public int priority() {
35+
return 0;
36+
}
37+
38+
@Override
39+
public Configuration getConfiguration(final ServletContext context)
40+
{
41+
Configuration config = ConfigurationBuilder
42+
.begin()
43+
44+
.addRule()
45+
.when(Direction.isInbound().and(Path.matches("/foo/{id}")))
46+
.perform(SendStatus.code(200))
47+
.where("id").bindsTo(Evaluation.property("id")).matches(".*")
48+
.convertedBy(longConverter)
49+
50+
.addRule()
51+
.when(Direction.isInbound().and(Path.matches("/bar/{id}/")))
52+
.perform(SendStatus.code(200))
53+
.where("id").bindsTo(Evaluation.property("id")).matches(".*")
54+
.convertedBy(longConverter);
55+
56+
return config;
57+
}
58+
59+
private final Converter<Long> longConverter = new Converter<Long>() {
60+
61+
@Override
62+
public Long convert(final Rewrite event, final EvaluationContext context, final Object value) {
63+
String sVal = (String)value;
64+
if (sVal == null || sVal.length() == 0) {
65+
return null;
66+
}
67+
else {
68+
return Long.valueOf(sVal);
69+
}
70+
}
71+
};
72+
}

0 commit comments

Comments
 (0)