Source Code Cross Referenced for SimpleNodeTest.java in  » Code-Analyzer » pmd-4.2rc1 » test » net » sourceforge » pmd » ast » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Code Analyzer » pmd 4.2rc1 » test.net.sourceforge.pmd.ast 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
003:         */package test.net.sourceforge.pmd.ast;
004:
005:        import static org.junit.Assert.assertEquals;
006:        import static org.junit.Assert.assertFalse;
007:        import static org.junit.Assert.assertNotNull;
008:        import static org.junit.Assert.assertTrue;
009:        import net.sourceforge.pmd.PMD;
010:        import net.sourceforge.pmd.ast.ASTAssignmentOperator;
011:        import net.sourceforge.pmd.ast.ASTBlock;
012:        import net.sourceforge.pmd.ast.ASTBlockStatement;
013:        import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
014:        import net.sourceforge.pmd.ast.ASTCompilationUnit;
015:        import net.sourceforge.pmd.ast.ASTExpression;
016:        import net.sourceforge.pmd.ast.ASTExtendsList;
017:        import net.sourceforge.pmd.ast.ASTFieldDeclaration;
018:        import net.sourceforge.pmd.ast.ASTImplementsList;
019:        import net.sourceforge.pmd.ast.ASTMethodDeclaration;
020:        import net.sourceforge.pmd.ast.ASTName;
021:        import net.sourceforge.pmd.ast.ASTReturnStatement;
022:        import net.sourceforge.pmd.ast.ASTStatement;
023:        import net.sourceforge.pmd.ast.ASTVariableInitializer;
024:        import net.sourceforge.pmd.ast.Node;
025:        import net.sourceforge.pmd.ast.SimpleNode;
026:
027:        import org.junit.Ignore;
028:        import org.junit.Test;
029:
030:        import test.net.sourceforge.pmd.testframework.ParserTst;
031:
032:        import java.util.ArrayList;
033:        import java.util.Iterator;
034:        import java.util.List;
035:        import java.util.Set;
036:
037:        public class SimpleNodeTest extends ParserTst {
038:
039:            @Test
040:            public void testMethodDiffLines() throws Throwable {
041:                Set methods = getNodes(ASTMethodDeclaration.class,
042:                        METHOD_DIFF_LINES);
043:                Iterator iter = methods.iterator();
044:                verifyNode((SimpleNode) iter.next(), 2, 9, 4, 2);
045:            }
046:
047:            @Test
048:            public void testMethodSameLine() throws Throwable {
049:                Set methods = getNodes(ASTMethodDeclaration.class,
050:                        METHOD_SAME_LINE);
051:                verifyNode((SimpleNode) methods.iterator().next(), 2, 9, 2, 21);
052:            }
053:
054:            @Test
055:            public void testNoLookahead() throws Throwable {
056:                String code = NO_LOOKAHEAD; // 1, 8 -> 1, 20
057:                Set uCD = getNodes(ASTClassOrInterfaceDeclaration.class, code);
058:                verifyNode((SimpleNode) uCD.iterator().next(), 1, 8, 1, 20);
059:            }
060:
061:            @Test
062:            public void testHasExplicitExtends() throws Throwable {
063:                String code = HAS_EXPLICIT_EXTENDS;
064:                ASTClassOrInterfaceDeclaration ucd = getNodes(
065:                        ASTClassOrInterfaceDeclaration.class, code).iterator()
066:                        .next();
067:                assertTrue(ucd.jjtGetChild(0) instanceof  ASTExtendsList);
068:            }
069:
070:            @Test
071:            public void testNoExplicitExtends() throws Throwable {
072:                String code = NO_EXPLICIT_EXTENDS;
073:                ASTClassOrInterfaceDeclaration ucd = getNodes(
074:                        ASTClassOrInterfaceDeclaration.class, code).iterator()
075:                        .next();
076:                assertFalse(ucd.jjtGetChild(0) instanceof  ASTExtendsList);
077:            }
078:
079:            @Test
080:            public void testHasExplicitImplements() throws Throwable {
081:                String code = HAS_EXPLICIT_IMPLEMENTS;
082:                ASTClassOrInterfaceDeclaration ucd = getNodes(
083:                        ASTClassOrInterfaceDeclaration.class, code).iterator()
084:                        .next();
085:                assertTrue(ucd.jjtGetChild(0) instanceof  ASTImplementsList);
086:            }
087:
088:            @Test
089:            public void testNoExplicitImplements() throws Throwable {
090:                String code = NO_EXPLICIT_IMPLEMENTS;
091:                ASTClassOrInterfaceDeclaration ucd = getNodes(
092:                        ASTClassOrInterfaceDeclaration.class, code).iterator()
093:                        .next();
094:                assertFalse(ucd.jjtGetChild(0) instanceof  ASTImplementsList);
095:            }
096:
097:            @Test
098:            public void testColumnsOnQualifiedName() throws Throwable {
099:                Set name = getNodes(ASTName.class, QUALIFIED_NAME);
100:                Iterator i = name.iterator();
101:                while (i.hasNext()) {
102:                    SimpleNode node = (SimpleNode) i.next();
103:                    if (node.getImage().equals("java.io.File")) {
104:                        verifyNode(node, 1, 8, 1, 19);
105:                    }
106:                }
107:            }
108:
109:            @Test
110:            public void testLineNumbersForNameSplitOverTwoLines()
111:                    throws Throwable {
112:                Set name = getNodes(ASTName.class, BROKEN_LINE_IN_NAME);
113:                Iterator i = name.iterator();
114:                while (i.hasNext()) {
115:                    SimpleNode node = (SimpleNode) i.next();
116:                    if (node.getImage().equals("java.io.File")) {
117:                        verifyNode(node, 1, 8, 2, 4);
118:                    }
119:                    if (node.getImage().equals("Foo")) {
120:                        verifyNode(node, 2, 15, 2, 18);
121:                    }
122:                }
123:            }
124:
125:            @Test
126:            public void testLineNumbersAreSetOnAllSiblings() throws Throwable {
127:                Set blocks = getNodes(ASTBlock.class, LINE_NUMBERS_ON_SIBLINGS);
128:                Iterator i = blocks.iterator();
129:                while (i.hasNext()) {
130:                    ASTBlock b = (ASTBlock) i.next();
131:                    assertTrue(b.getBeginLine() > 0);
132:                }
133:                blocks = getNodes(ASTVariableInitializer.class,
134:                        LINE_NUMBERS_ON_SIBLINGS);
135:                i = blocks.iterator();
136:                while (i.hasNext()) {
137:                    ASTVariableInitializer b = (ASTVariableInitializer) i
138:                            .next();
139:                    assertTrue(b.getBeginLine() > 0);
140:                }
141:                blocks = getNodes(ASTExpression.class, LINE_NUMBERS_ON_SIBLINGS);
142:                i = blocks.iterator();
143:                while (i.hasNext()) {
144:                    ASTExpression b = (ASTExpression) i.next();
145:                    assertTrue(b.getBeginLine() > 0);
146:                }
147:            }
148:
149:            @Test
150:            public void testFindChildrenOfType() {
151:                ASTBlock block = new ASTBlock(2);
152:                block.jjtAddChild(new ASTReturnStatement(1), 0);
153:                assertEquals(1, block.findChildrenOfType(
154:                        ASTReturnStatement.class).size());
155:            }
156:
157:            @Test
158:            public void testFindChildrenOfTypeMultiple() {
159:                ASTBlock block = new ASTBlock(1);
160:                block.jjtAddChild(new ASTBlockStatement(2), 0);
161:                block.jjtAddChild(new ASTBlockStatement(3), 1);
162:                List<ASTBlockStatement> nodes = new ArrayList<ASTBlockStatement>();
163:                block.findChildrenOfType(ASTBlockStatement.class, nodes);
164:                assertEquals(2, nodes.size());
165:            }
166:
167:            @Test
168:            public void testFindChildrenOfTypeRecurse() {
169:                ASTBlock block = new ASTBlock(1);
170:                ASTBlock childBlock = new ASTBlock(2);
171:                block.jjtAddChild(childBlock, 0);
172:                childBlock.jjtAddChild(new ASTMethodDeclaration(3), 0);
173:                List<ASTMethodDeclaration> nodes = new ArrayList<ASTMethodDeclaration>();
174:                block.findChildrenOfType(ASTMethodDeclaration.class, nodes);
175:                assertEquals(1, nodes.size());
176:            }
177:
178:            @Test
179:            public void testGetFirstChild() {
180:                ASTBlock block = new ASTBlock(1);
181:                ASTStatement x = new ASTStatement(2);
182:                block.jjtAddChild(x, 0);
183:                block.jjtAddChild(new ASTStatement(3), 1);
184:
185:                Node n = block.getFirstChildOfType(ASTStatement.class);
186:                assertNotNull(n);
187:                assertTrue(n instanceof  ASTStatement);
188:                assertEquals(x, n);
189:            }
190:
191:            @Test
192:            public void testGetFirstChildNested() {
193:                ASTBlock block = new ASTBlock(1);
194:                ASTStatement x = new ASTStatement(2);
195:                ASTAssignmentOperator x1 = new ASTAssignmentOperator(4);
196:                x.jjtAddChild(x1, 1);
197:                block.jjtAddChild(x, 0);
198:                block.jjtAddChild(new ASTStatement(3), 1);
199:
200:                Node n = block.getFirstChildOfType(ASTAssignmentOperator.class);
201:                assertNotNull(n);
202:                assertTrue(n instanceof  ASTAssignmentOperator);
203:                assertEquals(x1, n);
204:            }
205:
206:            @Test
207:            public void testGetFirstChildNestedDeeper() {
208:                ASTBlock block = new ASTBlock(1);
209:                ASTStatement x = new ASTStatement(2);
210:                ASTAssignmentOperator x1 = new ASTAssignmentOperator(4);
211:                ASTName x2 = new ASTName(5);
212:
213:                x.jjtAddChild(x1, 1);
214:                x1.jjtAddChild(x2, 0);
215:                block.jjtAddChild(x, 0);
216:                block.jjtAddChild(new ASTStatement(3), 1);
217:
218:                Node n = block.getFirstChildOfType(ASTName.class);
219:                assertNotNull(n);
220:                assertTrue(n instanceof  ASTName);
221:                assertEquals(x2, n);
222:            }
223:
224:            @Ignore
225:            @Test
226:            public void testContainsNoInner() throws Throwable {
227:                ASTCompilationUnit c = getNodes(ASTCompilationUnit.class,
228:                        CONTAINS_NO_INNER).iterator().next();
229:                List<ASTFieldDeclaration> res = new ArrayList<ASTFieldDeclaration>();
230:                c.findChildrenOfType(ASTFieldDeclaration.class, res, false);
231:                assertTrue(res.isEmpty());
232:                /*        String expectedXml = "<CompilationUnit BeginColumn=\"1\" BeginLine=\"5\" EndColumn=\"1\" EndLine=\"5\">" +
233:                 "<TypeDeclaration BeginColumn=\"1\" BeginLine=\"1\" EndColumn=\"1\" EndLine=\"5\">" +
234:                 "<ClassOrInterfaceDeclaration Abstract=\"false\" BeginColumn=\"8\" BeginLine=\"1\" EndColumn=\"1\" " +
235:                 "EndLine=\"5\" Final=\"false\" Image=\"Test\" Interface=\"false\" Native=\"false\" Nested=\"false\" PackagePrivate=\"false\" Private=\"false\" Protected=\"false\" Public=\"true\" Static=\"false\" Strictfp=\"false\" Synchronized=\"false\" Transient=\"false\" Volatile=\"false\">" +
236:                 "<ClassOrInterfaceBody BeginColumn=\"19\" BeginLine=\"1\" EndColumn=\"1\" EndLine=\"5\">" +
237:                 "<ClassOrInterfaceBodyDeclaration AnonymousInnerClass=\"false\" BeginColumn=\"3\" BeginLine=\"2\" EndColumn=\"3\" EndLine=\"4\">" +
238:                 "<ClassOrInterfaceDeclaration Abstract=\"false\" BeginColumn=\"10\" BeginLine=\"2\" EndColumn=\"3\" EndLine=\"4\" Final=\"false\" " +
239:                 "Image=\"Inner\" Interface=\"false\" Native=\"false\" Nested=\"true\" PackagePrivate=\"false\" Private=\"false\" Protected=\"false\" " +
240:                 "Public=\"true\" Static=\"false\" Strictfp=\"false\" Synchronized=\"false\" Transient=\"false\" Volatile=\"false\">" +
241:                 "<ClassOrInterfaceBody BeginColumn=\"22\" BeginLine=\"2\" EndColumn=\"3\" EndLine=\"4\">" +
242:                 "<ClassOrInterfaceBodyDeclaration AnonymousInnerClass=\"false\" BeginColumn=\"4\" BeginLine=\"3\" EndColumn=\"11\" EndLine=\"3\">" +
243:                 "<FieldDeclaration Abstract=\"false\" Array=\"false\" ArrayDepth=\"0\" BeginColumn=\"4\" BeginLine=\"3\" EndColumn=\"11\" EndLine=\"3\" Final=\"false\" Native=\"false\" PackagePrivate=\"true\" Private=\"false\" Protected=\"false\" Public=\"false\" Static=\"false\" Strictfp=\"false\" Synchronized=\"false\" Transient=\"false\" VariableName=\"foo\" Volatile=\"false\"><Type Array=\"false\" ArrayDepth=\"0\" BeginColumn=\"4\" BeginLine=\"3\" EndColumn=\"6\" EndLine=\"3\">" +
244:                 "<PrimitiveType Array=\"false\" ArrayDepth=\"0\" BeginColumn=\"4\" BeginLine=\"3\" Boolean=\"false\" EndColumn=\"6\" EndLine=\"3\" Image=\"int\"/>" +
245:                 "</Type>" +
246:                 "<VariableDeclarator BeginColumn=\"8\" BeginLine=\"3\" EndColumn=\"10\" EndLine=\"3\">" +
247:                 "<VariableDeclaratorId Array=\"false\" ArrayDepth=\"0\" BeginColumn=\"8\" BeginLine=\"3\" EndColumn=\"10\" EndLine=\"3\" ExceptionBlockParameter=\"false\" Image=\"foo\"/>" +
248:                 "</VariableDeclarator></FieldDeclaration></ClassOrInterfaceBodyDeclaration></ClassOrInterfaceBody>" +
249:                 "</ClassOrInterfaceDeclaration></ClassOrInterfaceBodyDeclaration></ClassOrInterfaceBody></ClassOrInterfaceDeclaration>" +
250:                 "</TypeDeclaration></CompilationUnit>";
251:                 assertEquals( expectedXml, getXmlString( c ) );
252:                 */}
253:
254:            @Test
255:            public void testContainsNoInnerWithAnonInner() throws Throwable {
256:                ASTCompilationUnit c = getNodes(ASTCompilationUnit.class,
257:                        CONTAINS_NO_INNER_WITH_ANON_INNER).iterator().next();
258:                List<ASTFieldDeclaration> res = new ArrayList<ASTFieldDeclaration>();
259:                c.findChildrenOfType(ASTFieldDeclaration.class, res, false);
260:                assertTrue(res.isEmpty());
261:            }
262:
263:            @Test
264:            public void testContainsChildOfType() throws Throwable {
265:                ASTClassOrInterfaceDeclaration c = getNodes(
266:                        ASTClassOrInterfaceDeclaration.class,
267:                        CONTAINS_CHILDREN_OF_TYPE).iterator().next();
268:                assertTrue(c.containsChildOfType(ASTFieldDeclaration.class));
269:            }
270:
271:            @Test
272:            public void testXPathNodeSelect() throws Throwable {
273:                ASTClassOrInterfaceDeclaration c = getNodes(
274:                        ASTClassOrInterfaceDeclaration.class, TEST_XPATH)
275:                        .iterator().next();
276:                List nodes = c.findChildNodesWithXPath("//FieldDeclaration");
277:                assertEquals(2, nodes.size());
278:                assertTrue(nodes.get(0) instanceof  ASTFieldDeclaration);
279:            }
280:
281:            private void verifyNode(SimpleNode node, int beginLine,
282:                    int beginCol, int endLine, int endCol) {
283:                assertEquals("Unexpected beginning line: ", beginLine, node
284:                        .getBeginLine());
285:                assertEquals("Unexpected beginning column: ", beginCol, node
286:                        .getBeginColumn());
287:                assertEquals("Unexpected ending line:", endLine, node
288:                        .getEndLine());
289:                assertEquals("Unexpected ending column:", endCol, node
290:                        .getEndColumn());
291:            }
292:
293:            private static final String HAS_EXPLICIT_EXTENDS = "public class Test extends Foo {}";
294:
295:            private static final String NO_EXPLICIT_EXTENDS = "public class Test {}";
296:
297:            private static final String HAS_EXPLICIT_IMPLEMENTS = "public class Test implements Foo {}";
298:
299:            private static final String NO_EXPLICIT_IMPLEMENTS = "public class Test {}";
300:
301:            private static final String METHOD_SAME_LINE = "public class Test {"
302:                    + PMD.EOL + " public void foo() {}" + PMD.EOL + "}";
303:
304:            private static final String QUALIFIED_NAME = "import java.io.File;"
305:                    + PMD.EOL + "public class Foo{}";
306:
307:            private static final String BROKEN_LINE_IN_NAME = "import java.io."
308:                    + PMD.EOL + "File;" + PMD.EOL + "public class Foo{}";
309:
310:            private static final String LINE_NUMBERS_ON_SIBLINGS = "public class Foo {"
311:                    + PMD.EOL
312:                    + " void bar() {"
313:                    + PMD.EOL
314:                    + "  try {"
315:                    + PMD.EOL
316:                    + "  } catch (Exception1 e) {"
317:                    + PMD.EOL
318:                    + "   int x =2;"
319:                    + PMD.EOL
320:                    + "  }"
321:                    + PMD.EOL
322:                    + " if (x != null) {}"
323:                    + PMD.EOL + " }" + PMD.EOL + "}";
324:
325:            private static final String NO_LOOKAHEAD = "public class Foo { }";
326:
327:            private static final String METHOD_DIFF_LINES = "public class Test {"
328:                    + PMD.EOL
329:                    + " public void foo() {"
330:                    + PMD.EOL
331:                    + "  int x;"
332:                    + PMD.EOL + " }" + PMD.EOL + "}";
333:
334:            private static final String CONTAINS_CHILDREN_OF_TYPE = "public class Test {"
335:                    + PMD.EOL + "  int x;" + PMD.EOL + "}";
336:
337:            private static final String CONTAINS_NO_INNER = "public class Test {"
338:                    + PMD.EOL
339:                    + "  public class Inner {"
340:                    + PMD.EOL
341:                    + "   int foo;" + PMD.EOL + "  }" + PMD.EOL + "}";
342:
343:            private static final String CONTAINS_NO_INNER_WITH_ANON_INNER = "public class Test {"
344:                    + PMD.EOL
345:                    + "  void bar() {"
346:                    + PMD.EOL
347:                    + "   foo(new Fuz() { int x = 2;});"
348:                    + PMD.EOL
349:                    + "  }"
350:                    + PMD.EOL + "}";
351:
352:            private static final String TEST_XPATH = "public class Test {"
353:                    + PMD.EOL + "  int x = 2;" + PMD.EOL + "  int y = 42;"
354:                    + PMD.EOL + "}";
355:
356:            public static junit.framework.Test suite() {
357:                return new junit.framework.JUnit4TestAdapter(
358:                        SimpleNodeTest.class);
359:            }
360:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.