Author: ivaynberg Date: Fri Jan 28 14:47:22 2011 New Revision: 1064706 URL: http://svn.apache.org/viewvc?rev=1064706&view=rev Log:
Issue: WICKET-3401 Added: wicket/trunk/wicket-core/src/test/java/META-INF/ wicket/trunk/wicket-core/src/test/java/META-INF/resources/ wicket/trunk/wicket-core/src/test/java/META-INF/resources/org/ wicket/trunk/wicket-core/src/test/java/META-INF/resources/org/apache/ wicket/trunk/wicket-core/src/test/java/META-INF/resources/org/apache/wicket/ wicket/trunk/wicket-core/src/test/java/META-INF/resources/org/apache/wicket/request/ wicket/trunk/wicket-core/src/test/java/META-INF/resources/org/apache/wicket/request/resource/ wicket/trunk/wicket-core/src/test/java/META-INF/resources/org/apache/wicket/request/resource/sample.js wicket/trunk/wicket-core/src/test/java/org/apache/wicket/request/resource/MetaInfStaticResourceReferenceTest.java (with props) Modified: wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java Modified: wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java?rev=1064706&r1=1064705&r2=1064706&view=diff ============================================================================== --- wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java (original) +++ wicket/trunk/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java Fri Jan 28 14:47:22 2011 @@ -86,6 +86,7 @@ import org.apache.wicket.protocol.http.W import org.apache.wicket.protocol.http.mock.MockHttpServletRequest; import org.apache.wicket.protocol.http.mock.MockHttpServletResponse; import org.apache.wicket.protocol.http.mock.MockHttpSession; +import org.apache.wicket.protocol.http.mock.MockServletContext; import org.apache.wicket.protocol.http.servlet.ServletWebRequest; import org.apache.wicket.protocol.http.servlet.ServletWebResponse; import org.apache.wicket.request.IExceptionMapper; @@ -159,7 +160,7 @@ public class BaseWicketTester } } - private org.apache.wicket.protocol.http.mock.MockServletContext servletContext; + private final MockServletContext servletContext; private MockHttpSession hsession; private final WebApplication application; @@ -239,7 +240,7 @@ public class BaseWicketTester */ public BaseWicketTester(final WebApplication application) { - this(application, null); + this(application, (MockServletContext)null); } /** @@ -254,8 +255,22 @@ public class BaseWicketTester */ public BaseWicketTester(final WebApplication application, String servletContextBasePath) { - servletContext = new org.apache.wicket.protocol.http.mock.MockServletContext(application, - servletContextBasePath); + this(application, new MockServletContext(application, servletContextBasePath)); + } + + /** + * Creates a <code>WicketTester</code>. + * + * @param application + * a <code>WicketTester</code> <code>WebApplication</code> object + * + * @param servletCtx + * the mock servlet context used as backend + */ + public BaseWicketTester(final WebApplication application, MockServletContext servletCtx) + { + servletContext = servletCtx != null ? servletCtx + : new MockServletContext(application, null); final FilterConfig filterConfig = new TestFilterConfig(); WicketFilter filter = new WicketFilter() @@ -415,7 +430,7 @@ public class BaseWicketTester * * @return servlet context */ - public org.apache.wicket.protocol.http.mock.MockServletContext getServletContext() + public MockServletContext getServletContext() { return servletContext; } Added: wicket/trunk/wicket-core/src/test/java/META-INF/resources/org/apache/wicket/request/resource/sample.js URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/test/java/META-INF/resources/org/apache/wicket/request/resource/sample.js?rev=1064706&view=auto ============================================================================== --- wicket/trunk/wicket-core/src/test/java/META-INF/resources/org/apache/wicket/request/resource/sample.js (added) +++ wicket/trunk/wicket-core/src/test/java/META-INF/resources/org/apache/wicket/request/resource/sample.js Fri Jan 28 14:47:22 2011 @@ -0,0 +1 @@ +// static test resource \ No newline at end of file Added: wicket/trunk/wicket-core/src/test/java/org/apache/wicket/request/resource/MetaInfStaticResourceReferenceTest.java URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/test/java/org/apache/wicket/request/resource/MetaInfStaticResourceReferenceTest.java?rev=1064706&view=auto ============================================================================== --- wicket/trunk/wicket-core/src/test/java/org/apache/wicket/request/resource/MetaInfStaticResourceReferenceTest.java (added) +++ wicket/trunk/wicket-core/src/test/java/org/apache/wicket/request/resource/MetaInfStaticResourceReferenceTest.java Fri Jan 28 14:47:22 2011 @@ -0,0 +1,193 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.request.resource; + +import java.lang.reflect.Field; +import java.net.MalformedURLException; +import java.net.URL; + +import org.apache.wicket.Application; +import org.apache.wicket.mock.MockApplication; +import org.apache.wicket.mock.MockWebRequest; +import org.apache.wicket.protocol.http.mock.MockServletContext; +import org.apache.wicket.request.IRequestHandler; +import org.apache.wicket.request.Url; +import org.apache.wicket.util.tester.BaseWicketTester; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Tests for {@link MetaInfStaticResourceReference}. + * + * @author akiraly + */ +public class MetaInfStaticResourceReferenceTest +{ + private static final String STATIC_RESOURCE_NAME = "sample.js"; + + /** + * Test with Servlet 2.5 container + */ + @Test + public void testWithServlet25() + { + BaseWicketTester tester = new BaseWicketTester(); + + MetaInfStaticResourceReference metaRes = new MetaInfStaticResourceReference(getClass(), + STATIC_RESOURCE_NAME); + PackageResourceReference packRes = new PackageResourceReference(getClass(), + STATIC_RESOURCE_NAME); + + Url packUrl = tester.getRequestCycle().mapUrlFor(packRes, null); + Url metaUrl = tester.getRequestCycle().mapUrlFor(metaRes, null); + + Assert.assertNotNull(metaUrl); + + // under 2.5 there should not be any difference between meta and pack resource urls + Assert.assertEquals(metaUrl, packUrl); + + MockWebRequest request = new MockWebRequest(metaUrl); + + IRequestHandler requestHandler = tester.getApplication() + .getRootRequestMapper() + .mapRequest(request); + + // meta resource is served by wicket under 2.5 + Assert.assertNotNull(requestHandler); + } + + /** + * Test with Servlet 3.0 container + * + * @throws MalformedURLException + * should not happen + */ + @Test + public void testWithServlet30() throws MalformedURLException + { + MockApplication application = new MockApplication(); + MockServletContext servletContext = new MockServletContext30(application, null); + BaseWicketTester tester = new BaseWicketTester(application, servletContext); + + MetaInfStaticResourceReference metaRes = new MetaInfStaticResourceReference(getClass(), + STATIC_RESOURCE_NAME); + PackageResourceReference packRes = new PackageResourceReference(getClass(), + STATIC_RESOURCE_NAME); + + Url packUrl = tester.getRequestCycle().mapUrlFor(packRes, null); + Url metaUrl = tester.getRequestCycle().mapUrlFor(metaRes, null); + + Assert.assertNotNull(metaUrl); + Assert.assertNotNull(packUrl); + Assert.assertFalse( + "Meta and pack resource should not map to the same url under servlet 3.0.", + metaUrl.equals(packUrl)); + + String metaUrlStr = metaUrl.toString(); + if (!metaUrlStr.startsWith("/")) + { + metaUrlStr = "/" + metaUrlStr; + } + + // meta resource is served by the servlet container under 3.0 + URL metaNetUrl = servletContext.getResource(metaUrlStr); + + Assert.assertNotNull("Meta resource is not found by the 3.0 servlet container.", metaNetUrl); + + MockWebRequest request = new MockWebRequest(packUrl); + + IRequestHandler requestHandler = tester.getApplication() + .getRootRequestMapper() + .mapRequest(request); + + // the pack resource is still served by wicket + Assert.assertNotNull(requestHandler); + } + + /** + * Needs to clear META_INF_RESOURCES_SUPPORTED field in {@link MetaInfStaticResourceReference} + * class between tests because classes do not get reloaded between junit tests. This is not a + * problem in production where the servlet container is not changing versions. + * + * @throws Exception + * if the reflection magic failed + */ + @Before + public void before() throws Exception + { + Field field = MetaInfStaticResourceReference.class.getDeclaredField("META_INF_RESOURCES_SUPPORTED"); + field.setAccessible(true); + field.set(null, null); + } + + /** + * {@link #before()} + * + * @throws Exception + * if before fails + */ + @After + public void after() throws Exception + { + before(); + } + + /** + * Partial mock implementation for Servlet 3.0 ServletContext. + */ + private static class MockServletContext30 extends MockServletContext + { + public MockServletContext30(Application application, String path) + { + super(application, path); + } + + @Override + public URL getResource(String name) throws MalformedURLException + { + URL url = super.getResource(name); + + if (url != null) + { + return url; + } + + return getClass().getClassLoader().getResource("META-INF/resources" + name); + } + + /** + * @return 3 + */ + @Override + public int getMajorVersion() + { + return 3; + } + + /** + * @return 0 + */ + @Override + public int getMinorVersion() + { + return 0; + } + + } +} \ No newline at end of file Propchange: wicket/trunk/wicket-core/src/test/java/org/apache/wicket/request/resource/MetaInfStaticResourceReferenceTest.java ------------------------------------------------------------------------------ svn:mime-type = text/plain