jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1399718 - in /jackrabbit/oak/trunk/oak-core/src: main/java/org/apache/jackrabbit/oak/ test/java/org/apache/jackrabbit/oak/ test/java/org/apache/jackrabbit/oak/plugins/name/ test/java/org/apache/jackrabbit/oak/util/
Date Thu, 18 Oct 2012 16:12:52 GMT
Author: jukka
Date: Thu Oct 18 16:12:52 2012
New Revision: 1399718

URL: http://svn.apache.org/viewvc?rev=1399718&view=rev
Log:
OAK-352: Oak builder for simplified repository construction

Add methods for simplifying test cases that need only a single ContentSession or Root

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/Oak.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/AbstractOakTest.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/name/ReadWriteNamespaceRegistryTest.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/util/JsopUtilTest.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/Oak.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/Oak.java?rev=1399718&r1=1399717&r2=1399718&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/Oak.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/Oak.java Thu Oct
18 16:12:52 2012
@@ -18,11 +18,15 @@ package org.apache.jackrabbit.oak;
 
 import java.util.List;
 import javax.annotation.Nonnull;
+import javax.jcr.NoSuchWorkspaceException;
+import javax.security.auth.login.LoginException;
 
 import com.google.common.collect.Lists;
 import org.apache.jackrabbit.mk.api.MicroKernel;
 import org.apache.jackrabbit.mk.core.MicroKernelImpl;
 import org.apache.jackrabbit.oak.api.ContentRepository;
+import org.apache.jackrabbit.oak.api.ContentSession;
+import org.apache.jackrabbit.oak.api.Root;
 import org.apache.jackrabbit.oak.core.ContentRepositoryImpl;
 import org.apache.jackrabbit.oak.kernel.KernelNodeStore;
 import org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore;
@@ -35,6 +39,7 @@ import org.apache.jackrabbit.oak.spi.com
 import org.apache.jackrabbit.oak.spi.commit.ValidatorProvider;
 import org.apache.jackrabbit.oak.spi.query.CompositeQueryIndexProvider;
 import org.apache.jackrabbit.oak.spi.query.QueryIndexProvider;
+import org.apache.jackrabbit.oak.spi.security.OpenSecurityProvider;
 import org.apache.jackrabbit.oak.spi.security.SecurityProvider;
 import org.apache.jackrabbit.oak.spi.state.NodeState;
 import org.apache.jackrabbit.oak.spi.state.NodeStore;
@@ -61,7 +66,7 @@ public class Oak {
 
     private final List<ValidatorProvider> validatorProviders = Lists.newArrayList();
 
-    private SecurityProvider securityProvider;
+    private SecurityProvider securityProvider = new OpenSecurityProvider();
 
     private ConflictHandler conflictHandler;
 
@@ -180,6 +185,54 @@ public class Oak {
                 securityProvider);
     }
 
+    /**
+     * Creates a content repository with the given configuration
+     * and logs in to the default workspace with no credentials,
+     * returning the resulting content session.
+     * <p>
+     * This method exists mostly as a convenience for one-off tests,
+     * as there's no way to create other sessions for accessing the
+     * same repository.
+     * <p>
+     * There is typically no need to explicitly close the returned
+     * session unless the repository has explicitly been configured
+     * to reserve some resources until all sessions have been closed.
+     * The repository will be garbage collected once the session is no
+     * longer used.
+     *
+     * @return content session
+     */
+    public ContentSession createContentSession() {
+        try {
+            return createContentRepository().login(null, null);
+        } catch (NoSuchWorkspaceException e) {
+            throw new IllegalStateException("Default workspace not found", e);
+        } catch (LoginException e) {
+            throw new IllegalStateException("Anonymous login not allowed", e);
+        }
+    }
+
+    /**
+     * Creates a content repository with the given configuration
+     * and returns a {@link Root} instance after logging in to the
+     * default workspace with no credentials.
+     * <p>
+     * This method exists mostly as a convenience for one-off tests, as
+     * the returned root is the only way to access the session or the
+     * repository.
+     * <p>
+     * Note that since there is no way to close the underlying content
+     * session, this method should only be used when no components that
+     * require sessions to be closed have been configured. The repository
+     * and the session will be garbage collected once the root is no longer
+     * used.
+     *
+     * @return root instance
+     */
+    public Root createRoot() {
+        return createContentSession().getLatestRoot();
+    }
+
     private CommitHook createCommitHook() {
         withValidatorHook();
         return CompositeHook.compose(commitHooks);

Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/AbstractOakTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/AbstractOakTest.java?rev=1399718&r1=1399717&r2=1399718&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/AbstractOakTest.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/AbstractOakTest.java
Thu Oct 18 16:12:52 2012
@@ -34,7 +34,6 @@ import org.apache.jackrabbit.oak.plugins
 import org.apache.jackrabbit.oak.security.OakConfiguration;
 import org.apache.jackrabbit.oak.spi.lifecycle.CompositeMicroKernelTracker;
 import org.apache.jackrabbit.oak.spi.lifecycle.MicroKernelTracker;
-import org.apache.jackrabbit.oak.spi.security.OpenSecurityProvider;
 import org.junit.Before;
 
 /**
@@ -59,10 +58,6 @@ public abstract class AbstractOakTest {
 
     protected abstract ContentRepository createRepository();
 
-    protected static ContentRepository createEmptyRepository() {
-        return new Oak().with(new OpenSecurityProvider()).createContentRepository();
-    }
-
     protected ContentRepository getContentRepository() {
         return contentRepository;
     }

Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/name/ReadWriteNamespaceRegistryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/name/ReadWriteNamespaceRegistryTest.java?rev=1399718&r1=1399717&r2=1399718&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/name/ReadWriteNamespaceRegistryTest.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/name/ReadWriteNamespaceRegistryTest.java
Thu Oct 18 16:12:52 2012
@@ -18,8 +18,7 @@ package org.apache.jackrabbit.oak.plugin
 
 import javax.jcr.NamespaceRegistry;
 
-import org.apache.jackrabbit.oak.AbstractOakTest;
-import org.apache.jackrabbit.oak.api.ContentRepository;
+import org.apache.jackrabbit.oak.Oak;
 import org.apache.jackrabbit.oak.api.ContentSession;
 import org.apache.jackrabbit.oak.api.Root;
 import org.apache.jackrabbit.oak.api.Tree;
@@ -27,16 +26,11 @@ import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
 
-public class ReadWriteNamespaceRegistryTest extends AbstractOakTest {
-
-    @Override
-    protected ContentRepository createRepository() {
-        return createEmptyRepository();
-    }
+public class ReadWriteNamespaceRegistryTest{
 
     @Test
     public void testMappings() throws Exception {
-        final ContentSession session = createAdminSession();
+        final ContentSession session = new Oak().createContentSession();
         NamespaceRegistry r = new ReadWriteNamespaceRegistry() {
             @Override
             protected Tree getReadTree() {

Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/util/JsopUtilTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/util/JsopUtilTest.java?rev=1399718&r1=1399717&r2=1399718&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/util/JsopUtilTest.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/util/JsopUtilTest.java
Thu Oct 18 16:12:52 2012
@@ -16,40 +16,23 @@
  */
 package org.apache.jackrabbit.oak.util;
 
-import org.apache.jackrabbit.oak.AbstractOakTest;
-import org.apache.jackrabbit.oak.api.ContentRepository;
-import org.apache.jackrabbit.oak.api.ContentSession;
-import org.apache.jackrabbit.oak.api.Root;
-import org.apache.jackrabbit.oak.api.Tree;
-import org.apache.jackrabbit.oak.query.JsopUtil;
-import org.junit.Before;
-import org.junit.Test;
-
 import static junit.framework.Assert.assertEquals;
 import static junit.framework.Assert.assertFalse;
 import static junit.framework.Assert.assertTrue;
 import static org.apache.jackrabbit.oak.api.Type.STRING;
 
-public class JsopUtilTest extends AbstractOakTest {
-
-    protected ContentSession session;
-    protected Root root;
-
-    @Override
-    @Before
-    public void before() throws Exception {
-        super.before();
-        session = createAdminSession();
-        root = session.getLatestRoot();
-    }
+import org.apache.jackrabbit.oak.Oak;
+import org.apache.jackrabbit.oak.api.Root;
+import org.apache.jackrabbit.oak.api.Tree;
+import org.apache.jackrabbit.oak.query.JsopUtil;
+import org.junit.Test;
 
-    @Override
-    protected ContentRepository createRepository() {
-        return createEmptyRepository();
-    }
+public class JsopUtilTest {
 
     @Test
     public void test() throws Exception {
+        Root root = new Oak().createRoot();
+
         Tree t = root.getTree("/");
         assertFalse(t.hasChild("test"));
 



Mime
View raw message