SLING-4527 - New unit tests for SessionProxyHandler and AccessControlUtil - contributed by Petr Shypila, thanks!

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1668887 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
index 831ced6..0536655 100644
--- a/pom.xml
+++ b/pom.xml
@@ -147,6 +147,18 @@
             <artifactId>junit</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.testing.jcr-mock</artifactId>
+            <version>1.1.5-SNAPSHOT</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-all</artifactId>
+            <version>1.9.5</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
 </project>
diff --git a/src/test/java/org/apache/sling/jcr/base/SessionProxyHandlerTest.java b/src/test/java/org/apache/sling/jcr/base/SessionProxyHandlerTest.java
new file mode 100644
index 0000000..a695c82
--- /dev/null
+++ b/src/test/java/org/apache/sling/jcr/base/SessionProxyHandlerTest.java
@@ -0,0 +1,111 @@
+/*
+ * 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.jcr.base;
+
+import org.apache.sling.jcr.api.NamespaceMapper;
+import org.apache.sling.serviceusermapping.ServiceUserMapper;
+import org.apache.sling.testing.mock.jcr.MockJcr;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.osgi.framework.Bundle;
+
+import javax.jcr.*;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Proxy;
+import java.util.Dictionary;
+
+import static org.mockito.Mockito.*;
+import static org.junit.Assert.*;
+
+public class SessionProxyHandlerTest {
+
+    /**
+     * Method verifies that session proxy with given repository manager could be created
+     * and proxy instance is a kind of <code>SessionProxyHandler.SessionProxyInvocationHandler</code>
+     * class.
+     */
+    @Test
+    public void testProxy() throws RepositoryException {
+        /* Create a new mockSession and mock it to return another one on impersonate() method call,
+         * since impersonate method doesn't implemented.
+         */
+        Session session = Mockito.spy(MockJcr.newSession("admin", "adminSpace"));
+        Session imperSession = MockJcr.newSession("test", "testSpace");
+        doReturn(imperSession).when(session).impersonate(null);
+
+        //Create a proxy handle and proxy session
+        SessionProxyHandler proxyHandler = new SessionProxyHandler(new CustomSlingRepositoryManager());
+        Session proxySession = proxyHandler.createProxy(session);
+
+        //sessionForTest is a proxy which contains a link to the imperSession object
+        Session sessionForTest = proxySession.impersonate(null);
+        InvocationHandler handler = Proxy.getInvocationHandler(sessionForTest);
+
+        /* Checking that proxySession is a kind of SessionProxyHandler.SessionProxyInvocationHandler class.
+         * So we sure that proxy object creation works fine.
+         */
+        assertEquals(SessionProxyHandler.SessionProxyInvocationHandler.class, handler.getClass());
+
+        //Checking that others methods of proxy object works also well and doesn't call impersonate method
+        assertNotNull(sessionForTest.getRootNode());
+        assertNotNull(sessionForTest.getRepository());
+    }
+
+    /**
+     * Create custom RepositoryManager to support namespace mapping, since the only one
+     * available <code>OakSlingRepositoryManager</code> lies under <code>org.apache.sling.jcr.oak.server</code>
+     * what creates circular dependencies with this module.
+     */
+    private static final class CustomSlingRepositoryManager extends AbstractSlingRepositoryManager{
+        @Override
+        protected ServiceUserMapper getServiceUserMapper() {
+            return null;
+        }
+
+        @Override
+        protected Repository acquireRepository() {
+            return null;
+        }
+
+        @Override
+        protected Dictionary<String, Object> getServiceRegistrationProperties() {
+            return null;
+        }
+
+        @Override
+        protected AbstractSlingRepository2 create(Bundle usingBundle) {
+            return null;
+        }
+
+        @Override
+        protected void destroy(AbstractSlingRepository2 repositoryServiceInstance) {
+
+        }
+
+        @Override
+        protected void disposeRepository(Repository repository) {
+
+        }
+
+        @Override
+        protected NamespaceMapper[] getNamespaceMapperServices() {
+            return new NamespaceMapper[0];
+        }
+    }
+}
diff --git a/src/test/java/org/apache/sling/jcr/base/util/AccessControlUtilTest.java b/src/test/java/org/apache/sling/jcr/base/util/AccessControlUtilTest.java
new file mode 100644
index 0000000..538e29e
--- /dev/null
+++ b/src/test/java/org/apache/sling/jcr/base/util/AccessControlUtilTest.java
@@ -0,0 +1,46 @@
+/*
+ * 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.jcr.base.util;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.apache.sling.testing.mock.jcr.MockJcr;
+
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+public class AccessControlUtilTest {
+
+    @Rule
+    public ExpectedException exception = ExpectedException.none();
+
+    @Test
+    public void testGetAccessControlManager() throws RepositoryException {
+        Session s = MockJcr.newSession();
+
+        /* Here we check that AccessControlUtil calls getAccessControlManager method of MockSession object.
+         * getAccessControlManager implementation of MockSession throws UnsupportedOperationException so we except it below.
+         * If AccessControlUtil.getAccessControlManager method will call method which even
+         * doesn't exist RepositoryException will be thrown
+         */
+        exception.expect(UnsupportedOperationException.class);
+        AccessControlUtil.getAccessControlManager(s);
+    }
+}