You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ie...@apache.org on 2013/02/12 02:21:03 UTC

svn commit: r1445011 - in /sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl: MockedResourceResolverImplTest.java SimpleValueMapImpl.java

Author: ieb
Date: Tue Feb 12 01:21:02 2013
New Revision: 1445011

URL: http://svn.apache.org/r1445011
Log:
SLING-2718 Coverage now at 68%, added some coverage for mapping and tests for earlier fixed bugs.

Added:
    sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/SimpleValueMapImpl.java   (with props)
Modified:
    sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/MockedResourceResolverImplTest.java

Modified: sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/MockedResourceResolverImplTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/MockedResourceResolverImplTest.java?rev=1445011&r1=1445010&r2=1445011&view=diff
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/MockedResourceResolverImplTest.java (original)
+++ sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/MockedResourceResolverImplTest.java Tue Feb 12 01:21:02 2013
@@ -35,6 +35,7 @@ import org.apache.sling.api.resource.Res
 import org.apache.sling.api.resource.ResourceProviderFactory;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.api.resource.ValueMap;
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
@@ -57,9 +58,6 @@ import org.osgi.service.event.EventAdmin
 // TODO: test external redirect.
 // TODO: Map to URI
 // TODO: Statresource
-// TODO: relative resource.
-// TODO: SLING-864, path with . in it.
-// TODO: search path eg text/html which will search on /apps/text/html and then /libs/text/html
 public class MockedResourceResolverImplTest {
 
     private static final List<Resource> EMPTY_RESOURCE_LIST = new ArrayList<Resource>();
@@ -96,9 +94,18 @@ public class MockedResourceResolverImplT
     @Mock
     private ResourceProvider factoryAdministrativeResourceProvider;
 
+    /**
+     * deals with /etc resolution.
+     */
     @Mock
     private ResourceProvider mappingResourceProvider;
 
+    /**
+     * deals with /apps and /libs resolution.
+     */
+    @Mock
+    private ResourceProvider appsResourceProvider;
+
 
     public MockedResourceResolverImplTest() {
         MockitoAnnotations.initMocks(this);
@@ -120,8 +127,9 @@ public class MockedResourceResolverImplT
                 new String[] { "/single" }));
         
         // setup mapping resources at /etc/map to exercise vanity etc.
-        Resource etcMapResource = buildResource("/etc/map", buildChildResources("/etc/map"));
-        Mockito.when(mappingResourceProvider.getResource(Mockito.any(ResourceResolver.class), Mockito.eq("/etc/map"))).thenReturn(etcMapResource);
+        // hmm, can't provide the resolver since its not up and ready.
+        // mapping almost certainly work properly until this can be setup correctly.
+        Resource etcMapResource = buildMappingResource("/etc/map", mappingResourceProvider, null);
         
         activator.bindResourceProvider(mappingResourceProvider,
             buildResourceProviderProperties("org.apache.sling.resourceresolver.impl.MapProvider",
@@ -146,6 +154,11 @@ public class MockedResourceResolverImplT
                 12L,
                 new String[] { "/factory" } ));
 
+        activator.bindResourceProvider(appsResourceProvider,
+            buildResourceProviderProperties("org.apache.sling.resourceresolver.impl.AppsProvider",
+                13L,
+                new String[] { "/apps", "/libs" }));
+
         // activate the components.
         activator.activate(componentContext);
 
@@ -173,6 +186,24 @@ public class MockedResourceResolverImplT
         Assert.assertTrue(rrf instanceof ResourceResolverFactoryImpl);
         resourceResolverFactory = (ResourceResolverFactoryImpl) rrf;
     }
+    
+    private Resource buildMappingResource(String path,
+            ResourceProvider provider, ResourceResolver resourceResolver) {
+        List<Resource> localHostAnyList = new ArrayList<Resource>();
+        localHostAnyList.add(buildResource(path+"/http/example.com.80/cgi-bin", EMPTY_RESOURCE_LIST, resourceResolver, provider, "sling:internalRedirect", "/scripts" ));
+        localHostAnyList.add(buildResource(path+"/http/example.com.80/gateway", EMPTY_RESOURCE_LIST, resourceResolver, provider,"sling:internalRedirect", "http://gbiv.com"));
+        localHostAnyList.add(buildResource(path+"/http/example.com.80/stories", EMPTY_RESOURCE_LIST, resourceResolver, provider,"sling:internalRedirect", "/anecdotes/$1"));
+        
+        List<Resource> mappingChildren = new ArrayList<Resource>();
+        mappingChildren.add(buildResource(path+"/http/example.com.80", EMPTY_RESOURCE_LIST, resourceResolver, provider,"sling:redirect", "http://www.example.com/"));
+        mappingChildren.add(buildResource(path+"/http/www.example.com.80", EMPTY_RESOURCE_LIST, resourceResolver, provider,"sling:internalRedirect", "/example"));
+        mappingChildren.add(buildResource(path+"/http/any_example.com.80", EMPTY_RESOURCE_LIST, resourceResolver, provider,"sling:match", ".+\\.example\\.com\\.80", "sling:redirect", "http://www.example.com/"));
+        mappingChildren.add(buildResource(path+"/http/localhost_any", localHostAnyList, resourceResolver, provider,"sling:match", "localhost\\.\\d*", "sling:internalRedirect", "/content"));
+        
+        Resource etcMapResource = buildResource(path+"/http", mappingChildren);
+        Mockito.when(provider.getResource(Mockito.any(ResourceResolver.class), Mockito.eq(path))).thenReturn(etcMapResource);
+        return etcMapResource;
+    }
 
     @After
     public void after() {
@@ -210,7 +241,7 @@ public class MockedResourceResolverImplT
      * @return
      */
     private Resource buildResource(String fullpath, Iterable<Resource> children) {
-        return buildResource(fullpath, children, null);
+        return buildResource(fullpath, children, null, null, null);
     }
 
     /**
@@ -220,7 +251,7 @@ public class MockedResourceResolverImplT
      * @param resourceResolver
      * @return
      */
-    private Resource buildResource(String fullpath, Iterable<Resource> children, ResourceResolver resourceResolver) {
+    private Resource buildResource(String fullpath, Iterable<Resource> children, ResourceResolver resourceResolver, ResourceProvider provider, String ... properties) {
         Resource resource = Mockito.mock(Resource.class);
         Mockito.when(resource.getName()).thenReturn(getName(fullpath));
         Mockito.when(resource.getPath()).thenReturn(fullpath);
@@ -228,6 +259,27 @@ public class MockedResourceResolverImplT
         Mockito.when(resource.getResourceMetadata()).thenReturn(resourceMetadata);
         Mockito.when(resource.listChildren()).thenReturn(children.iterator());
         Mockito.when(resource.getResourceResolver()).thenReturn(resourceResolver);
+
+        // register the resource with the provider
+        if ( provider != null ) {
+            Mockito.when(provider.listChildren(resource)).thenReturn(children.iterator());
+            if ( resourceResolver != null) {
+                Mockito.when(provider.getResource(Mockito.eq(resourceResolver), Mockito.eq(fullpath))).thenReturn(resource);
+                Mockito.when(provider.getResource(Mockito.eq(resourceResolver), Mockito.any(HttpServletRequest.class), Mockito.eq(fullpath))).thenReturn(resource);                
+            } else {
+                Mockito.when(provider.getResource(Mockito.any(ResourceResolver.class), Mockito.eq(fullpath))).thenReturn(resource);
+                Mockito.when(provider.getResource(Mockito.any(ResourceResolver.class), Mockito.any(HttpServletRequest.class), Mockito.eq(fullpath))).thenReturn(resource);                
+            }
+        }
+        if ( properties != null ) {
+            ValueMap vm = new SimpleValueMapImpl();
+            for ( int i=0; i < properties.length; i+=2) {
+                resourceMetadata.put(properties[i], properties[i+1]);
+                vm.put(properties[i], properties[i+1]);
+            }
+            Mockito.when(resource.adaptTo(Mockito.eq(ValueMap.class))).thenReturn(vm);
+        }
+        
         return resource;
     }
         
@@ -289,26 +341,6 @@ public class MockedResourceResolverImplT
     }
 
     /**
-     * Register a resource for testing purposes at a path, with a provider, with children.
-     * @param resourceResolver
-     * @param targetResourceProvider
-     * @param path
-     * @param children
-     * @return
-     */
-    private Resource registerTestResource(ResourceResolver resourceResolver, ResourceProvider targetResourceProvider, String path,  Iterable<Resource> children) {
-        Resource resource = buildResource(path, children, resourceResolver);
-        Mockito.when(
-            targetResourceProvider.getResource(
-                Mockito.any(ResourceResolver.class),
-                Mockito.eq(path))).thenReturn(resource);
-        Mockito.when(targetResourceProvider.listChildren(resource)).thenReturn(children.iterator());
-        return resource;
-    }
-
-
-
-    /**
      * Test getting a resolver.
      * @throws LoginException
      */
@@ -359,10 +391,41 @@ public class MockedResourceResolverImplT
     public void testGetResource() throws LoginException {
         ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);
         Assert.assertNotNull(resourceResolver);
-        Resource singleResource = registerTestResource(resourceResolver, resourceProvider, "/single/test", EMPTY_RESOURCE_LIST);
+        Resource singleResource = buildResource("/single/test", EMPTY_RESOURCE_LIST, resourceResolver, resourceProvider);
         Resource resource = resourceResolver.getResource("/single/test");
         Assert.assertEquals(singleResource, resource);
     }
+
+    /**
+     * Test getResource where path contains intermediate . verifying fix for SLING-864
+     * @throws LoginException
+     */
+    @Test
+    public void testGetResourceSLING864() throws LoginException {
+        ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);
+        Assert.assertNotNull(resourceResolver);
+        Resource singleResource = buildResource("/single/test.with/extra.dots/inthepath", EMPTY_RESOURCE_LIST,resourceResolver, resourceProvider);
+        Resource resource = resourceResolver.getResource("/single/test.with/extra.dots/inthepath");
+        Assert.assertEquals(singleResource, resource);
+    }
+    
+
+    /**
+     * Test search paths
+     * @throws LoginException
+     */
+    @Test
+    public void testRelativeResource() throws LoginException {
+        ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);
+        Assert.assertNotNull(resourceResolver);
+        Resource appResource = buildResource("/apps/store/inventory", EMPTY_RESOURCE_LIST, resourceResolver, appsResourceProvider);
+        Resource libResource = buildResource("/libs/store/catalog", EMPTY_RESOURCE_LIST, resourceResolver, appsResourceProvider);
+        Resource testResource = resourceResolver.getResource("store/inventory");
+        Assert.assertEquals(appResource, testResource);
+        testResource = resourceResolver.getResource("store/catalog");
+        Assert.assertEquals(libResource, testResource);
+    }
+
     /**
      * Test getResource for a resource provided by a factory provider.
      * @throws LoginException
@@ -372,7 +435,7 @@ public class MockedResourceResolverImplT
         ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);
         Assert.assertNotNull(resourceResolver);
 
-        Resource factoryResource = registerTestResource(resourceResolver, factoryResourceProvider, "/factory/test", EMPTY_RESOURCE_LIST);
+        Resource factoryResource = buildResource("/factory/test", EMPTY_RESOURCE_LIST, resourceResolver, factoryResourceProvider);
         Resource resource = resourceResolver.getResource("/factory/test");
         Assert.assertEquals(factoryResource, resource);
     }
@@ -387,10 +450,10 @@ public class MockedResourceResolverImplT
     @Test
     public void testMapping() throws LoginException {
         ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);
-        registerTestResource(resourceResolver, factoryResourceProvider, "/factory/test", EMPTY_RESOURCE_LIST);
+        buildResource("/factory/test", EMPTY_RESOURCE_LIST, resourceResolver, factoryResourceProvider);
         HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
         Mockito.when(request.getScheme()).thenReturn("http");
-        Mockito.when(request.getServerPort()).thenReturn(8080);
+        Mockito.when(request.getServerPort()).thenReturn(80);
         Mockito.when(request.getServerName()).thenReturn("localhost");
         String path = resourceResolver.map(request,"/factory/test?q=123123");
         Assert.assertEquals("/factory/test?q=123123", path);
@@ -401,7 +464,6 @@ public class MockedResourceResolverImplT
         path = resourceResolver.map("/factory/test");
         Assert.assertEquals("/factory/test", path);
 
-
     }
 
 
@@ -414,7 +476,7 @@ public class MockedResourceResolverImplT
     @Test
     public void testListChildren() throws LoginException {
         ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);
-        registerTestResource(resourceResolver, resourceProvider, "/single/test/withchildren", buildChildResources("/single/test/withchildren"));
+        buildResource("/single/test/withchildren", buildChildResources("/single/test/withchildren"), resourceResolver, resourceProvider );
 
 
         Resource resource = resourceResolver.getResource("/single/test/withchildren");
@@ -438,7 +500,7 @@ public class MockedResourceResolverImplT
     @Test
     public void testResourceResolverListChildren() throws LoginException {
         ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);
-        registerTestResource(resourceResolver, resourceProvider, "/single/test/withchildren", buildChildResources("/single/test/withchildren"));
+        buildResource("/single/test/withchildren", buildChildResources("/single/test/withchildren"), resourceResolver, resourceProvider);
 
 
         Resource resource = resourceResolver.getResource("/single/test/withchildren");
@@ -462,7 +524,7 @@ public class MockedResourceResolverImplT
     @Test
     public void testResourceResolverGetChildren() throws LoginException {
         ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);
-        registerTestResource(resourceResolver, resourceProvider, "/single/test/withchildren", buildChildResources("/single/test/withchildren"));
+        buildResource("/single/test/withchildren", buildChildResources("/single/test/withchildren"), resourceResolver, resourceProvider);
 
 
         Resource resource = resourceResolver.getResource("/single/test/withchildren");

Added: sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/SimpleValueMapImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/SimpleValueMapImpl.java?rev=1445011&view=auto
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/SimpleValueMapImpl.java (added)
+++ sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/SimpleValueMapImpl.java Tue Feb 12 01:21:02 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 SF 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 java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.sling.api.resource.ValueMap;
+
+public class SimpleValueMapImpl implements ValueMap {
+
+    private Map<String, Object> delegate;
+
+    public SimpleValueMapImpl() {
+        delegate = new HashMap<String, Object>();
+    }
+
+    public void clear() {
+        delegate.clear();
+    }
+
+    public boolean containsKey(Object key) {
+        return delegate.containsKey(key);
+    }
+
+    public boolean containsValue(Object value) {
+        return delegate.containsValue(value);
+    }
+
+    public Set<java.util.Map.Entry<String, Object>> entrySet() {
+        return delegate.entrySet();
+    }
+
+    public Object get(Object key) {
+        return delegate.get(key);
+    }
+
+    public boolean isEmpty() {
+        return delegate.isEmpty();
+    }
+
+    public Set<String> keySet() {
+        return delegate.keySet();
+    }
+
+    public Object put(String key, Object value) {
+        return delegate.put(key, value);
+    }
+
+    public void putAll(Map<? extends String, ? extends Object> m) {
+        delegate.putAll(m);
+    }
+
+    public Object remove(Object key) {
+        return delegate.remove(key);
+    }
+
+    public int size() {
+        return delegate.size();
+    }
+
+    public Collection<Object> values() {
+        return delegate.values();
+    }
+
+    @SuppressWarnings("unchecked")
+    public <T> T get(String name, Class<T> type) {
+        Object o = delegate.get(name);
+        if ( type.equals(String[].class) && ! ( o instanceof String[])) {
+            o = new String[] { String.valueOf(o) };
+        }
+        return (T) o;
+    }
+
+    @SuppressWarnings("unchecked")
+    public <T> T get(String name, T defaultValue) {
+        if ( delegate.containsKey(name)) {
+            return (T) delegate.get(name);
+        } 
+        return defaultValue;
+    }
+
+}

Propchange: sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/SimpleValueMapImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native