KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > BaseCheckTestCase


1 package com.puppycrawl.tools.checkstyle;
2
3 import com.puppycrawl.tools.checkstyle.api.AuditEvent;
4 import com.puppycrawl.tools.checkstyle.api.Configuration;
5
6 import java.io.ByteArrayInputStream JavaDoc;
7 import java.io.ByteArrayOutputStream JavaDoc;
8 import java.io.File JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.InputStreamReader JavaDoc;
11 import java.io.LineNumberReader JavaDoc;
12 import java.io.OutputStream JavaDoc;
13 import java.io.PrintStream JavaDoc;
14 import java.util.Locale JavaDoc;
15 import java.util.Properties JavaDoc;
16
17 import junit.framework.TestCase;
18
19 public abstract class BaseCheckTestCase
20     extends TestCase
21 {
22     /** a brief logger that only display info about errors */
23     protected static class BriefLogger
24         extends DefaultLogger
25     {
26         public BriefLogger(OutputStream JavaDoc out)
27         {
28             super(out, true);
29         }
30         public void auditStarted(AuditEvent evt) {}
31         public void fileFinished(AuditEvent evt) {}
32         public void fileStarted(AuditEvent evt) {}
33     }
34
35     protected final ByteArrayOutputStream JavaDoc mBAOS = new ByteArrayOutputStream JavaDoc();
36     protected final PrintStream JavaDoc mStream = new PrintStream JavaDoc(mBAOS);
37     protected final Properties JavaDoc mProps = new Properties JavaDoc();
38
39     public static DefaultConfiguration createCheckConfig(Class JavaDoc aClazz)
40     {
41         final DefaultConfiguration checkConfig =
42             new DefaultConfiguration(aClazz.getName());
43         return checkConfig;
44     }
45
46     protected Checker createChecker(Configuration aCheckConfig)
47         throws Exception JavaDoc
48     {
49         final DefaultConfiguration dc = createCheckerConfig(aCheckConfig);
50         final Checker c = new Checker();
51         // make sure the tests always run with english error messages
52
// so the tests don't fail in supported locales like german
53
final Locale JavaDoc locale = Locale.ENGLISH;
54         c.setLocaleCountry(locale.getCountry());
55         c.setLocaleLanguage(locale.getLanguage());
56         c.configure(dc);
57         c.addListener(new BriefLogger(mStream));
58         return c;
59     }
60
61     protected DefaultConfiguration createCheckerConfig(Configuration aConfig)
62     {
63         final DefaultConfiguration dc =
64             new DefaultConfiguration("configuration");
65         final DefaultConfiguration twConf = createCheckConfig(TreeWalker.class);
66         // make sure that the tests always run with this charset
67
twConf.addAttribute("charset", "iso-8859-1");
68         dc.addChild(twConf);
69         twConf.addChild(aConfig);
70         return dc;
71     }
72
73     protected static String JavaDoc getPath(String JavaDoc aFilename)
74         throws IOException JavaDoc
75     {
76         final File JavaDoc f = new File JavaDoc(System.getProperty("testinputs.dir"),
77                                 aFilename);
78         return f.getCanonicalPath();
79     }
80
81     protected void verify(Configuration aConfig, String JavaDoc aFileName, String JavaDoc[] aExpected)
82             throws Exception JavaDoc
83     {
84         verify(createChecker(aConfig), aFileName, aFileName, aExpected);
85     }
86
87     protected void verify(Checker aC, String JavaDoc aFileName, String JavaDoc[] aExpected)
88             throws Exception JavaDoc
89     {
90         verify(aC, aFileName, aFileName, aExpected);
91     }
92
93     protected void verify(Checker aC,
94                           String JavaDoc aProcessedFilename,
95                           String JavaDoc aMessageFileName,
96                           String JavaDoc[] aExpected)
97         throws Exception JavaDoc
98     {
99         verify(aC,
100             new File JavaDoc[] {new File JavaDoc(aProcessedFilename)},
101             aMessageFileName, aExpected);
102     }
103
104     protected void verify(Checker aC,
105                           File JavaDoc[] aProcessedFiles,
106                           String JavaDoc aMessageFileName,
107                           String JavaDoc[] aExpected)
108         throws Exception JavaDoc
109     {
110         mStream.flush();
111         final int errs = aC.process(aProcessedFiles);
112
113         // process each of the lines
114
final ByteArrayInputStream JavaDoc bais =
115             new ByteArrayInputStream JavaDoc(mBAOS.toByteArray());
116         final LineNumberReader JavaDoc lnr =
117             new LineNumberReader JavaDoc(new InputStreamReader JavaDoc(bais));
118
119         for (int i = 0; i < aExpected.length; i++) {
120             final String JavaDoc expected = aMessageFileName + ":" + aExpected[i];
121             final String JavaDoc actual = lnr.readLine();
122             assertEquals("error message " + i, expected, actual);
123         }
124
125         assertEquals("unexpected output: " + lnr.readLine(),
126                      aExpected.length, errs);
127         aC.destroy();
128     }
129 }
130
Popular Tags