Skip to content

Commit 776b39b

Browse files
committed
Added integration test for ServletMapping condition
1 parent d9c0787 commit 776b39b

4 files changed

Lines changed: 161 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</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.config.Configuration;
21+
import org.ocpsoft.rewrite.config.ConfigurationBuilder;
22+
import org.ocpsoft.rewrite.config.Not;
23+
24+
/**
25+
* @author Christian Kaltepoth
26+
*/
27+
public class ServletMappingConfigurationProvider extends HttpConfigurationProvider
28+
{
29+
@Override
30+
public int priority()
31+
{
32+
return 0;
33+
}
34+
35+
@Override
36+
public Configuration getConfiguration(final ServletContext context)
37+
{
38+
return ConfigurationBuilder.begin()
39+
40+
.addRule()
41+
.when(Path.matches("/test-with-mapped-url")
42+
.and(ServletMapping.includes("/something.mapped")))
43+
.perform(SendStatus.code(210))
44+
45+
.addRule()
46+
.when(Path.matches("/test-with-unmapped-url")
47+
.and(Not.any(ServletMapping.includes("/something.other"))))
48+
.perform(SendStatus.code(210));
49+
50+
}
51+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</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 java.io.IOException;
19+
20+
import javax.servlet.ServletException;
21+
import javax.servlet.annotation.WebServlet;
22+
import javax.servlet.http.HttpServlet;
23+
import javax.servlet.http.HttpServletRequest;
24+
import javax.servlet.http.HttpServletResponse;
25+
26+
/**
27+
* @author Christian Kaltepoth
28+
*/
29+
@WebServlet(urlPatterns = "*.mapped")
30+
public class ServletMappingConfigurationServlet extends HttpServlet
31+
{
32+
33+
private static final long serialVersionUID = 1L;
34+
35+
@Override
36+
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
37+
{
38+
resp.getOutputStream().println("This Servlet does nothing.");
39+
}
40+
41+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</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.test.HttpAction;
28+
import org.ocpsoft.rewrite.test.RewriteTest;
29+
30+
/**
31+
* @author Christian Kaltepoth
32+
*/
33+
@RunWith(Arquillian.class)
34+
public class ServletMappingConfigurationTest extends RewriteTest
35+
{
36+
37+
@Deployment(testable = false)
38+
public static WebArchive getDeployment()
39+
{
40+
return RewriteTest.getDeployment()
41+
.addPackages(true, ConfigRoot.class.getPackage())
42+
.addClass(ServletMappingConfigurationServlet.class)
43+
.addAsServiceProvider(ConfigurationProvider.class, ServletMappingConfigurationProvider.class);
44+
}
45+
46+
@Test
47+
public void testServletMappingConditionMatches() throws Exception
48+
{
49+
HttpAction<HttpGet> action = get("/test-with-mapped-url");
50+
Assert.assertEquals(210, action.getResponse().getStatusLine().getStatusCode());
51+
}
52+
53+
@Test
54+
public void testServletMappingConditionDoesNotMatch() throws Exception
55+
{
56+
HttpAction<HttpGet> action = get("/test-with-unmapped-url");
57+
Assert.assertEquals(210, action.getResponse().getStatusLine().getStatusCode());
58+
}
59+
60+
}

‎test-harness/src/main/java/org/ocpsoft/rewrite/test/RewriteTest.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,15 @@ protected static JavaArchive getRewriteArchive()
204204
.addAsResource(new File("../impl/target/classes/META-INF"))
205205
.addAsResource(new File("../addressbuilder/target/classes/org"))
206206
.addAsResource(new File("../api-servlet/target/classes/org"))
207+
208+
/*
209+
* We have to manually create the META-NF/services entry for ServletRegistrationProvider,
210+
* because the existing ones from the two modules overwrite each other when added to the
211+
* archive.
212+
*/
213+
.addAsServiceProvider("org.ocpsoft.rewrite.servlet.spi.ServletRegistrationProvider",
214+
"org.ocpsoft.rewrite.servlet.impl.Servlet3ServletRegistrationProvider",
215+
"org.ocpsoft.rewrite.servlet.impl.WebXmlServletRegistrationProvider")
207216
.addAsResource(new File("../impl-servlet-2.5/target/classes/org"))
208217
.addAsResource(new File("../impl-servlet-2.5/target/classes/META-INF"))
209218
.addAsResource(new File("../impl-servlet-3.0/target/classes/org"))

0 commit comments

Comments
 (0)