KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > incava > doctorj > Tester


1 package org.incava.doctorj;
2
3 import java.io.*;
4 import java.util.*;
5 import junit.framework.TestCase;
6 import net.sourceforge.pmd.ast.*;
7 import org.incava.analysis.*;
8 import org.incava.java.*;
9
10
11 public class Tester extends TestCase
12 {
13     public Tester(String JavaDoc name)
14     {
15         super(name);
16         
17         Options.warningLevel = Options.MAXIMUM_WARNING_LEVEL;
18     }
19
20     protected Report analyze(String JavaDoc contents, String JavaDoc version)
21     {
22         StringWriter reportOutput = new StringWriter();
23         Report report = new TerseReport(reportOutput);
24         JavaParserVisitor analyzer = new JavadocAnalyzer(report);
25         try {
26             report.reset(contents);
27
28             Reader rdr = new StringReader(contents);
29             JavaCharStream jcs = new JavaCharStream(rdr);
30             JavaParser parser = new JavaParser(jcs);
31             if ("1.3".equals(version)) {
32                 parser.setJDK13();
33             }
34             else if ("1.5".equals(version)) {
35                 parser.setJDK15();
36             }
37             ASTCompilationUnit cu = parser.CompilationUnit();
38
39             cu.jjtAccept(analyzer, null);
40         }
41         catch (ParseException e) {
42             System.out.println(e.getMessage());
43             System.out.println("Encountered errors during parse.");
44             fail(e.getMessage());
45         }
46
47         return report;
48     }
49
50     public void evaluate(String JavaDoc contents, Violation[] expectations)
51     {
52         evaluate(contents, expectations, "1.4");
53     }
54
55     public void evaluate(String JavaDoc contents, Violation[] expectations, String JavaDoc version)
56     {
57         tr.Ace.log("expectations", expectations);
58         Report report = analyze(contents, version);
59         
60         Set violations = report.getViolations();
61         tr.Ace.log("violations", violations);
62         
63         assertEquals("number of violations", expectations.length, violations.size());
64             
65         Iterator vit = violations.iterator();
66         for (int vi = 0; vit.hasNext() && vi < violations.size(); ++vi) {
67             Violation violation = (Violation)vit.next();
68             Violation exp = expectations[vi];
69                 
70             assertNotNull("violation not null", violation);
71             assertEquals("violation[" + vi + "]", exp, violation);
72         }
73
74         report.flush();
75     }
76     
77 }
78
Popular Tags