/*
* 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 java.util.Collections;
import javax.jcr.ImportUUIDBehavior;
import javax.jcr.Node;
import org.jboss.dna.graph.ExecutionContext;
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.observe.MockObservable;
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);
// Stub out the connection factory ...
RepositoryConnectionFactory connectionFactory = new RepositoryConnectionFactory() {
@SuppressWarnings( "synthetic-access" )
public RepositoryConnection createConnection( String sourceName ) throws RepositorySourceException {
return source.getConnection();
}
};
repository = new JcrRepository(context, connectionFactory, "unused", new MockObservable(), null, null, null);
SecurityContext mockSecurityContext = new MockSecurityContext("testuser",
Collections.singleton(JcrSession.DNA_WRITE_PERMISSION));
session = (JcrSession)repository.login(new SecurityContextCredentials(mockSecurityContext));
}
@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,
boolean useWorkspace ) 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());
if (useWorkspace) {
// import via workspace ...
session.getWorkspace().importXML(targetPath, bais, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
} else {
// import via session ...
session.importXML(targetPath, bais, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
}
}
@Test
public void shouldImportExportEscapedXmlCharactersInSystemViewUsingSession() 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, false);
Node newSourceNode = targetNode.getNode(testName + "Source");
newSourceNode.getNode(BAD_CHARACTER_STRING);
assertThat(newSourceNode.getProperty("badcharacters").getString(), is(BAD_CHARACTER_STRING));
}
@Test
public void shouldImportExportEscapedXmlCharactersInSystemViewUsingWorkspace() throws Exception {
String testName = "importExportEscapedXmlCharacters";
Node rootNode = session.getRootNode();
System.out.println(session);
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);
session.save();
testImportExport(sourceNode.getPath(), targetNode.getPath(), ExportType.SYSTEM, false, false, true);
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 shouldImportExportEscapedXmlCharactersInDocumentViewUsingSession() 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, false);
Node newSourceNode = targetNode.getNode(testName + "Source");
newSourceNode.getNode(BAD_CHARACTER_STRING);
assertThat(newSourceNode.getProperty("badcharacters").getString(), is(BAD_CHARACTER_STRING));
}
}