Absolutely ages ago, I found this great article on JUnit testing for UrlRewrite by Sujit Pal. It was very useful as at the time as I was looking for a way of testing that my usage of UrlRewrite was working correctly without having a servlet container running. Together with my contribution, both inbound and outbound rules are covered.
The solution is great for JUnit 3 but not really in the spirit of JUnit 4, where the test cases aren't necessarily part of an inheritance hierarchy, as this article demonstrates.
I had cause to test some rewrites again recently. However, I‘ve moved over to JUnit4 as a preference these days, so I decided to refresh Sujit‘s solution, decouple it from TestCase.
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.tuckey.web.MockRequest;
import org.tuckey.web.MockResponse;
import org.tuckey.web.filters.urlrewrite.Conf;
import org.tuckey.web.filters.urlrewrite.RewrittenUrl;
import org.tuckey.web.filters.urlrewrite.UrlRewriteWrappedResponse;
import org.tuckey.web.filters.urlrewrite.UrlRewriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
/**
* <p>A context for testing UrlRewrite rules for JUnit4</p>
*/
public class UrlRewriteTesting {
//----------------------------------------------------------------------- Instance Properties
protected Conf conf;
protected UrlRewriter rewriter;
//----------------------------------------------------------------------- Constructors
public UrlRewriteTesting(Conf conf) {
org.tuckey.web.filters.urlrewrite.utils.Log.setLevel("DEBUG");
this.conf = conf;
rewriter = new UrlRewriter(conf);
}
public UrlRewriteTesting(String pathToCfg, String confFile) throws FileNotFoundException {
this(new Conf(new FileInputStream(pathToCfg + "/" + confFile), confFile));
}
//----------------------------------------------------------------------- Instance Methods
/**
* Assertion to rewrite the URL using the UrlRewriteFilter and verify
* that fromUrl is rewritten to toUrl using rewriting rules in conf.
*
* @param fromUrl the URL to be rewritten from.
* @param toUrl the URL to be rewritten to.
*/
public void assertInboundRewrite(String fromUrl, String toUrl) {
RewrittenUrl rewrittenUrl = rewriter.processRequest(new MockRequest(fromUrl), new MockResponse());
assertNotNull("Could not rewrite URL from:" + fromUrl + " to:" + toUrl, rewrittenUrl);
assertEquals("Rewrite from:" + fromUrl + " to:" + toUrl + " did not succeed", toUrl, rewrittenUrl.getTarget());
}
/**
* Tests that an outbound url is processed correctly by the outbound rules
*
* @param fromUrl the url as you would be writing it in your JSPs
* @param toUrl what you expect it to be rewritten to when rendered
*/
public void assertOutboundRewrite(String fromUrl, String toUrl) {
UrlRewriteWrappedResponse wrappedResponse =
new UrlRewriteWrappedResponse(new MockResponse(), new MockRequest(fromUrl), new UrlRewriter(conf));
assertEquals("Rewrite from:" + fromUrl + " to:" + toUrl + " did not succeed", toUrl, wrappedResponse.encodeURL(fromUrl));
}
}
Usage
Below is an example of how SeemoreJ's rewrite rules in urlrewrite.xml for this site might look for the home page. At the time of writing, this site does not use UrlRewrite, so it's entirely hypothetical.
<rule>
<from>^/$</from>
<to>/seemore?sjController=blog&sjAction=home&sjFormat=html</to>
</rule>
<rule>
<from>^/index.(xml|html)$</from>
<to>/seemore?sjController=blog&sjAction=home&sjFormat=$1</to>
</rule>
<outbound-rule>
<from>/seemore\?sjController=blog&sjAction=home&sjFormat=(xml|html)</from>
<to>/index.$1</to>
</outbound-rule>
I might be making numerous minor or major changes to this file and I want the unit tests to defend against unwanted side-effects. So the test case below will (hopefully) make sure I don‘t mess things up.
import some.pakage.name.UrlRewriteTesting;
import org.junit.Test;
import java.io.FileNotFoundException;
public class UrlRewriteRulesTest {
//----------------------------------------------------------------------- Instance Properties
UrlRewriteTesting context;
//----------------------------------------------------------------------- Constructors
public UrlRewriteRulesTest() throws FileNotFoundException {
context = new UrlRewriteTesting(System.getProperty("basedir") +"/src/main/webapp/WEB-INF", "urlrewrite.xml");
}
//----------------------------------------------------------------------- Tests
@Test
public void testIndexInbound() {
context.assertInboundRewrite("/", "/seemore?sjController=blog&sjAction=home&sjFormat=html");
context.assertInboundRewrite("/index.html", "/seemore?sjController=blog&sjAction=home&sjFormat=html");
context.assertInboundRewrite("/index.xml", "/seemore?sjController=blog&sjAction=home&sjFormat=xml");
}
@Test
public void testIndexOutbound() {
context.assertOutboundRewrite("/seemore?sjController=blog&sjAction=home&sjFormat=html", "/index.html");
context.assertOutboundRewrite("/seemore?sjController=blog&sjAction=home&sjFormat=xml", "/index.xml");
}
}
First published on Dec 20, 2008. Last updated on: Dec 30, 2009.