xatest.java

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

JAVA
1,187
字号
/* * 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.core;import org.apache.jackrabbit.test.AbstractJCRTest;import javax.jcr.Repository;import javax.jcr.Node;import javax.jcr.ItemNotFoundException;import javax.jcr.Session;import javax.jcr.RepositoryException;import javax.jcr.NodeIterator;import javax.jcr.version.VersionException;import javax.jcr.version.Version;import javax.jcr.lock.Lock;import javax.transaction.UserTransaction;import javax.transaction.RollbackException;import java.util.StringTokenizer;/** * <code>XATest</code> contains the test cases for the methods * inside {@link org.apache.jackrabbit.core.XASession}. */public class XATest extends AbstractJCRTest {    /**     * Other superuser.     */    private Session otherSuperuser;    /**     * {@inheritDoc}     */    protected void setUp() throws Exception {        super.setUp();        otherSuperuser = helper.getSuperuserSession();        // clean testroot on second workspace        Session s2 = helper.getSuperuserSession(workspaceName);        Node root = s2.getRootNode();        if (root.hasNode(testPath)) {            // clean test root            Node testRootNode = root.getNode(testPath);            for (NodeIterator children = testRootNode.getNodes(); children.hasNext();) {                children.nextNode().remove();            }        } else {            // create nodes to testPath            StringTokenizer names = new StringTokenizer(testPath, "/");            Node currentNode = root;            while (names.hasMoreTokens()) {                String name = names.nextToken();                if (currentNode.hasNode(name)) {                    currentNode = currentNode.getNode(name);                } else {                    currentNode = currentNode.addNode(name, testNodeType);                }            }        }        root.save();    }    /**     * {@inheritDoc}     */    protected void tearDown() throws Exception {        if (otherSuperuser != null) {            otherSuperuser.logout();        }        super.tearDown();    }    /**     * @see junit.framework#runTest     *     * Make sure that tested repository supports transactions     */    protected void runTest() throws Throwable {        if (isSupported(Repository.OPTION_TRANSACTIONS_SUPPORTED)) {            super.runTest();        }    }    /**     * Add a node inside a transaction and commit changes. Make sure     * node exists for other sessions only after commit.     * @throws Exception     */    public void testAddNodeCommit() throws Exception {        // get user transaction object        UserTransaction utx = new UserTransactionImpl(superuser);        // start transaction        utx.begin();        // add node and save        Node n = testRootNode.addNode(nodeName1, testNodeType);        n.addMixin(mixReferenceable);        testRootNode.save();        // assertion: node exists in this session        try {            superuser.getNodeByUUID(n.getUUID());        } catch (ItemNotFoundException e) {            fail("New node not visible after save()");        }        // assertion: node does not exist in other session        Session otherSuperuser = helper.getSuperuserSession();        try {            otherSuperuser.getNodeByUUID(n.getUUID());            fail("Uncommitted node visible for other session");        } catch (ItemNotFoundException e) {            /* expected */        }        // commit        utx.commit();        // assertion: node exists in this session        try {            superuser.getNodeByUUID(n.getUUID());        } catch (ItemNotFoundException e) {            fail("Committed node not visible in this session");        }        // assertion: node also exists in other session        try {            otherSuperuser.getNodeByUUID(n.getUUID());        } catch (ItemNotFoundException e) {            fail("Committed node not visible in other session");        }        // logout        otherSuperuser.logout();    }    /**     * Set a property inside a transaction and commit changes. Make sure     * property exists for other sessions only after commit.     * @throws Exception     */    public void testSetPropertyCommit() throws Exception {        // prerequisite: non-existing property        if (testRootNode.hasProperty(propertyName1)) {            testRootNode.getProperty(propertyName1).remove();            testRootNode.save();        }        // get user transaction object        UserTransaction utx = new UserTransactionImpl(superuser);        // start transaction        utx.begin();        // set property and save        testRootNode.setProperty(propertyName1, "0");        testRootNode.save();        // assertion: property exists in this session        assertTrue(testRootNode.hasProperty(propertyName1));        // assertion: property does not exist in other session        Session otherSuperuser = helper.getSuperuserSession();        Node otherRootNode = otherSuperuser.getRootNode().getNode(testPath);        assertFalse(otherRootNode.hasProperty(propertyName1));        // commit        utx.commit();        // assertion: property exists in this session        assertTrue(testRootNode.hasProperty(propertyName1));        // assertion: property also exists in other session        assertTrue(otherRootNode.hasProperty(propertyName1));        // logout        otherSuperuser.logout();    }    /**     * @throws Exception     */    public void testAddAndSetProperty() throws Exception {        // prerequisite: non-existing property        if (testRootNode.hasProperty(propertyName1)) {            testRootNode.getProperty(propertyName1).remove();            testRootNode.save();        }        // get user transaction object        UserTransaction utx = new UserTransactionImpl(superuser);        // start transaction        utx.begin();        // 'add' property and save        testRootNode.setProperty(propertyName1, "0");        testRootNode.save();        // 'modify' property and save        testRootNode.setProperty(propertyName1, "1");        testRootNode.save();        // commit        utx.commit();        // check property value        Session otherSuperuser = helper.getSuperuserSession();        Node n = (Node) otherSuperuser.getItem(testRootNode.getPath());        assertEquals(n.getProperty(propertyName1).getString(), "1");        otherSuperuser.logout();    }    /**     * @throws Exception     */    public void testPropertyIsNew() throws Exception {        // prerequisite: non-existing property        if (testRootNode.hasProperty(propertyName1)) {            testRootNode.getProperty(propertyName1).remove();            testRootNode.save();        }        // get user transaction object        UserTransaction utx = new UserTransactionImpl(superuser);        // start transaction        utx.begin();        // 'add' property and save        testRootNode.setProperty(propertyName1, "0");        assertTrue("New property must be new.", testRootNode.getProperty(propertyName1).isNew());        testRootNode.save();        assertFalse("Saved property must not be new.", testRootNode.getProperty(propertyName1).isNew());        // commit        utx.commit();    }    /**     * @throws Exception     */    public void testNewNodeIsLocked() throws Exception {        // get user transaction object        UserTransaction utx = new UserTransactionImpl(superuser);        // start transaction        utx.begin();        // add node and save        Node n = testRootNode.addNode(nodeName1, testNodeType);        testRootNode.save();        assertFalse("New node must not be locked.", n.isLocked());        // commit        utx.commit();    }    /**     * @throws Exception     */    public void testPropertyIsModified() throws Exception {        // prerequisite: existing property        testRootNode.setProperty(propertyName1, "0");        testRootNode.save();        // get user transaction object        UserTransaction utx = new UserTransactionImpl(superuser);        // start transaction        utx.begin();        // 'add' property and save        testRootNode.setProperty(propertyName1, "1");        assertTrue("Unsaved property must be modified.", testRootNode.getProperty(propertyName1).isModified());        testRootNode.save();        assertFalse("Saved property must not be modified.", testRootNode.getProperty(propertyName1).isModified());        // commit        utx.commit();    }    /**     * @throws Exception     */    public void testDeleteAndAddProperty() throws Exception {        // prerequisite: existing property        testRootNode.setProperty(propertyName1, "0");        testRootNode.save();        // get user transaction object        UserTransaction utx = new UserTransactionImpl(superuser);        // start transaction        utx.begin();        // 'delete' property and save        testRootNode.getProperty(propertyName1).remove();        testRootNode.save();        // 'add' property and save        testRootNode.setProperty(propertyName1, "1");        testRootNode.save();        // commit        utx.commit();        // check property value        Session otherSuperuser = helper.getSuperuserSession();        Node n = (Node) otherSuperuser.getItem(testRootNode.getPath());        assertEquals(n.getProperty(propertyName1).getString(), "1");        otherSuperuser.logout();    }    /**     * @throws Exception     */    public void testModifyAndDeleteProperty() throws Exception {        // prerequisite: existing property        testRootNode.setProperty(propertyName1, "0");        testRootNode.save();        // get user transaction object        UserTransaction utx = new UserTransactionImpl(superuser);        // start transaction        utx.begin();        // 'modify' property and save        testRootNode.setProperty(propertyName1, "1");        testRootNode.save();        // 'delete' property and save        testRootNode.getProperty(propertyName1).remove();        testRootNode.save();        // commit        utx.commit();        // check property value        Session otherSuperuser = helper.getSuperuserSession();        Node n = (Node) otherSuperuser.getItem(testRootNode.getPath());        assertFalse("Property must be deleted.", n.hasProperty(propertyName1));        otherSuperuser.logout();    }    /**     * @throws Exception     */    public void testAddAndDeleteProperty() throws Exception {        // prerequisite: non-existing property        if (testRootNode.hasProperty(propertyName1)) {            testRootNode.getProperty(propertyName1).remove();            testRootNode.save();        }        // get user transaction object        UserTransaction utx = new UserTransactionImpl(superuser);        // start transaction        utx.begin();        // 'add' property and save        testRootNode.setProperty(propertyName1, "1");        testRootNode.save();        // 'delete' property and save        testRootNode.getProperty(propertyName1).remove();        testRootNode.save();        // commit

⌨️ 快捷键说明

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