Author: sseifert Date: Thu Sep 10 09:43:45 2015 New Revision: 1702199 URL: http://svn.apache.org/r1702199 Log: SLING-5011 Update Sling API Dependency to 2.4.0 and refactor unit tests to sling mocks
Modified: sling/trunk/contrib/extensions/rewriter/pom.xml sling/trunk/contrib/extensions/rewriter/src/main/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImpl.java sling/trunk/contrib/extensions/rewriter/src/test/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImplTest.java Modified: sling/trunk/contrib/extensions/rewriter/pom.xml URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/rewriter/pom.xml?rev=1702199&r1=1702198&r2=1702199&view=diff ============================================================================== --- sling/trunk/contrib/extensions/rewriter/pom.xml (original) +++ sling/trunk/contrib/extensions/rewriter/pom.xml Thu Sep 10 09:43:45 2015 @@ -100,7 +100,7 @@ <dependency> <groupId>org.apache.sling</groupId> <artifactId>org.apache.sling.api</artifactId> - <version>2.1.0</version> + <version>2.4.0</version> <scope>provided</scope> </dependency> <dependency> @@ -152,8 +152,14 @@ </dependency> <dependency> <groupId>org.apache.sling</groupId> - <artifactId>org.apache.sling.commons.testing</artifactId> - <version>2.0.16</version> + <artifactId>org.apache.sling.testing.sling-mock</artifactId> + <version>1.4.0</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.testing.logging-mock</artifactId> + <version>1.0.0</version> <scope>test</scope> </dependency> <dependency> Modified: sling/trunk/contrib/extensions/rewriter/src/main/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImpl.java URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/rewriter/src/main/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImpl.java?rev=1702199&r1=1702198&r2=1702199&view=diff ============================================================================== --- sling/trunk/contrib/extensions/rewriter/src/main/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImpl.java (original) +++ sling/trunk/contrib/extensions/rewriter/src/main/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImpl.java Thu Sep 10 09:43:45 2015 @@ -37,29 +37,29 @@ import org.apache.sling.rewriter.Process */ public class ProcessorConfigurationImpl implements PipelineConfiguration { - private static final String PROPERTY_ORDER = "order"; + static final String PROPERTY_ORDER = "order"; - private static final String PROPERTY_PATHS = "paths"; + static final String PROPERTY_PATHS = "paths"; - private static final String PROPERTY_EXTENSIONS = "extensions"; + static final String PROPERTY_EXTENSIONS = "extensions"; - private static final String PROPERTY_PROCESSOR_TYPE = "processorType"; + static final String PROPERTY_PROCESSOR_TYPE = "processorType"; - private static final String PROPERTY_CONTENT_TYPES = "contentTypes"; + static final String PROPERTY_CONTENT_TYPES = "contentTypes"; - private static final String PROPERTY_RESOURCE_TYPES = "resourceTypes"; + static final String PROPERTY_RESOURCE_TYPES = "resourceTypes"; - private static final String PROPERTY_SELECTORS = "selectors"; + static final String PROPERTY_SELECTORS = "selectors"; - private static final String PROPERTY_TRANFORMERS = "transformerTypes"; + static final String PROPERTY_TRANFORMERS = "transformerTypes"; - private static final String PROPERTY_GENERATOR = "generatorType"; + static final String PROPERTY_GENERATOR = "generatorType"; - private static final String PROPERTY_SERIALIZER = "serializerType"; + static final String PROPERTY_SERIALIZER = "serializerType"; - private static final String PROPERTY_ACTIVE = "enabled"; + static final String PROPERTY_ACTIVE = "enabled"; - private static final String PROPERTY_PROCESS_ERROR = "processError"; + static final String PROPERTY_PROCESS_ERROR = "processError"; /** For which content types should this processor be applied. */ Modified: sling/trunk/contrib/extensions/rewriter/src/test/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImplTest.java URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/rewriter/src/test/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImplTest.java?rev=1702199&r1=1702198&r2=1702199&view=diff ============================================================================== --- sling/trunk/contrib/extensions/rewriter/src/test/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImplTest.java (original) +++ sling/trunk/contrib/extensions/rewriter/src/test/java/org/apache/sling/rewriter/impl/ProcessorConfigurationImplTest.java Thu Sep 10 09:43:45 2015 @@ -16,147 +16,132 @@ */ package org.apache.sling.rewriter.impl; +import static org.apache.sling.rewriter.impl.ProcessorConfigurationImpl.PROPERTY_CONTENT_TYPES; +import static org.apache.sling.rewriter.impl.ProcessorConfigurationImpl.PROPERTY_EXTENSIONS; +import static org.apache.sling.rewriter.impl.ProcessorConfigurationImpl.PROPERTY_PATHS; +import static org.apache.sling.rewriter.impl.ProcessorConfigurationImpl.PROPERTY_RESOURCE_TYPES; +import static org.apache.sling.rewriter.impl.ProcessorConfigurationImpl.PROPERTY_SELECTORS; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import org.apache.sling.api.SlingHttpServletRequest; -import org.apache.sling.commons.testing.sling.MockResourceResolver; -import org.apache.sling.commons.testing.sling.MockSlingHttpServletRequest; +import java.util.Map; + +import org.apache.sling.api.resource.Resource; import org.apache.sling.rewriter.ProcessingContext; -import org.apache.sling.rewriter.ProcessorConfiguration; +import org.apache.sling.testing.mock.sling.junit.SlingContext; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; -/** - * @author diru - */ -public class ProcessorConfigurationImplTest { +import com.google.common.collect.ImmutableMap; - private MockSlingHttpServletRequest mockRequestWithSelector; - private MockSlingHttpServletRequest mockRequestWithoutSelector; +@RunWith(MockitoJUnitRunner.class) +public class ProcessorConfigurationImplTest { + + @Rule + public SlingContext context = new SlingContext(); + + @Mock + private ProcessingContext processingContext; @Before public void setup() { - this.mockRequestWithSelector = new MockSlingHttpServletRequest("/content/path", "sel1.sel2", "xml", null, null); - this.mockRequestWithoutSelector = new MockSlingHttpServletRequest("/content/path", null, "xml", null, null); - // mock the resource resolver and create also a mocked resource to prevent NPE in ResourceUtil. - final MockResourceResolver mockResourceResolver = new MockResourceResolver(); - mockResourceResolver.setSearchPath("/libs"); - this.mockRequestWithSelector.setResourceResolver(mockResourceResolver); - this.mockRequestWithoutSelector.setResourceResolver(mockResourceResolver); + when(processingContext.getContentType()).thenReturn("text/html"); + when(processingContext.getRequest()).thenReturn(context.request()); + when(processingContext.getResponse()).thenReturn(context.response()); + } + + private ProcessorConfigurationImpl buildConfig(Map<String,Object> configProps) { + Resource configResoruce = context.create().resource("/apps/myapp/rewriter/config", configProps); + return new ProcessorConfigurationImpl(configResoruce); + } + + private void assertMatch(Map<String,Object> configProps) { + assertTrue(buildConfig(configProps).match(processingContext)); + } + + private void assertNoMatch(Map<String,Object> configProps) { + assertFalse(buildConfig(configProps).match(processingContext)); } @Test public void testMatchContentTypeMismatch() { - this.doTestAgainstProcessingContextWithSelector(new ProcessorConfigurationImpl(new String[] {"text/html", - "text/plain" }, null, null, null, null), true); + assertNoMatch(ImmutableMap.<String,Object>of(PROPERTY_CONTENT_TYPES, new String[] {"text/xml", "text/plain"})); } @Test public void testMatchAtLeastOneContentType() { - this.doTestAgainstProcessingContextWithSelector(new ProcessorConfigurationImpl(new String[] {"text/html", - "text/xml" }, null, null, null, null)); + assertMatch(ImmutableMap.<String,Object>of(PROPERTY_CONTENT_TYPES, new String[] {"text/html", "text/xml"})); } @Test public void testMatchAnyContentType() { - this.doTestAgainstProcessingContextWithSelector(new ProcessorConfigurationImpl( - new String[] {"text/html", "*" }, null, null, null, null)); + assertMatch(ImmutableMap.<String,Object>of(PROPERTY_CONTENT_TYPES, new String[] {"text/xml", "*"})); } @Test public void testMatchExtensionMismatch() { - this.doTestAgainstProcessingContextWithSelector(new ProcessorConfigurationImpl(null, null, new String[] { - "html", "txt" }, null, null), true); + context.requestPathInfo().setExtension("html"); + assertNoMatch(ImmutableMap.<String,Object>of(PROPERTY_EXTENSIONS, new String[] {"xml","txt"})); } @Test public void testMatchAtLeastOneExtension() { - this.doTestAgainstProcessingContextWithSelector(new ProcessorConfigurationImpl(null, null, new String[] { - "html", "xml" }, null, null)); + context.requestPathInfo().setExtension("html"); + assertMatch(ImmutableMap.<String,Object>of(PROPERTY_EXTENSIONS, new String[] {"xml","html"})); } @Test public void testMatchResourceTypeMismatch() { - this.doTestAgainstProcessingContextWithSelector(new ProcessorConfigurationImpl(null, null, null, new String[] { - "a/b/c" }, null), true); + context.currentResource(context.create().resource("/content/test", + ImmutableMap.<String, Object>of("sling:resourceType", "type/1"))); + assertNoMatch(ImmutableMap.<String,Object>of(PROPERTY_RESOURCE_TYPES, new String[] {"type/2","type/3"})); } @Test public void testMatchAtLeastOneResourceType() { - this.doTestAgainstProcessingContextWithSelector(new ProcessorConfigurationImpl(null, null, null, new String[] { - "a/b/c", MockSlingHttpServletRequest.RESOURCE_TYPE }, null)); + context.currentResource(context.create().resource("/content/test", + ImmutableMap.<String, Object>of("sling:resourceType", "type/1"))); + assertMatch(ImmutableMap.<String,Object>of(PROPERTY_RESOURCE_TYPES, new String[] {"type/1","type/2"})); } @Test public void testMatchPathMismatch() { - this.doTestAgainstProcessingContextWithSelector(new ProcessorConfigurationImpl(null, new String[] {"/apps", - "/var" }, null, null, null), true); + context.requestPathInfo().setResourcePath("/content/test"); + assertNoMatch(ImmutableMap.<String,Object>of(PROPERTY_PATHS, new String[] {"/apps","/var"})); } @Test public void testMatchAtLeastOnePath() { - this.doTestAgainstProcessingContextWithSelector(new ProcessorConfigurationImpl(null, new String[] {"/libs", - "/content" }, null, null, null)); + context.requestPathInfo().setResourcePath("/content/test"); + assertMatch(ImmutableMap.<String,Object>of(PROPERTY_PATHS, new String[] {"/apps","/content"})); } @Test public void testMatchAnyPath() { - this.doTestAgainstProcessingContextWithSelector(new ProcessorConfigurationImpl(null, new String[] {"/libs", - "*" }, null, null, null)); + context.requestPathInfo().setResourcePath("/content/test"); + assertMatch(ImmutableMap.<String,Object>of(PROPERTY_PATHS, new String[] {"/apps","*"})); } @Test public void testMatchSelectorRequired() { - this.doTestAgainstProcessingContextWithoutSelector(new ProcessorConfigurationImpl(null, null, null, null, - new String[] {"sel" }), true); + assertNoMatch(ImmutableMap.<String,Object>of(PROPERTY_SELECTORS, new String[] {"sel"})); } @Test public void testMatchSelectorMismatch() { - this.doTestAgainstProcessingContextWithSelector(new ProcessorConfigurationImpl(null, null, null, null, - new String[] {"sel3" }), true); + context.requestPathInfo().setSelectorString("sel1.sel2"); + assertNoMatch(ImmutableMap.<String,Object>of(PROPERTY_SELECTORS, new String[] {"sel3"})); } @Test public void testMatchAtLeastOneSelector() { - this.doTestAgainstProcessingContextWithSelector(new ProcessorConfigurationImpl(null, null, null, null, - new String[] {"sel1" })); - } - - private void doTestAgainstProcessingContextWithSelector(ProcessorConfiguration configuration) { - this.doTestAgainstProcessingContextWithSelector(configuration, false); + context.requestPathInfo().setSelectorString("sel1.sel2"); + assertMatch(ImmutableMap.<String,Object>of(PROPERTY_SELECTORS, new String[] {"sel1"})); } - private void doTestAgainstProcessingContextWithSelector(ProcessorConfiguration configuration, boolean negate) { - // setup processing context - ProcessingContext context = createProcessingContext(this.mockRequestWithSelector); - // test the given configuration - doTest(configuration, context, negate); - } - - private void doTestAgainstProcessingContextWithoutSelector(ProcessorConfiguration configuration, boolean negate) { - // setup processing context - ProcessingContext context = createProcessingContext(this.mockRequestWithoutSelector); - // test the given configuration - doTest(configuration, context, negate); - } - - private void doTest(ProcessorConfiguration configuration, ProcessingContext context, boolean negate) { - if (!negate) { - assertTrue(configuration.match(context)); - } else { - assertFalse(configuration.match(context)); - } - } - - private ProcessingContext createProcessingContext(SlingHttpServletRequest request) { - final ProcessingContext context = mock(ProcessingContext.class); - when(context.getContentType()).thenReturn("text/xml"); - when(context.getRequest()).thenReturn(request); - - return context; - } }