jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r312816 - in /incubator/jackrabbit/trunk/src: java/org/apache/jackrabbit/core/config/RepositoryConfig.java test/org/apache/jackrabbit/core/config/RepositoryConfigTest.java
Date Tue, 11 Oct 2005 06:15:32 GMT
Author: jukka
Date: Mon Oct 10 23:15:23 2005
New Revision: 312816

URL: http://svn.apache.org/viewcvs?rev=312816&view=rev
Log:
JCR-248: Added convenience methods for configuration URIs and InputStreams
JCR-73: Improved RepositoryConfig javadocs.
Added RepositoryConfig.create unit tests.

Modified:
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/config/RepositoryConfig.java
    incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/core/config/RepositoryConfigTest.java

Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/config/RepositoryConfig.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/config/RepositoryConfig.java?rev=312816&r1=312815&r2=312816&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/config/RepositoryConfig.java
(original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/config/RepositoryConfig.java
Mon Oct 10 23:15:23 2005
@@ -16,9 +16,15 @@
  */
 package org.apache.jackrabbit.core.config;
 
-import org.apache.jackrabbit.core.fs.FileSystem;
-import org.w3c.dom.Element;
-import org.xml.sax.InputSource;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.InputStream;
+import java.net.URI;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
 
 import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Transformer;
@@ -27,13 +33,10 @@
 import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Properties;
+
+import org.apache.jackrabbit.core.fs.FileSystem;
+import org.w3c.dom.Element;
+import org.xml.sax.InputSource;
 
 /**
  * Repository configuration. This configuration class is used to
@@ -51,35 +54,63 @@
     private static final String WORKSPACE_XML = "workspace.xml";
 
     /**
-     * Parses the given repository configuration file and returns the parsed
-     * repository configuration. The given repository home directory path
-     * will be used as the ${rep.home} parser variable.
+     * Convenience method that wraps the configuration file name into an
+     * {@link InputSource} and invokes the
+     * {@link #create(InputSource, String)} method.
      *
-     * @param file repository configuration file
+     * @param file repository configuration file name
      * @param home repository home directory
      * @return repository configuration
      * @throws ConfigurationException on configuration errors
+     * @see #create(InputSource, String)
      */
     public static RepositoryConfig create(String file, String home)
             throws ConfigurationException {
-        try {
-            File config = new File(file);
+        URI uri = new File(file).toURI();
+        return create(new InputSource(uri.toString()), home);
+    }
 
-            InputSource xml = new InputSource(new FileReader(config));
-            xml.setSystemId(config.toURI().toString());
+    /**
+     * Convenience method that wraps the configuration URI into an
+     * {@link InputSource} and invokes the
+     * {@link #create(InputSource, String)} method.
+     *
+     * @param uri repository configuration URI
+     * @param home repository home directory
+     * @return repository configuration
+     * @throws ConfigurationException on configuration errors
+     * @see #create(InputSource, String)
+     */
+    public static RepositoryConfig create(URI uri, String home)
+            throws ConfigurationException {
+        return create(new InputSource(uri.toString()), home);
+    }
 
-            return create(xml, home);
-        } catch (FileNotFoundException e) {
-            throw new ConfigurationException(
-                    "The repository configuration file " + file
-                    + " could not be found.", e);
-        }
+    /**
+     * Convenience method that wraps the configuration input stream into an
+     * {@link InputSource} and invokes the
+     * {@link #create(InputSource, String)} method.
+     *
+     * @param input repository configuration input stream
+     * @param home repository home directory
+     * @return repository configuration
+     * @throws ConfigurationException on configuration errors
+     * @see #create(InputSource, String)
+     */
+    public static RepositoryConfig create(InputStream input, String home)
+            throws ConfigurationException {
+        return create(new InputSource(input), home);
     }
 
     /**
      * Parses the given repository configuration document and returns the
-     * parsed repository configuration. The given repository home directory
-     * path will be used as the ${rep.home} parser variable.
+     * parsed and initialized repository configuration. The given repository
+     * home directory path will be used as the ${rep.home} parser variable.
+     * <p>
+     * Note that in addition to parsing the repository configuration, this
+     * method also initializes the configuration (creates the configured
+     * directories, etc.). The {@link ConfigurationParser} class should be
+     * used directly to just parse the configuration.
      *
      * @param xml repository configuration document
      * @param home repository home directory

Modified: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/core/config/RepositoryConfigTest.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/core/config/RepositoryConfigTest.java?rev=312816&r1=312815&r2=312816&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/core/config/RepositoryConfigTest.java
(original)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/core/config/RepositoryConfigTest.java
Mon Oct 10 23:15:23 2005
@@ -17,23 +17,28 @@
 package org.apache.jackrabbit.core.config;
 
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
-
-import org.xml.sax.InputSource;
+import java.io.OutputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
 
 import junit.framework.TestCase;
 
+import org.xml.sax.InputSource;
+
 /**
  * Test cases for repository configuration handling.
  */
 public class RepositoryConfigTest extends TestCase {
 
-    private static final String REPOSITORY_XML = "org/apache/jackrabbit/core/config/repository.xml";
+    private static final String REPOSITORY_XML = "target/test-repository.xml";
 
     private static final String REPOSITORY_HOME = "target/test-repository";
 
-    private RepositoryConfig config;
-
     private static void deleteAll(File file) {
         if (file.exists()) {
             if (file.isDirectory()) {
@@ -47,22 +52,129 @@
     }
 
     /**
-     * Sets up the test case by reading the test repository configuration file.
+     * Sets up the test case by creating the repository home directory
+     * and copying the repository configuration file in place.
      */
     protected void setUp() throws Exception {
+        // Create the repository directory
+        File home = new File(REPOSITORY_HOME);
+        home.mkdirs();
+
+        // Copy the repository configuration file in place
         ClassLoader loader = getClass().getClassLoader();
-        InputStream xml = loader.getResourceAsStream(REPOSITORY_XML);
-        config = RepositoryConfig.create(new InputSource(xml), REPOSITORY_HOME);
+        InputStream input = loader.getResourceAsStream(
+                "org/apache/jackrabbit/core/config/repository.xml");
+        try {
+            OutputStream output = new FileOutputStream(REPOSITORY_XML);
+            try {
+                int n;
+                byte[] buffer = new byte[1024];
+                while ((n = input.read(buffer)) != -1) {
+                    output.write(buffer, 0, n);
+                }
+            } finally {
+                output.close();
+            }
+        } finally {
+            input.close();
+        }
     }
 
     protected void tearDown() {
-        deleteAll(new File(REPOSITORY_HOME));
+        File home = new File(REPOSITORY_HOME);
+        deleteAll(home);
+        File config = new File(REPOSITORY_XML);
+        config.delete();
+    }
+
+    /**
+     * Tests that a file name can be used for the configuration.
+     */
+    public void testRepositoryConfigCreateWithFileName() {
+        try {
+            RepositoryConfig.create(REPOSITORY_XML, REPOSITORY_HOME);
+        } catch (ConfigurationException e) {
+            fail("Valid configuration file name");
+        }
+        try {
+            RepositoryConfig.create("invalid-config-file", REPOSITORY_HOME);
+            fail("Invalid configuration file name");
+        } catch (ConfigurationException e) {
+        }
+    }
+
+    /**
+     * Tests that a URI can be used for the configuration.
+     */
+    public void testRepositoryConfigCreateWithURI() throws URISyntaxException {
+        try {
+            URI uri = new File(REPOSITORY_XML).toURI();
+            RepositoryConfig.create(uri, REPOSITORY_HOME);
+        } catch (ConfigurationException e) {
+            fail("Valid configuration URI");
+        }
+        try {
+            URI uri = new URI("invalid://config/uri");
+            RepositoryConfig.create(uri, REPOSITORY_HOME);
+            fail("Invalid configuration URI");
+        } catch (ConfigurationException e) {
+        }
+    }
+
+    /**
+     * Tests that an input stream can be used for the configuration.
+     */
+    public void testRepositoryConfigCreateWithInputStream() throws IOException {
+        InputStream input = new FileInputStream(REPOSITORY_XML);
+        try {
+            RepositoryConfig.create(input, REPOSITORY_HOME);
+        } catch (ConfigurationException e) {
+            fail("Valid configuration input stream");
+        } finally {
+            input.close();
+        }
+        input = new InputStream() {
+            public int read() throws IOException {
+                throw new IOException("invalid input stream");
+            }
+        };
+        try {
+            RepositoryConfig.create(input, REPOSITORY_HOME);
+            fail("Invalid configuration input stream");
+        } catch (ConfigurationException e) {
+        } finally {
+            input.close();
+        }
+    }
+
+    /**
+     * Tests that an InputSource can be used for the configuration.
+     */
+    public void testRepositoryConfigCreateWithInputSource() throws IOException {
+        try {
+            URI uri = new File(REPOSITORY_XML).toURI();
+            InputSource source = new InputSource(uri.toString());
+            RepositoryConfig.create(source, REPOSITORY_HOME);
+        } catch (ConfigurationException e) {
+            fail("Valid configuration input source with file URI");
+        }
+        InputStream stream = new FileInputStream(REPOSITORY_XML);
+        try {
+            InputSource source = new InputSource(stream);
+            RepositoryConfig.create(source, REPOSITORY_HOME);
+        } catch (ConfigurationException e) {
+            fail("Valid configuration input source with input stream");
+        } finally {
+            stream.close();
+        }
     }
 
     /**
      * Test that the repository configuration file is correctly parsed.
      */
     public void testRepositoryConfig() throws Exception {
+        RepositoryConfig config =
+            RepositoryConfig.create(REPOSITORY_XML, REPOSITORY_HOME);
         assertEquals(REPOSITORY_HOME, config.getHomeDir());
         assertEquals("default", config.getDefaultWorkspaceName());
         assertEquals(
@@ -85,13 +197,16 @@
     }
 
     public void testInit() throws Exception {
+        RepositoryConfig.create(REPOSITORY_XML, REPOSITORY_HOME);
         File workspaces_dir = new File(REPOSITORY_HOME, "workspaces");
         File workspace_dir = new File(workspaces_dir, "default");
         File workspace_xml = new File(workspace_dir, "workspace.xml");
-        assertTrue(workspace_xml.exists());
+        assertTrue("Default workspace is created", workspace_xml.exists());
     }
 
     public void testCreateWorkspaceConfig() throws Exception {
+        RepositoryConfig config =
+            RepositoryConfig.create(REPOSITORY_XML, REPOSITORY_HOME);
         config.createWorkspaceConfig("test-workspace");
         File workspaces_dir = new File(REPOSITORY_HOME, "workspaces");
         File workspace_dir = new File(workspaces_dir, "test-workspace");
@@ -101,6 +216,8 @@
 
     public void testCreateDuplicateWorkspaceConfig() throws Exception {
         try {
+            RepositoryConfig config =
+                RepositoryConfig.create(REPOSITORY_XML, REPOSITORY_HOME);
             config.createWorkspaceConfig("default");
             fail("No exception thrown when creating a duplicate workspace");
         } catch (ConfigurationException e) {



Mime
View raw message