package com.jme.scene;
-public class TestNode extends junit.framework.TestCase {
+import static org.junit.Assert.*;
+
+public class TestNode {
+ public TestNode() {}
+
private Node root;
- @Override
- protected void setUp() throws Exception {
+ @org.junit.Before
+ public void setUp() throws Exception {
root = new Node("root");
}
+ @org.junit.Test
public void testInit() {
assertEquals(0, root.getQuantity());
assertEquals(0, root.getVertexCount());
assertEquals("root", root.getName());
}
+ @org.junit.Test
public void testHierarchy() {
Node child1 = new Node("child1");
Node child2 = new Node("child2");
root.detachAllChildren();
assertEquals(0, root.getQuantity());
}
+
+ @org.junit.Test
+ public void matches() {
+ Node n1 = new Node("n1");
+ Node n2 = new Node("n2");
+ Node n11 = new Node("n11");
+ Node n12 = new Node("n12");
+ Node n21 = new Node("n21");
+ Node n111 = new Node("n111");
+ TriMesh t11 = new TriMesh("t11");
+ TriMesh t111 = new TriMesh("t111");
+ TriMesh t211 = new TriMesh("t211");
+ root.attachChild(n1);
+ root.attachChild(n2);
+ n1.attachChild(n11);
+ n1.attachChild(n12);
+ n2.attachChild(n21);
+ n1.attachChild(t11);
+ n11.attachChild(n111);
+ n11.attachChild(t111);
+ n21.attachChild(t211);
+
+ // Negative match tests
+ assertEquals(0, root.descendantMatches("nosuchName").size());
+ assertEquals(0, root.descendantMatches(Circle.class).size());
+ assertEquals(0, n11.descendantMatches("nosuchName").size());
+ assertEquals(0, n11.descendantMatches(Circle.class).size());
+ assertEquals(0, n2.descendantMatches(".+111").size());
+
+ assertEquals(3, root.descendantMatches(TriMesh.class).size());
+ assertEquals(2, n1.descendantMatches(TriMesh.class).size());
+ assertEquals(4, root.descendantMatches(".*2.*").size());
+ assertEquals(1, n1.descendantMatches(".*2.*").size());
+ assertEquals(1, root.descendantMatches(TriMesh.class, ".+111").size());
+ assertEquals(2, root.descendantMatches(".+111").size());
+ assertEquals(1, n1.descendantMatches(TriMesh.class, ".+111").size());
+ assertEquals(2, n1.descendantMatches(".+111").size());
+
+ // Test that "descendants" does not include self:
+ assertEquals(9, root.descendantMatches((String) null).size());
+ assertEquals(2, n2.descendantMatches((String) null).size());
+ assertEquals(1, n2.descendantMatches(Node.class).size());
+ assertEquals(0, n21.descendantMatches(Node.class).size());
+ }
}
* By design, it is always safe to code loops like:<CODE><PRE>
* for (Spatial spatial : node.descendantMatches(AClass.class, "regex"))
* </PRE></CODE>
+ * </P> <P>
+ * "Descendants" does not include self, per the definition of the word.
+ * To test for descendants AND self, you must do a
+ * <code>node.matches(aClass, aRegex)</code> +
+ * <code>node.descendantMatches(aClass, aRegex)</code>.
* <P>
*
- *
* @param spatialSubclass Subclass which matching Spatials must implement.
* Null causes all Spatials to qualify.
* @param nameRegex Regular expression to match Spatial name against.
public List<Spatial> descendantMatches(
Class<? extends Spatial> spatialSubclass, String nameRegex) {
List<Spatial> newList = new ArrayList<Spatial>();
- if (matches(spatialSubclass, nameRegex)) newList.add(this);
if (getQuantity() < 1) return newList;
for (Spatial child : getChildren()) {
- if (child instanceof Node) {
+ if (child.matches(spatialSubclass, nameRegex)) newList.add(child);
+ if (child instanceof Node)
newList.addAll(((Node) child).descendantMatches(
spatialSubclass, nameRegex));
- } else if (child.matches(spatialSubclass, nameRegex)) {
- newList.add(child);
- }
}
return newList;
}