You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2015/12/21 16:06:29 UTC

svn commit: r1721183 - in /sling/trunk/contrib/extensions/acldef/oak-jcr/src/test/java/org/apache/sling/acldef/jcr: CreateServiceUsersTest.java TestUtil.java

Author: bdelacretaz
Date: Mon Dec 21 15:06:28 2015
New Revision: 1721183

URL: http://svn.apache.org/viewvc?rev=1721183&view=rev
Log:
SLING-5355 - factor our TestUtil

Added:
    sling/trunk/contrib/extensions/acldef/oak-jcr/src/test/java/org/apache/sling/acldef/jcr/TestUtil.java
Modified:
    sling/trunk/contrib/extensions/acldef/oak-jcr/src/test/java/org/apache/sling/acldef/jcr/CreateServiceUsersTest.java

Modified: sling/trunk/contrib/extensions/acldef/oak-jcr/src/test/java/org/apache/sling/acldef/jcr/CreateServiceUsersTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/acldef/oak-jcr/src/test/java/org/apache/sling/acldef/jcr/CreateServiceUsersTest.java?rev=1721183&r1=1721182&r2=1721183&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/acldef/oak-jcr/src/test/java/org/apache/sling/acldef/jcr/CreateServiceUsersTest.java (original)
+++ sling/trunk/contrib/extensions/acldef/oak-jcr/src/test/java/org/apache/sling/acldef/jcr/CreateServiceUsersTest.java Mon Dec 21 15:06:28 2015
@@ -16,27 +16,12 @@
  */
 package org.apache.sling.acldef.jcr;
 
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-
-import java.io.Reader;
-import java.io.StringReader;
-import java.util.List;
 import java.util.Random;
 
 import javax.jcr.RepositoryException;
-import javax.jcr.Session;
 
-import org.apache.commons.io.IOUtils;
-import org.apache.jackrabbit.api.security.user.Authorizable;
-import org.apache.jackrabbit.api.security.user.User;
-import org.apache.jackrabbit.api.security.user.UserManager;
-import org.apache.sling.acldef.parser.ACLDefinitions;
-import org.apache.sling.acldef.parser.ParseException;
-import org.apache.sling.acldef.parser.operations.Operation;
 import org.apache.sling.testing.mock.sling.ResourceResolverType;
 import org.apache.sling.testing.mock.sling.junit.SlingContext;
-import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -47,56 +32,24 @@ public class CreateServiceUsersTest {
     @Rule
     public final SlingContext context = new SlingContext(ResourceResolverType.JCR_OAK);
     
-    private Session session;
-    private UserManager userManager;
     private static final Random random = new Random(42);
     private String namePrefix;
+    private TestUtil U;
     
-    private List<Operation> parse(String input) throws ParseException {
-        final Reader r = new StringReader(input);
-        try {
-            return new ACLDefinitions(r).parse();
-        } finally {
-            IOUtils.closeQuietly(r);
-        }
-    }
-
     @Before
     public void setup() throws RepositoryException {
-        // We don't need to logout this session, the Sling Mocks library
-        // takes care of that
-        session = context.resourceResolver().adaptTo(Session.class);
-        
-        userManager = ServiceUserUtil.getUserManager(session);
+        U = new TestUtil(context);
         namePrefix = "user_" + random.nextInt();
     }
 
-    private void assertServiceUser(String info, String id, boolean expectToExist) throws RepositoryException {
-        final Authorizable a = userManager.getAuthorizable(id);
-        if(!expectToExist) {
-            assertNull(info + ", expecting Principal to be absent:" + id, a);
-        } else {
-            assertNotNull(info + ", expecting Principal to exist:" + id, a);
-            final User u = (User)a;
-            assertNotNull(info + ", expecting Principal to be a System user:" + id, u.isSystemUser());
-        }
-    }
-    
-    private void exec(String input) throws ParseException {
-        final AclOperationVisitor v = new AclOperationVisitor(session);
-        for(Operation o : parse(input)) {
-            o.accept(v);
-        }
-    }
-
     @Test
     public void createDeleteSingleTest() throws Exception {
         final String userId = namePrefix + "_cdst";
-        assertServiceUser("at start of test", userId, false);
-        exec("create service user " + userId);
-        assertServiceUser("affter creating user", userId, true);
-        exec("delete service user " + userId);
-        assertServiceUser("after deleting user", userId, false);
+        U.assertServiceUser("at start of test", userId, false);
+        U.parseAndExecute("create service user " + userId);
+        U.assertServiceUser("after creating user", userId, true);
+        U.parseAndExecute("delete service user " + userId);
+        U.assertServiceUser("after deleting user", userId, false);
     }
     
     private String user(int index) {
@@ -110,24 +63,24 @@ public class CreateServiceUsersTest {
         {
             final StringBuilder input = new StringBuilder();
             for(int i=0; i < n; i++) {
-                assertServiceUser("at start of test", user(i), false);
+                U.assertServiceUser("at start of test", user(i), false);
                 input.append("create service user ").append(user(i)).append("\n");
             }
-            exec(input.toString());
+            U.parseAndExecute(input.toString());
         }
         
         {
             final StringBuilder input = new StringBuilder();
             for(int i=0; i < n; i++) {
-                assertServiceUser("before deleting user", user(i), true);
+                U.assertServiceUser("before deleting user", user(i), true);
                 input.append("delete service user ").append(user(i)).append("\n");
             }
-            exec(input.toString());
+            U.parseAndExecute(input.toString());
         }
         
 
         for(int i=0; i < n; i++) {
-            assertServiceUser("after deleting users", user(i), false);
+            U.assertServiceUser("after deleting users", user(i), false);
         }
     }
 }

Added: sling/trunk/contrib/extensions/acldef/oak-jcr/src/test/java/org/apache/sling/acldef/jcr/TestUtil.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/acldef/oak-jcr/src/test/java/org/apache/sling/acldef/jcr/TestUtil.java?rev=1721183&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/acldef/oak-jcr/src/test/java/org/apache/sling/acldef/jcr/TestUtil.java (added)
+++ sling/trunk/contrib/extensions/acldef/oak-jcr/src/test/java/org/apache/sling/acldef/jcr/TestUtil.java Mon Dec 21 15:06:28 2015
@@ -0,0 +1,78 @@
+/*
+ * 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.acldef.jcr;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.List;
+
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.SimpleCredentials;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.jackrabbit.api.security.user.Authorizable;
+import org.apache.jackrabbit.api.security.user.User;
+import org.apache.sling.acldef.parser.ACLDefinitions;
+import org.apache.sling.acldef.parser.ParseException;
+import org.apache.sling.acldef.parser.operations.Operation;
+import org.apache.sling.testing.mock.sling.junit.SlingContext;
+
+/** Test utilities */
+class TestUtil {
+    
+    final Session adminSession;
+    
+    TestUtil(SlingContext ctx) {
+        adminSession = ctx.resourceResolver().adaptTo(Session.class); 
+    }
+    
+    List<Operation> parse(String input) throws ParseException {
+        final Reader r = new StringReader(input);
+        try {
+            return new ACLDefinitions(r).parse();
+        } finally {
+            IOUtils.closeQuietly(r);
+        }
+    }
+
+    void assertServiceUser(String info, String id, boolean expectToExist) throws RepositoryException {
+        final Authorizable a = ServiceUserUtil.getUserManager(adminSession).getAuthorizable(id);
+        if(!expectToExist) {
+            assertNull(info + ", expecting Principal to be absent:" + id, a);
+        } else {
+            assertNotNull(info + ", expecting Principal to exist:" + id, a);
+            final User u = (User)a;
+            assertNotNull(info + ", expecting Principal to be a System user:" + id, u.isSystemUser());
+        }
+    }
+    
+    void parseAndExecute(String input) throws ParseException {
+        final AclOperationVisitor v = new AclOperationVisitor(adminSession);
+        for(Operation o : parse(input)) {
+            o.accept(v);
+        }
+    }
+    
+    Session getServiceSession(String serviceUsername) throws RepositoryException {
+        final SimpleCredentials cred = new SimpleCredentials(serviceUsername, new char[0]);
+        return adminSession.impersonate(cred);
+    }
+}