[dna-commits] DNA SVN: r906 - in trunk: dna-jcr/src/test/java/org/jboss/dna/jcr and 1 other directories.

dna-commits at lists.jboss.org dna-commits at lists.jboss.org
Fri May 15 12:17:11 EDT 2009


Author: bcarothers
Date: 2009-05-15 12:17:10 -0400 (Fri, 15 May 2009)
New Revision: 906

Added:
   trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrConfigurationTest.java
Modified:
   trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrConfiguration.java
   trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrEngine.java
   trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/InMemoryRepositoryStub.java
   trunk/dna-repository/src/main/java/org/jboss/dna/repository/Configurator.java
   trunk/dna-repository/src/main/java/org/jboss/dna/repository/DnaConfiguration.java
   trunk/dna-repository/src/main/java/org/jboss/dna/repository/DnaLexicon.java
Log:
DNA-400 JcrConfiguration Could Support Setting Options

Applied patch that adds support for with(<Option>).setTo(<Value>) syntax for JcrConfiguration and then converted InMemoryRepositoryStub to use JcrEngine/JcrConfiguration.


Modified: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrConfiguration.java
===================================================================
--- trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrConfiguration.java	2009-05-15 14:01:49 UTC (rev 905)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrConfiguration.java	2009-05-15 16:17:10 UTC (rev 906)
@@ -25,11 +25,14 @@
 
 import org.jboss.dna.common.util.CheckArg;
 import org.jboss.dna.graph.ExecutionContext;
+import org.jboss.dna.graph.Graph;
 import org.jboss.dna.graph.connector.RepositorySource;
 import org.jboss.dna.graph.mimetype.MimeTypeDetector;
+import org.jboss.dna.graph.property.Path;
 import org.jboss.dna.repository.Configurator;
 import org.jboss.dna.repository.DnaConfiguration;
 import org.jboss.dna.repository.DnaConfigurationException;
+import org.jboss.dna.repository.DnaLexicon;
 import org.jboss.dna.repository.Configurator.ChooseClass;
 import org.jboss.dna.repository.Configurator.ConfigRepositoryDetails;
 import org.jboss.dna.repository.Configurator.MimeTypeDetectorDetails;
@@ -58,7 +61,7 @@
     Configurator.RepositoryConfigurator<JcrConfiguration>, Configurator.MimeDetectorConfigurator<JcrConfiguration>,
     Configurator.Builder<JcrEngine> {
 
-    private final DnaConfiguration.Builder<JcrConfiguration> builder;
+    private final JcrConfiguration.Builder<JcrConfiguration> builder;
 
     /**
      * Create a new configuration for DNA.
@@ -74,7 +77,7 @@
      * @throws IllegalArgumentException if the supplied context reference is null
      */
     public JcrConfiguration( ExecutionContext context ) {
-        this.builder = new DnaConfiguration.Builder<JcrConfiguration>(context, this);
+        this.builder = new JcrConfiguration.Builder<JcrConfiguration>(context, this);
     }
 
     /**
@@ -110,7 +113,7 @@
      * 
      * @see org.jboss.dna.repository.Configurator.RepositoryConfigurator#addRepository(java.lang.String)
      */
-    public ChooseClass<RepositorySource, RepositoryDetails<JcrConfiguration>> addRepository( String id ) {
+    public ChooseClass<RepositorySource, JcrRepositoryDetails<JcrConfiguration>> addRepository( String id ) {
         CheckArg.isNotEmpty(id, "id");
         return builder.addRepository(id);
     }
@@ -145,6 +148,10 @@
         return builder.save();
     }
 
+    protected Graph graph() {
+        return builder.getGraph();
+    }
+
     /**
      * {@inheritDoc}
      * 
@@ -154,4 +161,100 @@
         save();
         return new JcrEngine(builder.buildDnaEngine());
     }
+
+    public interface JcrRepositoryDetails<ReturnType>
+        extends RepositoryDetails<ReturnType>, SetOptions<JcrRepositoryDetails<ReturnType>> {
+
+    }
+
+    /**
+     * Interface for configuring the {@link JcrRepository.Options JCR repository options} for a {@link JcrRepository JCR
+     * repository}.
+     * 
+     * @param <ReturnType> the interface returned after the option has been set.
+     */
+    public interface SetOptions<ReturnType> {
+        /**
+         * Specify the repository option that is to be set. The value may be set using the interface returned by this method.
+         * 
+         * @param option the option to be set
+         * @return the interface used to set the value for the property; never null
+         */
+        OptionSetter<ReturnType> with( JcrRepository.Options option );
+    }
+
+    /**
+     * The interface used to set the value for a {@link JcrRepository.Options JCR repository option}.
+     * 
+     * @param <ReturnType> the interface returned from these methods
+     * @see JcrConfiguration.SetOptions#with(org.jboss.dna.jcr.JcrRepository.Options)
+     */
+    public interface OptionSetter<ReturnType> {
+        /**
+         * Set the property value to an integer.
+         * 
+         * @param value the new value for the property
+         * @return the next component to continue configuration; never null
+         */
+        ReturnType setTo( String value );
+    }
+
+    public static class Builder<ReturnType> extends DnaConfiguration.Builder<ReturnType> {
+
+        /**
+         * Specify a new {@link ExecutionContext} that should be used for this DNA instance.
+         * 
+         * @param context the new context, or null if a default-constructed execution context should be used
+         * @param builder the builder object returned from all the methods
+         * @throws IllegalArgumentException if the supplied context reference is null
+         */
+        public Builder( ExecutionContext context,
+                        ReturnType builder ) {
+            super(context, builder);
+        }
+        
+        protected Graph getGraph() {
+            return graph();
+        }
+
+
+
+        /**
+         * {@inheritDoc}
+         * 
+         * @see org.jboss.dna.repository.Configurator.RepositoryConfigurator#addRepository(java.lang.String)
+         */
+        @Override
+        public ChooseClass<RepositorySource, JcrRepositoryDetails<ReturnType>> addRepository( String id ) {
+            CheckArg.isNotEmpty(id, "id");
+            // Now create the "dna:source" node with the supplied id ...
+            Path path = createOrReplaceNode(sourcesPath(), id);
+            JcrRepositoryDetails<ReturnType> details = new JcrGraphRepositoryDetails<ReturnType>(path, builder);
+            return new ClassChooser<RepositorySource, JcrRepositoryDetails<ReturnType>>(path, details);
+        }
+
+        public class JcrGraphRepositoryDetails<RT> extends GraphRepositoryDetails<RT> implements JcrRepositoryDetails<RT> {
+
+            protected JcrGraphRepositoryDetails( Path path,
+                                                 RT returnObject ) {
+                super(path, returnObject);
+            }
+
+            public OptionSetter<JcrRepositoryDetails<RT>> with( final JcrRepository.Options option ) {
+                final Path optionsPath = createOrReplaceNode(path(), DnaLexicon.OPTIONS);
+
+                final JcrRepositoryDetails<RT> details = this;
+                
+                return new OptionSetter<JcrRepositoryDetails<RT>>() {
+                    public JcrRepositoryDetails<RT> setTo( String value ) {
+                        Path optionPath = createOrReplaceNode(optionsPath, option.name());
+                        configuration().set(DnaLexicon.VALUE).to(value).on(optionPath);
+                        
+                        return details; 
+                    }
+                };
+            }
+        }
+
+    }
 }

Modified: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrEngine.java
===================================================================
--- trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrEngine.java	2009-05-15 14:01:49 UTC (rev 905)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrEngine.java	2009-05-15 16:17:10 UTC (rev 906)
@@ -23,7 +23,6 @@
  */
 package org.jboss.dna.jcr;
 
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.concurrent.locks.Lock;
@@ -33,8 +32,15 @@
 import org.jboss.dna.common.collection.Problems;
 import org.jboss.dna.common.util.CheckArg;
 import org.jboss.dna.graph.ExecutionContext;
+import org.jboss.dna.graph.Graph;
+import org.jboss.dna.graph.Location;
+import org.jboss.dna.graph.Node;
 import org.jboss.dna.graph.connector.RepositoryConnectionFactory;
 import org.jboss.dna.graph.connector.RepositorySource;
+import org.jboss.dna.graph.property.Path;
+import org.jboss.dna.graph.property.PathFactory;
+import org.jboss.dna.graph.property.PathNotFoundException;
+import org.jboss.dna.graph.property.Property;
 import org.jboss.dna.jcr.JcrRepository.Options;
 import org.jboss.dna.repository.DnaEngine;
 import org.jboss.dna.repository.RepositoryService;
@@ -108,7 +114,7 @@
      * @throws IllegalArgumentException if the repository name is null, blank or invalid
      * @throws RepositoryException if there is no repository with the specified name
      */
-    public final Repository getRepository( String repositoryName ) throws RepositoryException {
+    public final JcrRepository getRepository( String repositoryName ) throws RepositoryException {
         CheckArg.isNotEmpty(repositoryName, "repositoryName");
         try {
             repositoriesLock.lock();
@@ -131,7 +137,49 @@
     protected JcrRepository doCreateJcrRepository( String repositoryName ) {
         RepositoryConnectionFactory connectionFactory = getRepositoryConnectionFactory();
         Map<String, String> descriptors = null;
-        Map<Options, String> options = Collections.singletonMap(Options.PROJECT_NODE_TYPES, "false");
+
+        /*
+         * Extract the JCR options from the configuration graph
+         */
+        String configurationName = dnaEngine.getRepositoryService().getConfigurationSourceName();
+        Map<Options, String> options = new HashMap<Options, String>();
+
+        PathFactory pathFactory = getExecutionContext().getValueFactories().getPathFactory();
+        Graph configuration = Graph.create(connectionFactory.createConnection(configurationName), getExecutionContext());
+
+        try {
+            Node sources = configuration.getNodeAt(pathFactory.create(DnaLexicon.SOURCES));
+
+            /*
+             * Hopefully, this can all get cleaned up when the connector layer supports queries
+             */
+            for (Location childLocation : sources.getChildren()) {
+                Node source = configuration.getNodeAt(childLocation);
+
+                Property nameProperty = source.getProperty("name");
+                if (nameProperty != null && nameProperty.getFirstValue().toString().equals(repositoryName)) {
+                    for (Location optionsLocation : source.getChildren()) {
+                        if (DnaLexicon.OPTIONS.equals(optionsLocation.getPath().getLastSegment().getName())) {
+                            Node optionsNode = configuration.getNodeAt(optionsLocation);
+
+                            for (Location optionLocation : optionsNode.getChildren()) {
+                                Path.Segment segment = optionLocation.getPath().getLastSegment();
+                                Node optionNode = configuration.getNodeAt(optionLocation);
+                                Property valueProperty = optionNode.getProperty(DnaLexicon.VALUE);
+
+                                options.put(Options.valueOf(segment.getName().getLocalName()),
+                                            valueProperty.getFirstValue().toString());
+
+                            }
+
+                        }
+                    }
+                }
+            }
+        } catch (PathNotFoundException pnfe) {
+            // Must not be any configuration set up
+        }
+
         return new JcrRepository(getExecutionContext(), connectionFactory, repositoryName, descriptors, options);
     }
 

Modified: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/InMemoryRepositoryStub.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/InMemoryRepositoryStub.java	2009-05-15 14:01:49 UTC (rev 905)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/InMemoryRepositoryStub.java	2009-05-15 16:17:10 UTC (rev 906)
@@ -25,16 +25,12 @@
 
 import java.io.File;
 import java.net.URI;
-import java.util.Collections;
-import java.util.Map;
 import java.util.Properties;
 import org.apache.jackrabbit.test.RepositoryStub;
 import org.jboss.dna.common.collection.Problem;
 import org.jboss.dna.graph.ExecutionContext;
 import org.jboss.dna.graph.Graph;
 import org.jboss.dna.graph.Location;
-import org.jboss.dna.graph.connector.RepositoryConnection;
-import org.jboss.dna.graph.connector.RepositoryConnectionFactory;
 import org.jboss.dna.graph.connector.inmemory.InMemoryRepositorySource;
 import org.jboss.dna.graph.io.GraphImporter;
 import org.jboss.dna.graph.property.Path;
@@ -45,6 +41,8 @@
  * Concrete implementation of {@link RepositoryStub} based on DNA-specific configuration.
  */
 public class InMemoryRepositoryStub extends RepositoryStub {
+    private static final String REPOSITORY_SOURCE_NAME = "Test Repository Source";
+    
     private JcrRepository repository;
 
     static {
@@ -64,32 +62,34 @@
         super(env);
 
         // Create the in-memory (DNA) repository
-        final InMemoryRepositorySource source = new InMemoryRepositorySource();
-
-        // Various calls will fail if you do not set a non-null name for the source
-        source.setName("TestRepositorySource");
-
-        ExecutionContext executionContext = new ExecutionContext();
+        JcrEngine engine = new JcrConfiguration()
+            .withConfigurationRepository()
+            .usingClass(InMemoryRepositorySource.class.getName())
+            .loadedFromClasspath()
+            .describedAs("configuration repository")
+            .with("name").setTo("configuration")
+            .and()
+            .addRepository("JCR Repository")
+            .usingClass(InMemoryRepositorySource.class.getName())
+            .loadedFromClasspath()
+            .with(Options.PROJECT_NODE_TYPES).setTo(Boolean.FALSE.toString())
+            .describedAs("JCR Repository")
+            .with("name").setTo(REPOSITORY_SOURCE_NAME)
+            .and().build();
+        engine.start();
+        
+        ExecutionContext executionContext = engine.getExecutionContext();
         executionContext.getNamespaceRegistry().register(TestLexicon.Namespace.PREFIX, TestLexicon.Namespace.URI);
 
-        RepositoryConnectionFactory connectionFactory = new RepositoryConnectionFactory() {
-            public RepositoryConnection createConnection( String sourceName ) {
-                return source.getConnection();
-            }
-        };
+        try {
+            repository = engine.getRepository(REPOSITORY_SOURCE_NAME);
+            RepositoryNodeTypeManager nodeTypes = repository.getRepositoryTypeManager();
 
-        // Wrap a connection to the in-memory (DNA) repository in a (JCR) repository
-        Map<Options, String> options = Collections.singletonMap(Options.PROJECT_NODE_TYPES, "false");
+            // Set up some sample nodes in the graph to match the expected test configuration
+            Graph graph = Graph.create(repository.getRepositorySourceName(), engine.getRepositoryConnectionFactory(), executionContext);
+            GraphImporter importer = new GraphImporter(graph);
+            Path destinationPath = executionContext.getValueFactories().getPathFactory().createRootPath();
 
-        repository = new JcrRepository(executionContext, connectionFactory, source.getName(), null, options);
-        RepositoryNodeTypeManager nodeTypes = repository.getRepositoryTypeManager();
-
-        // Set up some sample nodes in the graph to match the expected test configuration
-        Graph graph = Graph.create(source.getName(), connectionFactory, executionContext);
-        GraphImporter importer = new GraphImporter(graph);
-        Path destinationPath = executionContext.getValueFactories().getPathFactory().createRootPath();
-
-        try {
             CndNodeTypeSource nodeTypeSource = new CndNodeTypeSource("/tck_test_types.cnd");
 
             for (Problem problem : nodeTypeSource.getProblems()) {

Added: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrConfigurationTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrConfigurationTest.java	                        (rev 0)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrConfigurationTest.java	2009-05-15 16:17:10 UTC (rev 906)
@@ -0,0 +1,312 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors. 
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.dna.jcr;
+
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsInstanceOf.instanceOf;
+import static org.hamcrest.core.IsNull.notNullValue;
+import static org.jboss.dna.graph.IsNodeWithChildren.hasChild;
+import static org.jboss.dna.graph.IsNodeWithProperty.hasProperty;
+import static org.junit.Assert.assertThat;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.TimeUnit;
+import org.jboss.dna.graph.ExecutionContext;
+import org.jboss.dna.graph.Subgraph;
+import org.jboss.dna.graph.cache.CachePolicy;
+import org.jboss.dna.graph.cache.ImmutableCachePolicy;
+import org.jboss.dna.graph.connector.RepositoryConnection;
+import org.jboss.dna.graph.connector.inmemory.InMemoryRepositorySource;
+import org.jboss.dna.graph.mimetype.ExtensionBasedMimeTypeDetector;
+import org.jboss.dna.graph.property.Path;
+import org.jboss.dna.jcr.JcrRepository.Options;
+import org.jboss.dna.repository.DnaConfiguration;
+import org.jboss.dna.repository.DnaEngine;
+import org.jboss.dna.repository.RepositoryLibrary;
+import org.junit.Before;
+import org.junit.Test;
+
+public class JcrConfigurationTest {
+
+    private ExecutionContext context;
+    private JcrConfiguration configuration;
+
+    @Before
+    public void beforeEach() {
+        context = new ExecutionContext();
+        configuration = new JcrConfiguration();
+    }
+
+    protected Path.Segment segment( String segment ) {
+        return context.getValueFactories().getPathFactory().createSegment(segment);
+    }
+
+    @Test
+    public void shouldAllowCreatingWithNoArguments() {
+        configuration = new JcrConfiguration();
+    }
+
+    @Test
+    public void shouldAllowCreatingWithSpecifiedExecutionContext() {
+        configuration = new JcrConfiguration(context);
+    }
+
+    @Test
+    public void shouldHaveDefaultConfigurationSourceIfNotSpecified() {
+        assertThat(configuration.graph(), is(notNullValue()));
+    }
+
+    @Test
+    public void shouldAllowAddingRepositorySourceInstance() {
+        UUID rootUuid = UUID.randomUUID();
+        CachePolicy cachePolicy = new ImmutableCachePolicy(100);
+        InMemoryRepositorySource newSource = new InMemoryRepositorySource();
+        newSource.setName("name");
+        newSource.setDefaultCachePolicy(cachePolicy);
+        newSource.setDefaultWorkspaceName("default workspace name");
+        newSource.setRetryLimit(100);
+        newSource.setRootNodeUuid(rootUuid);
+
+        // Update the configuration and save it ...
+        configuration.addRepository(newSource).save();
+
+        // Verify that the graph has been updated correctly ...
+        Subgraph subgraph = configuration.graph().getSubgraphOfDepth(3).at("/");
+        assertThat(subgraph.getNode("/dna:sources"), is(notNullValue()));
+        assertThat(subgraph.getNode("/dna:sources/name"), is(notNullValue()));
+        assertThat(subgraph.getNode("/dna:sources/name"), hasProperty(DnaLexicon.READABLE_NAME, "name"));
+        assertThat(subgraph.getNode("/dna:sources/name"), hasProperty(DnaLexicon.RETRY_LIMIT, 100));
+        assertThat(subgraph.getNode("/dna:sources/name"), hasProperty(DnaLexicon.DEFAULT_CACHE_POLICY, cachePolicy));
+        assertThat(subgraph.getNode("/dna:sources/name"), hasProperty("defaultWorkspaceName", "default workspace name"));
+        assertThat(subgraph.getNode("/dna:sources/name"), hasProperty("rootNodeUuid", rootUuid));
+    }
+
+    @Test
+    public void shouldAllowAddingRepositorySourceByClassNameAndSettingProperties() {
+        // Update the configuration and save it ...
+        configuration.addRepository("Source1")
+                     .usingClass(InMemoryRepositorySource.class.getName())
+                     .loadedFromClasspath()
+                     .describedAs("description")
+                     .with("retryLimit")
+                     .setTo(5)
+                     .and()
+                     .save();
+
+        // Verify that the graph has been updated correctly ...
+        Subgraph subgraph = configuration.graph().getSubgraphOfDepth(3).at("/");
+        assertThat(subgraph.getNode("/dna:sources"), is(notNullValue()));
+        assertThat(subgraph.getNode("/dna:sources/Source1"), is(notNullValue()));
+        assertThat(subgraph.getNode("/dna:sources/Source1"), hasProperty(DnaLexicon.READABLE_NAME, "Source1"));
+        assertThat(subgraph.getNode("/dna:sources/Source1"), hasProperty(DnaLexicon.RETRY_LIMIT, 5));
+        assertThat(subgraph.getNode("/dna:sources/Source1"), hasProperty(DnaLexicon.CLASSNAME,
+                                                                         InMemoryRepositorySource.class.getName()));
+    }
+
+    @Test
+    public void shouldAllowAddingRepositorySourceByClassNameAndClasspathAndSettingProperties() {
+        // Update the configuration and save it ...
+        configuration.addRepository("Source1")
+                     .usingClass(InMemoryRepositorySource.class.getName())
+                     .loadedFrom("cp1", "cp2")
+                     .describedAs("description")
+                     .with("retryLimit")
+                     .setTo(5)
+                     .and()
+                     .save();
+
+        // Verify that the graph has been updated correctly ...
+        Subgraph subgraph = configuration.graph().getSubgraphOfDepth(3).at("/");
+        assertThat(subgraph.getNode("/dna:sources"), is(notNullValue()));
+        assertThat(subgraph.getNode("/dna:sources/Source1"), is(notNullValue()));
+        assertThat(subgraph.getNode("/dna:sources/Source1"), hasProperty(DnaLexicon.READABLE_NAME, "Source1"));
+        assertThat(subgraph.getNode("/dna:sources/Source1"), hasProperty(DnaLexicon.RETRY_LIMIT, 5));
+        assertThat(subgraph.getNode("/dna:sources/Source1"), hasProperty(DnaLexicon.CLASSNAME,
+                                                                         InMemoryRepositorySource.class.getName()));
+        assertThat(subgraph.getNode("/dna:sources/Source1"), hasProperty(DnaLexicon.CLASSPATH, "cp1", "cp2"));
+    }
+
+    @Test
+    public void shouldAllowAddingRepositorySourceByClassReferenceAndSettingProperties() {
+        // Update the configuration and save it ...
+        configuration.addRepository("Source1")
+                     .usingClass(InMemoryRepositorySource.class)
+                     .describedAs("description")
+                     .with("retryLimit")
+                     .setTo(5)
+                     .and()
+                     .save();
+
+        // Verify that the graph has been updated correctly ...
+        Subgraph subgraph = configuration.graph().getSubgraphOfDepth(3).at("/");
+        assertThat(subgraph.getNode("/dna:sources"), is(notNullValue()));
+        assertThat(subgraph.getNode("/dna:sources/Source1"), is(notNullValue()));
+        assertThat(subgraph.getNode("/dna:sources/Source1"), hasProperty(DnaLexicon.READABLE_NAME, "Source1"));
+        assertThat(subgraph.getNode("/dna:sources/Source1"), hasProperty(DnaLexicon.RETRY_LIMIT, 5));
+        assertThat(subgraph.getNode("/dna:sources/Source1"), hasProperty(DnaLexicon.DESCRIPTION, "description"));
+        assertThat(subgraph.getNode("/dna:sources/Source1"), hasProperty(DnaLexicon.CLASSNAME,
+                                                                         InMemoryRepositorySource.class.getName()));
+    }
+
+    @Test
+    public void shouldAllowOverwritingRepositorySourceByRepositoryName() {
+        // Update the configuration and save it ...
+        configuration.addRepository("Source1")
+                     .usingClass(InMemoryRepositorySource.class)
+                     .describedAs("description")
+                     .with("retryLimit")
+                     .setTo(3)
+                     .and()
+                     .addRepository("Source1")
+                     .usingClass(InMemoryRepositorySource.class)
+                     .describedAs("new description")
+                     .with("retryLimit")
+                     .setTo(6)
+                     .and()
+                     .save();
+
+        // Verify that the graph has been updated correctly ...
+        Subgraph subgraph = configuration.graph().getSubgraphOfDepth(3).at("/");
+        assertThat(subgraph.getNode("/dna:sources"), is(notNullValue()));
+        assertThat(subgraph.getNode("/dna:sources").getChildren(), hasChild(segment("Source1")));
+        assertThat(subgraph.getNode("/dna:sources/Source1"), is(notNullValue()));
+        assertThat(subgraph.getNode("/dna:sources/Source1"), hasProperty(DnaLexicon.READABLE_NAME, "Source1"));
+        assertThat(subgraph.getNode("/dna:sources/Source1"), hasProperty(DnaLexicon.RETRY_LIMIT, 6));
+        assertThat(subgraph.getNode("/dna:sources/Source1"), hasProperty(DnaLexicon.DESCRIPTION, "new description"));
+        assertThat(subgraph.getNode("/dna:sources/Source1"), hasProperty(DnaLexicon.CLASSNAME,
+                                                                         InMemoryRepositorySource.class.getName()));
+    }
+
+    @Test
+    public void shouldAllowAddingMimeTypeDetector() {
+        // Update the configuration and save it ...
+        configuration.addRepository("Source1")
+                     .usingClass(InMemoryRepositorySource.class)
+                     .describedAs("description")
+                     .and()
+                     .addMimeTypeDetector("detector")
+                     .usingClass(ExtensionBasedMimeTypeDetector.class)
+                     .describedAs("default detector")
+                     .and()
+                     .save();
+
+        // Verify that the graph has been updated correctly ...
+        Subgraph subgraph = configuration.graph().getSubgraphOfDepth(3).at("/");
+        assertThat(subgraph.getNode("/dna:sources").getChildren(), hasChild(segment("Source1")));
+        assertThat(subgraph.getNode("/dna:sources/Source1"), is(notNullValue()));
+        assertThat(subgraph.getNode("/dna:sources/Source1"), hasProperty(DnaLexicon.READABLE_NAME, "Source1"));
+        assertThat(subgraph.getNode("/dna:sources/Source1"), hasProperty(DnaLexicon.DESCRIPTION, "description"));
+        assertThat(subgraph.getNode("/dna:sources/Source1"), hasProperty(DnaLexicon.CLASSNAME,
+                                                                         InMemoryRepositorySource.class.getName()));
+        assertThat(subgraph.getNode("/dna:mimeTypeDetectors").getChildren(), hasChild(segment("detector")));
+        assertThat(subgraph.getNode("/dna:mimeTypeDetectors/detector"), is(notNullValue()));
+        assertThat(subgraph.getNode("/dna:mimeTypeDetectors/detector"), hasProperty(DnaLexicon.READABLE_NAME, "detector"));
+        assertThat(subgraph.getNode("/dna:mimeTypeDetectors/detector"), hasProperty(DnaLexicon.DESCRIPTION, "default detector"));
+        assertThat(subgraph.getNode("/dna:mimeTypeDetectors/detector"),
+                   hasProperty(DnaLexicon.CLASSNAME, ExtensionBasedMimeTypeDetector.class.getName()));
+    }
+
+    @Test
+    public void shouldAllowConfigurationInMultipleSteps() {
+        configuration.addRepository("Source1").usingClass(InMemoryRepositorySource.class).describedAs("description");
+        configuration.addMimeTypeDetector("detector")
+                     .usingClass(ExtensionBasedMimeTypeDetector.class)
+                     .describedAs("default detector");
+        configuration.save();
+
+        // Verify that the graph has been updated correctly ...
+        Subgraph subgraph = configuration.graph().getSubgraphOfDepth(3).at("/");
+        assertThat(subgraph.getNode("/dna:sources").getChildren(), hasChild(segment("Source1")));
+        assertThat(subgraph.getNode("/dna:sources/Source1"), is(notNullValue()));
+        assertThat(subgraph.getNode("/dna:sources/Source1"), hasProperty(DnaLexicon.READABLE_NAME, "Source1"));
+        assertThat(subgraph.getNode("/dna:sources/Source1"), hasProperty(DnaLexicon.DESCRIPTION, "description"));
+        assertThat(subgraph.getNode("/dna:sources/Source1"), hasProperty(DnaLexicon.CLASSNAME,
+                                                                         InMemoryRepositorySource.class.getName()));
+        assertThat(subgraph.getNode("/dna:mimeTypeDetectors").getChildren(), hasChild(segment("detector")));
+        assertThat(subgraph.getNode("/dna:mimeTypeDetectors/detector"), is(notNullValue()));
+        assertThat(subgraph.getNode("/dna:mimeTypeDetectors/detector"), hasProperty(DnaLexicon.READABLE_NAME, "detector"));
+        assertThat(subgraph.getNode("/dna:mimeTypeDetectors/detector"), hasProperty(DnaLexicon.DESCRIPTION, "default detector"));
+        assertThat(subgraph.getNode("/dna:mimeTypeDetectors/detector"),
+                   hasProperty(DnaLexicon.CLASSNAME, ExtensionBasedMimeTypeDetector.class.getName()));
+    }
+
+    @Test
+    public void shouldAllowSpecifyingOptions() throws Exception {
+        // Update the configuration and save it ...
+
+        JcrEngine engine = configuration.withConfigurationRepository()
+                     .usingClass(InMemoryRepositorySource.class.getName())
+                     .loadedFromClasspath()
+                     .describedAs("Configuration Repository")
+                     .with("name").setTo("configuration")
+                     .with("retryLimit")
+                     .setTo(5)
+                     .and()
+                     .addRepository("Source2")
+                     .usingClass(InMemoryRepositorySource.class.getName())
+                     .loadedFromClasspath()
+                     .with(Options.JAAS_LOGIN_CONFIG_NAME).setTo("test")
+                     .describedAs("description")
+                     .with("name").setTo("JCR Repository")
+                     .and()
+                     .build();
+        engine.start();
+
+        // Verify that the graph has been updated correctly ...
+        Subgraph subgraph = configuration.graph().getSubgraphOfDepth(3).at("/");
+        assertThat(subgraph.getNode("/dna:sources"), is(notNullValue()));
+        assertThat(subgraph.getNode("/dna:sources/Source2"), is(notNullValue()));
+        assertThat(subgraph.getNode("/dna:sources/Source2"), hasProperty(DnaLexicon.READABLE_NAME, "Source2"));
+        assertThat(subgraph.getNode("/dna:sources/Source2"), hasProperty(DnaLexicon.CLASSNAME,
+                                                                         InMemoryRepositorySource.class.getName()));
+        
+        JcrRepository repository = engine.getRepository("JCR Repository");
+        
+        Map<Options, String> options = new HashMap<Options, String>();
+        options.put(Options.JAAS_LOGIN_CONFIG_NAME, "test");
+        options.put(Options.PROJECT_NODE_TYPES, "false");
+        assertThat(repository.getOptions(), is(options));
+    }
+    
+    @Test
+    public void shouldAllowCreatingWithConfigRepository() throws InterruptedException {
+        DnaEngine engine = new DnaConfiguration().withConfigurationRepository()
+                                       .usingClass(InMemoryRepositorySource.class)
+                                       .describedAs("Configuration Repository")
+                                       .with("name")
+                                       .setTo("config repo")
+                                       .and()
+                                       .build();
+
+        assertThat(engine.getRepositorySource("config repo"), is(notNullValue()));
+        assertThat(engine.getRepositorySource("config repo"), is(instanceOf(InMemoryRepositorySource.class)));
+
+        RepositoryLibrary library = engine.getRepositoryService().getRepositorySourceManager();
+        assertThat(library.getConnectionPool("config repo").getInUseCount(), is(0));
+
+        RepositoryConnection connection = library.getConnectionPool("config repo").getConnection();
+        assertThat(connection.ping(500, TimeUnit.MILLISECONDS), is(true));
+        connection.close();
+
+    }
+}


Property changes on: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrConfigurationTest.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF

Modified: trunk/dna-repository/src/main/java/org/jboss/dna/repository/Configurator.java
===================================================================
--- trunk/dna-repository/src/main/java/org/jboss/dna/repository/Configurator.java	2009-05-15 14:01:49 UTC (rev 905)
+++ trunk/dna-repository/src/main/java/org/jboss/dna/repository/Configurator.java	2009-05-15 16:17:10 UTC (rev 906)
@@ -98,7 +98,7 @@
          * @throws IllegalArgumentException if the repository name is null, empty, or otherwise invalid
          * @see #addRepository(RepositorySource)
          */
-        public ChooseClass<RepositorySource, RepositoryDetails<ReturnType>> addRepository( final String id );
+        public ChooseClass<RepositorySource, ? extends RepositoryDetails<ReturnType>> addRepository( final String id );
 
         /**
          * Add a new {@link RepositorySource repository} for this configuration. The new repository will have the supplied name,
@@ -518,8 +518,16 @@
         Path path = pathFactory().create(parentPath, id);
         configuration().create(path).with(DnaLexicon.READABLE_NAME, id).and();
         return path;
+
     }
 
+    protected Path createOrReplaceNode( Path parentPath,
+                                        Name id ) {
+        Path path = pathFactory().create(parentPath, id);
+        configuration().create(path).with(DnaLexicon.READABLE_NAME, id).and();
+        return path;
+    }
+    
     protected void recordBeanPropertiesInGraph( Path path,
                                                 Object javaBean ) {
         Reflection reflector = new Reflection(javaBean.getClass());
@@ -652,7 +660,7 @@
         protected final Path pathOfComponentNode;
         protected final ReturnType returnObject;
 
-        protected ClassChooser( Path pathOfComponentNode,
+        public ClassChooser( Path pathOfComponentNode,
                                 ReturnType returnObject ) {
             assert pathOfComponentNode != null;
             assert returnObject != null;
@@ -835,6 +843,10 @@
             this.returnObject = returnObject;
         }
 
+        protected Path path() {
+            return this.path;
+        }
+        
         /**
          * {@inheritDoc}
          * 

Modified: trunk/dna-repository/src/main/java/org/jboss/dna/repository/DnaConfiguration.java
===================================================================
--- trunk/dna-repository/src/main/java/org/jboss/dna/repository/DnaConfiguration.java	2009-05-15 14:01:49 UTC (rev 905)
+++ trunk/dna-repository/src/main/java/org/jboss/dna/repository/DnaConfiguration.java	2009-05-15 16:17:10 UTC (rev 906)
@@ -105,7 +105,7 @@
      * 
      * @see org.jboss.dna.repository.Configurator.RepositoryConfigurator#addRepository(java.lang.String)
      */
-    public ChooseClass<RepositorySource, RepositoryDetails<DnaConfiguration>> addRepository( String id ) {
+    public ChooseClass<RepositorySource, ? extends RepositoryDetails<DnaConfiguration>> addRepository( String id ) {
         return builder.addRepository(id);
     }
 
@@ -248,7 +248,7 @@
          * 
          * @see org.jboss.dna.repository.Configurator.RepositoryConfigurator#addRepository(java.lang.String)
          */
-        public ChooseClass<RepositorySource, RepositoryDetails<ReturnType>> addRepository( String id ) {
+        public ChooseClass<RepositorySource, ? extends RepositoryDetails<ReturnType>> addRepository( String id ) {
             CheckArg.isNotEmpty(id, "id");
             // Now create the "dna:source" node with the supplied id ...
             Path path = createOrReplaceNode(sourcesPath(), id);

Modified: trunk/dna-repository/src/main/java/org/jboss/dna/repository/DnaLexicon.java
===================================================================
--- trunk/dna-repository/src/main/java/org/jboss/dna/repository/DnaLexicon.java	2009-05-15 14:01:49 UTC (rev 905)
+++ trunk/dna-repository/src/main/java/org/jboss/dna/repository/DnaLexicon.java	2009-05-15 16:17:10 UTC (rev 906)
@@ -40,6 +40,8 @@
     public static final Name PATH_EXPRESSIONS = new BasicName(Namespace.URI, "pathExpressions");
     public static final Name MIME_TYPE_DETECTORS = new BasicName(Namespace.URI, "mimeTypeDetectors");
     public static final Name MIME_TYPE_DETECTOR = new BasicName(Namespace.URI, "mimeTypeDetector");
+    public static final Name OPTIONS = new BasicName(Namespace.URI, "options");
+    public static final Name VALUE = new BasicName(Namespace.URI, "value");
     public static final Name RETRY_LIMIT = new BasicName(Namespace.URI, "retryLimit");
     public static final Name DEFAULT_CACHE_POLICY = new BasicName(Namespace.URI, "defaultCachePolicy");
 }




More information about the dna-commits mailing list