[dna-commits] DNA SVN: r1070 - trunk/dna-jcr/src/test/java/org/jboss/dna/jcr.

dna-commits at lists.jboss.org dna-commits at lists.jboss.org
Sat Jul 4 09:16:05 EDT 2009


Author: bcarothers
Date: 2009-07-04 09:16:05 -0400 (Sat, 04 Jul 2009)
New Revision: 1070

Added:
   trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/AbstractJcrTest.java
Modified:
   trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/ItemDefinitionTest.java
   trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrPropertyDefinitionTest.java
   trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrSessionTest.java
   trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrWorkspaceTest.java
   trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/MixinTest.java
   trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/RepositoryNodeTypeManagerTest.java
   trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/TypeRegistrationTest.java
Log:
DNA-472 Reduce Code Duplication for Test Setup

Committed patch that sets up an abstract base class with hooks for customization on a per test case basis. Net reduction of about 500 lines of repeated code in the test library.

Added: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/AbstractJcrTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/AbstractJcrTest.java	                        (rev 0)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/AbstractJcrTest.java	2009-07-04 13:16:05 UTC (rev 1070)
@@ -0,0 +1,126 @@
+package org.jboss.dna.jcr;
+
+import static org.mockito.Mockito.stub;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.EnumMap;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import javax.jcr.RepositoryException;
+import org.jboss.dna.graph.ExecutionContext;
+import org.jboss.dna.graph.Graph;
+import org.jboss.dna.graph.MockSecurityContext;
+import org.jboss.dna.graph.SecurityContext;
+import org.jboss.dna.graph.connector.RepositoryConnection;
+import org.jboss.dna.graph.connector.RepositoryConnectionFactory;
+import org.jboss.dna.graph.connector.RepositorySourceException;
+import org.jboss.dna.graph.connector.inmemory.InMemoryRepositorySource;
+import org.jboss.dna.graph.property.NamespaceRegistry;
+import org.jboss.dna.jcr.nodetype.NodeTypeTemplate;
+import org.mockito.MockitoAnnotations;
+import org.mockito.MockitoAnnotations.Mock;
+
+public abstract class AbstractJcrTest {
+
+    
+    protected String workspaceName;
+    protected ExecutionContext context;
+    protected InMemoryRepositorySource source;
+    protected JcrWorkspace workspace;
+    protected JcrSession session;
+    protected Graph graph;
+    protected RepositoryConnectionFactory connectionFactory;
+    protected RepositoryNodeTypeManager repoTypeManager;
+    protected Map<String, Object> sessionAttributes;
+    protected Map<JcrRepository.Option, String> options;
+    protected NamespaceRegistry registry;
+    @Mock
+    protected JcrRepository repository;
+
+    protected void beforeEach() throws Exception {
+        MockitoAnnotations.initMocks(this);
+
+        workspaceName = "workspace1";
+        final String repositorySourceName = "repository";
+
+        // Set up the source ...
+        source = new InMemoryRepositorySource();
+        source.setName(workspaceName);
+        source.setDefaultWorkspaceName(workspaceName);
+
+        // Set up the execution context ...
+        context = new ExecutionContext();
+        // Register the test namespace
+        context.getNamespaceRegistry().register(TestLexicon.Namespace.PREFIX, TestLexicon.Namespace.URI);
+
+        // Set up the initial content ...
+        graph = Graph.create(source, context);
+        initializeContent();
+        
+        // Stub out the connection factory ...
+        connectionFactory = new RepositoryConnectionFactory() {
+            /**
+             * {@inheritDoc}
+             * 
+             * @see org.jboss.dna.graph.connector.RepositoryConnectionFactory#createConnection(java.lang.String)
+             */
+            @SuppressWarnings( "synthetic-access" )
+            public RepositoryConnection createConnection( String sourceName ) throws RepositorySourceException {
+                return repositorySourceName.equals(sourceName) ? source.getConnection() : null;
+            }
+        };
+
+        // Stub out the repository, since we only need a few methods ...
+        repoTypeManager = new RepositoryNodeTypeManager(context);
+
+        try {
+            this.repoTypeManager.registerNodeTypes(new CndNodeTypeSource(new String[] {"/org/jboss/dna/jcr/jsr_170_builtins.cnd",
+                "/org/jboss/dna/jcr/dna_builtins.cnd"}));
+            this.repoTypeManager.registerNodeTypes(new NodeTemplateNodeTypeSource(getTestTypes()));
+
+        } catch (RepositoryException re) {
+            re.printStackTrace();
+            throw new IllegalStateException("Could not load node type definition files", re);
+        } catch (IOException ioe) {
+            ioe.printStackTrace();
+            throw new IllegalStateException("Could not access node type definition files", ioe);
+        }
+
+        stub(repository.getRepositoryTypeManager()).toReturn(repoTypeManager);
+        stub(repository.getRepositorySourceName()).toReturn(repositorySourceName);
+        stub(repository.getConnectionFactory()).toReturn(connectionFactory);
+
+        initializeOptions();
+        stub(repository.getOptions()).toReturn(options);
+
+        // Set up the session attributes ...
+        // Set up the session attributes ...
+        sessionAttributes = new HashMap<String, Object>();
+        sessionAttributes.put("attribute1", "value1");
+
+        // Now create the workspace ...
+        SecurityContext mockSecurityContext = new MockSecurityContext(null,
+                                                                      Collections.singleton(JcrSession.DNA_WRITE_PERMISSION));
+        workspace = new JcrWorkspace(repository, workspaceName, context.with(mockSecurityContext), sessionAttributes);
+
+        // Create the session and log in ...
+        session = (JcrSession)workspace.getSession();
+        registry = session.getExecutionContext().getNamespaceRegistry();
+    }
+
+    protected List<NodeTypeTemplate> getTestTypes() {
+        return Collections.emptyList();
+    }
+
+    protected void initializeContent() {
+
+    }
+    
+    protected void initializeOptions() {
+        // Stub out the repository options ...
+        options = new EnumMap<JcrRepository.Option, String>(JcrRepository.Option.class);
+        options.put(JcrRepository.Option.PROJECT_NODE_TYPES, Boolean.FALSE.toString());
+
+    }
+}


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

Modified: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/ItemDefinitionTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/ItemDefinitionTest.java	2009-07-02 20:30:08 UTC (rev 1069)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/ItemDefinitionTest.java	2009-07-04 13:16:05 UTC (rev 1070)
@@ -28,37 +28,24 @@
 import static org.hamcrest.core.IsNull.nullValue;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.stub;
-import java.io.IOException;
 import java.util.Arrays;
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 import javax.jcr.PropertyType;
-import javax.jcr.RepositoryException;
 import javax.jcr.Value;
-import org.jboss.dna.graph.ExecutionContext;
-import org.jboss.dna.graph.Graph;
-import org.jboss.dna.graph.connector.RepositoryConnection;
-import org.jboss.dna.graph.connector.RepositoryConnectionFactory;
-import org.jboss.dna.graph.connector.RepositorySourceException;
-import org.jboss.dna.graph.connector.inmemory.InMemoryRepositorySource;
 import org.jboss.dna.graph.property.Name;
-import org.jboss.dna.graph.property.ValueFactory;
+import org.jboss.dna.graph.property.NameFactory;
 import org.jboss.dna.graph.property.basic.BasicName;
 import org.jboss.dna.jcr.nodetype.NodeTypeTemplate;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
-import org.mockito.MockitoAnnotations;
-import org.mockito.MockitoAnnotations.Mock;
 
 /**
  * BDD test cases for property and child node definition inheritance. Could be part of RepositoryNodeTypeManagerTest, but split
  * off to isolate tests for this behavior vs. projection/inference and registration/unregistration behavior.
  */
-public class ItemDefinitionTest {
+public class ItemDefinitionTest extends AbstractJcrTest {
 
     private static final Name NODE_TYPE_A = new BasicName(TestLexicon.Namespace.URI, "nodeA");
     private static final Name NODE_TYPE_B = new BasicName(TestLexicon.Namespace.URI, "nodeB");
@@ -67,85 +54,22 @@
     private static final Name SINGLE_PROP1 = new BasicName(TestLexicon.Namespace.URI, "singleProp1");
     private static final Name SINGLE_PROP2 = new BasicName(TestLexicon.Namespace.URI, "singleProp2");
 
-    private String workspaceName;
-    protected ExecutionContext context;
-    private InMemoryRepositorySource source;
-    private JcrWorkspace workspace;
-    private JcrSession session;
-    private Graph graph;
-    private RepositoryConnectionFactory connectionFactory;
-    private RepositoryNodeTypeManager repoTypeManager;
-    private Map<String, Object> sessionAttributes;
-    private ValueFactory<Name> nameFactory;
-    @Mock
-    private JcrRepository repository;
+    protected NameFactory nameFactory;
 
+    @Override
     @Before
     public void beforeEach() throws Exception {
-        MockitoAnnotations.initMocks(this);
+        super.beforeEach();
 
-        workspaceName = "workspace1";
-        final String repositorySourceName = "repository";
-
-        // Set up the source ...
-        source = new InMemoryRepositorySource();
-        source.setName(workspaceName);
-        source.setDefaultWorkspaceName(workspaceName);
-
-        // Set up the execution context ...
-        context = new ExecutionContext();
-        // Register the test namespace
-        context.getNamespaceRegistry().register(TestLexicon.Namespace.PREFIX, TestLexicon.Namespace.URI);
-
-        // Set up the initial content ...
-        graph = Graph.create(source, context);
-
-        // Make sure the path to the namespaces exists ...
-        graph.create("/jcr:system").and().create("/jcr:system/dna:namespaces");
-
-        // Stub out the connection factory ...
-        connectionFactory = new RepositoryConnectionFactory() {
-            /**
-             * {@inheritDoc}
-             * 
-             * @see org.jboss.dna.graph.connector.RepositoryConnectionFactory#createConnection(java.lang.String)
-             */
-            @SuppressWarnings( "synthetic-access" )
-            public RepositoryConnection createConnection( String sourceName ) throws RepositorySourceException {
-                return repositorySourceName.equals(sourceName) ? source.getConnection() : null;
-            }
-        };
-
-        // Stub out the repository, since we only need a few methods ...
-        repoTypeManager = new RepositoryNodeTypeManager(context);
-        try {
-            this.repoTypeManager.registerNodeTypes(new CndNodeTypeSource(new String[] {"/org/jboss/dna/jcr/jsr_170_builtins.cnd",
-                "/org/jboss/dna/jcr/dna_builtins.cnd"}));
-            this.repoTypeManager.registerNodeTypes(new NodeTemplateNodeTypeSource(getTestTypes()));
-        } catch (RepositoryException re) {
-            re.printStackTrace();
-            throw new IllegalStateException("Could not load node type definition files", re);
-        } catch (IOException ioe) {
-            ioe.printStackTrace();
-            throw new IllegalStateException("Could not access node type definition files", ioe);
-        }
-
-        stub(repository.getRepositoryTypeManager()).toReturn(repoTypeManager);
-        stub(repository.getRepositorySourceName()).toReturn(repositorySourceName);
-        stub(repository.getConnectionFactory()).toReturn(connectionFactory);
-
-        // Set up the session attributes ...
-        sessionAttributes = new HashMap<String, Object>();
-
-        // Now create the workspace ...
-        workspace = new JcrWorkspace(repository, workspaceName, context, sessionAttributes);
-
-        // Create the session and log in ...
-        session = (JcrSession)workspace.getSession();
-
         nameFactory = session.getExecutionContext().getValueFactories().getNameFactory();
     }
 
+    @Override
+    protected void initializeContent() {
+        graph.create("/jcr:system").and().create("/jcr:system/dna:namespaces");
+        
+    }
+    
     @After
     public void after() throws Exception {
         if (session != null && session.isLive()) {
@@ -267,7 +191,8 @@
     *      dnatest:singleProp2 of type LONG (note the double-definition)
     */
 
-    private List<NodeTypeTemplate> getTestTypes() {
+    @Override
+    protected List<NodeTypeTemplate> getTestTypes() {
         NodeTypeTemplate nodeA = new JcrNodeTypeTemplate(context);
         nodeA.setName("dnatest:nodeA");
 

Modified: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrPropertyDefinitionTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrPropertyDefinitionTest.java	2009-07-02 20:30:08 UTC (rev 1069)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrPropertyDefinitionTest.java	2009-07-04 13:16:05 UTC (rev 1070)
@@ -26,27 +26,14 @@
 import static org.hamcrest.core.Is.is;
 import static org.hamcrest.core.IsNull.notNullValue;
 import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.stub;
-import java.io.IOException;
 import java.util.Arrays;
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 import javax.jcr.PropertyType;
-import javax.jcr.RepositoryException;
 import javax.jcr.Value;
 import javax.jcr.nodetype.NodeType;
 import javax.jcr.nodetype.NodeTypeManager;
 import javax.jcr.nodetype.PropertyDefinition;
-import org.jboss.dna.graph.ExecutionContext;
-import org.jboss.dna.graph.Graph;
-import org.jboss.dna.graph.MockSecurityContext;
-import org.jboss.dna.graph.SecurityContext;
-import org.jboss.dna.graph.connector.RepositoryConnection;
-import org.jboss.dna.graph.connector.RepositoryConnectionFactory;
-import org.jboss.dna.graph.connector.RepositorySourceException;
-import org.jboss.dna.graph.connector.inmemory.InMemoryRepositorySource;
 import org.jboss.dna.graph.property.Name;
 import org.jboss.dna.graph.property.NamespaceRegistry;
 import org.jboss.dna.jcr.nodetype.NodeTypeTemplate;
@@ -54,14 +41,12 @@
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
-import org.mockito.MockitoAnnotations;
-import org.mockito.MockitoAnnotations.Mock;
 
 /**
  * Indirectly tests the JcrConstaintCheckerFactory through {@link JcrPropertyDefinition#satisfiesConstraints(Value)}, which
  * provides the wrapper around the factory that the rest of the API is expected to utilize.
  */
-public class JcrPropertyDefinitionTest {
+public class JcrPropertyDefinitionTest extends AbstractJcrTest {
 
     protected final String[] EXPECTED_BINARY_CONSTRAINTS = new String[] {"[,5)", "[10, 20)", "(30,40]", "[50,]"};
     protected final String[] EXPECTED_DATE_CONSTRAINTS = new String[] {"[,+1945-08-01T01:30:00.000Z]",
@@ -73,92 +58,26 @@
     protected final String[] EXPECTED_REFERENCE_CONSTRAINTS = new String[] {"dna:root"};
     protected final String[] EXPECTED_STRING_CONSTRAINTS = new String[] {"foo", "bar*", ".*baz"};
 
-    private String workspaceName;
-    private ExecutionContext context;
-    private InMemoryRepositorySource source;
-    private JcrWorkspace workspace;
-    private JcrSession session;
-    private Graph graph;
-    private RepositoryConnectionFactory connectionFactory;
-    private RepositoryNodeTypeManager repoTypeManager;
-    private NodeTypeManager nodeTypeManager;
-    private Map<String, Object> sessionAttributes;
-    @Mock
-    private JcrRepository repository;
+    protected NodeTypeManager nodeTypeManager;
 
+
+    @Override
     @Before
     public void beforeEach() throws Exception {
-        MockitoAnnotations.initMocks(this);
+        super.beforeEach();
 
-        workspaceName = "workspace1";
-        final String repositorySourceName = "repository";
+        nodeTypeManager = workspace.getNodeTypeManager();
+    }
 
-        // Set up the source ...
-        source = new InMemoryRepositorySource();
-        source.setName(workspaceName);
-        source.setDefaultWorkspaceName(workspaceName);
-
-        // Set up the execution context ...
-        context = new ExecutionContext();
-        // Register the test namespace
-        context.getNamespaceRegistry().register(TestLexicon.Namespace.PREFIX, TestLexicon.Namespace.URI);
-
-        // Set up the initial content ...
-        graph = Graph.create(source, context);
-
-        // Make sure the path to the namespaces exists ...
+    @Override
+    protected void initializeContent() {
         graph.create("/jcr:system").and().create("/jcr:system/dna:namespaces");
         graph.create("/a").and().create("/a/b").and().create("/a/b/c");
 
         graph.set("jcr:mixinTypes").on("/a").to(JcrMixLexicon.REFERENCEABLE);
 
-        // Stub out the connection factory ...
-        connectionFactory = new RepositoryConnectionFactory() {
-            /**
-             * {@inheritDoc}
-             * 
-             * @see org.jboss.dna.graph.connector.RepositoryConnectionFactory#createConnection(java.lang.String)
-             */
-            @SuppressWarnings( "synthetic-access" )
-            public RepositoryConnection createConnection( String sourceName ) throws RepositorySourceException {
-                return repositorySourceName.equals(sourceName) ? source.getConnection() : null;
-            }
-        };
-
-        // Stub out the repository, since we only need a few methods ...
-        repoTypeManager = new RepositoryNodeTypeManager(context);
-
-        try {
-            this.repoTypeManager.registerNodeTypes(new CndNodeTypeSource(new String[] {"/org/jboss/dna/jcr/jsr_170_builtins.cnd",
-                "/org/jboss/dna/jcr/dna_builtins.cnd"}));
-            this.repoTypeManager.registerNodeTypes(new NodeTemplateNodeTypeSource(getTestTypes()));
-
-        } catch (RepositoryException re) {
-            re.printStackTrace();
-            throw new IllegalStateException("Could not load node type definition files", re);
-        } catch (IOException ioe) {
-            ioe.printStackTrace();
-            throw new IllegalStateException("Could not access node type definition files", ioe);
-        }
-
-        stub(repository.getRepositoryTypeManager()).toReturn(repoTypeManager);
-        stub(repository.getRepositorySourceName()).toReturn(repositorySourceName);
-        stub(repository.getConnectionFactory()).toReturn(connectionFactory);
-
-        // Set up the session attributes ...
-        sessionAttributes = new HashMap<String, Object>();
-        sessionAttributes.put("attribute1", "value1");
-
-        // Now create the workspace ...
-        SecurityContext mockSecurityContext = new MockSecurityContext("testuser", Collections.singleton(JcrSession.DNA_WRITE_PERMISSION));
-        workspace = new JcrWorkspace(repository, workspaceName, context.with(mockSecurityContext), sessionAttributes);
-
-        // Create the session and log in ...
-        session = (JcrSession)workspace.getSession();
-
-        nodeTypeManager = workspace.getNodeTypeManager();
     }
-
+    
     @After
     public void after() throws Exception {
         if (session != null && session.isLive()) {
@@ -650,7 +569,8 @@
         assertThat(satisfiesConstraints(prop, new Value[] {value}), is(false));
     }
 
-    private List<NodeTypeTemplate> getTestTypes() {
+    @Override
+    protected List<NodeTypeTemplate> getTestTypes() {
         NodeTypeTemplate constrainedType = new JcrNodeTypeTemplate(this.context);
         constrainedType.setName("dnatest:constrainedType");
 

Modified: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrSessionTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrSessionTest.java	2009-07-02 20:30:08 UTC (rev 1069)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrSessionTest.java	2009-07-04 13:16:05 UTC (rev 1070)
@@ -35,14 +35,12 @@
 import static org.mockito.Mockito.stub;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
-import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.security.Principal;
 import java.util.Calendar;
 import java.util.Collections;
 import java.util.HashMap;
-import java.util.Map;
 import java.util.UUID;
 import javax.jcr.Item;
 import javax.jcr.NamespaceException;
@@ -58,57 +56,29 @@
 import javax.jcr.nodetype.NodeType;
 import javax.security.auth.Subject;
 import javax.security.auth.login.LoginContext;
-import org.jboss.dna.graph.ExecutionContext;
-import org.jboss.dna.graph.Graph;
 import org.jboss.dna.graph.JaasSecurityContext;
-import org.jboss.dna.graph.MockSecurityContext;
-import org.jboss.dna.graph.SecurityContext;
-import org.jboss.dna.graph.connector.RepositoryConnection;
-import org.jboss.dna.graph.connector.RepositoryConnectionFactory;
-import org.jboss.dna.graph.connector.RepositorySourceException;
-import org.jboss.dna.graph.connector.inmemory.InMemoryRepositorySource;
 import org.jboss.dna.graph.property.Path;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mockito;
-import org.mockito.MockitoAnnotations;
-import org.mockito.MockitoAnnotations.Mock;
 
 /**
  * @author jverhaeg
  */
-public class JcrSessionTest {
+public class JcrSessionTest extends AbstractJcrTest {
 
     private static final String MULTI_LINE_VALUE = "Line\t1\nLine 2\rLine 3\r\nLine 4";
 
-    private String workspaceName;
-    private ExecutionContext context;
-    private InMemoryRepositorySource source;
-    private JcrWorkspace workspace;
-    private JcrSession session;
-    private Graph graph;
-    private RepositoryConnectionFactory connectionFactory;
-    private RepositoryNodeTypeManager repoTypeManager;
-    private Map<String, Object> sessionAttributes;
-    @Mock
-    private JcrRepository repository;
-
+    @Override
     @Before
     public void beforeEach() throws Exception {
-        workspaceName = "workspace1";
-        final String repositorySourceName = "repository";
+        super.beforeEach();
 
-        // Set up the source ...
-        source = new InMemoryRepositorySource();
-        source.setName(workspaceName);
-        source.setDefaultWorkspaceName(workspaceName);
+    }
 
-        // Set up the execution context ...
-        context = new ExecutionContext();
-
-        // Set up the initial content ...
-        graph = Graph.create(source, context);
+    @Override
+    protected void initializeContent() {
         graph.create("/a").and().create("/a/b").and().create("/a/b/c");
         graph.set("booleanProperty").on("/a/b").to(true);
         graph.set("stringProperty").on("/a/b/c").to("value");
@@ -119,50 +89,9 @@
         // Make sure the path to the namespaces exists ...
         graph.create("/jcr:system").and().create("/jcr:system/dna:namespaces");
 
-        // Stub out the connection factory ...
-        connectionFactory = new RepositoryConnectionFactory() {
-            /**
-             * {@inheritDoc}
-             * 
-             * @see org.jboss.dna.graph.connector.RepositoryConnectionFactory#createConnection(java.lang.String)
-             */
-            @SuppressWarnings( "synthetic-access" )
-            public RepositoryConnection createConnection( String sourceName ) throws RepositorySourceException {
-                return repositorySourceName.equals(sourceName) ? source.getConnection() : null;
-            }
-        };
-
-        // Set up the repo type manager
-        repoTypeManager = new RepositoryNodeTypeManager(context);
-        try {
-            this.repoTypeManager.registerNodeTypes(new CndNodeTypeSource(new String[] {"/org/jboss/dna/jcr/jsr_170_builtins.cnd",
-                "/org/jboss/dna/jcr/dna_builtins.cnd"}));
-        } catch (RepositoryException re) {
-            re.printStackTrace();
-            throw new IllegalStateException("Could not load node type definition files", re);
-        } catch (IOException ioe) {
-            ioe.printStackTrace();
-            throw new IllegalStateException("Could not access node type definition files", ioe);
-        }
-
-        // Stub out the repository, since we only need a few methods ...
-        MockitoAnnotations.initMocks(this);
-        stub(repository.getRepositorySourceName()).toReturn(repositorySourceName);
-        stub(repository.getConnectionFactory()).toReturn(connectionFactory);
-        stub(repository.getRepositoryTypeManager()).toReturn(repoTypeManager);
-
-        // Set up the session attributes ...
-        sessionAttributes = new HashMap<String, Object>();
-        sessionAttributes.put("attribute1", "value1");
-
-        // Now create the workspace ...
-        SecurityContext mockSecurityContext = new MockSecurityContext(null, Collections.singleton(JcrSession.DNA_WRITE_PERMISSION));
-        workspace = new JcrWorkspace(repository, workspaceName, context.with(mockSecurityContext), sessionAttributes);
-
-        // Create the session and log in ...
-        session = (JcrSession)workspace.getSession();
     }
 
+    
     @After
     public void after() throws Exception {
         if (session.isLive()) {

Modified: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrWorkspaceTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrWorkspaceTest.java	2009-07-02 20:30:08 UTC (rev 1069)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrWorkspaceTest.java	2009-07-04 13:16:05 UTC (rev 1070)
@@ -26,47 +26,23 @@
 import static org.hamcrest.core.Is.is;
 import static org.hamcrest.core.IsNull.notNullValue;
 import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.stub;
 import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
 import javax.jcr.NamespaceRegistry;
 import javax.jcr.Node;
-import javax.jcr.RepositoryException;
 import javax.jcr.UnsupportedRepositoryOperationException;
 import javax.jcr.query.Query;
 import javax.jcr.query.QueryManager;
-import org.jboss.dna.graph.ExecutionContext;
-import org.jboss.dna.graph.Graph;
-import org.jboss.dna.graph.JaasSecurityContext;
 import org.jboss.dna.graph.JcrLexicon;
-import org.jboss.dna.graph.connector.RepositoryConnection;
-import org.jboss.dna.graph.connector.RepositoryConnectionFactory;
-import org.jboss.dna.graph.connector.RepositorySourceException;
-import org.jboss.dna.graph.connector.inmemory.InMemoryRepositorySource;
 import org.jboss.security.config.IDTrustConfiguration;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
-import org.mockito.MockitoAnnotations;
-import org.mockito.MockitoAnnotations.Mock;
 
 /**
  * @author jverhaeg
  */
-public class JcrWorkspaceTest {
+public class JcrWorkspaceTest extends AbstractJcrTest {
 
-    private String workspaceName;
-    private ExecutionContext context;
-    private InMemoryRepositorySource source;
-    private JcrWorkspace workspace;
-    private RepositoryConnectionFactory connectionFactory;
-    private Map<String, Object> sessionAttributes;
-    @Mock
-    private JcrRepository repository;
-    private RepositoryNodeTypeManager repoManager;
-
     @BeforeClass
     public static void beforeClass() {
         // Initialize IDTrust
@@ -80,22 +56,14 @@
         }
     }
 
+    @Override
     @Before
     public void beforeEach() throws Exception {
-        final String repositorySourceName = "repository";
-        workspaceName = "workspace1";
+        super.beforeEach();
+    }
 
-        // Set up the source ...
-        source = new InMemoryRepositorySource();
-        source.setName(repositorySourceName);
-        source.setDefaultWorkspaceName(workspaceName);
-
-        // Set up the execution context ...
-
-        context = new ExecutionContext().with(new JaasSecurityContext("dna-jcr", "superuser", "superuser".toCharArray()));
-
-        // Set up the initial content ...
-        Graph graph = Graph.create(source, context);
+    @Override
+    protected void initializeContent() {
         graph.create("/a").and().create("/a/b").and().create("/a/b/c").and().create("/b");
         graph.set("booleanProperty").on("/a/b").to(true);
         graph.set("jcr:primaryType").on("/a/b").to("nt:unstructured");
@@ -104,43 +72,8 @@
         // Make sure the path to the namespaces exists ...
         graph.create("/jcr:system").and().create("/jcr:system/dna:namespaces");
 
-        // Stub out the connection factory ...
-        connectionFactory = new RepositoryConnectionFactory() {
-            /**
-             * {@inheritDoc}
-             * 
-             * @see org.jboss.dna.graph.connector.RepositoryConnectionFactory#createConnection(java.lang.String)
-             */
-            @SuppressWarnings( "synthetic-access" )
-            public RepositoryConnection createConnection( String sourceName ) throws RepositorySourceException {
-                return repositorySourceName.equals(sourceName) ? source.getConnection() : null;
-            }
-        };
-
-        // Stub out the repository, since we only need a few methods ...
-        MockitoAnnotations.initMocks(this);
-
-        repoManager = new RepositoryNodeTypeManager(context);
-        try {
-            this.repoManager.registerNodeTypes(new CndNodeTypeSource(new String[] {"/org/jboss/dna/jcr/jsr_170_builtins.cnd",
-                "/org/jboss/dna/jcr/dna_builtins.cnd"}));
-        } catch (RepositoryException re) {
-            re.printStackTrace();
-            throw new IllegalStateException("Could not load node type definition files", re);
-        } catch (IOException ioe) {
-            ioe.printStackTrace();
-            throw new IllegalStateException("Could not access node type definition files", ioe);
-        }
-
-        stub(repository.getRepositorySourceName()).toReturn(repositorySourceName);
-        stub(repository.getRepositoryTypeManager()).toReturn(repoManager);
-        stub(repository.getConnectionFactory()).toReturn(connectionFactory);
-
-        // Now create the workspace ...
-        sessionAttributes = new HashMap<String, Object>();
-        workspace = new JcrWorkspace(repository, workspaceName, context, sessionAttributes);
     }
-
+    
     @Test( expected = IllegalArgumentException.class )
     public void shouldNotAllowCloneWithNullWorkspaceName() throws Exception {
         workspace.clone(null, "/src", "/dest", false);

Modified: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/MixinTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/MixinTest.java	2009-07-02 20:30:08 UTC (rev 1069)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/MixinTest.java	2009-07-04 13:16:05 UTC (rev 1070)
@@ -26,32 +26,16 @@
 import static org.hamcrest.core.Is.is;
 import static org.hamcrest.core.IsNull.notNullValue;
 import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.stub;
-import java.io.IOException;
 import java.util.Arrays;
-import java.util.Collections;
-import java.util.EnumMap;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 import javax.jcr.Node;
 import javax.jcr.Property;
 import javax.jcr.PropertyType;
-import javax.jcr.RepositoryException;
 import javax.jcr.nodetype.ConstraintViolationException;
 import javax.jcr.nodetype.NoSuchNodeTypeException;
 import javax.jcr.version.OnParentVersionAction;
-import org.jboss.dna.graph.ExecutionContext;
-import org.jboss.dna.graph.Graph;
 import org.jboss.dna.graph.JcrNtLexicon;
-import org.jboss.dna.graph.MockSecurityContext;
-import org.jboss.dna.graph.SecurityContext;
-import org.jboss.dna.graph.connector.RepositoryConnection;
-import org.jboss.dna.graph.connector.RepositoryConnectionFactory;
-import org.jboss.dna.graph.connector.RepositorySourceException;
-import org.jboss.dna.graph.connector.inmemory.InMemoryRepositorySource;
 import org.jboss.dna.graph.property.Name;
-import org.jboss.dna.graph.property.NamespaceRegistry;
 import org.jboss.dna.graph.property.basic.BasicName;
 import org.jboss.dna.jcr.nodetype.NodeDefinitionTemplate;
 import org.jboss.dna.jcr.nodetype.NodeTypeTemplate;
@@ -59,10 +43,8 @@
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
-import org.mockito.MockitoAnnotations;
-import org.mockito.MockitoAnnotations.Mock;
 
-public class MixinTest {
+public class MixinTest extends AbstractJcrTest {
 
     /*
      * Declares the following node types:
@@ -87,88 +69,10 @@
     static final Name CHILD_NODE_A = new BasicName("", "nodeA");
     static final Name CHILD_NODE_B = new BasicName("", "nodeB");
 
-    private String workspaceName;
-    private ExecutionContext context;
-    private InMemoryRepositorySource source;
-    private JcrWorkspace workspace;
-    private JcrSession session;
-    private Graph graph;
-    private RepositoryConnectionFactory connectionFactory;
-    private RepositoryNodeTypeManager repoTypeManager;
-    private Map<String, Object> sessionAttributes;
-    private Map<JcrRepository.Option, String> options;
-    private NamespaceRegistry registry;
-    @Mock
-    private JcrRepository repository;
-
+    @Override
     @Before
     public void beforeEach() throws Exception {
-        MockitoAnnotations.initMocks(this);
-
-        workspaceName = "workspace1";
-        final String repositorySourceName = "repository";
-
-        // Set up the source ...
-        source = new InMemoryRepositorySource();
-        source.setName(workspaceName);
-        source.setDefaultWorkspaceName(workspaceName);
-
-        // Set up the execution context ...
-        context = new ExecutionContext();
-        // Register the test namespace
-        context.getNamespaceRegistry().register(TestLexicon.Namespace.PREFIX, TestLexicon.Namespace.URI);
-
-        // Set up the initial content ...
-        graph = Graph.create(source, context);
-
-        // Stub out the connection factory ...
-        connectionFactory = new RepositoryConnectionFactory() {
-            /**
-             * {@inheritDoc}
-             * 
-             * @see org.jboss.dna.graph.connector.RepositoryConnectionFactory#createConnection(java.lang.String)
-             */
-            @SuppressWarnings( "synthetic-access" )
-            public RepositoryConnection createConnection( String sourceName ) throws RepositorySourceException {
-                return repositorySourceName.equals(sourceName) ? source.getConnection() : null;
-            }
-        };
-
-        // Stub out the repository, since we only need a few methods ...
-        repoTypeManager = new RepositoryNodeTypeManager(context);
-
-        try {
-            this.repoTypeManager.registerNodeTypes(new CndNodeTypeSource(new String[] {"/org/jboss/dna/jcr/jsr_170_builtins.cnd",
-                "/org/jboss/dna/jcr/dna_builtins.cnd"}));
-            this.repoTypeManager.registerNodeTypes(new NodeTemplateNodeTypeSource(getTestTypes()));
-
-        } catch (RepositoryException re) {
-            re.printStackTrace();
-            throw new IllegalStateException("Could not load node type definition files", re);
-        } catch (IOException ioe) {
-            ioe.printStackTrace();
-            throw new IllegalStateException("Could not access node type definition files", ioe);
-        }
-
-        stub(repository.getRepositoryTypeManager()).toReturn(repoTypeManager);
-        stub(repository.getRepositorySourceName()).toReturn(repositorySourceName);
-        stub(repository.getConnectionFactory()).toReturn(connectionFactory);
-
-        // Stub out the repository options ...
-        options = new EnumMap<JcrRepository.Option, String>(JcrRepository.Option.class);
-        options.put(JcrRepository.Option.PROJECT_NODE_TYPES, Boolean.FALSE.toString());
-        stub(repository.getOptions()).toReturn(options);
-
-        // Set up the session attributes ...
-        sessionAttributes = new HashMap<String, Object>();
-
-        // Now create the workspace ...
-        SecurityContext mockSecurityContext = new MockSecurityContext("testuser", Collections.singleton(JcrSession.DNA_WRITE_PERMISSION));
-        workspace = new JcrWorkspace(repository, workspaceName, context.with(mockSecurityContext), sessionAttributes);
-
-        // Create the session and log in ...
-        session = (JcrSession)workspace.getSession();
-        registry = session.getExecutionContext().getNamespaceRegistry();
+        super.beforeEach();
     }
 
     @After
@@ -475,7 +379,8 @@
         nodeA.removeMixin(MIXIN_TYPE_B.getString(registry));
     }
 
-    private List<NodeTypeTemplate> getTestTypes() {
+    @Override
+    protected List<NodeTypeTemplate> getTestTypes() {
         NodeTypeTemplate mixinTypeA = new JcrNodeTypeTemplate(this.context);
         mixinTypeA.setName("mixinTypeA");
         mixinTypeA.setMixin(true);

Modified: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/RepositoryNodeTypeManagerTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/RepositoryNodeTypeManagerTest.java	2009-07-02 20:30:08 UTC (rev 1069)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/RepositoryNodeTypeManagerTest.java	2009-07-04 13:16:05 UTC (rev 1070)
@@ -29,8 +29,6 @@
 import static org.hamcrest.core.IsNull.nullValue;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.stub;
-import java.io.IOException;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.EnumMap;
@@ -42,63 +40,30 @@
 import javax.jcr.PathNotFoundException;
 import javax.jcr.Property;
 import javax.jcr.PropertyType;
-import javax.jcr.RepositoryException;
 import javax.jcr.Value;
 import javax.jcr.nodetype.NodeDefinition;
 import javax.jcr.nodetype.NodeType;
 import javax.jcr.nodetype.PropertyDefinition;
 import javax.jcr.version.OnParentVersionAction;
-import org.jboss.dna.graph.ExecutionContext;
-import org.jboss.dna.graph.Graph;
-import org.jboss.dna.graph.MockSecurityContext;
-import org.jboss.dna.graph.SecurityContext;
-import org.jboss.dna.graph.connector.RepositoryConnection;
-import org.jboss.dna.graph.connector.RepositoryConnectionFactory;
-import org.jboss.dna.graph.connector.RepositorySourceException;
-import org.jboss.dna.graph.connector.inmemory.InMemoryRepositorySource;
 import org.jboss.dna.graph.property.Name;
 import org.jboss.dna.graph.property.NamespaceRegistry;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
-import org.mockito.MockitoAnnotations;
-import org.mockito.MockitoAnnotations.Mock;
 
-public class RepositoryNodeTypeManagerTest {
+public class RepositoryNodeTypeManagerTest extends AbstractJcrTest {
 
-    private String workspaceName;
-    private ExecutionContext context;
-    private InMemoryRepositorySource source;
-    private JcrWorkspace workspace;
-    private JcrSession session;
-    private Graph graph;
-    private RepositoryConnectionFactory connectionFactory;
-    private RepositoryNodeTypeManager repoTypeManager;
-    private Map<String, Object> sessionAttributes;
-    private Map<JcrRepository.Option, String> options;
-    @Mock
-    private JcrRepository repository;
-
+    @Override
     @Before
     public void beforeEach() throws Exception {
-        MockitoAnnotations.initMocks(this);
+        super.beforeEach();
 
-        workspaceName = "workspace1";
-        final String repositorySourceName = "repository";
+        graph.set("jcr:primaryType").on("/jcr:system/dna:namespaces").to(DnaLexicon.NAMESPACES);
 
-        // Set up the source ...
-        source = new InMemoryRepositorySource();
-        source.setName(workspaceName);
-        source.setDefaultWorkspaceName(workspaceName);
+    }
 
-        // Set up the execution context ...
-        context = new ExecutionContext();
-        // Register the test namespace
-        context.getNamespaceRegistry().register(TestLexicon.Namespace.PREFIX, TestLexicon.Namespace.URI);
-
-        // Set up the initial content ...
-        graph = Graph.create(source, context);
-
+    @Override
+    protected void initializeContent() {
         // Make sure the path to the namespaces exists ...
         graph.create("/jcr:system"); // .and().create("/jcr:system/dna:namespaces");
         graph.set("jcr:primaryType").on("/jcr:system").to(DnaLexicon.SYSTEM);
@@ -106,56 +71,16 @@
         graph.create("/a").and().create("/a/b").and().create("/a/b/c");
         graph.set("jcr:mixinTypes").on("/a").to(JcrMixLexicon.REFERENCEABLE);
 
-        // Stub out the connection factory ...
-        connectionFactory = new RepositoryConnectionFactory() {
-            /**
-             * {@inheritDoc}
-             * 
-             * @see org.jboss.dna.graph.connector.RepositoryConnectionFactory#createConnection(java.lang.String)
-             */
-            @SuppressWarnings( "synthetic-access" )
-            public RepositoryConnection createConnection( String sourceName ) throws RepositorySourceException {
-                return repositorySourceName.equals(sourceName) ? source.getConnection() : null;
-            }
-        };
+    }
 
-        // Stub out the repository, since we only need a few methods ...
-        repoTypeManager = new RepositoryNodeTypeManager(context);
-        try {
-            this.repoTypeManager.registerNodeTypes(new CndNodeTypeSource(new String[] {"/org/jboss/dna/jcr/jsr_170_builtins.cnd",
-                "/org/jboss/dna/jcr/dna_builtins.cnd"}));
-        } catch (RepositoryException re) {
-            re.printStackTrace();
-            throw new IllegalStateException("Could not load node type definition files", re);
-        } catch (IOException ioe) {
-            ioe.printStackTrace();
-            throw new IllegalStateException("Could not access node type definition files", ioe);
-        }
-
-        stub(repository.getRepositoryTypeManager()).toReturn(repoTypeManager);
-        stub(repository.getRepositorySourceName()).toReturn(repositorySourceName);
-        stub(repository.getConnectionFactory()).toReturn(connectionFactory);
-
+    @Override
+    protected void initializeOptions() {
         // Stub out the repository options ...
         options = new EnumMap<JcrRepository.Option, String>(JcrRepository.Option.class);
         options.put(JcrRepository.Option.PROJECT_NODE_TYPES, Boolean.TRUE.toString());
-        stub(repository.getOptions()).toReturn(options);
 
-        // Set up the session attributes ...
-        sessionAttributes = new HashMap<String, Object>();
-        sessionAttributes.put("attribute1", "value1");
-
-        // Now create the workspace ...
-        SecurityContext mockSecurityContext = new MockSecurityContext("testuser", Collections.singleton(JcrSession.DNA_READ_PERMISSION));
-        workspace = new JcrWorkspace(repository, workspaceName, context.with(mockSecurityContext), sessionAttributes);
-
-        // Create the session and log in ...
-        session = (JcrSession)workspace.getSession();
-
-        graph.set("jcr:primaryType").on("/jcr:system/dna:namespaces").to(DnaLexicon.NAMESPACES);
-
     }
-
+    
     @After
     public void after() throws Exception {
         if (session != null && session.isLive()) {

Modified: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/TypeRegistrationTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/TypeRegistrationTest.java	2009-07-02 20:30:08 UTC (rev 1069)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/TypeRegistrationTest.java	2009-07-04 13:16:05 UTC (rev 1070)
@@ -28,15 +28,12 @@
 import static org.hamcrest.core.IsNull.nullValue;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.fail;
-import java.io.IOException;
 import java.util.Arrays;
 import java.util.List;
 import javax.jcr.PropertyType;
-import javax.jcr.RepositoryException;
 import javax.jcr.nodetype.NoSuchNodeTypeException;
 import javax.jcr.nodetype.NodeDefinition;
 import javax.jcr.nodetype.PropertyDefinition;
-import org.jboss.dna.graph.ExecutionContext;
 import org.jboss.dna.graph.property.Name;
 import org.jboss.dna.graph.property.NameFactory;
 import org.jboss.dna.graph.property.NamespaceRegistry;
@@ -48,42 +45,25 @@
 import org.jboss.dna.jcr.nodetype.PropertyDefinitionTemplate;
 import org.junit.Before;
 import org.junit.Test;
-import org.mockito.MockitoAnnotations;
 
-public class TypeRegistrationTest {
+public class TypeRegistrationTest extends AbstractJcrTest {
 
     private static final String TEST_TYPE_NAME = "dna:testNode";
     private static final String TEST_TYPE_NAME2 = "dna:testNode2";
     private static final String TEST_PROPERTY_NAME = "dna:testProperty";
     private static final String TEST_CHILD_NODE_NAME = "dna:testChildNode";
 
-    private ExecutionContext context;
-    private RepositoryNodeTypeManager repoTypeManager;
     private JcrNodeTypeTemplate ntTemplate;
-    private NameFactory nameFactory;
     private NamespaceRegistry registry;
+    private NameFactory nameFactory;
 
+    @Override
     @Before
     public void beforeEach() throws Exception {
-        MockitoAnnotations.initMocks(this);
-        context = new ExecutionContext();
-        nameFactory = context.getValueFactories().getNameFactory();
-        registry = context.getNamespaceRegistry();
-
-        repoTypeManager = new RepositoryNodeTypeManager(context);
-
-        try {
-            this.repoTypeManager.registerNodeTypes(new CndNodeTypeSource(new String[] {"/org/jboss/dna/jcr/jsr_170_builtins.cnd",
-                "/org/jboss/dna/jcr/dna_builtins.cnd"}));
-        } catch (RepositoryException re) {
-            re.printStackTrace();
-            throw new IllegalStateException("Could not load node type definition files", re);
-        } catch (IOException ioe) {
-            ioe.printStackTrace();
-            throw new IllegalStateException("Could not access node type definition files", ioe);
-        }
-
+        super.beforeEach();
         ntTemplate = new JcrNodeTypeTemplate(context);
+        registry = context.getNamespaceRegistry();
+        nameFactory = context.getValueFactories().getNameFactory();
     }
 
     @Test( expected = AssertionError.class )




More information about the dna-commits mailing list