Return-Path: X-Original-To: apmail-sling-commits-archive@www.apache.org Delivered-To: apmail-sling-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 9B68C10BC2 for ; Fri, 6 Dec 2013 11:11:12 +0000 (UTC) Received: (qmail 42240 invoked by uid 500); 6 Dec 2013 11:11:08 -0000 Delivered-To: apmail-sling-commits-archive@sling.apache.org Received: (qmail 42182 invoked by uid 500); 6 Dec 2013 11:11:03 -0000 Mailing-List: contact commits-help@sling.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@sling.apache.org Delivered-To: mailing list commits@sling.apache.org Received: (qmail 42170 invoked by uid 99); 6 Dec 2013 11:11:00 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 06 Dec 2013 11:11:00 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 06 Dec 2013 11:10:58 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 09BB92388A3D; Fri, 6 Dec 2013 11:10:38 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1548491 - in /sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl: IgnoredResourcesTest.java ResourceDecorationTest.java ResourceDecoratorTestBase.java Date: Fri, 06 Dec 2013 11:10:37 -0000 To: commits@sling.apache.org From: bdelacretaz@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20131206111038.09BB92388A3D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: bdelacretaz Date: Fri Dec 6 11:10:37 2013 New Revision: 1548491 URL: http://svn.apache.org/r1548491 Log: SLING-3267 - add tests that clarify the current behavior Added: sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/IgnoredResourcesTest.java sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/ResourceDecorationTest.java sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/ResourceDecoratorTestBase.java Added: sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/IgnoredResourcesTest.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/IgnoredResourcesTest.java?rev=1548491&view=auto ============================================================================== --- sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/IgnoredResourcesTest.java (added) +++ sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/IgnoredResourcesTest.java Fri Dec 6 11:10:37 2013 @@ -0,0 +1,117 @@ +/* + * 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.resourceresolver.impl; + +import static org.junit.Assert.fail; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import org.apache.sling.api.resource.Resource; +import org.junit.Before; +import org.junit.Test; + +/** Verify that resources are ignored when ResourceDecorator returns null */ +public class IgnoredResourcesTest extends ResourceDecoratorTestBase { + + private Set pathsToIgnore = new HashSet(); + + /** Return null for ignored resources, will cause our + * test ResourceDecorator to return null */ + protected Resource wrapResourceForTest(Resource r) { + return isIgnored(r) ? null : r; + } + + private boolean isIgnored(Resource r) { + return pathsToIgnore.contains(r.getPath()); + } + + private void assertResources(Iterator it, String ...paths) { + assertNotNull("Expecting non-null Iterator", it); + final List actual = new ArrayList(); + int nullCounter = 1; + while(it.hasNext()) { + final Resource r = it.next(); + + // TODO should not get any null Resources here + // remove this once SLING-3267 is fixed + if(r == null) { + actual.add("NULL_" + nullCounter++); + continue; + } + + assertNotNull("Expecting no null Resources in iterator", r); + actual.add(r.getPath()); + } + for(String path : paths) { + assertTrue("Expecting path " + path + " in " + actual, actual.contains(path)); + } + if(actual.size() != paths.length) { + fail("Expecting the same number of items in " + Arrays.asList(paths) + " and " + actual); + } + } + + @Before + public void setup() { + super.setup(); + pathsToIgnore.add("/tmp/D"); + pathsToIgnore.add("/var/two"); + } + + @Test + public void testNotIgnored() { + assertExistent(resolver.resolve("/tmp/A"), true); + } + + @Test + public void testIgnored() { + // TODO this should return a non-existent resource instead of null + // use this once SLING-3267 is fixed + // assertExistent(resolver.resolve("/tmp/D"), false); + assertNull(resolver.resolve("/tmp/D")); + } + + @Test + public void testRootChildren() { + final Resource root = resolver.resolve("/"); + assertNotNull(root); + assertResources(resolver.listChildren(root), "/tmp", "/var"); + } + + @Test + public void testVarChildren() { + final Resource var = resolver.resolve("/var"); + assertNotNull(var); + // TODO remove the NULL once SLING-3267 is fixed + assertResources(resolver.listChildren(var), "/var/one", "/var/three", "NULL_1"); + } + + @Test + public void testFind() { + // TODO remove the NULL once SLING-3267 is fixed + assertResources(resolver.findResources("foo", QUERY_LANGUAGE), "/tmp/C", "/var/one", "NULL_1", "NULL_2"); + } +} \ No newline at end of file Added: sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/ResourceDecorationTest.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/ResourceDecorationTest.java?rev=1548491&view=auto ============================================================================== --- sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/ResourceDecorationTest.java (added) +++ sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/ResourceDecorationTest.java Fri Dec 6 11:10:37 2013 @@ -0,0 +1,100 @@ +/* + * 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.resourceresolver.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.Iterator; + +import org.apache.sling.api.resource.Resource; +import org.apache.sling.api.resource.ResourceWrapper; +import org.junit.Test; + +/** Test ResourceDecorator handling in ResourceResolverImpl */ +public class ResourceDecorationTest extends ResourceDecoratorTestBase { + + private static final String DECORATED_NAME = "decorated"; + + /** Wrap any resource so that its name is DECORATED_NAME */ + protected Resource wrapResourceForTest(Resource resource) { + return new ResourceWrapper(resource) { + @Override + public String getName() { + return DECORATED_NAME; + } + }; + } + + private void assertDecorated(Resource r) { + assertEquals("Expecting " + r + " to be decorated", DECORATED_NAME, r.getName()); + } + + private void assertDecorated(Iterator it, int expectedCount) { + assertNotNull(it); + assertTrue("Expecting non-empty Iterator", it.hasNext()); + int count = 0; + while(it.hasNext()) { + count++; + assertDecorated(it.next()); + } + assertEquals("Expecting " + expectedCount + " items in Iterator", expectedCount, count); + } + + @Test + public void rootIsDecorated() { + final Resource r = resolver.resolve((String)null); + assertDecorated(r); + assertExistent(r, true); + } + + @Test + public void existentIsDecorated() { + final Resource r = resolver.resolve("/tmp/C"); + assertDecorated(r); + assertExistent(r, true); + } + + @Test + public void NonExistentIsDecorated() { + final Resource r = resolver.resolve("/foo"); + assertDecorated(r); + assertExistent(r, false); + } + + @Test + public void childrenAreDecorated() { + final Resource root = resolver.resolve((String)null); + final Iterator it = resolver.listChildren(root); + assertTrue("Expecting root to have children", it.hasNext()); + assertDecorated(it, 2); + } + + @Test + public void listChildrenDecorates() { + final Resource testVar = resolver.resolve("/var"); + assertDecorated(resolver.listChildren(testVar), 3); + } + + @Test + public void findDecorates() { + assertDecorated(resolver.findResources("foo", QUERY_LANGUAGE), 4); + } +} \ No newline at end of file Added: sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/ResourceDecoratorTestBase.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/ResourceDecoratorTestBase.java?rev=1548491&view=auto ============================================================================== --- sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/ResourceDecoratorTestBase.java (added) +++ sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/ResourceDecoratorTestBase.java Fri Dec 6 11:10:37 2013 @@ -0,0 +1,145 @@ +/* + * 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.resourceresolver.impl; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.sling.api.resource.NonExistingResource; +import org.apache.sling.api.resource.QueriableResourceProvider; +import org.apache.sling.api.resource.Resource; +import org.apache.sling.api.resource.ResourceMetadata; +import org.apache.sling.api.resource.ResourceProvider; +import org.apache.sling.api.resource.ResourceResolver; +import org.apache.sling.api.resource.ValueMap; +import org.apache.sling.resourceresolver.impl.helper.ResourceDecoratorTracker; +import org.apache.sling.resourceresolver.impl.helper.ResourceResolverContext; +import org.apache.sling.resourceresolver.impl.tree.RootResourceProviderEntry; +import org.junit.Before; +import org.mockito.Mockito; +import org.osgi.framework.Constants; + +/** Base class for tests that involve ResourceDecorators */ +public abstract class ResourceDecoratorTestBase { + + protected ResourceResolver resolver; + protected static final String QUERY_LANGUAGE = "some.funnny.language"; + + protected abstract Resource wrapResourceForTest(Resource resource); + + @Before + public void setup() { + final ResourceDecoratorTracker t = new ResourceDecoratorTracker() { + @Override + public Resource decorate(Resource resource) { + return wrapResourceForTest(resource); + } + + }; + + final ResourceProvider provider = new QueriableResourceProvider() { + + public Resource getResource(ResourceResolver resourceResolver, HttpServletRequest request, String path) { + return getResource(null, path); + } + + public Resource getResource(ResourceResolver resourceResolver, String path) { + if(path.equals("/") || path.startsWith("/tmp") || path.startsWith("/var")) { + return mockResource(path); + } + return null; + } + + public Iterator listChildren(Resource parent) { + final List children = new ArrayList(); + if("/".equals(parent.getPath())) { + children.add(mockResource("/tmp")); + children.add(mockResource("/var")); + } else if("/var".equals(parent.getPath())) { + children.add(mockResource("/var/one")); + children.add(mockResource("/var/two")); + children.add(mockResource("/var/three")); + } else if("/tmp".equals(parent.getPath())) { + children.add(mockResource("/tmp/A")); + children.add(mockResource("/tmp/B")); + children.add(mockResource("/tmp/C")); + children.add(mockResource("/tmp/D")); + } + return children.iterator(); + } + + public Iterator queryResources(ResourceResolver resolver, String query, String language) { + return null; + } + + public Iterator findResources(ResourceResolver resolver, String query, String language) { + final List found = new ArrayList(); + found.add(mockResource("/tmp/C")); + found.add(mockResource("/tmp/D")); + found.add(mockResource("/var/one")); + found.add(mockResource("/var/two")); + return found.iterator(); + } + + }; + + final RootResourceProviderEntry rrp = new RootResourceProviderEntry(); + final Map props = new HashMap(); + props.put(Constants.SERVICE_ID, System.currentTimeMillis()); + props.put(ResourceProvider.ROOTS, "/"); + props.put(QueriableResourceProvider.LANGUAGES, QUERY_LANGUAGE); + rrp.bindResourceProvider(provider, props); + + final CommonResourceResolverFactoryImpl crf = new CommonResourceResolverFactoryImpl(new ResourceResolverFactoryActivator()) { + @Override + public ResourceDecoratorTracker getResourceDecoratorTracker() { + return t; + } + + @Override + public RootResourceProviderEntry getRootProviderEntry() { + return rrp; + } + }; + resolver = new ResourceResolverImpl(crf, new ResourceResolverContext(false, null, null)); + } + + protected void assertExistent(Resource r, boolean existent) { + assertNotNull("Expecting non-null Resource", r); + assertEquals("Expecting " + (existent ? "existent" : "non-existent") + " resource", + existent, + !NonExistingResource.RESOURCE_TYPE_NON_EXISTING.equals(r.getResourceType())); + } + + protected Resource mockResource(String path) { + final Resource result = Mockito.mock(Resource.class); + Mockito.when(result.getPath()).thenReturn(path); + final ResourceMetadata m = new ResourceMetadata(); + Mockito.when(result.getResourceMetadata()).thenReturn(m); + return result; + } +} \ No newline at end of file