⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 treecomparator.java

📁 jsr170接口的java实现。是个apache的开源项目。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * 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 javax.jcr.Session;import javax.jcr.Workspace;import javax.jcr.Item;import javax.jcr.Node;import javax.jcr.RepositoryException;import javax.jcr.NodeIterator;import javax.jcr.PropertyIterator;import javax.jcr.Property;import javax.jcr.PropertyType;import javax.jcr.Value;import javax.jcr.PathNotFoundException;import javax.jcr.nodetype.NodeTypeManager;import javax.jcr.nodetype.NodeTypeIterator;import javax.jcr.nodetype.NodeType;import java.util.Calendar;import java.io.ByteArrayInputStream;/** * <code>TreeComparator</code> compares two trees. This allows re-use for * different tests, and it allows to test a function on any tree, not just a * simple example node. * <p/> * TreeComparator also creates an example tree that contains as many features as * possible. */class TreeComparator extends AbstractJCRTest {    public SerializationContext sc;    public final boolean WORKSPACE = true;    public final boolean SESSION = false;    public final int CHECK_EMPTY = -1;    public final int IGNORE = 0;    public final int CHECK_SAME = 1;    public int check = CHECK_SAME;    private Session session;    private Workspace workspace;    public boolean skipBinary = false, noRecurse = false;    public String sourceFolder, targetFolder;    public String root;    public TreeComparator(SerializationContext sc, Session s) throws Exception {        this.sc = sc;        setUp();        session = s;        workspace = session.getWorkspace();        init();    }    public void tearDown() throws Exception {        super.tearDown();    }    public void setSession(Session session) {        this.session = session;    }        /**     * Makes sure that the source and target folder exist, and are empty     */    private void init() throws RepositoryException {        root = sc.testroot;        sourceFolder = root + "/" + sc.sourceFolderName;        targetFolder = root + "/" + sc.targetFolderName;        // make sure the node does not have a and target sub node.        try {            session.getItem(sourceFolder).remove();            session.save();        } catch (PathNotFoundException e) {            // item does not exist        }        try {            Item tgt = session.getItem(targetFolder);            tgt.remove();            session.save();        } catch (PathNotFoundException e) {            // item does not exist        }        // add the source and target nodes        Node rootNode = (Node) session.getItem(root);        rootNode.addNode(sc.sourceFolderName);        rootNode.addNode(sc.targetFolderName);        session.save();    }    /**     * Creates an example tree in the workspace.     */    public void createExampleTree() {        createExampleTree(true);    }    /**     * Creates a simple example tree. Use this tree for general repository     * functions, such as serialization, namespaces and versioning.     * <p/>     * The beauty of this is that the tree contains exactly the features that     * are supported by the repository. Any repository exceptions that occur are     * displayed on "out", but are otherwise ignored.     *     * @param save If true, the example tree is saved to the workspace. If     *             false, it remains in the session.     */    public void createExampleTree(boolean save) {        try {            Node src = (Node) session.getItem(sourceFolder);            Node root = src.addNode(sc.rootNodeName);            root.addNode(nodeName1);            root.addNode(nodeName2, testNodeType);            byte[] byteArray = {(byte) 0, (byte) 255, (byte) 167, (byte) 100, (byte) 21, (byte) 6, (byte) 19, (byte) 71, (byte) 221};            root.setProperty(propertyName1, new ByteArrayInputStream(byteArray));            root.setProperty(nodeName3, "hello");        } catch (Exception e) {            log.println("Error while creating example tree: " + e.getMessage());        }        if (save) {            try {                session.save();            } catch (RepositoryException e) {                fail("Cannot save the example tree to the repository: " + e);            }        }    }    /**     * Creates a complex example tree in the workspace     */    public void createComplexTree() {        createComplexTree(true);    }    /**     * Creates a complex example tree that uses as many features of the     * repository as possible     *     * @param save Save the tree to the workspace. If false, the tree remains in     *             the session.     */    public void createComplexTree(boolean save) {        Node rootNode = null;        Node nt = null;        Node pt = null;        Node mvp = null;        Node referenceable = null;        try {            Node src = (Node) session.getItem(sourceFolder);            rootNode = src.addNode(sc.rootNodeName);            nt = rootNode.addNode(sc.nodeTypesTestNode);            rootNode.addNode(sc.mixinTypeTestNode);            pt = rootNode.addNode(sc.propertyTypesTestNode);            rootNode.addNode(sc.sameNameChildrenTestNode);            mvp = rootNode.addNode(sc.multiValuePropertiesTestNode);            referenceable = rootNode.addNode(sc.referenceableNodeTestNode);            rootNode.addNode(sc.orderChildrenTestNode);            rootNode.addNode(sc.namespaceTestNode);        } catch (RepositoryException e) {            log.println("Error while creating example tree: " + e.getMessage());        }        // Add nodes with mixin types        NodeTypeManager ntmgr = null;        NodeTypeIterator types = null;        try {            ntmgr = workspace.getNodeTypeManager();            types = ntmgr.getMixinNodeTypes();        } catch (RepositoryException e) {            fail("Cannot access NodeType iterator: " + e);        }        while (types.hasNext()) {            NodeType t = (NodeType) types.next();            String name = t.getName();            name = "Node_" + name.replaceAll(":", "_");            Node n = null;            try {                n = nt.addNode(name);                n.addMixin(t.getName());                // try saving, because some exceptions are trown only at save time                session.save();            } catch (RepositoryException e) {                log.println("Cannot create node with mixin node type: " + e);                // if saving failed for a node, then remove it again (or else the next save will fail on it)                try {                    if (n != null) {                        n.remove();                    }                } catch (RepositoryException e1) {                    log.println("Could not remove node: " + e);                }            }        }        // Create all property types        try {            // String            pt.setProperty(sc.stringTestProperty, "This is a string.");            // Binary            byte[] byteArray = {(byte) 0, (byte) 255, (byte) 167, (byte) 100, (byte) 21, (byte) 6, (byte) 19, (byte) 71, (byte) 221};            pt.setProperty(sc.binaryTestProperty, new ByteArrayInputStream(byteArray));            // Date            Calendar c = Calendar.getInstance();            c.set(2005, 6, 21, 13, 30, 5);            pt.setProperty(sc.dateTestProperty, c);            // Long            pt.setProperty(sc.longTestProperty, (long) (1 / 3));            // Double            pt.setProperty(sc.doubleTestProperty, (double) Math.PI);            // Boolean            pt.setProperty(sc.booleanTestProperty, true);            // Name            pt.setProperty(sc.nameTestProperty, superuser.getValueFactory().createValue(jcrPrimaryType, PropertyType.NAME));            // Path            pt.setProperty(sc.pathTestProperty, superuser.getValueFactory().createValue("paths/dont/have/to/point/anywhere", PropertyType.PATH));            // Reference: Note that I only check if the node exists. We do not specify what happens with            // the UUID during serialization.            if (!referenceable.isNodeType(mixReferenceable)) {                referenceable.addMixin(mixReferenceable);                // some implementations may require a save after addMixin()                                session.save();            }            pt.setProperty(sc.referenceTestProperty, referenceable);            // Create a boolean property on the root node, so I can test noRecurse and skipBinary at the same time            rootNode.setProperty(sc.binaryTestProperty, new ByteArrayInputStream(byteArray));            session.save();        } catch (Exception e) {            fail("Could not add property: " + e);        }        // multi value properties        String[] s = {"one", "two", "three"};        try {            mvp.setProperty(sc.multiValueTestProperty, s);            session.save();        } catch (RepositoryException e) {            log.println("Could not create multi-value property: " + e);        }        // Save to the workspace. Note that export is from session anyway.        if (save) {            try {                session.save();            } catch (RepositoryException e) {                fail("Cannot save the example tree to the repository: " + e);            }        }    }    /**     * Compares the trees in the source and target folder.     *     * @param skipBinary True if skipbinary is set, so binary properties are not     *                   in the taget tree.     * @param noRecurse  True if noRecurse is used, so only the top node and its     *                   properties are in the target tree.     */    public void compare(boolean skipBinary, boolean noRecurse) {        this.skipBinary = skipBinary;        this.noRecurse = noRecurse;        compare();    }    /**     * Compares the source and target tree.     */    public void compare() {        compare(CHECK_SAME);    }    /**     * Compares the source and target tree.     *     * @param check CHECK_SAME checks if the two trees have the same nodes and     *              properties. CHECK_EMPTY checks that the target tree is     *              empty.     */    public void compare(int check) {

⌨️ 快捷键说明

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