incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From i..@apache.org
Subject svn commit: r1444661 - /sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/MockedResourceResolverImplTest.java
Date Mon, 11 Feb 2013 05:54:40 GMT
Author: ieb
Date: Mon Feb 11 05:54:40 2013
New Revision: 1444661

URL: http://svn.apache.org/r1444661
Log:
SLING-2725 Coverage at 65%, some areas not covered marked with TODOs in MockedResourceResolverImplTest.

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=1444661&r1=1444660&r2=1444661&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
Mon Feb 11 05:54:40 2013
@@ -25,14 +25,16 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
+import javax.servlet.http.HttpServletRequest;
+
 import org.apache.sling.api.resource.LoginException;
+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.ResourceProviderFactory;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ResourceResolverFactory;
-import org.apache.sling.api.resource.ResourceUtil;
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
@@ -41,24 +43,25 @@ import org.mockito.ArgumentCaptor;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
 import org.osgi.service.component.ComponentContext;
 import org.osgi.service.event.EventAdmin;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * This tests the ResourceResolver using mocks. The Unit test is in addition to
  * ResourceResolverImplTest which covers API conformance more than it covers all
  * code paths.
  */
+// TODO: Configure mapping to react correctly.
+// 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 Logger LOGGER = LoggerFactory.getLogger(MockedResourceResolverImplTest.class);
-
     private static final List<Resource> EMPTY_RESOURCE_LIST = new ArrayList<Resource>();
 
     private ResourceResolverFactoryActivator activator;
@@ -101,6 +104,7 @@ public class MockedResourceResolverImplT
         MockitoAnnotations.initMocks(this);
     }
 
+    @SuppressWarnings("unchecked")
     @Before
     public void before() throws LoginException {
         activator = new ResourceResolverFactoryActivator();
@@ -116,7 +120,7 @@ public class MockedResourceResolverImplT
                 new String[] { "/single" }));
         
         // setup mapping resources at /etc/map to exercise vanity etc.
-        Resource etcMapResource = buildResource("/etc/map", buildMapIterable());
+        Resource etcMapResource = buildResource("/etc/map", buildChildResources("/etc/map"));
         Mockito.when(mappingResourceProvider.getResource(Mockito.any(ResourceResolver.class),
Mockito.eq("/etc/map"))).thenReturn(etcMapResource);
         
         activator.bindResourceProvider(mappingResourceProvider,
@@ -148,6 +152,7 @@ public class MockedResourceResolverImplT
         // extract any services that were registered into a map.
         ArgumentCaptor<String> classesCaptor = ArgumentCaptor.forClass(String.class);
         ArgumentCaptor<Object> serviceCaptor = ArgumentCaptor.forClass(Object.class);
+        @SuppressWarnings("rawtypes")
         ArgumentCaptor<Dictionary> propertiesCaptor = ArgumentCaptor.forClass(Dictionary.class);
         Mockito.verify(bundleContext, Mockito.atLeastOnce()).registerService(
             classesCaptor.capture(), serviceCaptor.capture(),
@@ -155,6 +160,7 @@ public class MockedResourceResolverImplT
 
         int si = 0;
         List<Object> serviceList = serviceCaptor.getAllValues();
+        @SuppressWarnings({ "unused", "rawtypes" })
         List<Dictionary> servicePropertiesList = propertiesCaptor.getAllValues();
         for (String serviceName : classesCaptor.getAllValues()) {
             services.put(serviceName, serviceList.get(si));
@@ -168,30 +174,82 @@ public class MockedResourceResolverImplT
         resourceResolverFactory = (ResourceResolverFactoryImpl) rrf;
     }
 
-    private Iterable<Resource> buildMapIterable() {
+    @After
+    public void after() {
+        activator.unbindResourceProvider(resourceProvider,
+            buildResourceProviderProperties("org.apache.sling.resourceresolver.impl.DummyTestProvider",
+                                            10L,
+                                            new String[] { "/single" }));
+        activator.unbindResourceProvider(mappingResourceProvider,
+            buildResourceProviderProperties("org.apache.sling.resourceresolver.impl.MapProvider",
+                                            11L,
+                                            new String[] { "/etc" }));
+        activator.bindResourceProviderFactory(resourceProviderFactory,
+            buildResourceProviderProperties("org.apache.sling.resourceresolver.impl.DummyTestProviderFactory",
+                                            12L,
+                                            new String[] { "/factory" } ));
+
+    }
+
+    /**
+     * build child resources as an iterable of resources.
+     * @param parent
+     * @return
+     */
+    private Iterable<Resource> buildChildResources(String parent) {
         List<Resource> mappingResources = new ArrayList<Resource>();
-        mappingResources.add(buildResource("/etc/map/m1", EMPTY_RESOURCE_LIST));
-        mappingResources.add(buildResource("/etc/map/m2", EMPTY_RESOURCE_LIST));
-        mappingResources.add(buildResource("/etc/map/m3", EMPTY_RESOURCE_LIST));
+        for ( int i = 0; i < 5; i++ ) {
+            mappingResources.add(buildResource(parent+"/m"+i, EMPTY_RESOURCE_LIST));
+        }
         return mappingResources;
     }
-
+    /**
+     * Build a resource based on path and children.
+     * @param fullpath
+     * @param children
+     * @return
+     */
     private Resource buildResource(String fullpath, Iterable<Resource> children) {
+        return buildResource(fullpath, children, null);
+    }
+
+    /**
+     * Build a resource with path, children and resource resolver.
+     * @param fullpath
+     * @param children
+     * @param resourceResolver
+     * @return
+     */
+    private Resource buildResource(String fullpath, Iterable<Resource> children, ResourceResolver
resourceResolver) {
         Resource resource = Mockito.mock(Resource.class);
         Mockito.when(resource.getName()).thenReturn(getName(fullpath));
         Mockito.when(resource.getPath()).thenReturn(fullpath);
         ResourceMetadata resourceMetadata = new ResourceMetadata();
         Mockito.when(resource.getResourceMetadata()).thenReturn(resourceMetadata);
         Mockito.when(resource.listChildren()).thenReturn(children.iterator());
+        Mockito.when(resource.getResourceResolver()).thenReturn(resourceResolver);
         return resource;
     }
         
 
+    /**
+     * extract the name from a path.
+     * @param fullpath
+     * @return
+     */
     private String getName(String fullpath) {
         int n = fullpath.lastIndexOf("/");
-        return fullpath.substring(n);
+        return fullpath.substring(n+1);
     }
 
+
+    /**
+     * Build properties for a resource provider.
+     * @param servicePID
+     * @param serviceID
+     * @param roots
+     * @return
+     */
     private Map<String, Object> buildResourceProviderProperties(String servicePID,
long serviceID, String[] roots) {
         Map<String, Object> resourceProviderProperties = new HashMap<String, Object>();
         resourceProviderProperties.put(Constants.SERVICE_PID, servicePID);
@@ -199,10 +257,18 @@ public class MockedResourceResolverImplT
         resourceProviderProperties.put(Constants.SERVICE_VENDOR, "Apache");
         resourceProviderProperties.put(Constants.SERVICE_DESCRIPTION,
             "Dummy Provider");
+        resourceProviderProperties.put(QueriableResourceProvider.LANGUAGES,
+                new String[] { "lang1"} );
+
+
         resourceProviderProperties.put(ResourceProvider.ROOTS, roots);
         return resourceProviderProperties;
     }
 
+    /**
+     * build a properties for a resource resolver bundle.
+     * @return
+     */
     private Dictionary<String, Object> buildBundleProperties() {
         Dictionary<String, Object> properties = new Hashtable<String, Object>();
         properties.put("resource.resolver.virtual", new String[] { "/:/" });
@@ -222,23 +288,30 @@ public class MockedResourceResolverImplT
         return properties;
     }
 
-    @After
-    public void after() {
-        activator.unbindResourceProvider(resourceProvider, 
-            buildResourceProviderProperties("org.apache.sling.resourceresolver.impl.DummyTestProvider",

-                                            10L, 
-                                            new String[] { "/single" }));
-        activator.unbindResourceProvider(mappingResourceProvider,
-            buildResourceProviderProperties("org.apache.sling.resourceresolver.impl.MapProvider",
-                                            11L,
-                                            new String[] { "/etc" }));
-        activator.bindResourceProviderFactory(resourceProviderFactory,
-            buildResourceProviderProperties("org.apache.sling.resourceresolver.impl.DummyTestProviderFactory",
-                                            12L,
-                                            new String[] { "/factory" } ));
-        
+    /**
+     * 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
+     */
     @Test
     public void testGetResolver() throws LoginException {
         ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);
@@ -248,6 +321,26 @@ public class MockedResourceResolverImplT
         Assert.assertNotNull(resourceResolver);
     }
 
+    /**
+     * Misceleneous coverage.
+     * @throws LoginException
+     */
+    @Test
+    public void testResolverMisc() throws LoginException {
+        ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);
+        try {
+            resourceResolver.getAttribute(null);
+            Assert.fail("Should have thrown a NPE");
+        } catch ( NullPointerException e) {
+            // this is expected.
+        }
+        Assert.assertArrayEquals(new String[]{"/apps/","/libs/"}, resourceResolver.getSearchPath());
+    }
+
+    /**
+     * Test various administrative resource resolvers.
+     * @throws LoginException
+     */
     @Test
     public void testGetAuthenticatedResolve() throws LoginException {
         ResourceResolver resourceResolver = resourceResolverFactory.getAdministrativeResourceResolver(null);
@@ -258,28 +351,132 @@ public class MockedResourceResolverImplT
         Assert.assertNotNull(resourceResolver);
     }
 
+    /**
+     * Test getResource for a resource provided by a resource provider.
+     * @throws LoginException
+     */
     @Test
     public void testGetResource() throws LoginException {
         ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);
         Assert.assertNotNull(resourceResolver);
-        Resource singleResource = buildResource("/sling/test", EMPTY_RESOURCE_LIST);
-        Mockito.when(
-            resourceProvider.getResource(Mockito.any(ResourceResolver.class),
-                Mockito.eq("/single/test"))).thenReturn(singleResource);
+        Resource singleResource = registerTestResource(resourceResolver, resourceProvider,
"/single/test", EMPTY_RESOURCE_LIST);
         Resource resource = resourceResolver.getResource("/single/test");
         Assert.assertEquals(singleResource, resource);
     }
+    /**
+     * Test getResource for a resource provided by a factory provider.
+     * @throws LoginException
+     */
     @Test
     public void testGetFactoryResource() throws LoginException {
         ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);
         Assert.assertNotNull(resourceResolver);
-        Resource factoryResource = buildResource("/factory/test", EMPTY_RESOURCE_LIST);
-        Mockito.when(
-            factoryResourceProvider.getResource(
-                Mockito.any(ResourceResolver.class),
-                Mockito.eq("/factory/test"))).thenReturn(factoryResource);
+
+        Resource factoryResource = registerTestResource(resourceResolver, factoryResourceProvider,
"/factory/test", EMPTY_RESOURCE_LIST);
         Resource resource = resourceResolver.getResource("/factory/test");
-        Assert.assertEquals(factoryResource,resource);
+        Assert.assertEquals(factoryResource, resource);
     }
     
+
+    /**
+     * Basic test of mapping functionality, at the moment needs more
+     * configuration in the virtual /etc/map.
+     *
+     * @throws LoginException
+     */
+    @Test
+    public void testMapping() throws LoginException {
+        ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);
+        registerTestResource(resourceResolver, factoryResourceProvider, "/factory/test",
EMPTY_RESOURCE_LIST);
+        HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
+        Mockito.when(request.getScheme()).thenReturn("http");
+        Mockito.when(request.getServerPort()).thenReturn(8080);
+        Mockito.when(request.getServerName()).thenReturn("localhost");
+        String path = resourceResolver.map(request,"/factory/test?q=123123");
+        Assert.assertEquals("/factory/test?q=123123", path);
+        path = resourceResolver.map(request,"/factory/test");
+        Assert.assertEquals("/factory/test", path);
+
+        // test path mapping without a request.
+        path = resourceResolver.map("/factory/test");
+        Assert.assertEquals("/factory/test", path);
+
+
+    }
+
+
+    /**
+     * Tests list children via the resource (NB, this doesn't really test the
+     * resource resolver at all, but validates this unit test.)
+     *
+     * @throws LoginException
+     */
+    @Test
+    public void testListChildren() throws LoginException {
+        ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);
+        registerTestResource(resourceResolver, resourceProvider, "/single/test/withchildren",
buildChildResources("/single/test/withchildren"));
+
+
+        Resource resource = resourceResolver.getResource("/single/test/withchildren");
+        Assert.assertNotNull(resource);
+
+        // test via the resource list children itself, this really just tests this test case.
+        Iterator<Resource> resourceIterator = resource.listChildren();
+        Assert.assertNotNull(resourceResolver);
+        int i = 0;
+        while(resourceIterator.hasNext()) {
+            Assert.assertEquals("m"+i, resourceIterator.next().getName());
+            i++;
+        }
+        Assert.assertEquals(5, i);
+    }
+
+    /**
+     * Test listing children via the resource resolver listChildren call.
+     * @throws LoginException
+     */
+    @Test
+    public void testResourceResolverListChildren() throws LoginException {
+        ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);
+        registerTestResource(resourceResolver, resourceProvider, "/single/test/withchildren",
buildChildResources("/single/test/withchildren"));
+
+
+        Resource resource = resourceResolver.getResource("/single/test/withchildren");
+        Assert.assertNotNull(resource);
+
+        // test via the resource list children itself, this really just tests this test case.
+        Iterator<Resource> resourceIterator = resourceResolver.listChildren(resource);
+        Assert.assertNotNull(resourceResolver);
+        int i = 0;
+        while(resourceIterator.hasNext()) {
+            Assert.assertEquals("m"+i, resourceIterator.next().getName());
+            i++;
+        }
+        Assert.assertEquals(5,i);
+    }
+
+    /**
+     * Tests listing children via the resource resolver getChildren call.
+     * @throws LoginException
+     */
+    @Test
+    public void testResourceResolverGetChildren() throws LoginException {
+        ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);
+        registerTestResource(resourceResolver, resourceProvider, "/single/test/withchildren",
buildChildResources("/single/test/withchildren"));
+
+
+        Resource resource = resourceResolver.getResource("/single/test/withchildren");
+        Assert.assertNotNull(resource);
+
+        // test via the resource list children itself, this really just tests this test case.
+        Iterable<Resource> resourceIterator = resourceResolver.getChildren(resource);
+        Assert.assertNotNull(resourceResolver);
+        int i = 0;
+        for(Resource r : resourceIterator) {
+            Assert.assertEquals("m"+i, r.getName());
+            i++;
+        }
+        Assert.assertEquals(5,i);
+    }
+
 }



Mime
View raw message