001: /**
002: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
003: */package test.net.sourceforge.pmd.jaxen;
004:
005: import static org.junit.Assert.assertEquals;
006: import static org.junit.Assert.assertFalse;
007: import static org.junit.Assert.assertSame;
008: import static org.junit.Assert.fail;
009: import net.sourceforge.pmd.AbstractRule;
010: import net.sourceforge.pmd.PMD;
011: import net.sourceforge.pmd.Report;
012: import net.sourceforge.pmd.ast.ASTCompilationUnit;
013: import net.sourceforge.pmd.ast.ASTImportDeclaration;
014: import net.sourceforge.pmd.ast.ASTPrimaryExpression;
015: import net.sourceforge.pmd.ast.ASTPrimaryPrefix;
016: import net.sourceforge.pmd.ast.ASTStatement;
017: import net.sourceforge.pmd.ast.Node;
018: import net.sourceforge.pmd.jaxen.DocumentNavigator;
019:
020: import org.jaxen.BaseXPath;
021: import org.jaxen.JaxenException;
022: import org.jaxen.UnsupportedAxisException;
023: import org.junit.Before;
024: import org.junit.Test;
025:
026: import test.net.sourceforge.pmd.testframework.RuleTst;
027:
028: import java.util.Iterator;
029: import java.util.List;
030:
031: public class DocumentNavigatorTest extends RuleTst {
032:
033: private TestRule rule;
034:
035: private class TestRule extends AbstractRule {
036:
037: private Node compilationUnit;
038: private Node importDeclaration;
039: private Node statement;
040: private Node primaryPrefix;
041: private Node primaryExpression;
042:
043: /**
044: * @see net.sourceforge.pmd.ast.JavaParserVisitor#visit(ASTCompilationUnit, Object)
045: */
046: public Object visit(ASTCompilationUnit node, Object data) {
047: this .compilationUnit = node;
048: return super .visit(node, data);
049: }
050:
051: public Object visit(ASTImportDeclaration node, Object data) {
052: this .importDeclaration = node;
053: return super .visit(node, data);
054: }
055:
056: public Object visit(ASTStatement node, Object data) {
057: this .statement = node;
058: return super .visit(node, data);
059: }
060:
061: public Object visit(ASTPrimaryPrefix node, Object data) {
062: this .primaryPrefix = node;
063: return super .visit(node, data);
064: }
065:
066: public Object visit(ASTPrimaryExpression node, Object data) {
067: this .primaryExpression = node;
068: return super .visit(node, data);
069: }
070: }
071:
072: @Before
073: public void setUp() throws Exception {
074: try {
075: rule = new TestRule();
076: runTestFromString(TEST, rule, new Report());
077: } catch (Throwable xx) {
078: xx.printStackTrace();
079: fail();
080: }
081: }
082:
083: @Test
084: public void testChildAxisIterator() {
085: DocumentNavigator nav = new DocumentNavigator();
086: Iterator iter = nav.getChildAxisIterator(rule.compilationUnit);
087: assertSame(rule.compilationUnit.jjtGetChild(0), iter.next());
088: assertSame(rule.compilationUnit.jjtGetChild(1), iter.next());
089: assertFalse(iter.hasNext());
090: }
091:
092: @Test
093: public void testParentAxisIterator() {
094: DocumentNavigator nav = new DocumentNavigator();
095: Iterator iter = nav
096: .getParentAxisIterator(rule.importDeclaration);
097: assertSame(rule.importDeclaration.jjtGetParent(), iter.next());
098: assertFalse(iter.hasNext());
099: }
100:
101: @Test
102: public void testParentAxisIterator2() {
103: DocumentNavigator nav = new DocumentNavigator();
104: Iterator iter = nav.getParentAxisIterator(rule.compilationUnit);
105: assertFalse(iter.hasNext());
106: }
107:
108: @Test
109: public void testDescendantAxisIterator()
110: throws UnsupportedAxisException {
111: DocumentNavigator nav = new DocumentNavigator();
112: Iterator iter = nav.getDescendantAxisIterator(rule.statement);
113: Node statementExpression = rule.statement.jjtGetChild(0);
114: assertSame(statementExpression, iter.next());
115: Node primaryExpression = statementExpression.jjtGetChild(0);
116: assertSame(primaryExpression, iter.next());
117: Node primaryPrefix = primaryExpression.jjtGetChild(0);
118: assertSame(primaryPrefix, iter.next());
119: Node primarySuffix = primaryExpression.jjtGetChild(1);
120: // assertSame(primarySuffix, iter.next());
121: Node name = primaryPrefix.jjtGetChild(0);
122: // assertSame(name, iter.next());
123: Node arguments = primarySuffix.jjtGetChild(0);
124: // assertSame(arguments, iter.next());
125: // assertFalse(iter.hasNext());
126: }
127:
128: @Test
129: public void testDescendantAxisIterator2()
130: throws UnsupportedAxisException {
131: DocumentNavigator nav = new DocumentNavigator();
132: Iterator iter = nav
133: .getDescendantAxisIterator(rule.primaryPrefix);
134: Node name = rule.primaryPrefix.jjtGetChild(0);
135: assertSame(name, iter.next());
136: assertFalse(iter.hasNext());
137: }
138:
139: @Test
140: public void testFollowingSiblingAxisIterator() {
141: DocumentNavigator nav = new DocumentNavigator();
142: Iterator iter = nav
143: .getFollowingSiblingAxisIterator(rule.primaryExpression
144: .jjtGetChild(0));
145: assertSame(rule.primaryExpression.jjtGetChild(1), iter.next());
146: assertFalse(iter.hasNext());
147: }
148:
149: @Test
150: public void testFollowingSiblingAxisIterator2() {
151: DocumentNavigator nav = new DocumentNavigator();
152: Iterator iter = nav
153: .getFollowingSiblingAxisIterator(rule.primaryExpression
154: .jjtGetChild(1));
155: assertFalse(iter.hasNext());
156: }
157:
158: @Test
159: public void testPrecedingSiblingAxisIterator() {
160: DocumentNavigator nav = new DocumentNavigator();
161: Iterator iter = nav
162: .getPrecedingSiblingAxisIterator(rule.primaryExpression
163: .jjtGetChild(1));
164: assertSame(rule.primaryExpression.jjtGetChild(0), iter.next());
165: assertFalse(iter.hasNext());
166: }
167:
168: @Test
169: public void testPrecedingSiblingAxisIterator2() {
170: DocumentNavigator nav = new DocumentNavigator();
171: Iterator iter = nav
172: .getPrecedingSiblingAxisIterator(rule.primaryExpression
173: .jjtGetChild(0));
174: assertFalse(iter.hasNext());
175: }
176:
177: @Test
178: public void testXPath() throws JaxenException {
179: BaseXPath xPath = new BaseXPath(".//*", new DocumentNavigator());
180: List matches = xPath.selectNodes(rule.statement);
181: assertEquals(6, matches.size());
182: }
183:
184: @Test
185: public void testXPath2() throws JaxenException {
186: BaseXPath xPath = new BaseXPath(".//*", new DocumentNavigator());
187: List matches = xPath.selectNodes(rule.importDeclaration);
188: assertEquals(1, matches.size());
189: }
190:
191: public static final String TEST = "import java.io.*;" + PMD.EOL
192: + "public class Foo {" + PMD.EOL + " public Foo() {"
193: + PMD.EOL + " try {" + PMD.EOL
194: + " FileReader fr = new FileReader(\"/dev/null\");"
195: + PMD.EOL + " } catch (Exception e) {}" + PMD.EOL
196: + " try {" + PMD.EOL
197: + " FileReader fr = new FileReader(\"/dev/null\");"
198: + PMD.EOL + " } catch (Exception e) {" + PMD.EOL
199: + " e.printStackTrace();" + PMD.EOL
200: + " // this shouldn't show up on the report" + PMD.EOL
201: + " }" + PMD.EOL + " }" + PMD.EOL + "}";
202:
203: public static junit.framework.Test suite() {
204: return new junit.framework.JUnit4TestAdapter(
205: DocumentNavigatorTest.class);
206: }
207: }
|