Author: bdelacretaz Date: Wed Sep 2 12:08:53 2015 New Revision: 1700778 URL: http://svn.apache.org/r1700778 Log: SLING-4804 - InitResourceTest added
Added: sling/trunk/bundles/engine/src/test/java/org/apache/sling/engine/impl/request/InitResourceTest.java Added: sling/trunk/bundles/engine/src/test/java/org/apache/sling/engine/impl/request/InitResourceTest.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/engine/src/test/java/org/apache/sling/engine/impl/request/InitResourceTest.java?rev=1700778&view=auto ============================================================================== --- sling/trunk/bundles/engine/src/test/java/org/apache/sling/engine/impl/request/InitResourceTest.java (added) +++ sling/trunk/bundles/engine/src/test/java/org/apache/sling/engine/impl/request/InitResourceTest.java Wed Sep 2 12:08:53 2015 @@ -0,0 +1,108 @@ +/* + * 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.sling.engine.impl.request; + +import java.util.Arrays; +import java.util.Collection; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.sling.api.resource.ResourceResolver; +import org.jmock.Expectations; +import org.jmock.Mockery; +import org.jmock.integration.junit4.JUnit4Mockery; +import org.jmock.lib.legacy.ClassImposteriser; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +@RunWith(Parameterized.class) +public class InitResourceTest { + + private Mockery context; + private RequestData requestData; + private HttpServletRequest req; + private HttpServletResponse resp; + private ResourceResolver resourceResolver; + + private final String requestURL; + private final String pathInfo; + private final String expectedResolvePath; + + @Parameters(name="URL={0} path={1}") + public static Collection<Object[]> data() { + return Arrays.asList(new Object[][] { + { "/one;v=1.1", "one;v=1.1", "/one;v=1.1" }, + { "/two;v=1.1", "two", "/two;v=1.1" }, + { "/three", "three", "/three" }, + { "/four%3Bv=1.1", "four", "/four" }, + { "/five%3Bv=1.1", "five;v=1.1", "/five;v=1.1" }, + { "/six;v=1.1", "six;v=1.1", "/six;v=1.1" }, + { "/seven", "seven;v=1.1", "/seven;v=1.1" }, + }); + } + + public InitResourceTest(String requestURL, String pathInfo, String expectedResolvePath) { + this.requestURL = requestURL; + this.pathInfo = pathInfo; + this.expectedResolvePath = expectedResolvePath; + } + + @Before + public void setup() throws Exception { + context = new JUnit4Mockery() {{ + setImposteriser(ClassImposteriser.INSTANCE); + }}; + + req = context.mock(HttpServletRequest.class); + resp = context.mock(HttpServletResponse.class); + resourceResolver = context.mock(ResourceResolver.class); + + context.checking(new Expectations() {{ + allowing(req).getRequestURL(); + will(returnValue(new StringBuffer(requestURL))); + + allowing(req).getRequestURI(); + + allowing(req).getPathInfo(); + will(returnValue(pathInfo)); + + allowing(req).getServletPath(); + will(returnValue("/")); + + allowing(req).getMethod(); + will(returnValue("GET")); + + allowing(req).getAttribute(RequestData.REQUEST_RESOURCE_PATH_ATTR); + will(returnValue(null)); + allowing(req).setAttribute(with(equal(RequestData.REQUEST_RESOURCE_PATH_ATTR)), with(any(Object.class))); + + // Verify that the ResourceResolver is called with the expected path + allowing(resourceResolver).resolve(with(any(HttpServletRequest.class)),with(equal(expectedResolvePath))); + }}); + + requestData = new RequestData(null, req, resp); + } + + @Test + public void resolverPathMatches() { + requestData.initResource(resourceResolver); + } +}