jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ang...@apache.org
Subject svn commit: r1550129 - in /jackrabbit/oak/trunk/oak-jcr: ./ src/test/java/org/apache/jackrabbit/oak/jcr/random/
Date Wed, 11 Dec 2013 12:59:58 GMT
Author: angela
Date: Wed Dec 11 12:59:57 2013
New Revision: 1550129

URL: http://svn.apache.org/r1550129
Log:
OAK-884 : Add simple acl randomized test (adding missing dependency + major simplification)

Added:
    jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/RandomizedReadTest.java
      - copied, changed from r1549913, jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/SimpleAclRandomizedTest.java
Removed:
    jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/SimpleAclRandomizedTest.java
Modified:
    jackrabbit/oak/trunk/oak-jcr/pom.xml
    jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/AbstractRandomizedTest.java

Modified: jackrabbit/oak/trunk/oak-jcr/pom.xml
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/pom.xml?rev=1550129&r1=1550128&r2=1550129&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/pom.xml (original)
+++ jackrabbit/oak/trunk/oak-jcr/pom.xml Wed Dec 11 12:59:57 2013
@@ -320,6 +320,18 @@
       <scope>test</scope>
     </dependency>
     <dependency>
+      <groupId>org.apache.jackrabbit</groupId>
+      <artifactId>jackrabbit-core</artifactId>
+      <version>${jackrabbit.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.jackrabbit</groupId>
+      <artifactId>jackrabbit-jcr-server</artifactId>
+      <version>${jackrabbit.version}</version>
+       <scope>test</scope>
+    </dependency>
+    <dependency>
       <groupId>org.apache.geronimo.specs</groupId>
       <artifactId>geronimo-jta_1.0.1B_spec</artifactId>
       <version>1.0.1</version>

Modified: jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/AbstractRandomizedTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/AbstractRandomizedTest.java?rev=1550129&r1=1550128&r2=1550129&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/AbstractRandomizedTest.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/AbstractRandomizedTest.java
Wed Dec 11 12:59:57 2013
@@ -19,19 +19,18 @@ package org.apache.jackrabbit.oak.jcr.ra
 import java.security.Principal;
 import java.util.ArrayList;
 import java.util.List;
+import javax.annotation.Nonnull;
 import javax.jcr.Repository;
+import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.SimpleCredentials;
-import javax.jcr.security.AccessControlManager;
 
 import org.apache.jackrabbit.api.JackrabbitSession;
-import org.apache.jackrabbit.api.security.JackrabbitAccessControlList;
 import org.apache.jackrabbit.api.security.user.Authorizable;
 import org.apache.jackrabbit.api.security.user.Group;
 import org.apache.jackrabbit.api.security.user.User;
 import org.apache.jackrabbit.api.security.user.UserManager;
 import org.apache.jackrabbit.commons.JcrUtils;
-import org.apache.jackrabbit.commons.jackrabbit.authorization.AccessControlUtils;
 import org.apache.jackrabbit.oak.jcr.Jcr;
 import org.junit.After;
 import org.junit.Before;
@@ -42,129 +41,89 @@ import org.junit.Before;
  */
 public abstract class AbstractRandomizedTest {
 
-    protected  Repository jackrabbitRepository;
-    protected  Repository oakRepository;
-
-    protected  Session jackrabbitWriterSession;
-    protected  Session oakWriterSession;
-
-    protected  Session jackrabbitReaderSession;
-    protected  Session oakReaderSession;
-
-    protected  Principal jackrabbitPrincipal;
-    protected  Principal oakPrincipal;
+    private  Repository jackrabbitRepository;
+    private  Repository oakRepository;
 
     protected  String userId = "testuser";
+    protected String[] ids = new String[] {userId, "group1", "group2"};
 
-    protected  List<Principal> jackrabbitPrincipals = new ArrayList<Principal>();
-    protected  List<Principal> oakPrincipals = new ArrayList<Principal>();
+    protected List<JackrabbitSession> writeSessions = new ArrayList();
+    protected List<Session> readSessions = new ArrayList();
 
     @Before
     public void setUp() throws Exception {
         jackrabbitRepository = JcrUtils.getRepository();
-        jackrabbitWriterSession = jackrabbitRepository.login(new SimpleCredentials("admin",
"admin".toCharArray()));
         oakRepository = new Jcr().createRepository();
-        oakWriterSession = oakRepository.login(new SimpleCredentials("admin", "admin".toCharArray()));
-
-        jackrabbitPrincipal = setUpUser(jackrabbitWriterSession, userId);
-        jackrabbitPrincipals.add(jackrabbitPrincipal);
-
-        oakPrincipal = setUpUser(oakWriterSession, userId);
-        oakPrincipals.add(oakPrincipal);
-
-        Principal groupJR = setUpGroup(jackrabbitWriterSession, "group1", jackrabbitPrincipal);
-        jackrabbitPrincipals.add(groupJR);
 
-        Principal groupJR2 = setUpGroup(jackrabbitWriterSession, "group2", jackrabbitPrincipal);
-        jackrabbitPrincipals.add(groupJR2);
+        writeSessions.add((JackrabbitSession) jackrabbitRepository.login(new SimpleCredentials("admin",
"admin".toCharArray())));
+        writeSessions.add((JackrabbitSession) oakRepository.login(new SimpleCredentials("admin",
"admin".toCharArray())));
 
-        Principal groupOAK = setUpGroup(oakWriterSession, "group1", oakPrincipal);
-        oakPrincipals.add(groupOAK);
+        setupAuthorizables();
+        setupContent();
 
-        Principal groupOAK2 = setUpGroup(oakWriterSession, "group2", oakPrincipal);
-        oakPrincipals.add(groupOAK2);
-
-        oakReaderSession = oakRepository.login(new SimpleCredentials(userId, userId.toCharArray()));
-        jackrabbitReaderSession = jackrabbitRepository.login(new SimpleCredentials(userId,
userId.toCharArray()));
-
-        setupTree(jackrabbitWriterSession);
-        setupTree(oakWriterSession);
+        readSessions.add(jackrabbitRepository.login(new SimpleCredentials(userId, userId.toCharArray())));
+        readSessions.add(oakRepository.login(new SimpleCredentials(userId, userId.toCharArray())));
     }
 
     @After
     public void tearDown() throws Exception {
+        clearContent();
+        clearAuthorizables();
 
-        clearTree(jackrabbitWriterSession);
-        clearTree(oakWriterSession);
+        for (JackrabbitSession s : writeSessions) {
+            if (s.isLive()) {
+                s.logout();
+            }
+        }
 
-        removAuthorizable(jackrabbitWriterSession, userId);
-        removAuthorizable(oakWriterSession, userId);
+        for (Session s : readSessions) {
+            if (s.isLive()) {
+                s.logout();
+            }
+        }
 
-        removAuthorizable(jackrabbitWriterSession, "group1");
-        removAuthorizable(oakWriterSession, "group1");
-        removAuthorizable(jackrabbitWriterSession, "group2");
-        removAuthorizable(oakWriterSession, "group2");
+        jackrabbitRepository = null;
+        oakRepository = null;
+    }
 
-        oakPrincipals.clear();
-        jackrabbitPrincipals.clear();
+    protected Principal getTestPrincipal(@Nonnull JackrabbitSession session) throws RepositoryException
{
+        return session.getUserManager().getAuthorizable(userId).getPrincipal();
+    }
 
-        if (jackrabbitWriterSession.isLive()) {
-            jackrabbitWriterSession.logout();
-        }
+    protected Principal getPrincipal(@Nonnull JackrabbitSession session, int index) throws
RepositoryException {
+        return session.getPrincipalManager().getPrincipal(ids[index]);
 
-        if (oakWriterSession.isLive()) {
-            oakWriterSession.logout();
-        }
+    }
 
-        if (jackrabbitReaderSession.isLive()) {
-            jackrabbitReaderSession.logout();
-        }
+    protected void setupAuthorizables() throws RepositoryException {
+        for (JackrabbitSession s : writeSessions) {
+            UserManager userManager = s.getUserManager();
+            User user = userManager.createUser(userId, userId);
 
-        if (oakReaderSession.isLive()) {
-            oakReaderSession.logout();
-        }
+            Group group = userManager.createGroup("group1");
+            group.addMember(user);
 
-        jackrabbitRepository = null;
-        oakRepository = null;
-    }
+            Group group2 = userManager.createGroup("group2");
+            group2.addMember(user);
 
-    protected Principal setUpUser(Session session, String userId)
-            throws Exception {
-        UserManager userManager = ((JackrabbitSession) session).getUserManager();
-        User user = userManager.createUser(userId, userId);
-        session.save();
-        return user.getPrincipal();
-    }
-
-    protected Principal setUpGroup(Session session, String groupId,
-                                   Principal userPrincipal) throws Exception {
-        UserManager userManager = ((JackrabbitSession) session).getUserManager();
-        Group group = userManager.createGroup(groupId);
-        group.addMember(userManager.getAuthorizable(userPrincipal));
-        session.save();
-        return group.getPrincipal();
-    }
-
-    protected void removAuthorizable(Session session, String id)
-            throws Exception {
-        UserManager userManager = ((JackrabbitSession) session).getUserManager();
-        Authorizable  authorizable = userManager.getAuthorizable(id);
-        if (id != null) {
-            authorizable.remove();
+            s.save();
         }
-        session.save();
     }
 
-    protected void setupPermission(Session session, Principal principal, String path,
-                                   boolean allow, String... privilegeNames) throws Exception
{
-        AccessControlManager acm = session.getAccessControlManager();
-        JackrabbitAccessControlList acl = AccessControlUtils.getAccessControlList(acm, path);
-        acl.addEntry(principal, AccessControlUtils.privilegesFromNames(acm, privilegeNames),
allow);
-        acm.setPolicy(path, acl);
-        session.save();
+    protected void clearAuthorizables() throws RepositoryException {
+        for (JackrabbitSession s : writeSessions) {
+            UserManager userManager = s.getUserManager();
+            for (String id : ids) {
+                Authorizable a = userManager.getAuthorizable(id);
+                if (a != null) {
+                    a.remove();
+                }
+            }
+            s.save();
+        }
     }
 
-    protected abstract void setupTree(Session session) throws Exception;
+    protected abstract void setupContent() throws Exception;
 
-    protected abstract void clearTree(Session session) throws Exception;
+    protected abstract void clearContent() throws Exception;
 }

Copied: jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/RandomizedReadTest.java
(from r1549913, jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/SimpleAclRandomizedTest.java)
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/RandomizedReadTest.java?p2=jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/RandomizedReadTest.java&p1=jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/SimpleAclRandomizedTest.java&r1=1549913&r2=1550129&rev=1550129&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/SimpleAclRandomizedTest.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/random/RandomizedReadTest.java
Wed Dec 11 12:59:57 2013
@@ -17,25 +17,29 @@
 package org.apache.jackrabbit.oak.jcr.random;
 
 
+import java.security.Principal;
 import java.util.Random;
 import javax.jcr.Node;
 import javax.jcr.PathNotFoundException;
 import javax.jcr.Session;
+import javax.jcr.security.AccessControlEntry;
+import javax.jcr.security.AccessControlList;
+import javax.jcr.security.AccessControlManager;
 
 import com.google.common.collect.HashBasedTable;
 import com.google.common.collect.Table;
+import org.apache.jackrabbit.api.JackrabbitSession;
+import org.apache.jackrabbit.api.security.JackrabbitAccessControlList;
+import org.apache.jackrabbit.commons.jackrabbit.authorization.AccessControlUtils;
 import org.apache.jackrabbit.oak.spi.security.privilege.PrivilegeConstants;
 import org.junit.Assert;
-import org.junit.Ignore;
 import org.junit.Test;
 
-@Ignore("OAK-884")
-public class SimpleAclRandomizedTest extends AbstractRandomizedTest {
+public class RandomizedReadTest extends AbstractRandomizedTest {
 
-    private  int depth;
-
-    private Table<Integer, Integer, String> tree = HashBasedTable.create();
-    {
+    private static final int depth = 4;
+    private static final Table<Integer, Integer, String> tree = HashBasedTable.create();
+    static {
         tree.put(0, 0, "/");
         tree.put(1, 0, "/n1");
         tree.put(1, 1, "/n2");
@@ -48,34 +52,59 @@ public class SimpleAclRandomizedTest ext
         tree.put(3, 3, "/n1/n3/n9");
     }
 
-    protected void setupTree(Session session) throws Exception {
-        depth = 4;
-        Node n1 = session.getRootNode().addNode("n1");
-        session.getRootNode().addNode("n2");
-        Node n3 = n1.addNode("n3");
-        n1.addNode("n4");
-        n1.addNode("n5");
-        n3.addNode("n6");
-        n3.addNode("n7");
-        n3.addNode("n8");
-        n3.addNode("n9");
-        session.save();
-    }
-
-    protected void clearTree(Session session) throws Exception {       
-        session.getRootNode().getNode("n1").remove();
-        session.getRootNode().getNode("n2").remove();
-        session.save();
+    protected void setupContent() throws Exception {
+        for (JackrabbitSession session : writeSessions) {
+            Node root = session.getRootNode();
+            Node n1 = root.addNode("n1");
+            Node n3 = n1.addNode("n3");
+            n1.addNode("n4");
+            n1.addNode("n5");
+            n3.addNode("n6");
+            n3.addNode("n7");
+            n3.addNode("n8");
+            n3.addNode("n9");
+            root.addNode("n2");
+
+            Principal principal = getTestPrincipal(session);
+            AccessControlManager acm = session.getAccessControlManager();
+            JackrabbitAccessControlList acl = AccessControlUtils.getAccessControlList(acm,
"/");
+            acl.addEntry(principal, AccessControlUtils.privilegesFromNames(acm, PrivilegeConstants.JCR_READ),
true);
+            acm.setPolicy("/", acl);
+
+            session.save();
+        }
     }
 
-    @Test
-    public void testReadAcl() throws Exception {
+    protected void clearContent() throws Exception {
+        for (JackrabbitSession session : writeSessions) {
+            Node root = session.getRootNode();
+            if (root.hasNode("n1")) {
+                root.getNode("n1").remove();
+            }
+            if (root.hasNode("n2")) {
+                root.getNode("n2").remove();
+            }
 
-        setupPermission(jackrabbitWriterSession, jackrabbitPrincipal, "/", true, PrivilegeConstants.JCR_READ);
-        setupPermission(oakWriterSession, oakPrincipal, "/", true,PrivilegeConstants.JCR_READ);
+            AccessControlList acl = AccessControlUtils.getAccessControlList(session, "/");
+            if (acl != null) {
+                boolean modified = false;
+                for (AccessControlEntry ace : acl.getAccessControlEntries()) {
+                    if (getTestPrincipal(session).equals(ace.getPrincipal())) {
+                        acl.removeAccessControlEntry(ace);
+                        modified = true;
+                    }
+                }
+                if (modified) {
+                    session.getAccessControlManager().setPolicy("/", acl);
+                }
+            }
+            session.save();
+        }
+    }
 
+    @Test
+    public void testReadAcl() throws Exception {
         for (int j = 0; j < 1; j++) {
-
             Random r = new Random(j);
             int operations = 1000;
             int depthToApply;
@@ -86,26 +115,15 @@ public class SimpleAclRandomizedTest ext
             for (int i = 0; i < operations; i++) {
                 allow = r.nextBoolean();
                 depthToApply = r.nextInt(depth);
-                String path;
-                principalIndex = r.nextInt(jackrabbitPrincipals.size());
+                principalIndex = r.nextInt(ids.length);
 
-                if (depthToApply == 0) {
-                    path = "/";
-                    continue;
-                } else {
+                if (depthToApply > 0) {
                     index = r.nextInt(depthToApply + 1);
-                    path = getPath(depthToApply, index);
+                    String path = getPath(depthToApply, index);
+                    setupPermissions(principalIndex, path, allow, PrivilegeConstants.JCR_READ);
+                    check();
                 }
-
-                setupPermission(jackrabbitWriterSession,
-                        jackrabbitPrincipals.get(principalIndex), path, allow, PrivilegeConstants.JCR_READ);
-
-                setupPermission(oakWriterSession, oakPrincipals.get(principalIndex),
-                        path, allow, PrivilegeConstants.JCR_READ);
-
-                check();
             }
-
         }
     }
 
@@ -118,33 +136,33 @@ public class SimpleAclRandomizedTest ext
 
     public void check() throws Exception {
         boolean mustThrow;
-        boolean thrown;
-
         try {
             for (String path : tree.values()) {
                 mustThrow = false;
-                thrown = false;
-                Node njr = null, noak = null;
-                try {
-                    njr = jackrabbitReaderSession.getNode(path);
-                } catch (PathNotFoundException pnf) {
-                    mustThrow = true;
-                }
 
+                Session s1 = readSessions.get(0);
                 try {
-                    noak = oakReaderSession.getNode(path);
+                    Node n = s1.getNode(path);
+                    if (!path.equals(n.getPath())) {
+                        Assert.fail("did not resolved the same node");
+                    }
                 } catch (PathNotFoundException pnf) {
-                    thrown = true;
-                }
-
-                if (mustThrow != thrown) {
-                    Assert.fail("did not throw for both for path " + path);
+                    mustThrow = true;
                 }
 
-                if (!mustThrow) {
-                    if (!path.equals(njr.getPath())
-                            || !njr.getPath().equals(noak.getPath())) {
-                        Assert.fail("did not resolved the same node");
+                for (int i = 1; i < readSessions.size(); i++) {
+                    try {
+                        Node n = readSessions.get(i).getNode(path);
+                        if (mustThrow) {
+                            Assert.fail("did not throw for path " + path);
+                        }
+                        if (!path.equals(n.getPath())) {
+                            Assert.fail("did not resolved the same node");
+                        }
+                    } catch (PathNotFoundException pnf) {
+                        if (!mustThrow) {
+                            Assert.fail("did throw for path " + path);
+                        }
                     }
                 }
             }
@@ -154,4 +172,15 @@ public class SimpleAclRandomizedTest ext
         }
     }
 
+    private void setupPermissions(int principalIndex, String path, boolean allow, String...
privilegeNames) throws Exception {
+        for (JackrabbitSession session : writeSessions) {
+            Principal principal = getPrincipal(session, principalIndex);
+            AccessControlManager acm = session.getAccessControlManager();
+            JackrabbitAccessControlList acl = AccessControlUtils.getAccessControlList(acm,
path);
+            acl.addEntry(principal, AccessControlUtils.privilegesFromNames(acm, privilegeNames),
allow);
+            acm.setPolicy(path, acl);
+            session.save();
+        }
+    }
+
 }



Mime
View raw message