jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r728022 - /jackrabbit/trunk/jackrabbit-jcr-rmi/src/test/java/org/apache/jackrabbit/test/rmi/repository/RepositoryFactoryImplTest.java
Date Fri, 19 Dec 2008 13:54:08 GMT
Author: jukka
Date: Fri Dec 19 05:54:08 2008
New Revision: 728022

URL: http://svn.apache.org/viewvc?rev=728022&view=rev
Log:
JCR-1834: Create RepositoryFactory implementation

Use a random port for setting up a temporary local RMI registry

Modified:
    jackrabbit/trunk/jackrabbit-jcr-rmi/src/test/java/org/apache/jackrabbit/test/rmi/repository/RepositoryFactoryImplTest.java

Modified: jackrabbit/trunk/jackrabbit-jcr-rmi/src/test/java/org/apache/jackrabbit/test/rmi/repository/RepositoryFactoryImplTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-rmi/src/test/java/org/apache/jackrabbit/test/rmi/repository/RepositoryFactoryImplTest.java?rev=728022&r1=728021&r2=728022&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-rmi/src/test/java/org/apache/jackrabbit/test/rmi/repository/RepositoryFactoryImplTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-rmi/src/test/java/org/apache/jackrabbit/test/rmi/repository/RepositoryFactoryImplTest.java
Fri Dec 19 05:54:08 2008
@@ -16,33 +16,34 @@
  */
 package org.apache.jackrabbit.test.rmi.repository;
 
-import java.util.Map;
-import java.util.HashMap;
-import java.util.Iterator;
 import java.io.File;
+import java.io.FileOutputStream;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
 import java.rmi.Naming;
 import java.rmi.RemoteException;
 import java.rmi.registry.LocateRegistry;
 import java.rmi.registry.Registry;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Random;
 
-import javax.jcr.Repository;
+import javax.imageio.spi.ServiceRegistry;
 import javax.jcr.Credentials;
-import javax.jcr.SimpleCredentials;
+import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
-import javax.imageio.spi.ServiceRegistry;
+import javax.jcr.SimpleCredentials;
 
-import org.apache.jackrabbit.api.jsr283.RepositoryFactory;
+import junit.framework.TestCase;
+
+import org.apache.commons.io.IOUtils;
 import org.apache.jackrabbit.api.JackrabbitRepository;
+import org.apache.jackrabbit.api.jsr283.RepositoryFactory;
 import org.apache.jackrabbit.core.RepositoryFactoryImpl;
 import org.apache.jackrabbit.core.RepositoryImpl;
-import org.apache.jackrabbit.rmi.server.RemoteAdapterFactory;
 import org.apache.jackrabbit.rmi.jackrabbit.JackrabbitServerAdapterFactory;
-
-import junit.framework.TestCase;
+import org.apache.jackrabbit.rmi.server.RemoteAdapterFactory;
 
 /**
  * <code>RepositoryFactoryImplTest</code>...
@@ -58,54 +59,59 @@
 
     private static final File REPO_CONF = new File(REPO_HOME, "repository.xml");
 
-    private static final String RMI_URL = "rmi://localhost/repository";
+    /**
+     * Use a random port in the range 10k-60k for the RMI registry we use
+     * in this test. This way it's very unlikely for the test to interfere
+     * with normal runtime services.
+     */
+    private static final int RMI_PORT = new Random().nextInt(50000) + 10000;
+
+    private static final String RMI_URL =
+        "rmi://localhost:" + RMI_PORT + "/repository";
+
+    private Repository repository;
+
+    protected void setUp() throws Exception {
+        super.setUp();
 
-    static {
         REPO_HOME.mkdirs();
         if (!REPO_CONF.exists()) {
+            // get default configuration from jackrabbit-core
+            InputStream in = RepositoryImpl.class.getResourceAsStream("repository.xml");
             try {
-                // get default configuration from jackrabbit-core
-                InputStream in = RepositoryImpl.class.getResourceAsStream("repository.xml");
+                OutputStream out = new FileOutputStream(REPO_CONF);
                 try {
-                    OutputStream out = new FileOutputStream(REPO_CONF);
-                    try {
-                        byte[] buffer = new byte[1024];
-                        int len;
-                        while ((len = in.read(buffer)) != -1) {
-                            out.write(buffer, 0, len);
-                        }
-                    } finally {
-                        out.close();
-                    }
+                    IOUtils.copy(in, out);
                 } finally {
-                    in.close();
+                    out.close();
                 }
-            } catch (IOException e) {
-                throw new RuntimeException(e);
+            } finally {
+                in.close();
             }
         }
 
-        // try to create a registry
+        // Make sure that a local RMI registry is running at the selected port
         try {
-            LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
+            Registry registry = LocateRegistry.getRegistry(RMI_PORT);
+            registry.list();
         } catch (RemoteException e) {
-            // ignore
+            LocateRegistry.createRegistry(RMI_PORT);
         }
-    }
 
-    private Repository repository;
-
-    protected void setUp() throws Exception {
-        super.setUp();
         // get a local repository
         Map params = new HashMap();
         params.put(RepositoryFactoryImpl.REPOSITORY_CONF, REPO_CONF.getAbsolutePath());
         params.put(RepositoryFactoryImpl.REPOSITORY_HOME, REPO_HOME.getAbsolutePath());
         repository = RepositoryManager.getRepository(params);
-        
+
+        // setup remote repository
+        RemoteAdapterFactory raf = new JackrabbitServerAdapterFactory();
+        Naming.bind(RMI_URL, raf.getRemoteRepository(repository));
     }
 
     protected void tearDown() throws Exception {
+        Naming.unbind(RMI_URL);
+
         // shutdown local repository
         if (repository instanceof JackrabbitRepository) {
             ((JackrabbitRepository) repository).shutdown();
@@ -114,10 +120,6 @@
     }
 
     public void testConnect() throws Exception {
-        // setup remote repository
-        RemoteAdapterFactory raf = new JackrabbitServerAdapterFactory();
-        Naming.bind(RMI_URL, raf.getRemoteRepository(repository));
-
         Map params = new HashMap();
         params.put(org.apache.jackrabbit.rmi.repository.RepositoryFactoryImpl.REPOSITORY_RMI_URL,
RMI_URL);
         Repository r = RepositoryManager.getRepository(params);



Mime
View raw message