From b523b9cad097156532fde79b22c07a04a5953cec Mon Sep 17 00:00:00 2001 From: kkolinko Date: Mon, 25 Jul 2011 19:25:22 +0000 Subject: [PATCH] Converted the tests to JUnit 4. git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1150855 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/catalina/core/TestAsyncContextImpl.java | 92 +++++++++++++++------- .../catalina/core/TestNamingContextListener.java | 9 ++- .../apache/catalina/core/TestStandardContext.java | 21 ++++- .../catalina/core/TestStandardContextAliases.java | 9 ++- .../core/TestStandardContextResources.java | 15 +++- .../apache/catalina/core/TestStandardWrapper.java | 30 ++++++- .../catalina/core/TestSwallowAbortedUploads.java | 17 +++- .../catalina/filters/TestAddCharSetFilter.java | 16 +++- .../catalina/filters/TestCsrfPreventionFilter.java | 24 ++++-- .../apache/catalina/filters/TestExpiresFilter.java | 21 ++++- .../catalina/filters/TestRemoteIpFilter.java | 30 ++++++- 11 files changed, 225 insertions(+), 59 deletions(-) diff --git a/test/org/apache/catalina/core/TestAsyncContextImpl.java b/test/org/apache/catalina/core/TestAsyncContextImpl.java index efb5fa76f..117f6d9ab 100644 --- a/test/org/apache/catalina/core/TestAsyncContextImpl.java +++ b/test/org/apache/catalina/core/TestAsyncContextImpl.java @@ -34,16 +34,23 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.junit.Test; + import org.apache.catalina.Context; import org.apache.catalina.Wrapper; import org.apache.catalina.connector.Request; import org.apache.catalina.startup.Tomcat; -import org.apache.catalina.startup.TomcatBaseTest; +import org.apache.catalina.startup.TomcatBaseTestJUnit4; import org.apache.catalina.valves.TesterAccessLogValve; import org.apache.catalina.valves.TesterAccessLogValve.Entry; import org.apache.tomcat.util.buf.ByteChunk; -public class TestAsyncContextImpl extends TomcatBaseTest { +public class TestAsyncContextImpl extends TomcatBaseTestJUnit4 { // Time for a request to process (need to allow for threads to start etc.) private static final long REQUEST_TIME = 1000; @@ -54,6 +61,7 @@ public class TestAsyncContextImpl extends TomcatBaseTest { // Default timeout for these tests private static final long TIMEOUT = 3000; + @Test public void testBug49528() throws Exception { // Setup Tomcat instance Tomcat tomcat = getTomcatInstance(); @@ -90,7 +98,8 @@ public class TestAsyncContextImpl extends TomcatBaseTest { validateAccessLog(alv, 1, 200, Bug49528Servlet.THREAD_SLEEP_TIME, Bug49528Servlet.THREAD_SLEEP_TIME + REQUEST_TIME); } - + + @Test public void testBug49567() throws Exception { // Setup Tomcat instance Tomcat tomcat = getTomcatInstance(); @@ -127,7 +136,8 @@ public class TestAsyncContextImpl extends TomcatBaseTest { validateAccessLog(alv, 1, 200, Bug49567Servlet.THREAD_SLEEP_TIME, Bug49567Servlet.THREAD_SLEEP_TIME + REQUEST_TIME); } - + + @Test public void testAsyncStartNoComplete() throws Exception { // Setup Tomcat instance Tomcat tomcat = getTomcatInstance(); @@ -168,7 +178,8 @@ public class TestAsyncContextImpl extends TomcatBaseTest { AsyncStartNoCompleteServlet.ASYNC_TIMEOUT + TIMEOUT_MARGIN + REQUEST_TIME); } - + + @Test public void testAsyncStartWithComplete() throws Exception { // Setup Tomcat instance Tomcat tomcat = getTomcatInstance(); @@ -365,21 +376,25 @@ public class TestAsyncContextImpl extends TomcatBaseTest { } } + @Test public void testTimeoutListenerCompleteNoDispatch() throws Exception { // Should work doTestTimeout(true, null); } - + + @Test public void testTimeoutListenerNoCompleteNoDispatch() throws Exception { // Should trigger an error - must do one or other doTestTimeout(false, null); } + @Test public void testTimeoutListenerCompleteDispatch() throws Exception { // Should trigger an error - can't do both doTestTimeout(true, "/nonasync"); } + @Test public void testTimeoutListenerNoCompleteDispatch() throws Exception { // Should work doTestTimeout(false, "/nonasync"); @@ -491,26 +506,32 @@ public class TestAsyncContextImpl extends TomcatBaseTest { } } + @Test public void testDispatchSingle() throws Exception { doTestDispatch(1, false); } - + + @Test public void testDispatchDouble() throws Exception { doTestDispatch(2, false); } - + + @Test public void testDispatchMultiple() throws Exception { doTestDispatch(5, false); } - + + @Test public void testDispatchWithThreadSingle() throws Exception { doTestDispatch(1, true); } - + + @Test public void testDispatchWithThreadDouble() throws Exception { doTestDispatch(2, true); } - + + @Test public void testDispatchWithThreadMultiple() throws Exception { doTestDispatch(5, true); } @@ -620,7 +641,8 @@ public class TestAsyncContextImpl extends TomcatBaseTest { resp.flushBuffer(); } } - + + @Test public void testListeners() throws Exception { // Setup Tomcat instance Tomcat tomcat = getTomcatInstance(); @@ -779,52 +801,64 @@ public class TestAsyncContextImpl extends TomcatBaseTest { } } + @Test public void testDispatchErrorSingle() throws Exception { doTestDispatchError(1, false, false); } - + + @Test public void testDispatchErrorDouble() throws Exception { doTestDispatchError(2, false, false); } - + + @Test public void testDispatchErrorMultiple() throws Exception { doTestDispatchError(5, false, false); } - + + @Test public void testDispatchErrorWithThreadSingle() throws Exception { doTestDispatchError(1, true, false); } - + + @Test public void testDispatchErrorWithThreadDouble() throws Exception { doTestDispatchError(2, true, false); } - + + @Test public void testDispatchErrorWithThreadMultiple() throws Exception { doTestDispatchError(5, true, false); } - + + @Test public void testDispatchErrorSingleThenComplete() throws Exception { doTestDispatchError(1, false, true); } - + + @Test public void testDispatchErrorDoubleThenComplete() throws Exception { doTestDispatchError(2, false, true); } - + + @Test public void testDispatchErrorMultipleThenComplete() throws Exception { doTestDispatchError(5, false, true); } - + + @Test public void testDispatchErrorWithThreadSingleThenComplete() throws Exception { doTestDispatchError(1, true, true); } - + + @Test public void testDispatchErrorWithThreadDoubleThenComplete() throws Exception { doTestDispatchError(2, true, true); } - + + @Test public void testDispatchErrorWithThreadMultipleThenComplete() throws Exception { doTestDispatchError(5, true, true); @@ -904,7 +938,8 @@ public class TestAsyncContextImpl extends TomcatBaseTest { throw new ServletException("Opps."); } } - + + @Test public void testBug50352() throws Exception { // Setup Tomcat instance Tomcat tomcat = getTomcatInstance(); @@ -965,7 +1000,8 @@ public class TestAsyncContextImpl extends TomcatBaseTest { }); } } - + + @Test public void testBug50753() throws Exception { // Setup Tomcat instance Tomcat tomcat = getTomcatInstance(); @@ -1031,6 +1067,7 @@ public class TestAsyncContextImpl extends TomcatBaseTest { } } + @Test public void testErrorHandling() throws Exception { // Setup Tomcat instance Tomcat tomcat = getTomcatInstance(); @@ -1086,7 +1123,8 @@ public class TestAsyncContextImpl extends TomcatBaseTest { entry.getTime() < maxTime + ERROR_MARGIN); } } - + + @Test public void testCommitOnComplete() throws Exception { // Setup Tomcat instance Tomcat tomcat = getTomcatInstance(); @@ -1146,8 +1184,8 @@ public class TestAsyncContextImpl extends TomcatBaseTest { actxt.complete(); } } - + @Test public void testBug51197() throws Exception { // Setup Tomcat instance Tomcat tomcat = getTomcatInstance(); diff --git a/test/org/apache/catalina/core/TestNamingContextListener.java b/test/org/apache/catalina/core/TestNamingContextListener.java index e36f3e65f..b1558b84d 100644 --- a/test/org/apache/catalina/core/TestNamingContextListener.java +++ b/test/org/apache/catalina/core/TestNamingContextListener.java @@ -22,12 +22,16 @@ import javax.naming.NamingException; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + import org.apache.catalina.LifecycleState; import org.apache.catalina.deploy.ContextEnvironment; import org.apache.catalina.startup.Tomcat; -import org.apache.catalina.startup.TomcatBaseTest; +import org.apache.catalina.startup.TomcatBaseTestJUnit4; -public class TestNamingContextListener extends TomcatBaseTest { +public class TestNamingContextListener extends TomcatBaseTestJUnit4 { private static final String JNDI_NAME = "TestName"; private static final String JNDI_VALUE= "Test Value"; @@ -35,6 +39,7 @@ public class TestNamingContextListener extends TomcatBaseTest { /** * Test JNDI is available to ServletContextListeners. */ + @Test public void testBug49132() throws Exception { Tomcat tomcat = getTomcatInstance(); diff --git a/test/org/apache/catalina/core/TestStandardContext.java b/test/org/apache/catalina/core/TestStandardContext.java index 059246125..a2a5f6f92 100644 --- a/test/org/apache/catalina/core/TestStandardContext.java +++ b/test/org/apache/catalina/core/TestStandardContext.java @@ -41,6 +41,14 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.junit.Test; + import org.apache.catalina.Context; import org.apache.catalina.Wrapper; import org.apache.catalina.authenticator.BasicAuthenticator; @@ -50,10 +58,10 @@ import org.apache.catalina.deploy.LoginConfig; import org.apache.catalina.startup.SimpleHttpClient; import org.apache.catalina.startup.TestTomcat.MapRealm; import org.apache.catalina.startup.Tomcat; -import org.apache.catalina.startup.TomcatBaseTest; +import org.apache.catalina.startup.TomcatBaseTestJUnit4; import org.apache.tomcat.util.buf.ByteChunk; -public class TestStandardContext extends TomcatBaseTest { +public class TestStandardContext extends TomcatBaseTestJUnit4 { private static final String REQUEST = "GET / HTTP/1.1\r\n" + @@ -61,6 +69,7 @@ public class TestStandardContext extends TomcatBaseTest { "Connection: close\r\n" + "\r\n"; + @Test public void testBug46243() throws Exception { // Set up a container @@ -129,7 +138,7 @@ public class TestStandardContext extends TomcatBaseTest { } - + @Test public void testBug49922() throws Exception { // Set up a container @@ -251,7 +260,8 @@ public class TestStandardContext extends TomcatBaseTest { } } - + + @Test public void testBug50015() throws Exception { // Set up a container Tomcat tomcat = getTomcatInstance(); @@ -321,10 +331,12 @@ public class TestStandardContext extends TomcatBaseTest { } + @Test public void testBug51376a() throws Exception { doTestBug51376(false); } + @Test public void testBug51376b() throws Exception { doTestBug51376(true); } @@ -427,6 +439,7 @@ public class TestStandardContext extends TomcatBaseTest { * Test case for bug 49711: HttpServletRequest.getParts does not work * in a filter. */ + @Test public void testBug49711() { Bug49711Client client = new Bug49711Client(); client.setPort(getPort()); diff --git a/test/org/apache/catalina/core/TestStandardContextAliases.java b/test/org/apache/catalina/core/TestStandardContextAliases.java index 586046fc0..da9d7e62c 100644 --- a/test/org/apache/catalina/core/TestStandardContextAliases.java +++ b/test/org/apache/catalina/core/TestStandardContextAliases.java @@ -28,12 +28,17 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + import org.apache.catalina.startup.Tomcat; -import org.apache.catalina.startup.TomcatBaseTest; +import org.apache.catalina.startup.TomcatBaseTestJUnit4; import org.apache.tomcat.util.buf.ByteChunk; -public class TestStandardContextAliases extends TomcatBaseTest { +public class TestStandardContextAliases extends TomcatBaseTestJUnit4 { + @Test public void testDirContextAliases() throws Exception { Tomcat tomcat = getTomcatInstance(); diff --git a/test/org/apache/catalina/core/TestStandardContextResources.java b/test/org/apache/catalina/core/TestStandardContextResources.java index 9a22ee5cc..22c22b437 100644 --- a/test/org/apache/catalina/core/TestStandardContextResources.java +++ b/test/org/apache/catalina/core/TestStandardContextResources.java @@ -29,6 +29,11 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + import org.apache.catalina.Lifecycle; import org.apache.catalina.LifecycleEvent; import org.apache.catalina.LifecycleListener; @@ -36,10 +41,10 @@ import org.apache.catalina.deploy.WebXml; import org.apache.catalina.startup.Constants; import org.apache.catalina.startup.ContextConfig; import org.apache.catalina.startup.Tomcat; -import org.apache.catalina.startup.TomcatBaseTest; +import org.apache.catalina.startup.TomcatBaseTestJUnit4; import org.apache.tomcat.util.buf.ByteChunk; -public class TestStandardContextResources extends TomcatBaseTest { +public class TestStandardContextResources extends TomcatBaseTestJUnit4 { @Override public void setUp() throws Exception { @@ -55,6 +60,7 @@ public class TestStandardContextResources extends TomcatBaseTest { new JreMemoryLeakPreventionListener()); } + @Test public void testResources() throws Exception { Tomcat tomcat = getTomcatInstance(); @@ -78,6 +84,7 @@ public class TestStandardContextResources extends TomcatBaseTest { "

resourceG.jsp in WEB-INF/classes

", 404); } + @Test public void testResourcesWebInfClasses() throws Exception { Tomcat tomcat = getTomcatInstance(); @@ -105,6 +112,7 @@ public class TestStandardContextResources extends TomcatBaseTest { "

resourceG.jsp in WEB-INF/classes

"); } + @Test public void testResourcesAbsoluteOrdering() throws Exception { Tomcat tomcat = getTomcatInstance(); @@ -166,7 +174,8 @@ public class TestStandardContextResources extends TomcatBaseTest { "

resourceB.jsp in resources2.jar

"); } - + + @Test public void testResources2() throws Exception { Tomcat tomcat = getTomcatInstance(); diff --git a/test/org/apache/catalina/core/TestStandardWrapper.java b/test/org/apache/catalina/core/TestStandardWrapper.java index 4f3314404..d76213363 100644 --- a/test/org/apache/catalina/core/TestStandardWrapper.java +++ b/test/org/apache/catalina/core/TestStandardWrapper.java @@ -43,45 +43,60 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + import org.apache.catalina.Context; import org.apache.catalina.Wrapper; import org.apache.catalina.authenticator.BasicAuthenticator; import org.apache.catalina.deploy.LoginConfig; import org.apache.catalina.startup.TestTomcat.MapRealm; import org.apache.catalina.startup.Tomcat; -import org.apache.catalina.startup.TomcatBaseTest; +import org.apache.catalina.startup.TomcatBaseTestJUnit4; import org.apache.tomcat.util.buf.ByteChunk; -public class TestStandardWrapper extends TomcatBaseTest { +public class TestStandardWrapper extends TomcatBaseTestJUnit4 { + @Test public void testSecurityAnnotationsSimple() throws Exception { doTest(DenyAllServlet.class.getName(), false, false, false); } + @Test public void testSecurityAnnotationsSubclass1() throws Exception { doTest(SubclassDenyAllServlet.class.getName(), false, false, false); } + @Test public void testSecurityAnnotationsSubclass2() throws Exception { doTest(SubclassAllowAllServlet.class.getName(), false, false, true); } + @Test public void testSecurityAnnotationsMethods1() throws Exception { doTest(MethodConstraintServlet.class.getName(), false, false, false); } + @Test public void testSecurityAnnotationsMethods2() throws Exception { doTest(MethodConstraintServlet.class.getName(), true, false, true); } + @Test public void testSecurityAnnotationsRole1() throws Exception { doTest(RoleAllowServlet.class.getName(), false, true, true); } + @Test public void testSecurityAnnotationsRole2() throws Exception { doTest(RoleDenyServlet.class.getName(), false, true, false); } + @Test public void testSecurityAnnotationsWebXmlPriority() throws Exception { // Setup Tomcat instance @@ -102,6 +117,7 @@ public class TestStandardWrapper extends TomcatBaseTest { assertEquals(403, rc); } + @Test public void testSecurityAnnotationsMetaDataPriority() throws Exception { // Setup Tomcat instance @@ -122,14 +138,17 @@ public class TestStandardWrapper extends TomcatBaseTest { assertEquals(200, rc); } + @Test public void testSecurityAnnotationsAddServlet1() throws Exception { doTestSecurityAnnotationsAddServlet(false); } - + + @Test public void testSecurityAnnotationsAddServlet2() throws Exception { doTestSecurityAnnotationsAddServlet(true); } - + + @Test public void testSecurityAnnotationsNoWebXmlConstraints() throws Exception { // Setup Tomcat instance Tomcat tomcat = getTomcatInstance(); @@ -148,6 +167,7 @@ public class TestStandardWrapper extends TomcatBaseTest { assertEquals(403, rc); } + @Test public void testSecurityAnnotationsNoWebXmlLoginConfig() throws Exception { // Setup Tomcat instance Tomcat tomcat = getTomcatInstance(); @@ -338,6 +358,7 @@ public class TestStandardWrapper extends TomcatBaseTest { public static CountDownLatch latch = null; public static AtomicInteger counter = new AtomicInteger(0); + @Test public void testBug51445AddServlet() throws Exception { latch = new CountDownLatch(BUG51445_THREAD_COUNT); @@ -381,6 +402,7 @@ public class TestStandardWrapper extends TomcatBaseTest { } } + @Test public void testBug51445AddChild() throws Exception { latch = new CountDownLatch(BUG51445_THREAD_COUNT); diff --git a/test/org/apache/catalina/core/TestSwallowAbortedUploads.java b/test/org/apache/catalina/core/TestSwallowAbortedUploads.java index c12a1127f..5f067b312 100644 --- a/test/org/apache/catalina/core/TestSwallowAbortedUploads.java +++ b/test/org/apache/catalina/core/TestSwallowAbortedUploads.java @@ -29,15 +29,20 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Part; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + import org.apache.catalina.Context; import org.apache.catalina.Wrapper; import org.apache.catalina.startup.SimpleHttpClient; import org.apache.catalina.startup.Tomcat; -import org.apache.catalina.startup.TomcatBaseTest; +import org.apache.catalina.startup.TomcatBaseTestJUnit4; import org.apache.juli.logging.Log; import org.apache.juli.logging.LogFactory; -public class TestSwallowAbortedUploads extends TomcatBaseTest { +public class TestSwallowAbortedUploads extends TomcatBaseTestJUnit4 { private static Log log = LogFactory.getLog(TestSwallowAbortedUploads.class); @@ -79,6 +84,7 @@ public class TestSwallowAbortedUploads extends TomcatBaseTest { return ex; } + @Test public void testAbortedUploadUnlimitedSwallow() { log.info("Unlimited, swallow enabled"); AbortedUploadClient client = new AbortedUploadClient(); @@ -90,6 +96,7 @@ public class TestSwallowAbortedUploads extends TomcatBaseTest { client.reset(); } + @Test public void testAbortedUploadUnlimitedNoSwallow() { log.info("Unlimited, swallow disabled"); AbortedUploadClient client = new AbortedUploadClient(); @@ -101,6 +108,7 @@ public class TestSwallowAbortedUploads extends TomcatBaseTest { client.reset(); } + @Test public void testAbortedUploadLimitedSwallow() { log.info("Limited, swallow enabled"); AbortedUploadClient client = new AbortedUploadClient(); @@ -112,6 +120,7 @@ public class TestSwallowAbortedUploads extends TomcatBaseTest { client.reset(); } + @Test public void testAbortedUploadLimitedNoSwallow() { log.info("Limited, swallow disabled"); AbortedUploadClient client = new AbortedUploadClient(); @@ -121,6 +130,7 @@ public class TestSwallowAbortedUploads extends TomcatBaseTest { client.reset(); } + @Test public void testAbortedPOSTOKSwallow() { log.info("Aborted (OK), swallow enabled"); AbortedPOSTClient client = new AbortedPOSTClient(); @@ -132,6 +142,7 @@ public class TestSwallowAbortedUploads extends TomcatBaseTest { client.reset(); } + @Test public void testAbortedPOSTOKNoSwallow() { log.info("Aborted (OK), swallow disabled"); AbortedPOSTClient client = new AbortedPOSTClient(); @@ -143,6 +154,7 @@ public class TestSwallowAbortedUploads extends TomcatBaseTest { client.reset(); } + @Test public void testAbortedPOST413Swallow() { log.info("Aborted (413), swallow enabled"); AbortedPOSTClient client = new AbortedPOSTClient(); @@ -154,6 +166,7 @@ public class TestSwallowAbortedUploads extends TomcatBaseTest { client.reset(); } + @Test public void testAbortedPOST413NoSwallow() { log.info("Aborted (413), swallow disabled"); AbortedPOSTClient client = new AbortedPOSTClient(); diff --git a/test/org/apache/catalina/filters/TestAddCharSetFilter.java b/test/org/apache/catalina/filters/TestAddCharSetFilter.java index 74f618a4c..257c5610e 100644 --- a/test/org/apache/catalina/filters/TestAddCharSetFilter.java +++ b/test/org/apache/catalina/filters/TestAddCharSetFilter.java @@ -28,43 +28,55 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + import org.apache.catalina.Context; import org.apache.catalina.deploy.FilterDef; import org.apache.catalina.deploy.FilterMap; import org.apache.catalina.startup.Tomcat; -import org.apache.catalina.startup.TomcatBaseTest; +import org.apache.catalina.startup.TomcatBaseTestJUnit4; import org.apache.tomcat.util.buf.ByteChunk; -public class TestAddCharSetFilter extends TomcatBaseTest { +public class TestAddCharSetFilter extends TomcatBaseTestJUnit4 { + @Test public void testNoneSpecifiedMode1() throws Exception { doTest(null, "ISO-8859-1"); } + @Test public void testNoneSpecifiedMode2() throws Exception { doTest(null, "ISO-8859-2", 2); } + @Test public void testNoneSpecifiedMode3() throws Exception { doTest(null, "ISO-8859-3", 3); } + @Test public void testDefault() throws Exception { doTest("default", "ISO-8859-1"); } + @Test public void testDefaultMixedCase() throws Exception { doTest("dEfAuLt", "ISO-8859-1"); } + @Test public void testSystem() throws Exception { doTest("system", Charset.defaultCharset().name()); } + @Test public void testSystemMixedCase() throws Exception { doTest("SyStEm", Charset.defaultCharset().name()); } + @Test public void testUTF8() throws Exception { doTest("utf-8", "utf-8"); } diff --git a/test/org/apache/catalina/filters/TestCsrfPreventionFilter.java b/test/org/apache/catalina/filters/TestCsrfPreventionFilter.java index 92f8905ef..a42edf0bb 100644 --- a/test/org/apache/catalina/filters/TestCsrfPreventionFilter.java +++ b/test/org/apache/catalina/filters/TestCsrfPreventionFilter.java @@ -24,10 +24,16 @@ import java.io.ObjectOutputStream; import javax.servlet.http.HttpServletResponse; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + import org.apache.catalina.filters.CsrfPreventionFilter.LruCache; -import org.apache.catalina.startup.TomcatBaseTest; +import org.apache.catalina.startup.TomcatBaseTestJUnit4; -public class TestCsrfPreventionFilter extends TomcatBaseTest { +public class TestCsrfPreventionFilter extends TomcatBaseTestJUnit4 { private static final String RESULT_NONCE = Constants.CSRF_NONCE_SESSION_ATTR_NAME + "=TESTNONCE"; @@ -36,26 +42,31 @@ public class TestCsrfPreventionFilter extends TomcatBaseTest { new CsrfPreventionFilter.CsrfResponseWrapper( new NonEncodingResponse(), "TESTNONCE"); + @Test public void testAddNonceNoQueryNoAnchor() throws Exception { assertEquals("/test?" + RESULT_NONCE , wrapper.encodeRedirectURL("/test")); } - + + @Test public void testAddNonceQueryNoAnchor() throws Exception { assertEquals("/test?a=b&" + RESULT_NONCE , wrapper.encodeRedirectURL("/test?a=b")); } - + + @Test public void testAddNonceNoQueryAnchor() throws Exception { assertEquals("/test?" + RESULT_NONCE + "#c", wrapper.encodeRedirectURL("/test#c")); } - + + @Test public void testAddNonceQueryAnchor() throws Exception { assertEquals("/test?a=b&" + RESULT_NONCE + "#c", wrapper.encodeRedirectURL("/test?a=b#c")); } - + + @Test public void testLruCacheSerializable() throws Exception { LruCache cache = new LruCache(5); cache.add("key1"); @@ -85,6 +96,7 @@ public class TestCsrfPreventionFilter extends TomcatBaseTest { assertTrue(cache2.contains("key7")); } + @Test public void testLruCacheSerializablePerformance() throws Exception { for (int i = 0; i < 10000; i++) { testLruCacheSerializable(); diff --git a/test/org/apache/catalina/filters/TestExpiresFilter.java b/test/org/apache/catalina/filters/TestExpiresFilter.java index 715c131b0..e20dfc2b6 100644 --- a/test/org/apache/catalina/filters/TestExpiresFilter.java +++ b/test/org/apache/catalina/filters/TestExpiresFilter.java @@ -33,6 +33,8 @@ import javax.servlet.http.HttpServletResponse; import junit.framework.Assert; +import org.junit.Test; + import org.apache.catalina.Context; import org.apache.catalina.deploy.FilterDef; import org.apache.catalina.deploy.FilterMap; @@ -41,11 +43,12 @@ import org.apache.catalina.filters.ExpiresFilter.DurationUnit; import org.apache.catalina.filters.ExpiresFilter.ExpiresConfiguration; import org.apache.catalina.filters.ExpiresFilter.StartingPoint; import org.apache.catalina.startup.Tomcat; -import org.apache.catalina.startup.TomcatBaseTest; +import org.apache.catalina.startup.TomcatBaseTestJUnit4; -public class TestExpiresFilter extends TomcatBaseTest { +public class TestExpiresFilter extends TomcatBaseTestJUnit4 { public static final String TEMP_DIR = System.getProperty("java.io.tmpdir"); + @Test public void testConfiguration() throws Exception { Tomcat tomcat = getTomcatInstance(); @@ -181,7 +184,7 @@ public class TestExpiresFilter extends TomcatBaseTest { /** * Test that a resource with empty content is also processed */ - + @Test public void testEmptyContent() throws Exception { HttpServlet servlet = new HttpServlet() { private static final long serialVersionUID = 1L; @@ -198,6 +201,7 @@ public class TestExpiresFilter extends TomcatBaseTest { validate(servlet, Integer.valueOf(7 * 60)); } + @Test public void testParseExpiresConfigurationCombinedDuration() { ExpiresFilter expiresFilter = new ExpiresFilter(); ExpiresConfiguration actualConfiguration = expiresFilter.parseExpiresConfiguration("access plus 1 month 15 days 2 hours"); @@ -209,6 +213,7 @@ public class TestExpiresFilter extends TomcatBaseTest { } + @Test public void testParseExpiresConfigurationMonoDuration() { ExpiresFilter expiresFilter = new ExpiresFilter(); ExpiresConfiguration actualConfiguration = expiresFilter.parseExpiresConfiguration("access plus 2 hours"); @@ -224,6 +229,7 @@ public class TestExpiresFilter extends TomcatBaseTest { } + @Test public void testSkipBecauseCacheControlMaxAgeIsDefined() throws Exception { HttpServlet servlet = new HttpServlet() { private static final long serialVersionUID = 1L; @@ -241,6 +247,7 @@ public class TestExpiresFilter extends TomcatBaseTest { validate(servlet, Integer.valueOf(232)); } + @Test public void testExcludedResponseStatusCode() throws Exception { HttpServlet servlet = new HttpServlet() { private static final long serialVersionUID = 1L; @@ -258,6 +265,7 @@ public class TestExpiresFilter extends TomcatBaseTest { validate(servlet, null, HttpServletResponse.SC_NOT_MODIFIED); } + @Test public void testNullContentType() throws Exception { HttpServlet servlet = new HttpServlet() { private static final long serialVersionUID = 1L; @@ -273,6 +281,7 @@ public class TestExpiresFilter extends TomcatBaseTest { validate(servlet, Integer.valueOf(1 * 60)); } + @Test public void testSkipBecauseExpiresIsDefined() throws Exception { HttpServlet servlet = new HttpServlet() { private static final long serialVersionUID = 1L; @@ -290,6 +299,7 @@ public class TestExpiresFilter extends TomcatBaseTest { validate(servlet, null); } + @Test public void testUseContentTypeExpiresConfiguration() throws Exception { HttpServlet servlet = new HttpServlet() { private static final long serialVersionUID = 1L; @@ -306,6 +316,7 @@ public class TestExpiresFilter extends TomcatBaseTest { validate(servlet, Integer.valueOf(3 * 60)); } + @Test public void testUseContentTypeWithoutCharsetExpiresConfiguration() throws Exception { HttpServlet servlet = new HttpServlet() { @@ -323,6 +334,7 @@ public class TestExpiresFilter extends TomcatBaseTest { validate(servlet, Integer.valueOf(5 * 60)); } + @Test public void testUseDefaultConfiguration1() throws Exception { HttpServlet servlet = new HttpServlet() { private static final long serialVersionUID = 1L; @@ -339,6 +351,7 @@ public class TestExpiresFilter extends TomcatBaseTest { validate(servlet, Integer.valueOf(1 * 60)); } + @Test public void testUseDefaultConfiguration2() throws Exception { HttpServlet servlet = new HttpServlet() { private static final long serialVersionUID = 1L; @@ -357,6 +370,7 @@ public class TestExpiresFilter extends TomcatBaseTest { validate(servlet, Integer.valueOf(1 * 60)); } + @Test public void testUseMajorTypeExpiresConfiguration() throws Exception { HttpServlet servlet = new HttpServlet() { private static final long serialVersionUID = 1L; @@ -482,6 +496,7 @@ public class TestExpiresFilter extends TomcatBaseTest { } } + @Test public void testIntsToCommaDelimitedString() { String actual = ExpiresFilter.intsToCommaDelimitedString(new int[] { 500, 503 }); diff --git a/test/org/apache/catalina/filters/TestRemoteIpFilter.java b/test/org/apache/catalina/filters/TestRemoteIpFilter.java index 4c3be0f9c..337e49f79 100644 --- a/test/org/apache/catalina/filters/TestRemoteIpFilter.java +++ b/test/org/apache/catalina/filters/TestRemoteIpFilter.java @@ -39,6 +39,12 @@ import javax.servlet.http.HttpServletResponse; import junit.framework.Assert; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; + +import org.junit.Test; + import org.apache.catalina.Context; import org.apache.catalina.LifecycleException; import org.apache.catalina.connector.Connector; @@ -47,9 +53,9 @@ import org.apache.catalina.connector.Response; import org.apache.catalina.deploy.FilterDef; import org.apache.catalina.deploy.FilterMap; import org.apache.catalina.startup.Tomcat; -import org.apache.catalina.startup.TomcatBaseTest; +import org.apache.catalina.startup.TomcatBaseTestJUnit4; -public class TestRemoteIpFilter extends TomcatBaseTest { +public class TestRemoteIpFilter extends TomcatBaseTestJUnit4 { /** * Mock {@link FilterChain} to keep a handle on the passed @@ -127,23 +133,27 @@ public class TestRemoteIpFilter extends TomcatBaseTest { public static final String TEMP_DIR = System.getProperty("java.io.tmpdir"); + @Test public void testCommaDelimitedListToStringArray() { List elements = Arrays.asList("element1", "element2", "element3"); String actual = RemoteIpFilter.listToCommaDelimitedString(elements); assertEquals("element1, element2, element3", actual); } + @Test public void testCommaDelimitedListToStringArrayEmptyList() { List elements = new ArrayList(); String actual = RemoteIpFilter.listToCommaDelimitedString(elements); assertEquals("", actual); } + @Test public void testCommaDelimitedListToStringArrayNullList() { String actual = RemoteIpFilter.listToCommaDelimitedString(null); assertEquals("", actual); } + @Test public void testHeaderNamesCaseInsensitivity() { RemoteIpFilter.XForwardedRequest request = new RemoteIpFilter.XForwardedRequest(new MockHttpServletRequest()); request.setHeader("myheader", "lower Case"); @@ -153,6 +163,7 @@ public class TestRemoteIpFilter extends TomcatBaseTest { assertEquals("Camel Case", request.getHeader("myheader")); } + @Test public void testIncomingRequestIsSecuredButProtocolHeaderSaysItIsNotWithCustomValues() throws Exception { // PREPARE FilterDef filterDef = new FilterDef(); @@ -172,7 +183,7 @@ public class TestRemoteIpFilter extends TomcatBaseTest { // VERIFY boolean actualSecure = actualRequest.isSecure(); - assertEquals("request must be unsecured as header x-forwarded-proto said it is http", false, actualSecure); + assertFalse("request must be unsecured as header x-forwarded-proto said it is http", actualSecure); String actualScheme = actualRequest.getScheme(); assertEquals("scheme must be http as header x-forwarded-proto said it is http", "http", actualScheme); @@ -187,6 +198,7 @@ public class TestRemoteIpFilter extends TomcatBaseTest { assertEquals("remoteHost", "140.211.11.130", actualRemoteHost); } + @Test public void testIncomingRequestIsSecuredButProtocolHeaderSaysItIsNotWithDefaultValues() throws Exception { // PREPARE FilterDef filterDef = new FilterDef(); @@ -204,7 +216,7 @@ public class TestRemoteIpFilter extends TomcatBaseTest { // VERIFY boolean actualSecure = actualRequest.isSecure(); - assertEquals("request must be unsecured as header x-forwarded-proto said it is http", false, actualSecure); + assertFalse("request must be unsecured as header x-forwarded-proto said it is http", actualSecure); String actualScheme = actualRequest.getScheme(); assertEquals("scheme must be http as header x-forwarded-proto said it is http", "http", actualScheme); @@ -217,6 +229,7 @@ public class TestRemoteIpFilter extends TomcatBaseTest { } + @Test public void testInvokeAllowedRemoteAddrWithNullRemoteIpHeader() throws Exception { // PREPARE FilterDef filterDef = new FilterDef(); @@ -247,6 +260,7 @@ public class TestRemoteIpFilter extends TomcatBaseTest { } + @Test public void testInvokeAllProxiesAreInternal() throws Exception { // PREPARE @@ -278,6 +292,7 @@ public class TestRemoteIpFilter extends TomcatBaseTest { assertEquals("remoteHost", "140.211.11.130", actualRemoteHost); } + @Test public void testInvokeAllProxiesAreTrusted() throws Exception { // PREPARE @@ -312,6 +327,7 @@ public class TestRemoteIpFilter extends TomcatBaseTest { assertEquals("remoteHost", "140.211.11.130", actualRemoteHost); } + @Test public void testInvokeAllProxiesAreTrustedAndRemoteAddrMatchRegexp() throws Exception { // PREPARE @@ -345,6 +361,7 @@ public class TestRemoteIpFilter extends TomcatBaseTest { assertEquals("remoteHost", "140.211.11.130", actualRemoteHost); } + @Test public void testInvokeAllProxiesAreTrustedOrInternal() throws Exception { // PREPARE @@ -377,6 +394,7 @@ public class TestRemoteIpFilter extends TomcatBaseTest { assertEquals("remoteHost", "140.211.11.130", actualRemoteHost); } + @Test public void testInvokeNotAllowedRemoteAddr() throws Exception { // PREPARE FilterDef filterDef = new FilterDef(); @@ -408,6 +426,7 @@ public class TestRemoteIpFilter extends TomcatBaseTest { assertEquals("remoteHost", "not-allowed-internal-proxy-host", actualRemoteHost); } + @Test public void testInvokeUntrustedProxyInTheChain() throws Exception { // PREPARE FilterDef filterDef = new FilterDef(); @@ -439,6 +458,7 @@ public class TestRemoteIpFilter extends TomcatBaseTest { assertEquals("remoteHost", "untrusted-proxy", actualRemoteHost); } + @Test public void testListToCommaDelimitedString() { String[] actual = RemoteIpFilter.commaDelimitedListToStringArray("element1, element2, element3"); String[] expected = new String[] { "element1", "element2", "element3" }; @@ -448,6 +468,7 @@ public class TestRemoteIpFilter extends TomcatBaseTest { } } + @Test public void testListToCommaDelimitedStringMixedSpaceChars() { String[] actual = RemoteIpFilter.commaDelimitedListToStringArray("element1 , element2,\t element3"); String[] expected = new String[] { "element1", "element2", "element3" }; @@ -485,6 +506,7 @@ public class TestRemoteIpFilter extends TomcatBaseTest { /** * Test {@link RemoteIpFilter} in Tomcat standalone server */ + @Test public void testWithTomcatServer() throws Exception { // mostly default configuration : enable "x-forwarded-proto" -- 2.11.0