You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by fi...@apache.org on 2006/03/24 09:21:01 UTC
svn commit: r388450 [4/4] - in /jackrabbit/trunk: contrib/bdb-persistence/
contrib/classloader/ contrib/jcr-ext/src/java/org/apache/jackrabbit/session/
contrib/jcr-ext/src/java/org/apache/jackrabbit/session/nodetype/
contrib/jcr-ext/src/java/org/apache...
Modified: jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/util/DOMWalker.java
URL: http://svn.apache.org/viewcvs/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/util/DOMWalker.java?rev=388450&r1=388449&r2=388450&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/util/DOMWalker.java (original)
+++ jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/util/DOMWalker.java Fri Mar 24 00:20:59 2006
@@ -1,220 +1,220 @@
-/*
- * Copyright 2004-2005 The Apache Software Foundation or its licensors,
- * as applicable.
- *
- * Licensed 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.util;
-
-import org.w3c.dom.Attr;
-import org.w3c.dom.CharacterData;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.NamedNodeMap;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Properties;
-
-/**
- * Document walker class. This class provides an intuitive
- * interface for traversing a parsed DOM document.
- */
-public final class DOMWalker {
-
- /** Static factory for creating stream to DOM transformers. */
- private static final DocumentBuilderFactory factory =
- DocumentBuilderFactory.newInstance();
-
- /** The DOM document being traversed by this walker. */
- private final Document document;
-
- /** The current element. */
- private Element current;
-
- /**
- * Creates a walker for traversing a DOM document read from the given
- * input stream. The root element of the document is set as the current
- * element.
- *
- * @param xml XML input stream
- * @throws IOException if a document cannot be read from the stream
- */
- public DOMWalker(InputStream xml) throws IOException {
- try {
- DocumentBuilder builder = factory.newDocumentBuilder();
- document = builder.parse(xml);
- current = document.getDocumentElement();
- } catch (IOException e) {
- throw e;
- } catch (Exception e) {
- throw (IOException) new IOException(e.getMessage()).initCause(e);
- }
- }
-
- /**
- * Returns the namespace mappings defined in the current element.
- * The returned property set contains the prefix to namespace
- * mappings specified by the <code>xmlns</code> attributes of the
- * current element.
- *
- * @return prefix to namespace mappings of the current element
- */
- public Properties getNamespaces() {
- Properties namespaces = new Properties();
- NamedNodeMap attributes = current.getAttributes();
- for (int i = 0; i < attributes.getLength(); i++) {
- Attr attribute = (Attr) attributes.item(i);
- if (attribute.getName().startsWith("xmlns:")) {
- namespaces.setProperty(
- attribute.getName().substring(6), attribute.getValue());
- }
- }
- return namespaces;
- }
-
- /**
- * Returns the name of the current element.
- *
- * @return element name
- */
- public String getName() {
- return current.getNodeName();
- }
-
- /**
- * Returns the value of the named attribute of the current element.
- *
- * @param name attribute name
- * @return attribute value, or <code>null</code> if not found
- */
- public String getAttribute(String name) {
- Attr attribute = current.getAttributeNode(name);
- if (attribute != null) {
- return attribute.getValue();
- } else {
- return null;
- }
- }
-
- /**
- * Returns the text content of the current element.
- *
- * @return text content
- */
- public String getContent() {
- StringBuffer content = new StringBuffer();
-
- NodeList nodes = current.getChildNodes();
- for (int i = 0; i < nodes.getLength(); i++) {
- Node node = nodes.item(i);
- if (node.getNodeType() == Node.TEXT_NODE) {
- content.append(((CharacterData) node).getData());
- }
- }
-
- return content.toString();
- }
-
- /**
- * Enters the named child element. If the named child element is
- * found, then it is made the current element and <code>true</code>
- * is returned. Otherwise the current element is not changed and
- * <code>false</code> is returned.
- * <p>
- * The standard call sequence for this method is show below.
- * <pre>
- * DOMWalker walker = ...;
- * if (walker.enterElement("...")) {
- * ...;
- * walker.leaveElement();
- * }
- * </pre>
- *
- * @param name child element name
- * @return <code>true</code> if the element was entered,
- * <code>false</code> otherwise
- */
- public boolean enterElement(String name) {
- NodeList children = current.getChildNodes();
- for (int i = 0; i < children.getLength(); i++) {
- Node child = children.item(i);
- if (child.getNodeType() == Node.ELEMENT_NODE
- && name.equals(child.getNodeName())) {
- current = (Element) child;
- return true;
- }
- }
- return false;
- }
-
- /**
- * Leaves the current element. The parent element is set as the new
- * current element.
- *
- * @see #enterElement(String)
- */
- public void leaveElement() {
- current = (Element) current.getParentNode();
- }
-
- /**
- * Iterates through the named child elements over multiple calls.
- * This method makes it possible to use the following code to
- * walk through all the child elements with the given name.
- * <pre>
- * DOMWalker walker = ...;
- * while (walker.iterateElements("...")) {
- * ...;
- * }
- * </pre>
- * <p>
- * <strong>WARNING:</strong> This method should only be used when
- * <code>walker.getName()</code> does not equal <code>name</code> when
- * the while loop is started. Otherwise the walker will not be positioned
- * at the same node when the while loop ends.
- *
- * @param name name of the iterated elements
- * @return <code>true</code> if another iterated element was entered, or
- * <code>false</code> if no more iterated elements were found
- * and the original element is restored as the current element
- */
- public boolean iterateElements(String name) {
- Node next;
- if (name.equals(current.getNodeName())) {
- next = current.getNextSibling();
- } else {
- next = current.getFirstChild();
- }
-
- while (next != null) {
- if (next.getNodeType() == Node.ELEMENT_NODE
- && name.equals(next.getNodeName())) {
- current = (Element) next;
- return true;
- } else {
- next = next.getNextSibling();
- }
- }
-
- if (name.equals(current.getNodeName())) {
- current = (Element) current.getParentNode();
- }
- return false;
- }
-
-}
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ * as applicable.
+ *
+ * Licensed 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.util;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.CharacterData;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
+/**
+ * Document walker class. This class provides an intuitive
+ * interface for traversing a parsed DOM document.
+ */
+public final class DOMWalker {
+
+ /** Static factory for creating stream to DOM transformers. */
+ private static final DocumentBuilderFactory factory =
+ DocumentBuilderFactory.newInstance();
+
+ /** The DOM document being traversed by this walker. */
+ private final Document document;
+
+ /** The current element. */
+ private Element current;
+
+ /**
+ * Creates a walker for traversing a DOM document read from the given
+ * input stream. The root element of the document is set as the current
+ * element.
+ *
+ * @param xml XML input stream
+ * @throws IOException if a document cannot be read from the stream
+ */
+ public DOMWalker(InputStream xml) throws IOException {
+ try {
+ DocumentBuilder builder = factory.newDocumentBuilder();
+ document = builder.parse(xml);
+ current = document.getDocumentElement();
+ } catch (IOException e) {
+ throw e;
+ } catch (Exception e) {
+ throw (IOException) new IOException(e.getMessage()).initCause(e);
+ }
+ }
+
+ /**
+ * Returns the namespace mappings defined in the current element.
+ * The returned property set contains the prefix to namespace
+ * mappings specified by the <code>xmlns</code> attributes of the
+ * current element.
+ *
+ * @return prefix to namespace mappings of the current element
+ */
+ public Properties getNamespaces() {
+ Properties namespaces = new Properties();
+ NamedNodeMap attributes = current.getAttributes();
+ for (int i = 0; i < attributes.getLength(); i++) {
+ Attr attribute = (Attr) attributes.item(i);
+ if (attribute.getName().startsWith("xmlns:")) {
+ namespaces.setProperty(
+ attribute.getName().substring(6), attribute.getValue());
+ }
+ }
+ return namespaces;
+ }
+
+ /**
+ * Returns the name of the current element.
+ *
+ * @return element name
+ */
+ public String getName() {
+ return current.getNodeName();
+ }
+
+ /**
+ * Returns the value of the named attribute of the current element.
+ *
+ * @param name attribute name
+ * @return attribute value, or <code>null</code> if not found
+ */
+ public String getAttribute(String name) {
+ Attr attribute = current.getAttributeNode(name);
+ if (attribute != null) {
+ return attribute.getValue();
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Returns the text content of the current element.
+ *
+ * @return text content
+ */
+ public String getContent() {
+ StringBuffer content = new StringBuffer();
+
+ NodeList nodes = current.getChildNodes();
+ for (int i = 0; i < nodes.getLength(); i++) {
+ Node node = nodes.item(i);
+ if (node.getNodeType() == Node.TEXT_NODE) {
+ content.append(((CharacterData) node).getData());
+ }
+ }
+
+ return content.toString();
+ }
+
+ /**
+ * Enters the named child element. If the named child element is
+ * found, then it is made the current element and <code>true</code>
+ * is returned. Otherwise the current element is not changed and
+ * <code>false</code> is returned.
+ * <p>
+ * The standard call sequence for this method is show below.
+ * <pre>
+ * DOMWalker walker = ...;
+ * if (walker.enterElement("...")) {
+ * ...;
+ * walker.leaveElement();
+ * }
+ * </pre>
+ *
+ * @param name child element name
+ * @return <code>true</code> if the element was entered,
+ * <code>false</code> otherwise
+ */
+ public boolean enterElement(String name) {
+ NodeList children = current.getChildNodes();
+ for (int i = 0; i < children.getLength(); i++) {
+ Node child = children.item(i);
+ if (child.getNodeType() == Node.ELEMENT_NODE
+ && name.equals(child.getNodeName())) {
+ current = (Element) child;
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Leaves the current element. The parent element is set as the new
+ * current element.
+ *
+ * @see #enterElement(String)
+ */
+ public void leaveElement() {
+ current = (Element) current.getParentNode();
+ }
+
+ /**
+ * Iterates through the named child elements over multiple calls.
+ * This method makes it possible to use the following code to
+ * walk through all the child elements with the given name.
+ * <pre>
+ * DOMWalker walker = ...;
+ * while (walker.iterateElements("...")) {
+ * ...;
+ * }
+ * </pre>
+ * <p>
+ * <strong>WARNING:</strong> This method should only be used when
+ * <code>walker.getName()</code> does not equal <code>name</code> when
+ * the while loop is started. Otherwise the walker will not be positioned
+ * at the same node when the while loop ends.
+ *
+ * @param name name of the iterated elements
+ * @return <code>true</code> if another iterated element was entered, or
+ * <code>false</code> if no more iterated elements were found
+ * and the original element is restored as the current element
+ */
+ public boolean iterateElements(String name) {
+ Node next;
+ if (name.equals(current.getNodeName())) {
+ next = current.getNextSibling();
+ } else {
+ next = current.getFirstChild();
+ }
+
+ while (next != null) {
+ if (next.getNodeType() == Node.ELEMENT_NODE
+ && name.equals(next.getNodeName())) {
+ current = (Element) next;
+ return true;
+ } else {
+ next = next.getNextSibling();
+ }
+ }
+
+ if (name.equals(current.getNodeName())) {
+ current = (Element) current.getParentNode();
+ }
+ return false;
+ }
+
+}
Propchange: jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/util/DOMWalker.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/name/NameException.java
URL: http://svn.apache.org/viewcvs/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/name/NameException.java?rev=388450&r1=388449&r2=388450&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/name/NameException.java (original)
+++ jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/name/NameException.java Fri Mar 24 00:20:59 2006
@@ -1,47 +1,47 @@
-/*
- * Copyright 2004-2005 The Apache Software Foundation or its licensors,
- * as applicable.
- *
- * Licensed 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.name;
-
-import org.apache.jackrabbit.BaseException;
-
-/**
- * Base class for exceptions about malformed or otherwise
- * invalid JCR names and paths.
- */
-public class NameException extends BaseException {
-
- /**
- * Creates a NameException with the given error message.
- *
- * @param message error message
- */
- public NameException(String message) {
- super(message);
- }
-
- /**
- * Creates a NameException with the given error message and
- * root cause exception.
- *
- * @param message error message
- * @param rootCause root cause exception
- */
- public NameException(String message, Throwable rootCause) {
- super(message, rootCause);
- }
-
-}
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ * as applicable.
+ *
+ * Licensed 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.name;
+
+import org.apache.jackrabbit.BaseException;
+
+/**
+ * Base class for exceptions about malformed or otherwise
+ * invalid JCR names and paths.
+ */
+public class NameException extends BaseException {
+
+ /**
+ * Creates a NameException with the given error message.
+ *
+ * @param message error message
+ */
+ public NameException(String message) {
+ super(message);
+ }
+
+ /**
+ * Creates a NameException with the given error message and
+ * root cause exception.
+ *
+ * @param message error message
+ * @param rootCause root cause exception
+ */
+ public NameException(String message, Throwable rootCause) {
+ super(message, rootCause);
+ }
+
+}
Propchange: jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/name/NameException.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: jackrabbit/trunk/jackrabbit/src/test/java/org/apache/jackrabbit/test/api/NodeDiscoveringNodeTypesTest.java
URL: http://svn.apache.org/viewcvs/jackrabbit/trunk/jackrabbit/src/test/java/org/apache/jackrabbit/test/api/NodeDiscoveringNodeTypesTest.java?rev=388450&r1=388449&r2=388450&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit/src/test/java/org/apache/jackrabbit/test/api/NodeDiscoveringNodeTypesTest.java (original)
+++ jackrabbit/trunk/jackrabbit/src/test/java/org/apache/jackrabbit/test/api/NodeDiscoveringNodeTypesTest.java Fri Mar 24 00:20:59 2006
@@ -1,211 +1,211 @@
-/*
- * Copyright 2004-2005 The Apache Software Foundation or its licensors,
- * as applicable.
- *
- * Licensed 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.Session;
-import javax.jcr.Node;
-import javax.jcr.NodeIterator;
-import javax.jcr.Value;
-import javax.jcr.RepositoryException;
-import javax.jcr.nodetype.NodeType;
-import java.util.NoSuchElementException;
-
-/**
- * All test cases in this class rely on content in the repository. That is the
- * default workspace must at least contain one child node under {@link #testRoot}
- * otherwise a {@link NotExecutableException} is thrown.
- *
- * @test
- * @sources NodeDiscoveringNodeTypesTest.java
- * @executeClass org.apache.jackrabbit.test.api.NodeDiscoveringNodeTypesTest
- * @keywords level1
- */
-public class NodeDiscoveringNodeTypesTest extends AbstractJCRTest {
-
- /**
- * The session we use for the tests
- */
- private Session session;
-
- /**
- * A child node of the root node in the default workspace.
- */
- private Node childNode;
-
- /**
- * Sets up the fixture for the test cases.
- */
- protected void setUp() throws Exception {
- isReadOnly = true;
- super.setUp();
-
- session = helper.getReadOnlySession();
- testRootNode = session.getRootNode().getNode(testPath);
- NodeIterator nodes = testRootNode.getNodes();
- try {
- childNode = nodes.nextNode();
- } catch (NoSuchElementException e) {
- }
- }
-
- /**
- * Releases the session aquired in {@link #setUp()}.
- */
- protected void tearDown() throws Exception {
- if (session != null) {
- session.logout();
- }
- super.tearDown();
- }
-
- /**
- * Test if getPrimaryNodeType() returns the node type according to the
- * property "jcr:primaryType"
- */
- public void testGetPrimaryNodeType()
- throws NotExecutableException, RepositoryException {
-
- if (childNode == null) {
- throw new NotExecutableException("Workspace does not have sufficient content for this test. " +
- "Root node must have at least one child node.");
- }
-
- NodeType type = childNode.getPrimaryNodeType();
- String name = childNode.getProperty(jcrPrimaryType).getString();
-
- assertEquals("getPrimaryNodeType() must return the node type stored " +
- "as property \"jcr:primaryType\"",
- name, type.getName());
-
- assertFalse("getPrimaryNodeType() must return a primary node type",
- type.isMixin());
- }
-
-
- /**
- * Test if getMixinNodeType returns the node types according to the property
- * "jcr:mixinTypes". Therefor a node with mixin types is located recursively
- * in the entire repository. A NotExecutableException is thrown when no such
- * node is found.
- */
- public void testGetMixinNodeTypes()
- throws NotExecutableException, RepositoryException {
-
- if (childNode == null) {
- throw new NotExecutableException("Workspace does not have sufficient content for this test. " +
- "Root node must have at least one child node.");
- }
-
- Node node = locateNodeWithMixinNodeTypes(testRootNode);
-
- if (node == null) {
- throw new NotExecutableException("Workspace does not contain a node with mixin node types defined");
- }
-
- Value names[] = node.getProperty(jcrMixinTypes).getValues();
- NodeType types[] = node.getMixinNodeTypes();
-
- assertEquals("getMixinNodeTypes() does not return the same number of " +
- "node types as " +
- "getProperty(\"jcr:mixinTypes\").getValues()",
- types.length,
- names.length);
-
- StringBuffer namesString = new StringBuffer();
- for (int i = 0; i < names.length; i++) {
- namesString.append("|" + names[i].getString() + "|");
- }
-
- for (int i = 0; i < types.length; i++) {
- String pattern = "|" + types[i].getName() + "|";
-
- assertTrue("getMixinNodeTypes() does not return the same node" +
- "types as getProperty(\"jcr:mixinTypes\").getValues()",
- namesString.indexOf(pattern) != -1);
-
- assertTrue("All nodes returned by getMixinNodeTypes() must be" +
- "mixin",
- types[i].isMixin());
- }
- }
-
-
- /**
- * Test if isNodeTye(String nodeTypeName) returns true if nodeTypeName is the
- * name of the primary node type, the name of a mixin node type and the name
- * of a supertype.
- */
- public void testIsNodeType()
- throws NotExecutableException, RepositoryException {
-
- String nodeTypeName;
-
- // test with primary node's name
- nodeTypeName = testRootNode.getPrimaryNodeType().getName();
- assertTrue("isNodeType(String nodeTypeName) must return true if " +
- "nodeTypeName is the name of the primary node type",
- testRootNode.isNodeType(nodeTypeName));
-
- // test with mixin node's name
- // (if such a node is available)
- Node nodeWithMixin = locateNodeWithMixinNodeTypes(testRootNode);
- if (nodeWithMixin != null) {
- NodeType types[] = nodeWithMixin.getMixinNodeTypes();
- nodeTypeName = types[0].getName();
- assertTrue("isNodeType(String nodeTypeName) must return true if " +
- "nodeTypeName is the name of one of the " +
- "mixin node types",
- nodeWithMixin.isNodeType(nodeTypeName));
- }
-
- // test with the name of predefined supertype "nt:base"
- assertTrue("isNodeType(String nodeTypeName) must return true if " +
- "nodeTypeName is the name of a node type of a supertype",
- testRootNode.isNodeType(ntBase));
- }
-
- //-----------------------< internal >---------------------------------------
-
- /**
- * Returns the first descendant of <code>node</code> which defines mixin
- * node type(s).
- *
- * @param node <code>Node</code> to start traversal.
- * @return first node with mixin node type(s)
- */
- private Node locateNodeWithMixinNodeTypes(Node node)
- throws RepositoryException {
-
- if (node.getMixinNodeTypes().length != 0) {
- return node;
- }
-
- NodeIterator nodes = node.getNodes();
- while (nodes.hasNext()) {
- Node returnedNode = this.locateNodeWithMixinNodeTypes(nodes.nextNode());
- if (returnedNode != null) {
- return returnedNode;
- }
- }
- return null;
- }
-
-
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ * as applicable.
+ *
+ * Licensed 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.Session;
+import javax.jcr.Node;
+import javax.jcr.NodeIterator;
+import javax.jcr.Value;
+import javax.jcr.RepositoryException;
+import javax.jcr.nodetype.NodeType;
+import java.util.NoSuchElementException;
+
+/**
+ * All test cases in this class rely on content in the repository. That is the
+ * default workspace must at least contain one child node under {@link #testRoot}
+ * otherwise a {@link NotExecutableException} is thrown.
+ *
+ * @test
+ * @sources NodeDiscoveringNodeTypesTest.java
+ * @executeClass org.apache.jackrabbit.test.api.NodeDiscoveringNodeTypesTest
+ * @keywords level1
+ */
+public class NodeDiscoveringNodeTypesTest extends AbstractJCRTest {
+
+ /**
+ * The session we use for the tests
+ */
+ private Session session;
+
+ /**
+ * A child node of the root node in the default workspace.
+ */
+ private Node childNode;
+
+ /**
+ * Sets up the fixture for the test cases.
+ */
+ protected void setUp() throws Exception {
+ isReadOnly = true;
+ super.setUp();
+
+ session = helper.getReadOnlySession();
+ testRootNode = session.getRootNode().getNode(testPath);
+ NodeIterator nodes = testRootNode.getNodes();
+ try {
+ childNode = nodes.nextNode();
+ } catch (NoSuchElementException e) {
+ }
+ }
+
+ /**
+ * Releases the session aquired in {@link #setUp()}.
+ */
+ protected void tearDown() throws Exception {
+ if (session != null) {
+ session.logout();
+ }
+ super.tearDown();
+ }
+
+ /**
+ * Test if getPrimaryNodeType() returns the node type according to the
+ * property "jcr:primaryType"
+ */
+ public void testGetPrimaryNodeType()
+ throws NotExecutableException, RepositoryException {
+
+ if (childNode == null) {
+ throw new NotExecutableException("Workspace does not have sufficient content for this test. " +
+ "Root node must have at least one child node.");
+ }
+
+ NodeType type = childNode.getPrimaryNodeType();
+ String name = childNode.getProperty(jcrPrimaryType).getString();
+
+ assertEquals("getPrimaryNodeType() must return the node type stored " +
+ "as property \"jcr:primaryType\"",
+ name, type.getName());
+
+ assertFalse("getPrimaryNodeType() must return a primary node type",
+ type.isMixin());
+ }
+
+
+ /**
+ * Test if getMixinNodeType returns the node types according to the property
+ * "jcr:mixinTypes". Therefor a node with mixin types is located recursively
+ * in the entire repository. A NotExecutableException is thrown when no such
+ * node is found.
+ */
+ public void testGetMixinNodeTypes()
+ throws NotExecutableException, RepositoryException {
+
+ if (childNode == null) {
+ throw new NotExecutableException("Workspace does not have sufficient content for this test. " +
+ "Root node must have at least one child node.");
+ }
+
+ Node node = locateNodeWithMixinNodeTypes(testRootNode);
+
+ if (node == null) {
+ throw new NotExecutableException("Workspace does not contain a node with mixin node types defined");
+ }
+
+ Value names[] = node.getProperty(jcrMixinTypes).getValues();
+ NodeType types[] = node.getMixinNodeTypes();
+
+ assertEquals("getMixinNodeTypes() does not return the same number of " +
+ "node types as " +
+ "getProperty(\"jcr:mixinTypes\").getValues()",
+ types.length,
+ names.length);
+
+ StringBuffer namesString = new StringBuffer();
+ for (int i = 0; i < names.length; i++) {
+ namesString.append("|" + names[i].getString() + "|");
+ }
+
+ for (int i = 0; i < types.length; i++) {
+ String pattern = "|" + types[i].getName() + "|";
+
+ assertTrue("getMixinNodeTypes() does not return the same node" +
+ "types as getProperty(\"jcr:mixinTypes\").getValues()",
+ namesString.indexOf(pattern) != -1);
+
+ assertTrue("All nodes returned by getMixinNodeTypes() must be" +
+ "mixin",
+ types[i].isMixin());
+ }
+ }
+
+
+ /**
+ * Test if isNodeTye(String nodeTypeName) returns true if nodeTypeName is the
+ * name of the primary node type, the name of a mixin node type and the name
+ * of a supertype.
+ */
+ public void testIsNodeType()
+ throws NotExecutableException, RepositoryException {
+
+ String nodeTypeName;
+
+ // test with primary node's name
+ nodeTypeName = testRootNode.getPrimaryNodeType().getName();
+ assertTrue("isNodeType(String nodeTypeName) must return true if " +
+ "nodeTypeName is the name of the primary node type",
+ testRootNode.isNodeType(nodeTypeName));
+
+ // test with mixin node's name
+ // (if such a node is available)
+ Node nodeWithMixin = locateNodeWithMixinNodeTypes(testRootNode);
+ if (nodeWithMixin != null) {
+ NodeType types[] = nodeWithMixin.getMixinNodeTypes();
+ nodeTypeName = types[0].getName();
+ assertTrue("isNodeType(String nodeTypeName) must return true if " +
+ "nodeTypeName is the name of one of the " +
+ "mixin node types",
+ nodeWithMixin.isNodeType(nodeTypeName));
+ }
+
+ // test with the name of predefined supertype "nt:base"
+ assertTrue("isNodeType(String nodeTypeName) must return true if " +
+ "nodeTypeName is the name of a node type of a supertype",
+ testRootNode.isNodeType(ntBase));
+ }
+
+ //-----------------------< internal >---------------------------------------
+
+ /**
+ * Returns the first descendant of <code>node</code> which defines mixin
+ * node type(s).
+ *
+ * @param node <code>Node</code> to start traversal.
+ * @return first node with mixin node type(s)
+ */
+ private Node locateNodeWithMixinNodeTypes(Node node)
+ throws RepositoryException {
+
+ if (node.getMixinNodeTypes().length != 0) {
+ return node;
+ }
+
+ NodeIterator nodes = node.getNodes();
+ while (nodes.hasNext()) {
+ Node returnedNode = this.locateNodeWithMixinNodeTypes(nodes.nextNode());
+ if (returnedNode != null) {
+ return returnedNode;
+ }
+ }
+ return null;
+ }
+
+
}
Propchange: jackrabbit/trunk/jackrabbit/src/test/java/org/apache/jackrabbit/test/api/NodeDiscoveringNodeTypesTest.java
------------------------------------------------------------------------------
svn:eol-style = native