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

dna-commits at lists.jboss.org dna-commits at lists.jboss.org
Mon May 4 15:01:44 EDT 2009


Author: rhauch
Date: 2009-05-04 15:01:44 -0400 (Mon, 04 May 2009)
New Revision: 881

Added:
   trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/ImportExportTest.java
Log:
DNA-391 Round-trip Test Case for Import/Export

Applied patch to add a basic import/export roundtrip test.

Added: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/ImportExportTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/ImportExportTest.java	                        (rev 0)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/ImportExportTest.java	2009-05-04 19:01:44 UTC (rev 881)
@@ -0,0 +1,163 @@
+/*
+ * JBoss DNA (http://www.jboss.org/dna)
+ * See the COPYRIGHT.txt file distributed with this work for information
+ * regarding copyright ownership.  Some portions may be licensed
+ * to Red Hat, Inc. under one or more contributor license agreements.
+ * See the AUTHORS.txt file in the distribution for a full listing of 
+ * individual contributors.
+ *
+ * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
+ * is licensed to you 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.
+ * 
+ * JBoss DNA 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.junit.Assert.assertThat;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import javax.jcr.ImportUUIDBehavior;
+import javax.jcr.Node;
+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.junit.After;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.mockito.MockitoAnnotations;
+
+/**
+ * Tests of round-trip importing/exporting of repository content.
+ *
+ */
+public class ImportExportTest {
+
+    private enum ExportType {
+        SYSTEM,
+        DOCUMENT
+    }
+    
+    private static final String BAD_CHARACTER_STRING = "Test & <Test>*";
+
+    
+    private InMemoryRepositorySource source;
+    private JcrSession session;
+    private JcrRepository repository;
+    
+    @Before
+    public void beforeEach() throws Exception {
+        MockitoAnnotations.initMocks(this);
+
+        String workspaceName = "workspace1";
+
+        // Set up the source ...
+        source = new InMemoryRepositorySource();
+        source.setName(workspaceName);
+        source.setDefaultWorkspaceName(workspaceName);
+
+        // Set up the execution context ...
+        ExecutionContext context = new ExecutionContext();
+        // Register the test namespace
+        context.getNamespaceRegistry().register(TestLexicon.Namespace.PREFIX, TestLexicon.Namespace.URI);
+
+        // Set up the initial content ...
+        Graph graph = Graph.create(source, context);
+
+        // 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);
+
+        // Stub out the connection factory ...
+        RepositoryConnectionFactory 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 source.getConnection();
+            }
+        };
+
+        repository = new JcrRepository(context, connectionFactory, "unused");
+
+        session = (JcrSession) repository.login();
+    }
+
+    @After
+    public void after() throws Exception {
+        if (session != null && session.isLive()) {
+            session.logout();
+        }
+    }
+    
+    private void testImportExport(String sourcePath, String targetPath, ExportType useSystemView, boolean skipBinary, boolean noRecurse) 
+        throws Exception
+    {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        
+        if (useSystemView == ExportType.SYSTEM) {
+            session.exportSystemView(sourcePath, baos, skipBinary, noRecurse);
+        }
+        else {
+            session.exportDocumentView(sourcePath, baos, skipBinary, noRecurse);
+        }
+        
+        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+        
+        session.importXML(targetPath, bais, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
+    }
+
+    @Test
+    public void shouldImportExportEscapedXmlCharactersInSystemView() throws Exception {
+        String testName = "importExportEscapedXmlCharacters";
+        Node rootNode = session.getRootNode();
+        Node sourceNode = rootNode.addNode(testName + "Source", "nt:unstructured");
+        Node targetNode = rootNode.addNode(testName + "Target", "nt:unstructured");
+        
+        // Test data
+        sourceNode.setProperty("badcharacters", BAD_CHARACTER_STRING);
+        assertThat(sourceNode.getProperty("badcharacters").getString(), is(BAD_CHARACTER_STRING));
+        sourceNode.addNode(BAD_CHARACTER_STRING);
+        
+        testImportExport(sourceNode.getPath(), targetNode.getPath(), ExportType.SYSTEM, false, false);
+        Node newSourceNode = targetNode.getNode(testName + "Source");
+        newSourceNode.getNode(BAD_CHARACTER_STRING);
+        assertThat(newSourceNode.getProperty("badcharacters").getString(), is(BAD_CHARACTER_STRING));
+    }
+
+    @Ignore("JR TCK is broken")
+    @Test
+    public void shouldImportExportEscapedXmlCharactersInDocumentView() throws Exception {
+        String testName = "importExportEscapedXmlCharacters";
+        Node rootNode = session.getRootNode();
+        Node sourceNode = rootNode.addNode(testName + "Source", "nt:unstructured");
+        Node targetNode = rootNode.addNode(testName + "Target", "nt:unstructured");
+        
+        // Test data
+        sourceNode.setProperty("badcharacters", BAD_CHARACTER_STRING);
+        assertThat(sourceNode.getProperty("badcharacters").getString(), is(BAD_CHARACTER_STRING));
+        sourceNode.addNode(BAD_CHARACTER_STRING);
+        
+        testImportExport(sourceNode.getPath(), targetNode.getPath(), ExportType.DOCUMENT, false, false);
+        Node newSourceNode = targetNode.getNode(testName + "Source");
+        newSourceNode.getNode(BAD_CHARACTER_STRING);
+        assertThat(newSourceNode.getProperty("badcharacters").getString(), is(BAD_CHARACTER_STRING));
+    }
+}


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




More information about the dna-commits mailing list