serializationtest.java

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

JAVA
756
字号
            // success        } finally {            in.close();        }    }// ---------------< Overwrite existing target node tests     >------------------------------// ---------------< in case same name siblings not supported >------------------------------    /**     * Tries to overwrite an existing node. This only works for nodes that do     * not allow same-name siblings.     */    public void doTestOverwriteException(boolean useWorkspace, boolean useHandler)            throws Exception {        //If deserialization would overwrite an existing item,        // an ItemExistsException respective a SAXException is thrown.        Node folder = testRootNode.addNode("myFolder", treeComparator.sc.sameNameSibsFalseChildNodeDefinition);        Node subfolder = folder.addNode("subfolder");        session.save();        FileOutputStream out = new FileOutputStream(file);        try {            session.exportSystemView(subfolder.getPath(), out, true, true);        } finally {            out.close();        }        FileInputStream in = new FileInputStream(file);        try {            if (useHandler) {                try {                    doImport(folder.getPath(), in, useWorkspace, useHandler);                    fail("Overwriting an existing node during import must throw a SAXException");                } catch (SAXException e) {                    // success                }            } else {                try {                    doImport(folder.getPath(), in, useWorkspace, useHandler);                    fail("Overwriting an existing node during import must throw an ItemExistsException");                } catch (ItemExistsException e) {                    // success                }            }        } finally {            in.close();        }    }    public void testOverwriteExceptionWorkspaceWithHandler() throws Exception {        doTestOverwriteException(WORKSPACE, CONTENTHANDLER);    }    public void testOverwriteExceptionSessionWithHandler() throws Exception {        doTestOverwriteException(SESSION, CONTENTHANDLER);    }    public void testOverwriteExceptionWorkspace() throws Exception {        doTestOverwriteException(WORKSPACE, STREAM);    }    public void testOverwriteExceptionSession() throws Exception {        doTestOverwriteException(SESSION, STREAM);    }    // ------------------< Node type constraint violation tests >--------------------------------    /**     * Create a node named ntBase with node type nt:base     * and creates a tree in the repository which will be exported     * and reimported below the node ntBase.     *     * @param useWorkspace     * @param useHandler     * @throws RepositoryException     * @throws FileNotFoundException     * @throws IOException     */    public void doTestNodeTypeConstraintViolation(boolean useWorkspace, boolean useHandler)            throws Exception {        treeComparator.createExampleTree();        Node node = testRootNode.addNode("ntBase", ntBase);        session.save();        FileInputStream in = new FileInputStream(file);        try {            if (useHandler) {                try {                    doImport(node.getPath(), in, useWorkspace, useHandler);                    fail("Node type constraint violation should throw a SAXException " +                            "during xml import using a Contenthandler.");                } catch (SAXException se) {                    // ok                }            } else {                try {                    doImport(node.getPath(), in, useWorkspace, useHandler);                    fail("Node type constraint violation should throw a  " +                            " InvalidSerializedDataException during xml import " +                            "using a Contenthandler.");                } catch (InvalidSerializedDataException isde) {                    // ok                }            }        } finally {            in.close();        }    }    public void testNodeTypeConstraintViolationWorkspaceWithHandler() throws Exception {        doTestNodeTypeConstraintViolation(WORKSPACE, CONTENTHANDLER);    }    public void testNodeTypeConstraintViolationSessionWithHandler() throws Exception {        doTestNodeTypeConstraintViolation(SESSION, CONTENTHANDLER);    }    public void testNodeTypeConstraintViolationWorkspace() throws Exception {        doTestNodeTypeConstraintViolation(WORKSPACE, STREAM);    }    public void testNodeTypeConstraintViolationSession() throws Exception {        doTestNodeTypeConstraintViolation(SESSION, STREAM);    }// ------------< tests that nothing is imported if session is closed before saving the import >-----------------    /**     * Makes sure that importing into the session does not save anything if the     * session is closed.     */    public void testSessionImportXml() throws RepositoryException, IOException {        FileInputStream in = new FileInputStream(file);        try {            exportRepository(SAVEBINARY, RECURSE);            session.importXML(treeComparator.targetFolder, in,                    ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);        } finally {            in.close();        }        // after logout/login, no nodes are in the session        session.logout();        superuser = null; //so tearDown won't fail        session = helper.getReadWriteSession();        treeComparator.setSession(session);        treeComparator.compare(treeComparator.CHECK_EMPTY);    }    /**     * Makes sure that importing into the session does not save anything if the     * session is closed.     */    public void testSessionGetContentHandler() throws Exception {        FileInputStream in = new FileInputStream(file);        try {            exportRepository(SAVEBINARY, RECURSE);            doImportNoSave(treeComparator.targetFolder, in, CONTENTHANDLER);        } finally {            in.close();        }        // after logout/login, no nodes are in the session        session.logout();        superuser = null; //so tearDown won't fail        session = helper.getReadWriteSession();        treeComparator.setSession(session);        treeComparator.compare(treeComparator.CHECK_EMPTY);    }//----------------< import test helper >--------------------------------------------------------    /**     * Helper method which imports the given FileInputStream using Workspace or Session     * and via the methods importXML respective getImportContentHandler. Teh target node of the     * import is specified with its absolut path.     *     * @param absPath     * @param in     * @param useWorkspace     * @param useHandler     * @throws RepositoryException     * @throws IOException     */    public void doImport(String absPath, FileInputStream in, boolean useWorkspace, boolean useHandler)            throws Exception {        if (useHandler) {            if (useWorkspace) {                ContentHandler ih = workspace.getImportContentHandler(absPath, 0);                createXMLReader(ih).parse(new InputSource(in));            } else {                ContentHandler ih = session.getImportContentHandler(absPath, 0);                createXMLReader(ih).parse(new InputSource(in));                session.save();            }        } else {            if (useWorkspace) {                workspace.importXML(absPath, in, 0);            } else {                session.importXML(absPath, in, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);                session.save();            }        }    }    public void doImportNoSave(String absPath, FileInputStream in, boolean useHandler)            throws Exception {        if (useHandler) {            ContentHandler ih = session.getImportContentHandler(absPath, 0);            createXMLReader(ih).parse(new InputSource(in));        } else {            session.importXML(absPath, in, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);        }    }//------------< System view export import tests >-----------------------------------    public void testExportSysView_stream_workspace_skipBinary_noRecurse() throws Exception {        doTest(STREAM, WORKSPACE, SKIPBINARY, NORECURSE);    }    public void testExportSysView_stream_workspace_skipBinary_recurse() throws Exception {        doTest(STREAM, WORKSPACE, SKIPBINARY, RECURSE);    }    public void testExportSysView_stream_workspace_saveBinary_noRecurse() throws Exception {        doTest(STREAM, WORKSPACE, SAVEBINARY, NORECURSE);    }    public void testExportSysView_stream_workspace_saveBinary_recurse() throws Exception {        doTest(STREAM, WORKSPACE, SAVEBINARY, RECURSE);    }    public void testExportSysView_stream_session_skipBinary_noRecurse() throws Exception {        doTest(STREAM, SESSION, SKIPBINARY, NORECURSE);    }    public void testExportSysView_stream_session_skipBinary_recurse() throws Exception {        doTest(STREAM, SESSION, SKIPBINARY, RECURSE);    }    public void testExportSysView_stream_session_saveBinary_noRecurse() throws Exception {        doTest(STREAM, SESSION, SAVEBINARY, NORECURSE);    }    public void testExportSysView_stream_session_saveBinary_recurse() throws Exception {        doTest(STREAM, SESSION, SAVEBINARY, RECURSE);    }    public void testExportSysView_handler_workspace_skipBinary_noRecurse() throws Exception {        doTest(CONTENTHANDLER, WORKSPACE, SKIPBINARY, NORECURSE);    }    public void testExportSysView_handler_workspace_skipBinary_recurse() throws Exception {        doTest(CONTENTHANDLER, WORKSPACE, SKIPBINARY, RECURSE);    }    public void testExportSysView_handler_workspace_saveBinary_noRecurse() throws Exception {        doTest(CONTENTHANDLER, WORKSPACE, SAVEBINARY, NORECURSE);    }    public void testExportSysView_handler_workspace_saveBinary_recurse() throws Exception {        doTest(CONTENTHANDLER, WORKSPACE, SAVEBINARY, RECURSE);    }    public void testExportSysView_handler_session_skipBinary_noRecurse() throws Exception {        doTest(CONTENTHANDLER, SESSION, SKIPBINARY, NORECURSE);    }    public void testExportSysView_handler_session_skipBinary_recurse() throws Exception {        doTest(CONTENTHANDLER, SESSION, SKIPBINARY, RECURSE);    }    public void testExportSysView_handler_session_saveBinary_noRecurse() throws Exception {        doTest(CONTENTHANDLER, SESSION, SAVEBINARY, NORECURSE);    }    public void testExportSysView_handler_session_saveBinary_recurse() throws Exception {        doTest(CONTENTHANDLER, SESSION, SAVEBINARY, RECURSE);    }    /**     * Exports the tree at source node, imports it at the traget node, and     * compares the source and target tree.     *     * @param handler    true = use content handler for import, false = use the     *                   importXML method     * @param workspace  true = import to the workspace, false = import to the     *                   session (export is from session anyway)     * @param skipBinary true = skip binary properties. The binary properties     *                   are omitted (without any replacement)     * @param noRecurse  true = export only top node, false = export entire     *                   subtree     */    private void doTest(boolean handler, boolean workspace, boolean skipBinary, boolean noRecurse)            throws Exception {        exportRepository(skipBinary, noRecurse);        importRepository(handler, workspace);        treeComparator.showTree();        treeComparator.compare(skipBinary, noRecurse);    }    /**     * Exports the repository to a temporary file using the system view     * serialization.     *     * @param skipBinary true = omit any binary properties (without any     *                   replacement)     * @param noRecurse  true = save only top node, false = save entire subtree     * @throws IOException     */    private void exportRepository(boolean skipBinary, boolean noRecurse) throws IOException {        FileOutputStream out = new FileOutputStream(file);        try {            session.refresh(false); //move the workspace into the session, then save it. The workspace is always valid, the session not necessarily.            session.exportSystemView(treeComparator.getSourceRootPath(), out, skipBinary, noRecurse);        } catch (RepositoryException e) {            fail("Could not export the repository: " + e);        } finally {            out.close();        }    }    /**     * Imports the repository     *     * @param useHandler True = use the import handler, false = use file input     *                   stream     * @param workspace  True = import into workspace, false = import into     *                   session     */    public void importRepository(boolean useHandler, boolean workspace) throws Exception {        FileInputStream in = new FileInputStream(file);        try {            if (useHandler) {                if (workspace) {                    ContentHandler ih = this.workspace.getImportContentHandler(treeComparator.targetFolder, 0);                    createXMLReader(ih).parse(new InputSource(in));                } else {                    ContentHandler ih = session.getImportContentHandler(treeComparator.targetFolder,                            ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);                    createXMLReader(ih).parse(new InputSource(in));                    session.save();                }            } else {                if (workspace) {                    this.workspace.importXML(treeComparator.targetFolder, in, 0);                } else {                    session.importXML(treeComparator.targetFolder, in,                            ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);                    session.save();                }            }        } catch (SAXException e) {            fail("Error while parsing the imported repository: " + e);        } finally {            in.close();        }    }    /**     * Creates an XMLReader for the given content handler.     *     * @param handler the content handler.     * @return an XMLReader for the given content handler.     * @throws SAXException if the reader cannot be created.     */    private XMLReader createXMLReader(ContentHandler handler) throws SAXException {        XMLReader reader = XMLReaderFactory.createXMLReader();        reader.setFeature("http://xml.org/sax/features/namespaces", true);        reader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);        reader.setContentHandler(handler);        return reader;    }}

⌨️ 快捷键说明

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