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<String> pathsToIgnore = new HashSet<String>();
+    
+    /** 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<Resource> it, String ...paths) {
+        assertNotNull("Expecting non-null Iterator", it);
+        final List<String> actual = new ArrayList<String>();
+        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<Resource> 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<Resource> 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<Resource> listChildren(Resource parent) {
+                final List<Resource> children = new ArrayList<Resource>();
+                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<ValueMap> queryResources(ResourceResolver 
resolver, String query, String language) {
+                return null;
+            }
+            
+            public Iterator<Resource> findResources(ResourceResolver resolver, 
String query, String language) {
+                final List<Resource> found = new ArrayList<Resource>();
+                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<String, Object> props = new HashMap<String, Object>();
+        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


Reply via email to