You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by an...@apache.org on 2009/05/27 18:21:57 UTC

svn commit: r779230 - in /jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api: NodeSetPrimaryTypeTest.java TestAll.java

Author: angela
Date: Wed May 27 16:21:57 2009
New Revision: 779230

URL: http://svn.apache.org/viewvc?rev=779230&view=rev
Log:
JCR-1104: JSR 283 support

- Add some trivial tests for Node.setPrimaryType(String)

Added:
    jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/NodeSetPrimaryTypeTest.java
Modified:
    jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/TestAll.java

Added: jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/NodeSetPrimaryTypeTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/NodeSetPrimaryTypeTest.java?rev=779230&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/NodeSetPrimaryTypeTest.java (added)
+++ jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/NodeSetPrimaryTypeTest.java Wed May 27 16:21:57 2009
@@ -0,0 +1,270 @@
+/*
+ * 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 javax.jcr.Node;
+import javax.jcr.Repository;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.lock.LockException;
+import javax.jcr.nodetype.ConstraintViolationException;
+import javax.jcr.nodetype.NoSuchNodeTypeException;
+import javax.jcr.nodetype.NodeTypeIterator;
+import javax.jcr.nodetype.NodeTypeManager;
+import javax.jcr.nodetype.NodeType;
+import javax.jcr.version.VersionException;
+
+/**
+ * <code>SetPrimaryType</code>...
+ */
+public class NodeSetPrimaryTypeTest extends AbstractJCRTest {
+
+    // TODO: test if node definition is properly reset
+    // TODO: test if child items are properly reset upon changing definition
+    // TODO: test if conflicts are properly detected
+
+    /**
+     * Tests a successful call to <code>Node.setPrimaryType(String)</code>
+     */
+    public void testSetPrimaryType() throws RepositoryException {
+        Session session = testRootNode.getSession();
+        Session otherSession = null;
+
+        String nonExistingMixinName = NodeMixinUtil.getNonExistingMixinName(session);
+
+        Node node = testRootNode.addNode(nodeName1, testNodeType);
+        superuser.save();
+
+        // TODO improve. retrieve settable node type name from config.
+        NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
+        NodeTypeIterator nts = manager.getPrimaryNodeTypes();
+        while (nts.hasNext()) {
+            NodeType nt = nts.nextNodeType();
+            String ntName = nt.getName();
+            if (!nt.isAbstract()) {
+                try {
+                    node.setPrimaryType(ntName);
+                    superuser.save();
+
+                    assertEquals("Node.getPrimaryNodeType must reflect the changes made.", ntName, node.getPrimaryNodeType().getName());
+
+                    otherSession = helper.getReadOnlySession();
+                    assertEquals("Node.getPrimaryNodeType must reflect the changes made.", ntName, otherSession.getNode(node.getPath()).getPrimaryNodeType().getName());
+
+                    // was successful
+                    return;
+
+                } catch (ConstraintViolationException e) {
+                    // may happen as long as arbitrary primary types are used for testing -> ignore
+                } finally {
+                    if (otherSession != null) {
+                        otherSession.logout();
+                    }
+                    // revert any unsaved changes.
+                    session.refresh(false);
+                }
+            }
+        }
+    }
+
+    /**
+     * Tests if <code>Node.setPrimaryType(String)</code> throws a
+     * <code>NoSuchNodeTypeException</code> if the
+     * name of an existing node type is passed.
+     */
+    public void testAddNonExisting() throws RepositoryException {
+        Session session = testRootNode.getSession();
+
+        NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
+        String nonExistingMixinName = "abc";
+        while (manager.hasNodeType(nonExistingMixinName)) {
+            nonExistingMixinName += "_";
+        }
+        Node node = testRootNode.addNode(nodeName1, testNodeType);
+
+        try {
+            node.setPrimaryType(nonExistingMixinName);
+            fail("Node.setPrimaryType(String) must throw a NoSuchNodeTypeException if no nodetype exists with the given name.");
+        } catch (NoSuchNodeTypeException e) {
+            // success
+        }
+    }
+
+    /**
+     * Tests if <code>Node.setPrimaryType(String)</code> throws a
+     * <code>ConstraintViolationException</code> if the
+     * name of a mixin type is passed
+     */
+    public void testSetMixinAsPrimaryType() throws RepositoryException {
+        Session session = testRootNode.getSession();
+
+        NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
+        NodeTypeIterator nts = manager.getMixinNodeTypes();
+        while (nts.hasNext()) {
+            try {
+                Node node = testRootNode.addNode(nodeName1, testNodeType);
+                node.setPrimaryType(nts.nextNodeType().getName());
+                fail("Node.setPrimaryType(String) must throw ConstraintViolationException if the specified node type name refers to a mixin.");
+            } catch (ConstraintViolationException e) {
+                // success
+            } finally {
+                // reset the changes.
+                session.refresh(false);
+            }
+        }
+    }
+
+    /**
+     * Tests if <code>Node.setPrimaryType(String)</code> throws a
+     * <code>ConstraintViolationException</code> if the
+     * name of a mixin type is passed
+     */
+    public void testSetAbstractAsPrimaryType() throws RepositoryException {
+        Session session = testRootNode.getSession();
+
+        NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
+        NodeTypeIterator nts = manager.getPrimaryNodeTypes();
+        while (nts.hasNext()) {
+            NodeType nt = nts.nextNodeType();
+            if (nt.isAbstract()) {
+                try {
+                    Node node = testRootNode.addNode(nodeName1, testNodeType);
+                    node.setPrimaryType(nt.getName());
+                    fail("Node.setPrimaryType(String) must throw ConstraintViolationException if the specified node type name refers to an abstract node type.");
+                } catch (ConstraintViolationException e) {
+                    // success
+                } finally {
+                    // reset the changes.
+                    session.refresh(false);
+                }
+            }
+        }
+    }
+
+    /**
+     * Tests if <code>Node.setPrimaryType(String)</code> throws a
+     * <code>LockException</code> if <code>Node</code> is locked.
+     */
+    public void testLocked() throws NotExecutableException, RepositoryException {
+
+        Session session = testRootNode.getSession();
+
+        if (!isSupported(Repository.OPTION_LOCKING_SUPPORTED)) {
+            throw new NotExecutableException("Locking is not supported.");
+        }
+
+        // create a node that is lockable
+        Node node = testRootNode.addNode(nodeName1, testNodeType);
+        // or try to make it lockable if it is not
+        if (!node.isNodeType(mixLockable)) {
+            if (node.canAddMixin(mixLockable)) {
+                node.addMixin(mixLockable);
+            } else {
+                throw new NotExecutableException("Node " + nodeName1 + " is not lockable and does not allow to add mix:lockable");
+            }
+        }
+        testRootNode.save();
+
+        String primaryTypeName = getPrimaryTypeName(session, node);
+        if (primaryTypeName == null) {
+            throw new NotExecutableException("No testable node type found");
+        }
+
+        // remove first slash of path to get rel path to root
+        String pathRelToRoot = node.getPath().substring(1);
+
+        // access node through another session to lock it
+        Session session2 = helper.getSuperuserSession();
+        try {
+            Node node2 = session2.getRootNode().getNode(pathRelToRoot);
+            node2.lock(true, true);
+
+            try {
+                // implementation specific: either throw LockException upon
+                // addMixin or upon save.
+                node.setPrimaryType(primaryTypeName);
+                node.save();
+                fail("Node.setPrimaryType(String) must throw a LockException if the node is locked.");
+            } catch (LockException e) {
+                // success
+            }
+
+            // unlock to remove node at tearDown()
+            node2.unlock();
+        } finally {
+            session2.logout();
+        }
+    }
+
+    /**
+     * Tests if <code>Node.setPrimaryType(String)</code> throws a
+     * <code>VersionException</code> if <code>Node</code> is checked-in.
+     */
+    public void testCheckedIn() throws NotExecutableException, RepositoryException {
+
+        Session session = testRootNode.getSession();
+
+        if (!isSupported(Repository.OPTION_VERSIONING_SUPPORTED)) {
+            throw new NotExecutableException("Versioning is not supported.");
+        }
+
+        // create a node that is versionable
+        Node node = testRootNode.addNode(nodeName1, testNodeType);
+        // or try to make it versionable if it is not
+        if (!node.isNodeType(mixVersionable)) {
+            if (node.canAddMixin(mixVersionable)) {
+                node.addMixin(mixVersionable);
+            } else {
+                throw new NotExecutableException("Node " + nodeName1 + " is not versionable and does not " +
+                        "allow to add mix:versionable");
+            }
+        }
+        superuser.save();
+
+        String primaryTypeName = getPrimaryTypeName(session, node);
+        if (primaryTypeName == null) {
+            throw new NotExecutableException("No testable node type found");
+        }
+
+        node.checkin();
+
+        try {
+            node.setPrimaryType(primaryTypeName);
+            fail("Node.setPrimaryType(String) must throw a VersionException if the node is checked-in.");
+        } catch (VersionException e) {
+            // success
+        }
+    }
+
+    private static String getPrimaryTypeName(Session session, Node node)
+            throws RepositoryException {
+
+        NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
+        NodeTypeIterator nts = manager.getPrimaryNodeTypes();
+
+        while (nts.hasNext()) {
+            String name = nts.nextNodeType().getName();
+            if (!name.equals(node.getPrimaryNodeType().getName())) {
+                return name;
+            }
+        }
+        return null;
+    }
+}
\ No newline at end of file

Modified: jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/TestAll.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/TestAll.java?rev=779230&r1=779229&r2=779230&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/TestAll.java (original)
+++ jackrabbit/trunk/jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/TestAll.java Wed May 27 16:21:57 2009
@@ -112,6 +112,8 @@
         suite.addTestSuite(NodeCanAddMixinTest.class);
         suite.addTestSuite(NodeRemoveMixinTest.class);
 
+        suite.addTestSuite(NodeSetPrimaryTypeTest.class);
+
         suite.addTestSuite(WorkspaceCloneReferenceableTest.class);
         suite.addTestSuite(WorkspaceCloneSameNameSibsTest.class);
         suite.addTestSuite(WorkspaceCloneTest.class);