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 2012/01/23 18:14:57 UTC

svn commit: r1234906 - in /sling/trunk/bundles/commons/testing/src: main/java/org/apache/sling/commons/testing/jcr/ test/java/org/apache/sling/commons/testing/jcr/

Author: bdelacretaz
Date: Mon Jan 23 17:14:57 2012
New Revision: 1234906

URL: http://svn.apache.org/viewvc?rev=1234906&view=rev
Log:
SLING-2380 - RepositoryProvider added, with tests

Added:
    sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/jcr/RepositoryProvider.java   (with props)
    sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/jcr/RepositoryProviderTest.java   (with props)
    sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/jcr/RepositoryTestBaseTest.java   (with props)
Modified:
    sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/jcr/RepositoryTestBase.java

Added: sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/jcr/RepositoryProvider.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/jcr/RepositoryProvider.java?rev=1234906&view=auto
==============================================================================
--- sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/jcr/RepositoryProvider.java (added)
+++ sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/jcr/RepositoryProvider.java Mon Jan 23 17:14:57 2012
@@ -0,0 +1,75 @@
+/*
+ * 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.commons.testing.jcr;
+
+import javax.jcr.RepositoryException;
+
+import org.apache.sling.jcr.api.SlingRepository;
+
+/** Provide a Repository to test classes, on-demand,
+ *  with auto-cleanup at JVM shutdown.
+ */
+public class RepositoryProvider {
+    private static RepositoryProvider INSTANCE;
+    private SlingRepository repository;
+    
+    private static class ShutdownThread extends Thread {
+        @Override
+        public void run() {
+            try {
+                RepositoryUtil.stopRepository();
+            } catch(Exception e) {
+                System.out.println("Exception in ShutdownThread:" + e);
+            }
+        }
+        
+    };
+    
+    private RepositoryProvider() {
+    }
+    
+    public static RepositoryProvider instance() {
+        if(INSTANCE == null) {
+            synchronized (RepositoryProvider.class) {
+                if(INSTANCE == null) {
+                    INSTANCE = new RepositoryProvider();
+                }
+            }
+        }
+        return INSTANCE;
+    }
+    
+    /** Return a SlingRepository. First call initializes it, and a JVM
+    *  shutdown hook is registered to stop it.
+    **/
+    public SlingRepository getRepository() throws RepositoryException {
+        if(repository != null) {
+            return repository;
+        }
+        
+        synchronized (RepositoryTestBase.class) {
+            if(repository == null) {
+                RepositoryUtil.startRepository();
+                repository = RepositoryUtil.getRepository();
+                Runtime.getRuntime().addShutdownHook(new ShutdownThread());
+            }
+            return repository;
+        }
+    }
+}
\ No newline at end of file

Propchange: sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/jcr/RepositoryProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/jcr/RepositoryProvider.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/jcr/RepositoryTestBase.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/jcr/RepositoryTestBase.java?rev=1234906&r1=1234905&r2=1234906&view=diff
==============================================================================
--- sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/jcr/RepositoryTestBase.java (original)
+++ sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/jcr/RepositoryTestBase.java Mon Jan 23 17:14:57 2012
@@ -27,27 +27,15 @@ import junit.framework.TestCase;
 
 import org.apache.sling.jcr.api.SlingRepository;
 
-/** Base class for tests which need a Repository. Uses static
- *  variables to initialize it only once per test run.
+/** Base class for JUnit3-style tests which need a Repository. 
+ *  Should eventually be deprecated in favor of {@link RepositoryProvider}
+ *  which is less intrusive
  */
 public class RepositoryTestBase extends TestCase {
-    private static SlingRepository repository;
     protected Node testRoot;
     protected Session session;
     private int counter;
     
-    private static class ShutdownThread extends Thread {
-        @Override
-        public void run() {
-            try {
-                RepositoryUtil.stopRepository();
-            } catch(Exception e) {
-                System.out.println("Exception in ShutdownThread:" + e);
-            }
-        }
-        
-    };
-    
     @Override
     protected void tearDown() throws Exception {
         super.tearDown();
@@ -74,21 +62,8 @@ public class RepositoryTestBase extends 
         return testRoot;
     }
 
-    /** Return a Repository - first call initializes it, and a JVM
-     *  shutdown hook is registered to stop it
-     */
+    /** Return a Repository */
     protected SlingRepository getRepository() throws RepositoryException, NamingException {
-        if(repository != null) {
-            return repository;
-        }
-        
-        synchronized (RepositoryTestBase.class) {
-            if(repository == null) {
-                RepositoryUtil.startRepository();
-                repository = RepositoryUtil.getRepository();
-                Runtime.getRuntime().addShutdownHook(new ShutdownThread());
-            }
-            return repository;
-        }
+        return RepositoryProvider.instance().getRepository();
     }
-}
+}
\ No newline at end of file

Added: sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/jcr/RepositoryProviderTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/jcr/RepositoryProviderTest.java?rev=1234906&view=auto
==============================================================================
--- sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/jcr/RepositoryProviderTest.java (added)
+++ sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/jcr/RepositoryProviderTest.java Mon Jan 23 17:14:57 2012
@@ -0,0 +1,52 @@
+/*
+ * 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.commons.testing.jcr;
+
+import static org.junit.Assert.assertNotNull;
+
+import javax.jcr.Session;
+
+import org.apache.sling.jcr.api.SlingRepository;
+import org.junit.Before;
+import org.junit.Test;
+
+/** JUnit 4 style RepositoryProvider test */
+public class RepositoryProviderTest {
+    private SlingRepository repo;
+    
+    @Before
+    public void getRepo() throws Exception {
+        repo = RepositoryProvider.instance().getRepository();
+    }
+    
+    @Test 
+    public void testRepository() throws Exception {
+        assertNotNull("Expecting SlingRepository to be setup", repo);
+    }
+    
+    @Test 
+    public void testRootNode() throws Exception {
+        final Session s = repo.loginAdministrative(null);
+        try {
+            assertNotNull("Expecting a non-null Session", s);
+        } finally {
+            s.logout();
+        }
+    }
+}

Propchange: sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/jcr/RepositoryProviderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/jcr/RepositoryProviderTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/jcr/RepositoryTestBaseTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/jcr/RepositoryTestBaseTest.java?rev=1234906&view=auto
==============================================================================
--- sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/jcr/RepositoryTestBaseTest.java (added)
+++ sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/jcr/RepositoryTestBaseTest.java Mon Jan 23 17:14:57 2012
@@ -0,0 +1,30 @@
+/*
+ * 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.commons.testing.jcr;
+
+import javax.jcr.Node;
+
+/** Test the RepositoryTestBase */
+public class RepositoryTestBaseTest extends RepositoryTestBase {
+    
+    public void testTestNode() throws Exception {
+        final Node n = getTestRootNode();
+        assertNotNull("Expecting to get a test Node", n);
+    }
+}

Propchange: sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/jcr/RepositoryTestBaseTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/jcr/RepositoryTestBaseTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL