jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mreut...@apache.org
Subject svn commit: r162058 - in /incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api: AbstractImportXmlTest.java DocumentViewImportTest.java TestAll.java
Date Wed, 20 Apr 2005 19:53:15 GMT
Author: mreutegg
Date: Wed Apr 20 12:53:14 2005
New Revision: 162058

URL: http://svn.apache.org/viewcvs?rev=162058&view=rev
Log:
Test cases for document view import, contributed by Norbert Braeker.

Added:
    incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/AbstractImportXmlTest.java
  (with props)
    incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/DocumentViewImportTest.java
  (with props)
Modified:
    incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/TestAll.java

Added: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/AbstractImportXmlTest.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/AbstractImportXmlTest.java?rev=162058&view=auto
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/AbstractImportXmlTest.java
(added)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/AbstractImportXmlTest.java
Wed Apr 20 12:53:14 2005
@@ -0,0 +1,433 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.test.api;
+
+import org.apache.jackrabbit.test.AbstractJCRTest;
+import org.apache.xml.serialize.OutputFormat;
+import org.apache.xml.serialize.XMLSerializer;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Attr;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.InputSource;
+import org.xml.sax.helpers.XMLReaderFactory;
+
+import javax.jcr.nodetype.NodeTypeManager;
+import javax.jcr.nodetype.NoSuchNodeTypeException;
+import javax.jcr.Session;
+import javax.jcr.Workspace;
+import javax.jcr.NamespaceRegistry;
+import javax.jcr.Node;
+import javax.jcr.ImportUUIDBehavior;
+import javax.jcr.RepositoryException;
+import javax.jcr.PathNotFoundException;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import java.util.Set;
+import java.util.HashSet;
+import java.util.Arrays;
+import java.util.StringTokenizer;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.BufferedOutputStream;
+import java.io.FileOutputStream;
+
+/**
+ * <code>AbstractImportXmlTest</code> Provides names, data and methods to create
+ * xml documents and referenceable nodes for the tests of document view import
+ * methods of Workspace and Session.
+ */
+abstract class AbstractImportXmlTest extends AbstractJCRTest {
+
+    protected final boolean WORKSPACE = true;
+    protected final boolean SESSION = false;
+
+    // the absolute path to the target nodes for the imports
+    protected String target;
+    protected String refTarget;
+
+    protected Session session;
+    protected Workspace workspace;
+    protected NodeTypeManager ntManager;
+    protected NamespaceRegistry nsp;
+
+    protected String ntUnstructured;
+
+    protected File file;
+
+    // some node names
+    protected String referenced;
+    protected String referencing;
+
+    // the target nodes
+    protected Node targetNode;
+    protected Node refTargetNode;
+
+    protected String unusedPrefix;
+    protected String unusedURI;
+
+    // names for namespace import
+    protected final String TEST_PREFIX = "docview";
+    protected final String TEST_URI = "www.apache.org/jackrabbit/test/namespaceImportTest";
+    protected final String XML_NS = "xmlns";
+
+    // xml document related names
+    protected static final String rootElem = "docRoot";
+    protected static final String refNodeElem = "refNodeElem";
+    protected static final String xmltextElem = "xmltextElem";
+    protected static final String childElem = "childElem";
+    protected static final String grandChildElem = "grandChildElem";
+
+    protected static final String encodedElemName = "Element_x003C__x003E_Name";
+    protected static final String decodedElemName = "Element<>Name";
+
+    protected static final String attributeName = "attribute";
+    protected static final String attributeValue = "attrVal";
+
+    protected static final String encodedAttributeName = "Prop_x0020_Name";
+    protected static final String decodedAttributeName = "Prop Name";
+    protected static final String encodedAttributeValue = "Hello_x0009_&_x0009_GoodBye";
+    protected static final String decodedAttributeValue = "Hello\\t&\\tGoodBye";
+
+    //String value for the test with leading and trailing spaces and entity reference charachters
+    protected String xmltext = "\\t Text for docView Export test_x0009_with escaped _x003C_
characters.  ";
+
+    // is semantic of mix:referenceable respected?
+    protected boolean respectMixRef = false;
+
+    // default uuidBehaviour for the tests
+    protected int uuidBehaviour = ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW;
+
+    protected DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+    protected DocumentBuilder dom;
+
+    /**
+     * Sets up the fixture for the test cases.
+     */
+    public void setUp() throws Exception {
+        super.setUp();
+
+        dom = factory.newDocumentBuilder();
+        file = File.createTempFile("docViewImportTest", ".xml");
+        log.print("Tempfile: " + file.getAbsolutePath());
+        session = superuser;
+        workspace = session.getWorkspace();
+        // create the target nodes for the imports
+        target = testRoot + "/target";
+        targetNode = createAncestors(target);
+        refTarget = testRoot + "/refTarget";
+        refTargetNode = createAncestors(refTarget);
+
+        nsp = workspace.getNamespaceRegistry();
+        ntManager = workspace.getNodeTypeManager();
+
+        // construct a namespace not existing in the repository
+        unusedPrefix = getUnusedPrefix();
+        unusedURI = getUnusedURI();
+        referenced = nodeName1;
+        referencing = nodeName2;
+        // test if jcr:uuid of mix:referenceable node type is respected
+        respectMixRef = isMixRefRespected();
+    }
+
+    public void tearDown() throws Exception {
+        file.delete();
+        super.tearDown();
+    }
+
+    /**
+     * Creates a document with some nodes and props for Namespace adding test
+     * and for correct tree structure tests after having imported.
+     *
+     * @return
+     */
+    public Document createSimpleDocument() {
+        Document doc = dom.newDocument();
+        Element root = doc.createElementNS(unusedURI, unusedPrefix + ":" + rootElem);
+        root.setAttribute(XML_NS + ":" + unusedPrefix, unusedURI);
+        Element child = doc.createElement(childElem);
+        Element xmlElem = doc.createElement(xmltextElem);
+        Element encoded = doc.createElement(encodedElemName);
+        Element grandchild = doc.createElement(grandChildElem);
+
+        Attr attr = doc.createAttribute(attributeName);
+        attr.setValue(attributeValue);
+        Attr encodedAttr = doc.createAttribute(encodedAttributeName);
+        encodedAttr.setValue(encodedAttributeValue);
+
+        child.appendChild(encoded);
+        child.setAttributeNode(encodedAttr);
+
+        grandchild.setAttributeNode(attr);
+
+        xmlElem.appendChild(doc.createTextNode(xmltext));
+        xmlElem.appendChild(grandchild);
+        xmlElem.setAttribute(attributeName, attributeValue);
+
+        root.appendChild(child);
+        root.appendChild(xmlElem);
+
+        doc.appendChild(root);
+        return doc;
+    }
+
+    /**
+     * Imports a given document using either Workspace.importXML or
+     * Session.importXML method.
+     *
+     * @param absPath       the absPath to the parent node where to import the
+     *                      document
+     * @param document      the document to import
+     * @param uuidBehaviour how the uuid collisions should be handled
+     * @param withWorkspace if workspace or session interface should be used
+     * @throws RepositoryException
+     * @throws IOException
+     */
+    protected void importXML(String absPath, Document document,
+                             int uuidBehaviour, boolean withWorkspace)
+            throws RepositoryException, IOException {
+        serialize(document);
+        BufferedInputStream bin = new BufferedInputStream(new FileInputStream(file));
+        try {
+            if (withWorkspace) {
+                workspace.importXML(absPath, bin, uuidBehaviour);
+            } else {
+                session.importXML(absPath, bin, uuidBehaviour);
+                session.save();
+            }
+        } finally {
+            bin.close();
+        }
+    }
+
+    /**
+     * Imports a given document using the ContentHandler received either with
+     * Workspace.getImportContentHandler or Session.getImportContentHandler.
+     * This handler is then passed to a XML parser which parses the given
+     * document.
+     *
+     * @param absPath       the absPath to the parent node where to import the
+     *                      document
+     * @param document      the document to import
+     * @param uuidBehaviour how the uuid collisions should be handled
+     * @param withWorkspace if workspace or session interface should be used
+     * @throws RepositoryException
+     * @throws SAXException
+     * @throws IOException
+     */
+    public void importWithHandler(String absPath, Document document,
+                                  int uuidBehaviour, boolean withWorkspace)
+            throws RepositoryException, SAXException, IOException {
+
+        serialize(document);
+        BufferedInputStream bin = new BufferedInputStream(new FileInputStream(file));
+
+        ContentHandler handler;
+        if (withWorkspace) {
+            handler = workspace.getImportContentHandler(absPath, uuidBehaviour);
+        } else {
+            handler = session.getImportContentHandler(absPath, uuidBehaviour);
+        }
+        XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
+        parser.setContentHandler(handler);
+
+        InputSource source = new InputSource(bin);
+        parser.parse(source);
+        if (!withWorkspace) {
+            session.save();
+        }
+    }
+
+//--------------------------------< helpers >-----------------------------------
+    /**
+     * Tests if jcr:uuid property of mix:referenceable nodetype is respected.
+     * This is believed as true when during import with uuidBehaviour
+     * IMPORT_UUID_COLLISION_REMOVE_EXISTING a node with the same uuid as a node
+     * to be imported will be deleted.
+     *
+     * @return
+     * @throws RepositoryException
+     * @throws IOException
+     */
+    public boolean isMixRefRespected() throws RepositoryException, IOException {
+        boolean respected = false;
+        if (supportsNodeType(mixReferenceable)) {
+            String uuid = createReferenceableNode(referenced);
+            Document document = dom.newDocument();
+            Element root = document.createElement(rootElem);
+            root.setAttribute(XML_NS + ":jcr", NS_JCR_URI);
+            root.setAttributeNS(NS_JCR_URI, jcrUUID, uuid);
+            root.setAttributeNS(NS_JCR_URI, jcrMixinTypes, mixReferenceable);
+            document.appendChild(root);
+
+            importXML(refTargetNode.getPath(), document,
+                    ImportUUIDBehavior.IMPORT_UUID_COLLISION_REMOVE_EXISTING, SESSION);
+            session.save();
+
+            // existing node with same uuid should now be deleted
+            respected = !testRootNode.hasNode(referenced);
+
+            // check if imported document node is referenceable
+            Node rootNode = refTargetNode.getNode(rootElem);
+            respected &= rootNode.isNodeType(mixReferenceable);
+        }
+        return respected;
+    }
+
+    /**
+     * Creates a node with given name below the testRootNode which will be
+     * referenced by the node nodeName2 and returns the UUID assigned to the
+     * created node.
+     *
+     * @param name
+     * @return
+     * @throws RepositoryException
+     */
+    public String createReferenceableNode(String name) throws RepositoryException {
+        // remove a yet existing node at the target
+        try {
+            Node node = testRootNode.getNode(name);
+            node.remove();
+            session.save();
+        } catch (PathNotFoundException pnfe) {
+            // ok
+        }
+        // a referenceable node
+        Node n1 = testRootNode.addNode(name);
+        n1.addMixin(mixReferenceable);
+        // make sure jcr:uuid is available
+        testRootNode.save();
+        return n1.getUUID();
+    }
+
+    /**
+     * Creates a document with a element rootElem containing a jcr:uuid
+     * attribute with the given uuid as value. This document is imported below
+     * the refTargetNode. If there is yet a node rootElem then this node is
+     * romoved in advance.
+     *
+     * @param uuid
+     * @param uuidBehaviour
+     * @param withWorkspace
+     * @param withHandler
+     * @throws RepositoryException
+     * @throws IOException
+     */
+    public void importRefNodeDocument(String uuid, int uuidBehaviour,
+                                      boolean withWorkspace, boolean withHandler)
+            throws RepositoryException, IOException, SAXException {
+
+        Document document = dom.newDocument();
+        Element root = document.createElement(rootElem);
+        root.setAttribute(XML_NS + ":jcr", NS_JCR_URI);
+        root.setAttributeNS(NS_JCR_URI, jcrUUID, uuid);
+        root.setAttributeNS(NS_JCR_URI, jcrMixinTypes, mixReferenceable);
+        root.setAttribute(propertyName1, "some text");
+        document.appendChild(root);
+
+        // remove a yet existing node at the target
+        try {
+            Node node = refTargetNode.getNode(rootElem);
+            node.remove();
+            session.save();
+        } catch (PathNotFoundException pnfe) {
+            // ok
+        }
+
+        if (withHandler) {
+            importWithHandler(refTargetNode.getPath(), document, uuidBehaviour, withWorkspace);
+        } else {
+            importXML(refTargetNode.getPath(), document, uuidBehaviour, withWorkspace);
+        }
+        session.save();
+    }
+
+    protected Node createAncestors(String absPath) throws RepositoryException {
+        // create nodes to name of absPath
+        Node root = session.getRootNode();
+        StringTokenizer names = new StringTokenizer(absPath, "/");
+        Node currentNode = root;
+        while (names.hasMoreTokens()) {
+            String name = names.nextToken();
+            if (currentNode.hasNode(name)) {
+                currentNode = currentNode.getNode(name);
+            } else {
+                currentNode = currentNode.addNode(name);
+            }
+        }
+        root.save();
+        return currentNode;
+    }
+
+    public void serialize(Document document) throws IOException {
+        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
+        try {
+            OutputFormat format = new OutputFormat("xml", "UTF-8", true);
+            XMLSerializer serializer = new XMLSerializer(bos, format);
+            serializer.serialize(document);
+        } finally {
+            bos.close();
+        }
+    }
+
+    public boolean supportsNodeType(String ntName) throws RepositoryException {
+        boolean support = false;
+        try {
+            ntManager.getNodeType(ntName);
+            support = true;
+        } catch (NoSuchNodeTypeException nste) {
+            //
+        }
+        return support;
+    }
+
+    /**
+     * Returns a namespace prefix that currently not used in the namespace
+     * registry.
+     *
+     * @return an unused namespace prefix.
+     */
+    protected String getUnusedPrefix() throws RepositoryException {
+        Set prefixes = new HashSet(Arrays.asList(nsp.getPrefixes()));
+        String prefix = TEST_PREFIX;
+        int i = 0;
+        while (prefixes.contains(prefix)) {
+            prefix += i++;
+        }
+        return prefix;
+    }
+
+    /**
+     * Returns a namespace URI that currently not used in the namespace
+     * registry.
+     *
+     * @return an unused namespace URI.
+     */
+    protected String getUnusedURI() throws RepositoryException {
+        Set uris = new HashSet(Arrays.asList(nsp.getURIs()));
+        String uri = TEST_URI;
+        int i = 0;
+        while (uris.contains(uri)) {
+            uri += i++;
+        }
+        return uri;
+    }
+}

Propchange: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/AbstractImportXmlTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/DocumentViewImportTest.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/DocumentViewImportTest.java?rev=162058&view=auto
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/DocumentViewImportTest.java
(added)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/DocumentViewImportTest.java
Wed Apr 20 12:53:14 2005
@@ -0,0 +1,369 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.test.api;
+
+import org.apache.jackrabbit.test.NotExecutableException;
+import org.xml.sax.SAXException;
+
+import javax.jcr.RepositoryException;
+import javax.jcr.Property;
+import javax.jcr.Node;
+import javax.jcr.PathNotFoundException;
+import javax.jcr.PropertyType;
+import javax.jcr.PropertyIterator;
+import javax.jcr.NamespaceException;
+import javax.jcr.ImportUUIDBehavior;
+import javax.jcr.ItemExistsException;
+import java.io.IOException;
+
+/**
+ * <code>DocumentViewImportTest</code> Tests importXML and
+ * getImportContentHandler methods of the Workspace and Session class. Also
+ * tests the UuidBehaviour flag.
+ *
+ * @test
+ * @sources DocumentViewImportTest.java
+ * @executeClass org.apache.jackrabbit.test.api.DocumentViewImportTest
+ * @keywords level2
+ */
+public class DocumentViewImportTest extends AbstractImportXmlTest {
+
+    private String JCR_XMLTEXT;
+    private String JCR_XMLCHAR;
+
+    private boolean withHandler;
+    private boolean withWorkspace;
+
+    public void setUp() throws Exception {
+        super.setUp();
+        JCR_XMLTEXT = superuser.getNamespacePrefix(NS_JCR_URI) + ":xmltext";
+        JCR_XMLCHAR = superuser.getNamespacePrefix(NS_JCR_URI) + ":xmlcharacters";
+    }
+
+    public void tearDown() throws Exception {
+        file.delete();
+        super.tearDown();
+    }
+
+    public void testWorkspaceImportXml() throws RepositoryException,
+            IOException, SAXException, NotExecutableException {
+        withHandler = false;
+        withWorkspace = WORKSPACE;
+        doTestImportXML();
+    }
+
+    public void testSessionImportXml() throws RepositoryException,
+            IOException, SAXException, NotExecutableException {
+        withHandler = false;
+        withWorkspace = SESSION;
+        doTestImportXML();
+    }
+
+    public void testWorkspaceGetImportContentHandler() throws RepositoryException,
+            SAXException, IOException, NotExecutableException {
+        withHandler = true;
+        withWorkspace = SESSION;
+        doTestGetImportContentHandler();
+    }
+
+    public void testSessionGetImportContentHandler() throws RepositoryException,
+            SAXException, IOException, NotExecutableException {
+        withHandler = true;
+        withWorkspace = WORKSPACE;
+        doTestGetImportContentHandler();
+    }
+
+    /**
+     * Tests importXML method with uuidBehaviour IMPORT_UUID_CREATE_NEW. It
+     * imports the document created with createSimpleDocument method and checks
+     * the imported tree according the rules outlined in chapter 7.3.2 of the
+     * specification.
+     * <p/>
+     * Additionally it checks the uuidBehaviour flag if the jcr:uuid property is
+     * respected during import.
+     *
+     * @throws RepositoryException
+     * @throws IOException
+     * @throws SAXException
+     * @throws NotExecutableException
+     */
+    public void doTestImportXML() throws RepositoryException, IOException,
+            SAXException, NotExecutableException {
+
+        importXML(target, createSimpleDocument(), uuidBehaviour, withWorkspace);
+        performTests();
+    }
+
+    /**
+     * Tests getImportContentHandler method with uuidBehaviour
+     * IMPORT_UUID_CREATE_NEW. It imports the document created with
+     * createSimpleDocument method and checks the imported tree according the
+     * rules outlined in chapter 7.3.2 of the specification.
+     * <p/>
+     * Additionally it checks the uuidBehaviour flag if the jcr:uuid property is
+     * respected during import.
+     *
+     * @throws RepositoryException
+     * @throws SAXException
+     * @throws IOException
+     * @throws NotExecutableException
+     */
+    public void doTestGetImportContentHandler() throws RepositoryException,
+            SAXException, IOException, NotExecutableException {
+
+        importWithHandler(target, createSimpleDocument(), uuidBehaviour, withWorkspace);
+        performTests();
+    }
+
+
+    private void performTests() throws RepositoryException, SAXException,
+            IOException, NotExecutableException {
+
+        checkImportSimpleXMLTree();
+        checkNamespaceAdded();
+        if (!respectMixRef) {
+            throw new NotExecutableException("ImportXML tests with " +
+                    "uuidBehaviour flag not executable.");
+        } else {
+            checkImportDocumentView_IMPORT_UUID_CREATE_NEW();
+            checkImportDocumentView_IMPORT_UUID_COLLISION_REMOVE_EXISTING();
+            checkImportDocumentView_IMPORT_UUID_COLLISION_REPLACE_EXISTING();
+            checkImportDocumentView_IMPORT_UUID_COLLISION_THROW();
+        }
+    }
+
+    /**
+     * Tests if the simple xml document defined in createSimpleDocument() is
+     * imported correctly according the specification rules given in  7.3.2
+     */
+    public void checkImportSimpleXMLTree() throws RepositoryException, IOException {
+        Node parent = (Node) session.getItem(target);
+
+        try {
+            // check the node names
+            String prefix = session.getNamespacePrefix(unusedURI);
+            String rootName = prefix + ":" + rootElem;
+            //String rootName = rootElem;
+            Node rootNode = parent.getNode(rootName);
+            Node child = rootNode.getNode(childElem);
+            Node xmlTextNode = rootNode.getNode(xmltextElem);
+            Node grandChild = xmlTextNode.getNode(grandChildElem);
+
+            // check xmltext
+            checkXmlTextNode(xmlTextNode);
+
+            // check the property names and values
+            Property prop = grandChild.getProperty(attributeName);
+            Property prop2 = xmlTextNode.getProperty(attributeName);
+            String value = prop.getString();
+            String value2 = prop2.getString();
+            assertEquals("Value " + attributeValue + " of attribute " +
+                    attributeName + " is imported to different property values.", value,
value2);
+            assertEquals("Value " + attributeValue + "  of attribute " +
+                    attributeName + " is not correctly imported.", value, attributeValue);
+
+            // check the encoded names and values
+            Property decodedProp;
+            // is decoded
+            try {
+                child.getNode(decodedElemName);
+                decodedProp = child.getProperty(decodedAttributeName);
+                String propVal = decodedProp.getString();
+                // both possibilities
+                if (!propVal.equals(encodedAttributeValue)
+                        || !propVal.equals(encodedAttributeValue)) {
+                    fail("Value " + encodedAttributeValue + "  of attribute " +
+                            decodedAttributeName + " is not correctly imported.");
+                }
+
+            } catch (PathNotFoundException pnfe) {
+                try {
+                    // is not decoded
+                    child.getNode(encodedElemName);
+                    decodedProp = child.getProperty(encodedAttributeName);
+                    String propVal = decodedProp.getString();
+                    // both possibilities
+                    if (!propVal.equals(encodedAttributeValue)
+                            || !propVal.equals(encodedAttributeValue)) {
+                        fail("Value " + encodedAttributeValue + "  of attribute " +
+                                encodedAttributeName + " is not correctly imported.");
+                    }
+                } catch (PathNotFoundException pnfe2) {
+                    fail("XML Element " + encodedElemName + " or attribute "
+                            + encodedAttributeName + " not imported: " + pnfe2);
+                }
+            }
+        } catch (PathNotFoundException pne) {
+            fail("Element or attribute is not imported: " + pne);
+        }
+    }
+
+    /**
+     * Tests if xmltext in a body of a xml element is correctly imported to a
+     * node with name jcr:xmltext and that the value of the text is stored in
+     * the singlevalued jcr:xmlcharacters property of String type.
+     *
+     * @throws RepositoryException
+     */
+    public void checkXmlTextNode(Node node) throws RepositoryException, IOException {
+
+        if (node.hasNode(JCR_XMLTEXT)) {
+            Node xmlNode = node.getNode(JCR_XMLTEXT);
+            if (xmlNode.hasProperty(JCR_XMLCHAR)) {
+                Property prop = xmlNode.getProperty(JCR_XMLCHAR);
+                // correct type?
+                assertTrue("Property " + prop.getPath() + " is not of type String.",
+                        prop.getType() == PropertyType.STRING);
+                // correct text?
+                // todo remove the trim as only the white spaces of the current text should
be collected
+                assertEquals("Xml text is not correctly stored.",
+                        xmltext.trim(), prop.getString().trim());
+                // only jcr:xmlcharacters property beneath the jcr:primaryType
+                PropertyIterator iter = xmlNode.getProperties();
+                assertTrue(JCR_XMLCHAR + " is not the only property beneath " +
+                        jcrPrimaryType + " in a " + JCR_XMLTEXT + " node.", getSize(iter)
== 2);
+            } else {
+                fail("Xmltext not stored in property named " + JCR_XMLCHAR);
+            }
+        } else {
+            fail("Xmltext not imported to Node named " + JCR_XMLTEXT);
+        }
+    }
+
+    /**
+     * Checks if a namespace not yet existing in the repository is registered
+     * after an according document import.
+     *
+     * @throws RepositoryException
+     * @throws IOException
+     */
+    public void checkNamespaceAdded() throws RepositoryException, IOException {
+        try {
+            assertEquals("URI not correctly imported.", nsp.getURI(unusedPrefix), unusedURI);
+            assertEquals("Prefix not correctly imported", nsp.getPrefix(unusedURI), unusedPrefix);
+        } catch (NamespaceException nse) {
+            fail("Namespace " + unusedPrefix + ":" + unusedURI +
+                    " not imported during document view import.");
+        }
+    }
+
+    //------------------< uuid collision behaviour tests >--------------------------
+    // we create a node named referenced below the testRootNode, also we create a xml
+    // document with an element named rootElem having the uuid of the referenced node
+    // as attribute and we import this document below the refTargetNode.
+
+    /**
+     * Checks {@link ImportUUIDBehavior#IMPORT_UUID_CREATE_NEW} i.e. that a node
+     * receives a new uuid when imported in any case.
+     */
+    public void checkImportDocumentView_IMPORT_UUID_CREATE_NEW()
+            throws RepositoryException, IOException, SAXException {
+
+        String uuid = createReferenceableNode(referenced);
+        // import a document with a element having the same uuid as the node referenced
+        importRefNodeDocument(uuid, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW, withWorkspace,
withHandler);
+
+        // different uuid?
+        Node node = refTargetNode.getNode(rootElem);
+        String rootElemUUID = node.getUUID();
+        assertFalse("Imported node " + rootElem + " has a UUID which is " +
+                "yet assigned to another node", uuid.equals(rootElemUUID));
+    }
+
+    /**
+     * Checks ImportUUIDBehavior.IMPORT_UUID_COLLISION_REMOVE_EXISTING i.e that
+     * the existing node is removed in case of uuid collision.
+     */
+    public void checkImportDocumentView_IMPORT_UUID_COLLISION_REMOVE_EXISTING()
+            throws RepositoryException, IOException, SAXException {
+
+        String uuid = createReferenceableNode(referenced);
+        // import a document with a element having the same uuid as the node referenced
+        importRefNodeDocument(uuid, ImportUUIDBehavior.IMPORT_UUID_COLLISION_REMOVE_EXISTING,
+                withWorkspace, withHandler);
+
+        try {
+            // should be removed now
+            testRootNode.getNode(referenced);
+            fail("UUID behavior IMPORT_UUID_COLLISION_REMOVE_EXISTING test is failed: " +
+                    "existing node not removed");
+        } catch (PathNotFoundException pnfe) {
+            // ok
+        }
+        try {
+            // should be there
+            refTargetNode.getNode(rootElem);
+        } catch (PathNotFoundException pnfe) {
+            fail("UUID behavior IMPORT_UUID_COLLISION_REMOVE_EXISTING test is failed: " +
+                    "imported node not in its correct place.");
+        }
+    }
+
+    /**
+     * Checks ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING i.e that
+     * the existing node is replaced by the imported one node when uuid
+     * collision occurs.
+     */
+    public void checkImportDocumentView_IMPORT_UUID_COLLISION_REPLACE_EXISTING()
+            throws RepositoryException, IOException, SAXException {
+
+        String uuid = createReferenceableNode(referenced);
+        // import a document with a element having the same uuid as the node referenced
+        importRefNodeDocument(uuid, ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING,
+                withWorkspace, withHandler);
+
+        // should be replaced i.e should be modified, in case Workspace method is used
+        //  we cannot decide unless we have some additional property of the imported node.
+        if (!withWorkspace) {
+            Node node = testRootNode.getNode(rootElem);
+            assertTrue("Node " + node.getPath() + " not replaced during " +
+                    "import with IMPORT_UUID_COLLISION_REPLACE_EXISTING", node.hasProperty(propertyName1));
+        }
+    }
+
+    /**
+     * Checks ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW i.e that a
+     * ItemExistsException is thrown in case of importing with an input stream
+     * or a SAXException is thrown in case of importing with a ContentHandler.
+     *
+     * @throws RepositoryException
+     * @throws IOException
+     */
+    public void checkImportDocumentView_IMPORT_UUID_COLLISION_THROW()
+            throws RepositoryException, IOException, SAXException {
+
+        String uuid = createReferenceableNode(referenced);
+        try {
+            importRefNodeDocument(uuid, ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW,
+                    withWorkspace, withHandler);
+            fail("UUID behavior IMPORT_UUID_COLLISION_THROW test is failed: " +
+                    "should throw an Exception.");
+        } catch (ItemExistsException e) {
+            if (!withHandler) {
+                // ok
+            } else {
+                throw e;
+            }
+        } catch (SAXException e) {
+            if (withHandler) {
+                // ok
+            } else {
+                throw e;
+            }
+        }
+    }
+}
\ No newline at end of file

Propchange: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/DocumentViewImportTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/TestAll.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/TestAll.java?rev=162058&r1=162057&r2=162058&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/TestAll.java (original)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/TestAll.java Wed Apr
20 12:53:14 2005
@@ -125,6 +125,8 @@
         suite.addTestSuite(ImpersonateTest.class);
         suite.addTestSuite(CheckPermissionTest.class);
 
+        suite.addTestSuite(DocumentViewImportTest.class);
+
         return suite;
     }
 }



Mime
View raw message