You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by dp...@apache.org on 2008/04/23 12:29:06 UTC
svn commit: r650818 -
/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/CachingHierarchyManagerTest.java
Author: dpfister
Date: Wed Apr 23 03:29:01 2008
New Revision: 650818
URL: http://svn.apache.org/viewvc?rev=650818&view=rev
Log:
JCR-1104 - JSR 283 support
- shareble nodes (work in progress)
- add test that checks correct return values of resolveNodePath/resolvePropertyPath
- add javadoc
Modified:
jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/CachingHierarchyManagerTest.java
Modified: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/CachingHierarchyManagerTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/CachingHierarchyManagerTest.java?rev=650818&r1=650817&r2=650818&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/CachingHierarchyManagerTest.java (original)
+++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/CachingHierarchyManagerTest.java Wed Apr 23 03:29:01 2008
@@ -28,6 +28,7 @@
import org.apache.jackrabbit.core.state.NodeReferencesId;
import org.apache.jackrabbit.core.state.NodeState;
import org.apache.jackrabbit.core.state.NodeStateListener;
+import org.apache.jackrabbit.core.state.PropertyState;
import org.apache.jackrabbit.spi.Name;
import org.apache.jackrabbit.spi.Path;
import org.apache.jackrabbit.spi.PathFactory;
@@ -95,9 +96,61 @@
};
+ //-------------------------------------------------------------- basic tests
+
+ /**
+ * Verify that resolving node and property paths will only return valid hits.
+ */
+ public void testResolveNodePropertyPath() throws Exception {
+ StaticItemStateManager ism = new StaticItemStateManager();
+ cache = new CachingHierarchyManager(ism.getRootNodeId(), ism, null);
+ ism.setContainer(cache);
+ NodeState a = ism.addNode(ism.getRoot(), "a");
+ NodeState b = ism.addNode(a, "b");
+
+ Path path = toPath("{}\t{}a\t{}b");
+
+ // /a/b points to node only
+ assertIsNodeId(cache.resolvePath(path));
+ assertIsNodeId(cache.resolveNodePath(path));
+ assertNull(cache.resolvePropertyPath(path));
+
+ ism.addProperty(a, "b");
+
+ // /a/b points to node and property
+ assertNotNull(cache.resolvePath(path));
+ assertIsNodeId(cache.resolveNodePath(path));
+ assertIsPropertyId(cache.resolvePropertyPath(path));
+
+ ism.removeNode(b);
+
+ // /a/b points to property only
+ assertIsPropertyId(cache.resolvePath(path));
+ assertNull(cache.resolveNodePath(path));
+ assertIsPropertyId(cache.resolvePropertyPath(path));
+ }
+
+ /**
+ * Assert that an item id is a property id.
+ * @param id item id
+ */
+ private static void assertIsPropertyId(ItemId id) {
+ assertTrue(id instanceof PropertyId);
+ }
+
+ /**
+ * Assert that an item id is a node id.
+ * @param id item id
+ */
+ private static void assertIsNodeId(ItemId id) {
+ assertTrue(id instanceof NodeId);
+ }
+
+ //------------------------------------------------------------ caching tests
+
/**
* Clone a node, cache its path and remove it afterwards. Should remove
- * the cached path as well as no longer resolve the path.
+ * the cached path as well.
*/
public void testCloneAndRemove() throws Exception {
StaticItemStateManager ism = new StaticItemStateManager();
@@ -117,6 +170,9 @@
ism.removeNode((NodeState) ism.getItemState(id));
}
+ /**
+ * Move a node and verify that cached path is adapted.
+ */
public void testMove() throws Exception {
StaticItemStateManager ism = new StaticItemStateManager();
cache = new CachingHierarchyManager(ism.getRootNodeId(), ism, null);
@@ -131,6 +187,9 @@
assertEquals("{}\t{}a2\t{}b2", path.toString());
}
+ /**
+ * Reorder child nodes and verify that cached paths are still adequate.
+ */
public void testOrderBefore() throws Exception {
StaticItemStateManager ism = new StaticItemStateManager();
cache = new CachingHierarchyManager(ism.getRootNodeId(), ism, null);
@@ -147,6 +206,9 @@
assertEquals("{}\t{}a\t{}b[2]", path.toString());
}
+ /**
+ * Remove a node and verify that cached path is gone.
+ */
public void testRemove() throws Exception {
StaticItemStateManager ism = new StaticItemStateManager();
cache = new CachingHierarchyManager(ism.getRootNodeId(), ism, null);
@@ -160,6 +222,9 @@
assertFalse(cache.isCached(c.getId()));
}
+ /**
+ * Rename a node and verify that cached path is adapted.
+ */
public void testRename() throws Exception {
StaticItemStateManager ism = new StaticItemStateManager();
cache = new CachingHierarchyManager(ism.getRootNodeId(), ism, null);
@@ -178,22 +243,49 @@
assertEquals("{}\t{}a1\t{}b1", path.toString());
}
+ /**
+ * Static item state manager, that can be filled programmatically and that
+ * keeps a hash map of item states. <code>ItemId</code>s generated by
+ * this state manager start with <code>0</code>.
+ */
static class StaticItemStateManager implements ItemStateManager {
+ /** Root node id */
private final NodeId rootNodeId;
+
+ /** Map of item states */
private final HashMap states = new HashMap();
+
+ /** UUID generator base */
private long lsbGenerator;
+
+ /** Root node state */
private NodeState root;
+
+ /** Node state listener to register in item states */
private NodeStateListener listener;
+ /**
+ * Create a new instance of this class.
+ */
public StaticItemStateManager() {
rootNodeId = new NodeId(nextUUID());
}
+ /**
+ * Return the root node id.
+ *
+ * @return root node id
+ */
public NodeId getRootNodeId() {
return rootNodeId;
}
+ /**
+ * Return the root node.
+ *
+ * @return root node
+ */
public NodeState getRoot() {
if (root == null) {
root = new NodeState(rootNodeId, NameConstants.JCR_ROOT,
@@ -205,10 +297,22 @@
return root;
}
+ /**
+ * Set the listener that should be registered in new item states.
+ *
+ * @param listener listener
+ */
public void setContainer(NodeStateListener listener) {
this.listener = listener;
}
+ /**
+ * Add a node.
+ *
+ * @param parent parent node
+ * @param name node name
+ * @return new node
+ */
public NodeState addNode(NodeState parent, String name) {
NodeId id = new NodeId(nextUUID());
NodeState child = new NodeState(id, NameConstants.NT_UNSTRUCTURED,
@@ -221,11 +325,45 @@
return child;
}
+ /**
+ * Add a property.
+ *
+ * @param parent parent node
+ * @param name property name
+ * @return new property
+ */
+ public PropertyState addProperty(NodeState parent, String name) {
+ PropertyId id = new PropertyId(parent.getNodeId(), toName(name));
+ PropertyState child = new PropertyState(id,
+ PropertyState.STATUS_EXISTING, false);
+ if (listener != null) {
+ child.setContainer(listener);
+ }
+ states.put(id, child);
+ parent.addPropertyName(toName(name));
+ return child;
+ }
+
+ /**
+ * Clone a node.
+ *
+ * @param src node to clone
+ * @param parent destination parent node
+ * @param name node name
+ */
public void cloneNode(NodeState src, NodeState parent, String name) {
src.addShare(parent.getNodeId());
parent.addChildNodeEntry(toName(name), src.getNodeId());
}
+ /**
+ * Move a node.
+ *
+ * @param child node to move
+ * @param newParent destination parent node
+ * @param name node name
+ * @throws ItemStateException if getting the old parent node fails
+ */
public void moveNode(NodeState child, NodeState newParent, String name)
throws ItemStateException {
@@ -239,6 +377,13 @@
newParent.addChildNodeEntry(toName(name), child.getNodeId());
}
+ /**
+ * Order a child node before another node.
+ *
+ * @param src src node
+ * @param dest destination node, may be <code>null</code>
+ * @throws ItemStateException if getting the parent node fails
+ */
public void orderBefore(NodeState src, NodeState dest)
throws ItemStateException {
@@ -268,6 +413,12 @@
parent.setChildNodeEntries(list);
}
+ /**
+ * Remove a node.
+ *
+ * @param child node to remove
+ * @throws ItemStateException if getting the parent node fails
+ */
public void removeNode(NodeState child) throws ItemStateException {
NodeState parent = (NodeState) getItemState(child.getParentId());
if (child.isShareable()) {
@@ -278,6 +429,13 @@
parent.removeChildNodeEntry(child.getNodeId());
}
+ /**
+ * Rename a node.
+ *
+ * @param child node to rename
+ * @param newName new name
+ * @throws ItemStateException if getting the parent node fails
+ */
public void renameNode(NodeState child, String newName) throws ItemStateException {
NodeState parent = (NodeState) getItemState(child.getParentId());
NodeState.ChildNodeEntry cne = parent.getChildNodeEntry(child.getNodeId());
@@ -287,10 +445,21 @@
parent.renameChildNodeEntry(cne.getName(), cne.getIndex(), toName(newName));
}
+ /**
+ * Return the next available UUID. Simply increments the last UUID
+ * returned by <code>1</code>.
+ *
+ * @return next UUID
+ */
private UUID nextUUID() {
return new UUID(0, lsbGenerator++);
}
+ //----------------------------------------------------- ItemStateManager
+
+ /**
+ * {@inheritDoc}
+ */
public ItemState getItemState(ItemId id)
throws NoSuchItemStateException, ItemStateException {
@@ -304,6 +473,9 @@
return item;
}
+ /**
+ * {@inheritDoc}
+ */
public boolean hasItemState(ItemId id) {
if (id.equals(root.getId())) {
return true;
@@ -311,20 +483,38 @@
return states.containsKey(id);
}
+ /**
+ * {@inheritDoc}
+ */
public NodeReferences getNodeReferences(NodeReferencesId id)
throws NoSuchItemStateException, ItemStateException {
return null;
}
+ /**
+ * {@inheritDoc}
+ */
public boolean hasNodeReferences(NodeReferencesId id) {
return false;
}
}
+ /**
+ * Utility method, converting a string into a path.
+ *
+ * @param s string
+ * @return path
+ */
private static Path toPath(String s) {
return PathFactoryImpl.getInstance().create(s);
}
+ /**
+ * Utility method, converting a string into a name.
+ *
+ * @param s string
+ * @return name
+ */
private static Name toName(String s) {
return NameFactoryImpl.getInstance().create("", s);
}