serializationtest.java

来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 756 行 · 第 1/2 页

JAVA
756
字号
/* * 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.AbstractJCRTest;import org.apache.jackrabbit.test.NotExecutableException;import org.xml.sax.ContentHandler;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import org.xml.sax.XMLReader;import org.xml.sax.helpers.XMLReaderFactory;import javax.jcr.ImportUUIDBehavior;import javax.jcr.InvalidSerializedDataException;import javax.jcr.ItemExistsException;import javax.jcr.Node;import javax.jcr.PathNotFoundException;import javax.jcr.Repository;import javax.jcr.RepositoryException;import javax.jcr.Session;import javax.jcr.Workspace;import javax.jcr.lock.Lock;import javax.jcr.lock.LockException;import javax.jcr.version.VersionException;import javax.xml.parsers.ParserConfigurationException;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.Reader;import java.io.StringReader;/** * <code>SerializationTest</code> contains the test cases for the method * <code>Workspace.exportSysView()</code> and <code>Session.importSysView()</code>. * <p/> * This class exports and re-imports the repository. The tests check for * differences between the original and the re-imported repository. * * @test * @sources SerializationTest.java * @executeClass org.apache.jackrabbit.test.api.SerializationTest * @keywords level2 */public class SerializationTest extends AbstractJCRTest {    protected Workspace workspace;    protected File file;    protected TreeComparator treeComparator;    protected final boolean CONTENTHANDLER = true, STREAM = false;    protected final boolean WORKSPACE = true, SESSION = false;    protected final boolean SKIPBINARY = true, SAVEBINARY = false;    protected final boolean NORECURSE = true, RECURSE = false;    protected Session session;    public void setUp() throws RepositoryException, Exception {        super.setUp();        try {            session = superuser;            workspace = session.getWorkspace();            file = File.createTempFile("serializationTest", ".xml");            log.print("Tempfile: " + file.getAbsolutePath());              SerializationContext sc = new SerializationContext(this);            treeComparator = new TreeComparator(sc, session);            treeComparator.createComplexTree(treeComparator.WORKSPACE);        }        catch (Exception ex) {            if (file != null) {                file.delete();                file = null;            }            throw (ex);        }    }    public void tearDown() throws Exception {        if (file != null) {            file.delete();            file = null;        }        if (session != null && session.isLive()) {            session.logout();        }        if (treeComparator != null) {            treeComparator.tearDown();        }        super.tearDown();    }// ---------------< versioning exception tests >-----------------------------------------    /**     * Creates a simple target tree consisting of a checked-in node and an     * ordinary child node below. Throws a {@link NotExecutableException} if     * {@link #testNodeType} is not versionable.     *     * @param returnParent Whether the method returns a checked-in parent or the     *                     child of a checked-in parent.     * @return The requested node (a node that is checked in or that has a     *         parent that is checked in).     */    protected Node initVersioningException(boolean returnParent) throws RepositoryException, NotExecutableException, IOException {        Node vNode = testRootNode.addNode(nodeName1, testNodeType);        if (!vNode.isNodeType(mixVersionable)) {            if (vNode.canAddMixin(mixVersionable)) {                vNode.addMixin(mixVersionable);            } else {                throw new NotExecutableException("NodeType: " + testNodeType + " is not versionable");            }        }        Node vChild = vNode.addNode(nodeName2, testNodeType);        session.save();        vNode.checkin();        exportRepository(SKIPBINARY, RECURSE);        if (returnParent) {            return vNode;        } else {            return vChild;        }    }    public void doTestVersioningExceptionFileParent(boolean useWorkspace, boolean useHandler)            throws Exception {        Node n = initVersioningException(true);        FileInputStream in = new FileInputStream(file);        try {            doImport(n.getPath(), in, useWorkspace, useHandler);            fail("Importing to a checked-in node must throw a ConstraintViolationException.");        } catch (VersionException e) {            // success        } finally {            in.close();        }    }    public void doTestVersioningExceptionFileChild(boolean useWorkspace, boolean useHandler)            throws Exception {        Node n = initVersioningException(false);        FileInputStream in = new FileInputStream(file);        try {            doImport(n.getPath(), in, useWorkspace, useHandler);            fail("Importing to a child of a checked-in node must throw a ConstraintViolationException.");        } catch (VersionException e) {            // success        } finally {            in.close();        }    }    public void testVersioningExceptionFileParentWorkspaceContentHandler() throws Exception {        doTestVersioningExceptionFileParent(WORKSPACE, CONTENTHANDLER);    }    public void testVersioningExceptionFileParentSessionContentHandler() throws Exception {        doTestVersioningExceptionFileParent(SESSION, CONTENTHANDLER);    }    public void testVersioningExceptionFileParentWorkspace() throws Exception {        doTestVersioningExceptionFileParent(WORKSPACE, STREAM);    }    public void testVersioningExceptionFileParentSession() throws Exception {        doTestVersioningExceptionFileParent(SESSION, STREAM);    }    public void testVersioningExceptionFileChildWorkspaceContentHandler() throws Exception {        doTestVersioningExceptionFileChild(WORKSPACE, CONTENTHANDLER);    }    public void testVersioningExceptionFileChildSessionContentHandler() throws Exception {        doTestVersioningExceptionFileChild(SESSION, CONTENTHANDLER);    }    public void testVersioningExceptionFileChildWorkspace() throws Exception {        doTestVersioningExceptionFileChild(WORKSPACE, STREAM);    }    public void testVersioningExceptionFileChildSession() throws Exception {        doTestVersioningExceptionFileChild(SESSION, STREAM);    }// ----------------< locking exceptions tests >----------------------------    /**     * Tests whether importing a tree respects locking.     */    public void doTestLockException(boolean useWorkspace, boolean useHandler)            throws Exception {        exportRepository(SKIPBINARY, RECURSE);        if (isSupported(Repository.OPTION_LOCKING_SUPPORTED)) {            //A LockException is thrown if a lock prevents the addition of the subtree.            Node lNode = testRootNode.addNode(nodeName1);            lNode.addMixin(mixLockable);            testRootNode.save();            Lock lock = lNode.lock(true, true);            session.removeLockToken(lock.getLockToken());   //remove the token, so the lock is for me, too            FileInputStream in = new FileInputStream(file);            try {                doImport(lNode.getPath(), in, useWorkspace, useHandler);            } catch (LockException e) {                // success            } finally {                in.close();            }        } else {            log.println("Locking not supported.");        }    }    public void testLockExceptionWorkspaceWithHandler() throws Exception {        doTestVersioningExceptionFileChild(WORKSPACE, CONTENTHANDLER);    }    public void testLockExceptionSessionWithHandler() throws Exception {        doTestVersioningExceptionFileChild(SESSION, CONTENTHANDLER);    }    public void testLockExceptionWorkspace() throws Exception {        doTestVersioningExceptionFileChild(WORKSPACE, STREAM);    }    public void testLockExceptionSession() throws Exception {        doTestVersioningExceptionFileChild(SESSION, STREAM);    }//--------------< Import of invalid xml file tests >-------------------------------------------    /**     * Tests whether importing an invalid XML file throws a SAX exception. The     * file used here is more or less garbage.     */    public void testInvalidXmlThrowsSaxException()            throws IOException, ParserConfigurationException {        StringReader in = new StringReader("<this is not a <valid> <xml> file/>");        ContentHandler ih = null;        try {            ih = session.getImportContentHandler(treeComparator.targetFolder,                    ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);        } catch (RepositoryException e) {            fail("ImportHandler not created: " + e);        }        helpTestSaxException(ih, in, "session");        in = new StringReader("<this is not a <valid> <xml> file/>");        try {            ih = workspace.getImportContentHandler(treeComparator.targetFolder, 0);        } catch (RepositoryException e) {            fail("ImportHandler not created: " + e);        }        helpTestSaxException(ih, in, "workspace");    }    /**     * Helper method for testSaxException.     *     * @param ih     * @param in     */    private void helpTestSaxException(ContentHandler ih, Reader in, String mode)            throws IOException {        try {            createXMLReader(ih).parse(new InputSource(in));            fail("Parsing an invalid XML file with via " + mode + " ContentHandler did not throw a SAXException.");        } catch (SAXException e) {            // success        }    }    /**     * Tests whether importing an invalid XML file throws a InvalidSerializedDataException.     * The file used here is more or less garbage.     */    public void testInvalidXmlThrowsInvalidSerializedDataException()            throws RepositoryException, IOException {        String data = "<this is not a <valid> <xml> file/>";        ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes());        try {            session.importXML(treeComparator.targetFolder, in,                    ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);            fail("Importing a invalid XML file should throw a InvalidSerializedDataException.");        } catch (InvalidSerializedDataException e) {            // ok        }        in = new ByteArrayInputStream(data.getBytes());        try {            workspace.importXML(treeComparator.targetFolder, in, 0);            fail("Importing a invalid XML file should throw a InvalidSerializedDataException.");        } catch (InvalidSerializedDataException e) {            // ok        }    }// -------------------< PathNotFoundException tests >------------------------------------    /**     * Supplying an invalid repository path for import must throw a     * PathNotFoundException     */    public void testWorkspaceGetImportContentHandlerExceptions() throws RepositoryException {        //Specifying a path that does not exist throws a PathNotFound exception        try {            workspace.getImportContentHandler(treeComparator.targetFolder + "/thisIsNotAnExistingNode",                    ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);            fail("Specifying a non-existing path must throw a PathNotFoudException.");        } catch (PathNotFoundException e) {            // success        }    }    /**     * Supplying an invalid repository path for import must throw a     * PathNotFoundException     */    public void testSessionGetImportContentHandlerExceptions() throws RepositoryException {        //Specifying a path that does not exist throws a PathNotFound exception        try {            session.getImportContentHandler(treeComparator.targetFolder + "/thisIsNotAnExistingNode",                    ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);            fail("Specifying a non-existing path must throw a PathNotFoudException.");        } catch (PathNotFoundException e) {            // success        }    }    /**     * Tests the exception when importing: If the parent node does not exist.     */    public void testSessionImportXmlExceptions() throws RepositoryException, IOException {        exportRepository(SKIPBINARY, RECURSE);        FileInputStream in = new FileInputStream(file);        // If no node exists at parentAbsPath, a PathNotFoundException is thrown.        try {            session.importXML(treeComparator.targetFolder + "/thisNodeDoesNotExist",                    in, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);            fail("Importing to a non-existing node does not throw a PathNotFoundException.");        } catch (PathNotFoundException e) {            // success        } finally {            in.close();        }    }    /**     * Tests the exceptions when importing: If the parent node does not exist,     * and if an IO error occurs.     */    public void testWorkspaceImportXmlExceptions() throws RepositoryException, IOException {        exportRepository(SKIPBINARY, RECURSE);        FileInputStream in = new FileInputStream(file);        //If no node exists at parentAbsPath, a PathNotFoundException is thrown.        try {            workspace.importXML(treeComparator.targetFolder + "/thisNodeDoesNotExist", in, 0);            fail("Importing to a non-existing node does not throw a PathNotFoundException.");        } catch (PathNotFoundException e) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?