OSDN Git Service

Fixed self-testing behavior of descendantMatches method.
authorblaine.dev <blaine.dev@75d07b2b-3a1a-0410-a2c5-0572b91ccdca>
Thu, 23 Jul 2009 16:34:50 +0000 (16:34 +0000)
committerblaine.dev <blaine.dev@75d07b2b-3a1a-0410-a2c5-0572b91ccdca>
Thu, 23 Jul 2009 16:34:50 +0000 (16:34 +0000)
Updated TestNode test class to JUnit v4 style.

git-svn-id: http://jmonkeyengine.googlecode.com/svn/trunk@4545 75d07b2b-3a1a-0410-a2c5-0572b91ccdca

junit/com/jme/scene/TestNode.java
src/com/jme/scene/Node.java

index 2cdfe5e..ebd7852 100644 (file)
@@ -1,13 +1,18 @@
 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());
@@ -18,6 +23,7 @@ public class TestNode extends junit.framework.TestCase {
         assertEquals("root", root.getName());
     }
 
+    @org.junit.Test
     public void testHierarchy() {
         Node child1 = new Node("child1");
         Node child2 = new Node("child2");
@@ -55,4 +61,48 @@ public class TestNode extends junit.framework.TestCase {
         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());
+    }
 }
index cc01943..28feef8 100644 (file)
@@ -717,9 +717,13 @@ public class Node extends Spatial implements Serializable, Savable {
      * 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.
@@ -732,15 +736,12 @@ public class Node extends Spatial implements Serializable, Savable {
     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;
     }