KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > api > DetailASTTest


1 package com.puppycrawl.tools.checkstyle.api;
2
3 import junit.framework.TestCase;
4
5 import java.io.File JavaDoc;
6 import java.io.FileFilter JavaDoc;
7 import java.text.MessageFormat JavaDoc;
8
9 import com.puppycrawl.tools.checkstyle.TreeWalker;
10
11 /**
12  * TestCase to check DetailAST.
13  * @author Oliver Burn
14  */

15 public class DetailASTTest extends TestCase {
16
17     public void testGetChildCount() {
18         final DetailAST root = new DetailAST();
19         final DetailAST firstLevelA = new DetailAST();
20         final DetailAST firstLevelB = new DetailAST();
21         final DetailAST secondLevelA = new DetailAST();
22
23         root.setFirstChild(firstLevelA);
24
25         firstLevelA.setParent(root);
26         firstLevelA.setFirstChild(secondLevelA);
27         firstLevelA.setNextSibling(firstLevelB);
28
29         firstLevelB.setParent(root);
30
31         secondLevelA.setParent(firstLevelA);
32
33         assertEquals(0, secondLevelA.getChildCount());
34         assertEquals(0, firstLevelB.getChildCount());
35         assertEquals(1, firstLevelA.getChildCount());
36         assertEquals(2, root.getChildCount());
37         assertEquals(2, root.getChildCount());
38
39         assertNull(root.getPreviousSibling());
40         assertNull(firstLevelA.getPreviousSibling());
41         assertNull(secondLevelA.getPreviousSibling());
42         assertEquals(firstLevelA, firstLevelB.getPreviousSibling());
43     }
44
45     public void testTreeStructure() throws Exception JavaDoc
46     {
47         checkDir(new File JavaDoc(System.getProperty("testinputs.dir")));
48     }
49
50     private void checkDir(File JavaDoc dir) throws Exception JavaDoc
51     {
52         File JavaDoc[] files = dir.listFiles(new FileFilter JavaDoc() {
53                 public boolean accept(File JavaDoc file) {
54                     return (file.getName().endsWith(".java")
55                             || file.isDirectory())
56                         && !file.getName().endsWith("InputGrammar.java");
57                 }
58             });
59         for (int i = 0; i < files.length; i++) {
60             if (files[i].isFile()) {
61                 checkFile(files[i].getCanonicalPath());
62             }
63             else if (files[i].isDirectory()) {
64                 checkDir(files[i]);
65             }
66         }
67     }
68
69     private void checkFile(String JavaDoc filename) throws Exception JavaDoc
70     {
71         final String JavaDoc[] lines =
72             Utils.getLines(filename,
73                            System.getProperty("file.encoding", "UTF-8"));
74         final FileContents contents = new FileContents(filename, lines);
75         final DetailAST rootAST = TreeWalker.parse(contents);
76         checkTree(rootAST, null, null, filename, rootAST);
77     }
78
79     private void checkTree(final DetailAST node,
80                            final DetailAST parent,
81                            final DetailAST prev,
82                            final String JavaDoc filename,
83                            final DetailAST root)
84     {
85         Object JavaDoc[] params = new Object JavaDoc[] {
86             node, parent, prev, filename, root,
87         };
88         String JavaDoc msg = MessageFormat.format(
89             "Bad parent node={0} parent={1} filename={3} root={4}",
90             params);
91         assertEquals(msg, parent, node.getParent());
92         msg = MessageFormat.format(
93             "Bad prev node={0} prev={2} parent={1} filename={3} root={4}",
94             params);
95         assertEquals(msg, prev, node.getPreviousSibling());
96
97         if (node.getFirstChild() != null) {
98             checkTree((DetailAST) node.getFirstChild(), node, null,
99                       filename, root);
100         }
101         if (node.getNextSibling() != null) {
102             checkTree((DetailAST) node.getNextSibling(), parent, node,
103                       filename, root);
104         }
105     }
106 }
107
Popular Tags