You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2013/02/22 15:35:40 UTC

svn commit: r1449061 - /sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/ResourceResolverImplTest.java

Author: cziegeler
Date: Fri Feb 22 14:35:40 2013
New Revision: 1449061

URL: http://svn.apache.org/r1449061
Log:
Use Mockito instead of extra mock classes

Modified:
    sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/ResourceResolverImplTest.java

Modified: sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/ResourceResolverImplTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/ResourceResolverImplTest.java?rev=1449061&r1=1449060&r2=1449061&view=diff
==============================================================================
--- sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/ResourceResolverImplTest.java (original)
+++ sling/trunk/bundles/resourceresolver/src/test/java/org/apache/sling/resourceresolver/impl/ResourceResolverImplTest.java Fri Feb 22 14:35:40 2013
@@ -25,35 +25,26 @@ import static org.junit.Assert.assertNul
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
-import java.io.BufferedReader;
-import java.security.Principal;
 import java.util.ArrayList;
-import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Locale;
 import java.util.Map;
 
 import javax.jcr.Session;
-import javax.servlet.RequestDispatcher;
-import javax.servlet.ServletInputStream;
-import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpSession;
 
-import org.apache.sling.api.resource.AbstractResource;
 import org.apache.sling.api.resource.LoginException;
 import org.apache.sling.api.resource.NonExistingResource;
 import org.apache.sling.api.resource.Resource;
-import org.apache.sling.api.resource.ResourceMetadata;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ResourceResolverFactory;
 import org.apache.sling.api.resource.SyntheticResource;
 import org.apache.sling.resourceresolver.impl.helper.ResourceResolverContext;
 import org.junit.Before;
 import org.junit.Test;
+import org.mockito.Mockito;
 
 public class ResourceResolverImplTest {
 
@@ -215,8 +206,11 @@ public class ResourceResolverImplTest {
         assertEquals("Path must be the original path", no_resource_path,
                 res0.getPath());
 
-        final HttpServletRequest req1 = new ResourceResolverTestRequest(
-                no_resource_path);
+        final HttpServletRequest req1 = Mockito.mock(HttpServletRequest.class);
+        Mockito.when(req1.getProtocol()).thenReturn("http");
+        Mockito.when(req1.getServerName()).thenReturn("localhost");
+        Mockito.when(req1.getPathInfo()).thenReturn(no_resource_path);
+
         final Resource res1 = resResolver.resolve(req1);
         assertNotNull("Expecting resource if resolution fails", res1);
         assertTrue("Resource must be NonExistingResource",
@@ -224,7 +218,10 @@ public class ResourceResolverImplTest {
         assertEquals("Path must be the original path", no_resource_path,
                 res1.getPath());
 
-        final HttpServletRequest req2 = new ResourceResolverTestRequest(null);
+        final HttpServletRequest req2 = Mockito.mock(HttpServletRequest.class);
+        Mockito.when(req2.getProtocol()).thenReturn("http");
+        Mockito.when(req2.getServerName()).thenReturn("localhost");
+        Mockito.when(req2.getPathInfo()).thenReturn(null);
         final Resource res2 = resResolver.resolve(req2);
         assertNotNull("Expecting resource if resolution fails", res2);
         assertTrue("Resource must be NonExistingResource",
@@ -316,6 +313,8 @@ public class ResourceResolverImplTest {
     }
 
     @Test public void testBasicCrud() throws Exception {
+        final Resource r = Mockito.mock(Resource.class);
+        Mockito.when(r.getPath()).thenReturn("/some");
         try {
             this.resResolver.create(null, "a", null);
             fail("Null parent resource should throw NPE");
@@ -323,19 +322,19 @@ public class ResourceResolverImplTest {
             // correct
         }
         try {
-            this.resResolver.create(new ResourceImpl(), null, null);
+            this.resResolver.create(r, null, null);
             fail("Null name should throw NPE");
         } catch (final NullPointerException npe) {
             // correct
         }
         try {
-            this.resResolver.create(new ResourceImpl(), "a/b", null);
+            this.resResolver.create(r, "a/b", null);
             fail("Slash in name should throw illegal argument exception");
         } catch (final IllegalArgumentException pe) {
             // correct
         }
         try {
-            this.resResolver.create(new ResourceImpl(), "a", null);
+            this.resResolver.create(r, "a", null);
             fail("This should be unsupported.");
         } catch (final UnsupportedOperationException uoe) {
             // correct
@@ -359,30 +358,15 @@ public class ResourceResolverImplTest {
                 new ResourceResolverContext(false, null));
         resolvers.add(resolver);
 
-        // the resource to test
-        final Resource r = new ResourceImpl() {
-            @Override
-            public String getResourceType() {
-                return "a:b";
-            }
-        };
-        final Resource r2 = new ResourceImpl() {
-            @Override
-            public String getResourceType() {
-                return "a:c";
-            }
-        };
-        final Resource typeResource = new ResourceImpl() {
-            @Override
-            public String getResourceType() {
-                return "x:y";
-            }
+        // the resources to test
+        final Resource r = Mockito.mock(Resource.class);
+        Mockito.when(r.getResourceType()).thenReturn("a:b");
+        final Resource r2 = Mockito.mock(Resource.class);
+        Mockito.when(r2.getResourceType()).thenReturn("a:c");
+        final Resource typeResource = Mockito.mock(Resource.class);
+        Mockito.when(typeResource.getResourceType()).thenReturn("x:y");
+        Mockito.when(typeResource.getResourceSuperType()).thenReturn("t:c");
 
-            @Override
-            public String getResourceSuperType() {
-                return "t:c";
-            }
-        };
         resolver.setResource("/a", r);
         resolver.setResource("/a/b", typeResource);
 
@@ -391,18 +375,10 @@ public class ResourceResolverImplTest {
     }
 
     @Test public void test_isA() {
-        final Resource typeResource = new ResourceImpl() {
+        final Resource typeResource = Mockito.mock(Resource.class);
+        Mockito.when(typeResource.getResourceType()).thenReturn("x:y");
+        Mockito.when(typeResource.getResourceSuperType()).thenReturn("t:c");
 
-            @Override
-            public String getResourceType() {
-                return "x:y";
-            }
-
-            @Override
-            public String getResourceSuperType() {
-                return "t:c";
-            }
-        };
         final List<ResourceResolver> resolvers = new ArrayList<ResourceResolver>();
         final PathBasedResourceResolverImpl resolver = new PathBasedResourceResolverImpl(
                 new ResourceResolverFactoryImpl(new ResourceResolverFactoryActivator()) {
@@ -433,273 +409,6 @@ public class ResourceResolverImplTest {
         assertFalse(resolver.isResourceType(r, "h:p"));
     }
 
-    private static class ResourceImpl extends AbstractResource {
-
-        public String getPath() {
-            return "/some";
-        }
-
-        public String getResourceType() {
-            return null;
-        }
-
-        public String getResourceSuperType() {
-            return null;
-        }
-
-        public ResourceMetadata getResourceMetadata() {
-            return null;
-        }
-
-        public ResourceResolver getResourceResolver() {
-            return null;
-        }
-    };
-
-    private static final class ResourceResolverTestRequest implements
-    HttpServletRequest {
-
-        private final String pathInfo;
-
-        private final String method;
-
-        private final String scheme;
-
-        private final String host;
-
-        private final int port;
-
-        private final Map<String, Object> attrs = new HashMap<String, Object>();
-
-        private String contextPath;
-
-        ResourceResolverTestRequest(String pathInfo) {
-            this(null, null, -1, pathInfo, null);
-        }
-
-        ResourceResolverTestRequest(String scheme, String host, int port,
-                String pathInfo, String httpMethod) {
-            this.scheme = (scheme == null) ? "http" : scheme;
-            this.host = (host == null) ? "localhost" : host;
-            this.port = port;
-            this.pathInfo = pathInfo;
-            this.method = httpMethod;
-        }
-
-        public String getPathInfo() {
-            return pathInfo;
-        }
-
-        public Object getAttribute(String name) {
-            return attrs.get(name);
-        }
-
-        public Enumeration<?> getAttributeNames() {
-            return null;
-        }
-
-        public String getCharacterEncoding() {
-            return null;
-        }
-
-        public int getContentLength() {
-            return 0;
-        }
-
-        public String getContentType() {
-            return null;
-        }
-
-        public ServletInputStream getInputStream() {
-            return null;
-        }
-
-        public String getLocalAddr() {
-            return null;
-        }
-
-        public String getLocalName() {
-            return null;
-        }
-
-        public int getLocalPort() {
-            return 0;
-        }
-
-        public Locale getLocale() {
-            return null;
-        }
-
-        public Enumeration<?> getLocales() {
-            return null;
-        }
-
-        public String getParameter(String name) {
-            return null;
-        }
-
-        public Map<?, ?> getParameterMap() {
-            return null;
-        }
-
-        public Enumeration<?> getParameterNames() {
-            return null;
-        }
-
-        public String[] getParameterValues(String name) {
-            return null;
-        }
-
-        public String getProtocol() {
-            return null;
-        }
-
-        public BufferedReader getReader() {
-            return null;
-        }
-
-        public String getRealPath(String path) {
-            return null;
-        }
-
-        public String getRemoteAddr() {
-            return null;
-        }
-
-        public String getRemoteHost() {
-            return null;
-        }
-
-        public int getRemotePort() {
-            return 0;
-        }
-
-        public RequestDispatcher getRequestDispatcher(String path) {
-            return null;
-        }
-
-        public String getScheme() {
-            return scheme;
-        }
-
-        public String getServerName() {
-            return host;
-        }
-
-        public int getServerPort() {
-            return port;
-        }
-
-        public boolean isSecure() {
-            return false;
-        }
-
-        public String getContextPath() {
-            return contextPath;
-        }
-
-        public void removeAttribute(String name) {
-        }
-
-        public void setAttribute(String name, Object o) {
-            attrs.put(name, o);
-        }
-
-        public void setCharacterEncoding(String env) {
-        }
-
-        public String getAuthType() {
-            return null;
-        }
-
-        public Cookie[] getCookies() {
-            return null;
-        }
-
-        public long getDateHeader(String name) {
-            return 0;
-        }
-
-        public String getHeader(String name) {
-            return null;
-        }
-
-        public Enumeration<?> getHeaderNames() {
-            return null;
-        }
-
-        public Enumeration<?> getHeaders(String name) {
-            return null;
-        }
-
-        public int getIntHeader(String name) {
-            return 0;
-        }
-
-        public String getMethod() {
-            return method;
-        }
-
-        public String getPathTranslated() {
-            return null;
-        }
-
-        public String getQueryString() {
-            return null;
-        }
-
-        public String getRemoteUser() {
-            return null;
-        }
-
-        public String getRequestURI() {
-            return null;
-        }
-
-        public StringBuffer getRequestURL() {
-            return null;
-        }
-
-        public String getRequestedSessionId() {
-            return null;
-        }
-
-        public String getServletPath() {
-            return null;
-        }
-
-        public HttpSession getSession() {
-            return null;
-        }
-
-        public HttpSession getSession(boolean create) {
-            return null;
-        }
-
-        public Principal getUserPrincipal() {
-            return null;
-        }
-
-        public boolean isRequestedSessionIdFromCookie() {
-            return false;
-        }
-
-        public boolean isRequestedSessionIdFromURL() {
-            return false;
-        }
-
-        public boolean isRequestedSessionIdFromUrl() {
-            return false;
-        }
-
-        public boolean isRequestedSessionIdValid() {
-            return false;
-        }
-
-        public boolean isUserInRole(String role) {
-            return false;
-        }
-    }
     private static class PathBasedResourceResolverImpl extends ResourceResolverImpl {
 
         private final Map<String, Resource> resources = new HashMap<String, Resource>();