📄 documentviewimporttest.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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 javax.jcr.nodetype.ConstraintViolationException;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 Exception { withHandler = false; withWorkspace = WORKSPACE; doTestImportXML(); } public void testSessionImportXml() throws Exception { withHandler = false; withWorkspace = SESSION; doTestImportXML(); } public void testWorkspaceGetImportContentHandler() throws Exception { withHandler = true; withWorkspace = SESSION; doTestGetImportContentHandler(); } public void testSessionGetImportContentHandler() throws Exception { 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 Exception { importXML(target, createSimpleDocument(), uuidBehaviour, withWorkspace); // some implementations may require a refresh to get content // added diretly to the workspace if (withWorkspace) { session.refresh(false); } 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 Exception { importWithHandler(target, createSimpleDocument(), uuidBehaviour, withWorkspace); // some implementations may require a refresh to get content // added diretly to the workspace if (withWorkspace) { session.refresh(false); } performTests(); } private void performTests() throws Exception { 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 "
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -