WICKET-5307 Export resources' version cache attribute as request cycle metadata
Fix the parsing of the version in FilenameWithVersionResourceCachingStrategy. Add unit tests. Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/12ba1910 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/12ba1910 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/12ba1910 Branch: refs/heads/5299-ajax-strategy Commit: 12ba191004cd27408f6a9f78d282b3e3b8637a55 Parents: 823e571 Author: Martin Tzvetanov Grigorov <mgrigo...@apache.org> Authored: Wed Aug 7 16:50:02 2013 +0200 Committer: Martin Tzvetanov Grigorov <mgrigo...@apache.org> Committed: Wed Aug 7 16:52:51 2013 +0200 ---------------------------------------------------------------------- ...enameWithVersionResourceCachingStrategy.java | 2 +- ...eWithVersionResourceCachingStrategyTest.java | 87 ++++++++++++++++++ ...gWithVersionResourceCachingStrategyTest.java | 94 ++++++++++++++++++++ .../request/resource/caching/TestResource.java | 51 +++++++++++ 4 files changed, 233 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/12ba1910/wicket-core/src/main/java/org/apache/wicket/request/resource/caching/FilenameWithVersionResourceCachingStrategy.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/request/resource/caching/FilenameWithVersionResourceCachingStrategy.java b/wicket-core/src/main/java/org/apache/wicket/request/resource/caching/FilenameWithVersionResourceCachingStrategy.java index 0d00cef..2125f35 100644 --- a/wicket-core/src/main/java/org/apache/wicket/request/resource/caching/FilenameWithVersionResourceCachingStrategy.java +++ b/wicket-core/src/main/java/org/apache/wicket/request/resource/caching/FilenameWithVersionResourceCachingStrategy.java @@ -182,7 +182,7 @@ public class FilenameWithVersionResourceCachingStrategy implements IResourceCach if (requestCycle != null) { int idx = fullname.indexOf(versionPrefix); - String urlVersion = fullname.substring(pos + idx + 1); + String urlVersion = fullname.substring(idx + versionPrefix.length()); requestCycle.setMetaData(URL_VERSION, urlVersion); } } http://git-wip-us.apache.org/repos/asf/wicket/blob/12ba1910/wicket-core/src/test/java/org/apache/wicket/request/resource/caching/FilenameWithVersionResourceCachingStrategyTest.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/request/resource/caching/FilenameWithVersionResourceCachingStrategyTest.java b/wicket-core/src/test/java/org/apache/wicket/request/resource/caching/FilenameWithVersionResourceCachingStrategyTest.java new file mode 100644 index 0000000..4923003 --- /dev/null +++ b/wicket-core/src/test/java/org/apache/wicket/request/resource/caching/FilenameWithVersionResourceCachingStrategyTest.java @@ -0,0 +1,87 @@ +/* + * 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.caching; + +import org.apache.wicket.request.http.WebResponse; +import org.apache.wicket.request.mapper.parameter.PageParameters; +import org.apache.wicket.request.resource.AbstractResource; +import org.apache.wicket.request.resource.caching.version.IResourceVersion; +import org.apache.wicket.request.resource.caching.version.MessageDigestResourceVersion; +import org.apache.wicket.util.tester.WicketTester; +import org.junit.Assert; +import org.junit.Test; + +/** + * Tests for FilenameWithVersionResourceCachingStrategy + */ +public class FilenameWithVersionResourceCachingStrategyTest extends Assert +{ + private final String versionPrefix = "--vers--"; + private final IResourceVersion resourceVersion = new MessageDigestResourceVersion(); + private final FilenameWithVersionResourceCachingStrategy strategy = + new FilenameWithVersionResourceCachingStrategy(versionPrefix, resourceVersion); + + @Test + public void testDecorateUrl() throws Exception + { + ResourceUrl resourceUrl = new ResourceUrl("some-resource.txt", new PageParameters()); + strategy.decorateUrl(resourceUrl, new TestResource()); + + assertEquals("some-resource--vers--9A0364B9E99BB480DD25E1F0284C8555.txt", resourceUrl.getFileName()); + } + + @Test + public void testUndecorateUrl() throws Exception + { + ResourceUrl resourceUrl = new ResourceUrl("some-resource--vers--9A0364B9E99BB480DD25E1F0284C8555.txt", new PageParameters()); + strategy.undecorateUrl(resourceUrl); + + assertEquals("some-resource.txt", resourceUrl.getFileName()); + } + + @Test + public void testDecorateResponse() throws Exception + { + AbstractResource.ResourceResponse response = new AbstractResource.ResourceResponse(); + strategy.decorateResponse(response, new TestResource()); + + assertEquals(WebResponse.MAX_CACHE_DURATION, response.getCacheDuration()); + assertEquals(WebResponse.CacheScope.PUBLIC, response.getCacheScope()); + } + + @Test + public void testUrlVersionStoredInRequestCycle() + { + WicketTester tester = new WicketTester(); + tester.getApplication().getResourceSettings().setCachingStrategy(strategy); + + try + { + ResourceUrl resourceUrl = new ResourceUrl("some-resource--vers--9A0364B9E99BB480DD25E1F0284C8555.txt", new PageParameters()); + strategy.undecorateUrl(resourceUrl); + + String version = tester.getRequestCycle().getMetaData(IResourceCachingStrategy.URL_VERSION); + + assertEquals("9A0364B9E99BB480DD25E1F0284C8555", version); + } + finally + { + tester.destroy(); + } + } + +} http://git-wip-us.apache.org/repos/asf/wicket/blob/12ba1910/wicket-core/src/test/java/org/apache/wicket/request/resource/caching/QueryStringWithVersionResourceCachingStrategyTest.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/request/resource/caching/QueryStringWithVersionResourceCachingStrategyTest.java b/wicket-core/src/test/java/org/apache/wicket/request/resource/caching/QueryStringWithVersionResourceCachingStrategyTest.java new file mode 100644 index 0000000..acfee96 --- /dev/null +++ b/wicket-core/src/test/java/org/apache/wicket/request/resource/caching/QueryStringWithVersionResourceCachingStrategyTest.java @@ -0,0 +1,94 @@ +/* + * 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.caching; + +import org.apache.wicket.request.http.WebResponse; +import org.apache.wicket.request.mapper.parameter.PageParameters; +import org.apache.wicket.request.resource.AbstractResource; +import org.apache.wicket.request.resource.caching.version.IResourceVersion; +import org.apache.wicket.request.resource.caching.version.MessageDigestResourceVersion; +import org.apache.wicket.util.tester.WicketTester; +import org.junit.Assert; +import org.junit.Test; + +/** + * Tests for QueryStringWithVersionResourceCachingStrategy + */ +public class QueryStringWithVersionResourceCachingStrategyTest extends Assert +{ + private final String versionParameter = "vers"; + private final IResourceVersion resourceVersion = new MessageDigestResourceVersion(); + private final IResourceCachingStrategy strategy = + new QueryStringWithVersionResourceCachingStrategy(versionParameter, resourceVersion); + + @Test + public void testDecorateUrl() throws Exception + { + ResourceUrl resourceUrl = new ResourceUrl("some-resource.txt", new PageParameters()); + strategy.decorateUrl(resourceUrl, new TestResource()); + + assertEquals("some-resource.txt", resourceUrl.getFileName()); + assertEquals("9A0364B9E99BB480DD25E1F0284C8555", resourceUrl.getParameters().get(versionParameter).toString()); + } + + @Test + public void testUndecorateUrl() throws Exception + { + PageParameters urlParameters = new PageParameters(); + urlParameters.add(versionParameter, "9A0364B9E99BB480DD25E1F0284C8555"); + ResourceUrl resourceUrl = new ResourceUrl("some-resource.txt", urlParameters); + strategy.undecorateUrl(resourceUrl); + + assertEquals("some-resource.txt", resourceUrl.getFileName()); + assertNull(resourceUrl.getParameters().get(versionParameter).toString()); + } + + @Test + public void testDecorateResponse() throws Exception + { + AbstractResource.ResourceResponse response = new AbstractResource.ResourceResponse(); + strategy.decorateResponse(response, new TestResource()); + + assertEquals(WebResponse.MAX_CACHE_DURATION, response.getCacheDuration()); + assertEquals(WebResponse.CacheScope.PUBLIC, response.getCacheScope()); + } + + @Test + public void testUrlVersionStoredInRequestCycle() + { + WicketTester tester = new WicketTester(); + tester.getApplication().getResourceSettings().setCachingStrategy(strategy); + + try + { + PageParameters urlParameters = new PageParameters(); + urlParameters.add(versionParameter, "9A0364B9E99BB480DD25E1F0284C8555"); + ResourceUrl resourceUrl = new ResourceUrl("some-resource.txt", urlParameters); + strategy.undecorateUrl(resourceUrl); + + String version = tester.getRequestCycle().getMetaData(IResourceCachingStrategy.URL_VERSION); + + assertEquals("9A0364B9E99BB480DD25E1F0284C8555", version); + } + finally + { + tester.destroy(); + } + } + + +} http://git-wip-us.apache.org/repos/asf/wicket/blob/12ba1910/wicket-core/src/test/java/org/apache/wicket/request/resource/caching/TestResource.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/request/resource/caching/TestResource.java b/wicket-core/src/test/java/org/apache/wicket/request/resource/caching/TestResource.java new file mode 100644 index 0000000..da21e4f --- /dev/null +++ b/wicket-core/src/test/java/org/apache/wicket/request/resource/caching/TestResource.java @@ -0,0 +1,51 @@ +/* + * 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.caching; + +import java.io.Serializable; + +import org.apache.wicket.util.resource.IResourceStream; +import org.apache.wicket.util.resource.StringResourceStream; + +/** + * Resource used by the tests for {@link IResourceCachingStrategy} implementations + */ +class TestResource implements IStaticCacheableResource +{ + @Override + public boolean isCachingEnabled() + { + return true; + } + + @Override + public Serializable getCacheKey() + { + return "key"; + } + + @Override + public IResourceStream getCacheableResourceStream() + { + return new StringResourceStream("content"); + } + + @Override + public void respond(Attributes attributes) + { + } +}