View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.jaxen;
5   
6   import static org.junit.Assert.assertEquals;
7   import static org.junit.Assert.assertFalse;
8   import static org.junit.Assert.assertSame;
9   import static org.junit.Assert.fail;
10  
11  import java.util.Iterator;
12  import java.util.List;
13  
14  import net.sourceforge.pmd.PMD;
15  import net.sourceforge.pmd.Report;
16  import net.sourceforge.pmd.lang.ast.Node;
17  import net.sourceforge.pmd.lang.ast.xpath.DocumentNavigator;
18  import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
19  import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
20  import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
21  import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
22  import net.sourceforge.pmd.lang.java.ast.ASTStatement;
23  import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
24  import net.sourceforge.pmd.testframework.RuleTst;
25  
26  import org.jaxen.BaseXPath;
27  import org.jaxen.JaxenException;
28  import org.jaxen.UnsupportedAxisException;
29  import org.junit.Before;
30  import org.junit.Test;
31  
32  
33  public class DocumentNavigatorTest extends RuleTst {
34  
35  
36      private TestRule rule;
37  
38      private class TestRule extends AbstractJavaRule {
39  
40          private Node compilationUnit;
41          private Node importDeclaration;
42          private Node statement;
43          private Node primaryPrefix;
44          private Node primaryExpression;
45  
46          /**
47           * @see net.sourceforge.pmd.lang.java.ast.JavaParserVisitor#visit(ASTCompilationUnit, Object)
48           */
49          public Object visit(ASTCompilationUnit node, Object data) {
50              this.compilationUnit = node;
51              return super.visit(node, data);
52          }
53  
54          public Object visit(ASTImportDeclaration node, Object data) {
55              this.importDeclaration = node;
56              return super.visit(node, data);
57          }
58  
59          public Object visit(ASTStatement node, Object data) {
60              this.statement = node;
61              return super.visit(node, data);
62          }
63  
64          public Object visit(ASTPrimaryPrefix node, Object data) {
65              this.primaryPrefix = node;
66              return super.visit(node, data);
67          }
68  
69          public Object visit(ASTPrimaryExpression node, Object data) {
70              this.primaryExpression = node;
71              return super.visit(node, data);
72          }
73      }
74  
75      @Before 
76      public void setUp() throws Exception {
77          try {
78              rule = new TestRule();
79              runTestFromString(TEST, rule, new Report());
80          } catch (Throwable xx) {
81              xx.printStackTrace();
82              fail();
83          }
84      }
85  
86      @Test
87      public void testChildAxisIterator() {
88          DocumentNavigator nav = new DocumentNavigator();
89          Iterator iter = nav.getChildAxisIterator(rule.compilationUnit);
90          assertSame(rule.compilationUnit.jjtGetChild(0), iter.next());
91          assertSame(rule.compilationUnit.jjtGetChild(1), iter.next());
92          assertFalse(iter.hasNext());
93      }
94  
95      @Test
96      public void testParentAxisIterator() {
97          DocumentNavigator nav = new DocumentNavigator();
98          Iterator iter = nav.getParentAxisIterator(rule.importDeclaration);
99          assertSame(rule.importDeclaration.jjtGetParent(), iter.next());
100         assertFalse(iter.hasNext());
101     }
102 
103     @Test
104     public void testParentAxisIterator2() {
105         DocumentNavigator nav = new DocumentNavigator();
106         Iterator iter = nav.getParentAxisIterator(rule.compilationUnit);
107         assertFalse(iter.hasNext());
108     }
109 
110     @Test
111     public void testDescendantAxisIterator() throws UnsupportedAxisException {
112         DocumentNavigator nav = new DocumentNavigator();
113         Iterator iter = nav.getDescendantAxisIterator(rule.statement);
114         Node statementExpression = rule.statement.jjtGetChild(0);
115         assertSame(statementExpression, iter.next());
116         Node primaryExpression = statementExpression.jjtGetChild(0);
117         assertSame(primaryExpression, iter.next());
118         Node primaryPrefix = primaryExpression.jjtGetChild(0);
119         assertSame(primaryPrefix, iter.next());
120         Node primarySuffix = primaryExpression.jjtGetChild(1);
121 //        assertSame(primarySuffix, iter.next());
122         Node name = primaryPrefix.jjtGetChild(0);
123 //        assertSame(name, iter.next());
124         Node arguments = primarySuffix.jjtGetChild(0);
125 //        assertSame(arguments, iter.next());
126 //        assertFalse(iter.hasNext());
127     }
128 
129     @Test
130     public void testDescendantAxisIterator2() throws UnsupportedAxisException {
131         DocumentNavigator nav = new DocumentNavigator();
132         Iterator iter = nav.getDescendantAxisIterator(rule.primaryPrefix);
133         Node name = rule.primaryPrefix.jjtGetChild(0);
134         assertSame(name, iter.next());
135         assertFalse(iter.hasNext());
136     }
137 
138     @Test
139     public void testFollowingSiblingAxisIterator() {
140         DocumentNavigator nav = new DocumentNavigator();
141         Iterator iter = nav.getFollowingSiblingAxisIterator(rule.primaryExpression.jjtGetChild(0));
142         assertSame(rule.primaryExpression.jjtGetChild(1), iter.next());
143         assertFalse(iter.hasNext());
144     }
145 
146     @Test
147     public void testFollowingSiblingAxisIterator2() {
148         DocumentNavigator nav = new DocumentNavigator();
149         Iterator iter = nav.getFollowingSiblingAxisIterator(rule.primaryExpression.jjtGetChild(1));
150         assertFalse(iter.hasNext());
151     }
152 
153     @Test
154     public void testPrecedingSiblingAxisIterator() {
155         DocumentNavigator nav = new DocumentNavigator();
156         Iterator iter = nav.getPrecedingSiblingAxisIterator(rule.primaryExpression.jjtGetChild(1));
157         assertSame(rule.primaryExpression.jjtGetChild(0), iter.next());
158         assertFalse(iter.hasNext());
159     }
160 
161     @Test
162     public void testPrecedingSiblingAxisIterator2() {
163         DocumentNavigator nav = new DocumentNavigator();
164         Iterator iter = nav.getPrecedingSiblingAxisIterator(rule.primaryExpression.jjtGetChild(0));
165         assertFalse(iter.hasNext());
166     }
167 
168     @Test
169     public void testXPath() throws JaxenException {
170         BaseXPath xPath = new BaseXPath(".//*", new DocumentNavigator());
171         List matches = xPath.selectNodes(rule.statement);
172         assertEquals(6, matches.size());
173     }
174 
175     @Test
176     public void testXPath2() throws JaxenException {
177         BaseXPath xPath = new BaseXPath(".//*", new DocumentNavigator());
178         List matches = xPath.selectNodes(rule.importDeclaration);
179         assertEquals(1, matches.size());
180     }
181 
182 
183     public static final String TEST =
184             "import java.io.*;" + PMD.EOL +
185             "public class Foo {" + PMD.EOL +
186             " public Foo() {" + PMD.EOL +
187             "  try {" + PMD.EOL +
188             "   FileReader fr = new FileReader(\"/dev/null\");" + PMD.EOL +
189             "  } catch (Exception e) {}" + PMD.EOL +
190             "  try {" + PMD.EOL +
191             "   FileReader fr = new FileReader(\"/dev/null\");" + PMD.EOL +
192             "  } catch (Exception e) {" + PMD.EOL +
193             "   e.printStackTrace();" + PMD.EOL +
194             "   // this shouldn't show up on the report" + PMD.EOL +
195             "  }" + PMD.EOL +
196             " }" + PMD.EOL +
197             "}";
198 
199     public static junit.framework.Test suite() {
200         return new junit.framework.JUnit4TestAdapter(DocumentNavigatorTest.class);
201     }
202 }