KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.puppycrawl.tools.checkstyle;
2
3 import java.io.File JavaDoc;
4
5 import junit.framework.TestCase;
6
7 import com.puppycrawl.tools.checkstyle.api.AuditEvent;
8 import com.puppycrawl.tools.checkstyle.api.AuditListener;
9 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
10 import com.puppycrawl.tools.checkstyle.api.Filter;
11 import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
12
13 public class CheckerTest extends TestCase
14 {
15     public void testDosBasedir() throws Exception JavaDoc
16     {
17         Checker c = new Checker();
18
19         c.setBasedir("c:/a\\b/./c\\..\\d");
20         assertEquals("C:\\a\\b\\d", c.getBasedir());
21     }
22
23     public void testOsBasedir() throws Exception JavaDoc
24     {
25         Checker c = new Checker();
26
27         // we need something to create absolute path
28
// let's take testinputs.dir
29
String JavaDoc testinputs_dir = System.getProperty("testinputs.dir");
30
31         if (!testinputs_dir.endsWith(File.separator)) {
32             testinputs_dir += File.separator;
33         }
34
35         c.setBasedir(testinputs_dir + "indentation/./..\\coding\\");
36
37
38         assertEquals(testinputs_dir + "coding", c.getBasedir());
39     }
40
41
42     public void testDestroy() throws Exception JavaDoc
43     {
44         DebugChecker c= new DebugChecker();
45         DebugAuditAdapter aa = new DebugAuditAdapter();
46         c.addListener(aa);
47         DebugFilter f = new DebugFilter();
48         c.addFilter(f);
49
50         c.destroy(); // should remove al listeners and filters
51

52         // Let's try fire some events
53
c.fireAuditStarted();
54         c.fireAuditFinished();
55         c.fireFileStarted("Some File Name");
56         c.fireFileFinished("Some File Name");
57
58         LocalizedMessage[] msgs = new LocalizedMessage[1];
59         msgs[0] = new LocalizedMessage(0, 0, "a Bundle", "message.key",
60                                        new Object JavaDoc[] {"arg"}, null,
61                                        getClass());
62         c.fireErrors("Some File Name", msgs);
63
64         assertFalse("Checker.destroy() doesn't remove listeners.", aa.wasCalled());
65         assertFalse("Checker.destroy() doesn't remove filters.", f.wasCalled());
66     }
67
68     public void testAddListener() throws Exception JavaDoc
69     {
70         DebugChecker c= new DebugChecker();
71         DebugAuditAdapter aa = new DebugAuditAdapter();
72         c.addListener(aa);
73
74         // Let's try fire some events
75
c.fireAuditStarted();
76         assertTrue("Checker.fireAuditStarted() doesn't call listener", aa.wasCalled());
77
78         aa.resetListener();
79         c.fireAuditFinished();
80         assertTrue("Checker.fireAuditFinished() doesn't call listener", aa.wasCalled());
81
82         aa.resetListener();
83         c.fireFileStarted("Some File Name");
84         assertTrue("Checker.fireFileStarted() doesn't call listener", aa.wasCalled());
85
86         aa.resetListener();
87         c.fireFileFinished("Some File Name");
88         assertTrue("Checker.fireFileFinished() doesn't call listener", aa.wasCalled());
89
90         aa.resetListener();
91         LocalizedMessage[] msgs = new LocalizedMessage[1];
92         msgs[0] = new LocalizedMessage(0, 0, "a Bundle", "message.key",
93                                        new Object JavaDoc[] {"arg"}, null,
94                                        getClass());
95         c.fireErrors("Some File Name", msgs);
96         assertTrue("Checker.fireErrors() doesn't call listener", aa.wasCalled());
97     }
98
99     public void testRemoveListener() throws Exception JavaDoc
100     {
101         DebugChecker c= new DebugChecker();
102         DebugAuditAdapter aa = new DebugAuditAdapter();
103         DebugAuditAdapter aa2 = new DebugAuditAdapter();
104         c.addListener(aa);
105         c.addListener(aa2);
106         c.removeListener(aa);
107
108         // Let's try fire some events
109
c.fireAuditStarted();
110         assertTrue("Checker.fireAuditStarted() doesn't call listener", aa2.wasCalled());
111         assertFalse("Checker.fireAuditStarted() does call removed listener", aa.wasCalled());
112
113         aa2.resetListener();
114         c.fireAuditFinished();
115         assertTrue("Checker.fireAuditFinished() doesn't call listener", aa2.wasCalled());
116         assertFalse("Checker.fireAuditFinished() does call removed listener", aa.wasCalled());
117
118         aa2.resetListener();
119         c.fireFileStarted("Some File Name");
120         assertTrue("Checker.fireFileStarted() doesn't call listener", aa2.wasCalled());
121         assertFalse("Checker.fireFileStarted() does call removed listener", aa.wasCalled());
122
123         aa2.resetListener();
124         c.fireFileFinished("Some File Name");
125         assertTrue("Checker.fireFileFinished() doesn't call listener", aa2.wasCalled());
126         assertFalse("Checker.fireFileFinished() does call removed listener", aa.wasCalled());
127
128         aa2.resetListener();
129         LocalizedMessage[] msgs = new LocalizedMessage[1];
130         msgs[0] = new LocalizedMessage(0, 0, "a Bundle", "message.key",
131                                        new Object JavaDoc[] {"arg"}, null,
132                                        getClass());
133         c.fireErrors("Some File Name", msgs);
134         assertTrue("Checker.fireErrors() doesn't call listener", aa2.wasCalled());
135         assertFalse("Checker.fireErrors() does call removed listener", aa.wasCalled());
136
137     }
138
139     public void testAddFilter() throws Exception JavaDoc
140     {
141         DebugChecker c= new DebugChecker();
142         DebugFilter f = new DebugFilter();
143
144         c.addFilter(f);
145
146         // Let's try fire some events
147
// we do not call filter in fireAuditStarted() (fix for 1080343)
148
// c.fireAuditStarted();
149
// assertTrue("Checker.fireAuditStarted() doesn't call filter", f.wasCalled());
150

151     // we do not call filter in fireAuditFinished() (fix for 1080343)
152
// f.resetFilter();
153
// c.fireAuditFinished();
154
// assertTrue("Checker.fireAuditFinished() doesn't call filter", f.wasCalled());
155

156     // we do not call filter in fireFileStarted() (fix for 1080343)
157
// f.resetFilter();
158
// c.fireFileStarted("Some File Name");
159
// assertTrue("Checker.fireFileStarted() doesn't call filter", f.wasCalled());
160

161     // we do not call filter in fireFileFinished() (fix for 1080343)
162
// f.resetFilter();
163
// c.fireFileFinished("Some File Name");
164
// assertTrue("Checker.fireFileFinished() doesn't call filter", f.wasCalled());
165

166         f.resetFilter();
167         LocalizedMessage[] msgs = new LocalizedMessage[1];
168         msgs[0] = new LocalizedMessage(0, 0, "a Bundle", "message.key",
169                                        new Object JavaDoc[] {"arg"}, null,
170                                        getClass());
171         c.fireErrors("Some File Name", msgs);
172         assertTrue("Checker.fireErrors() doesn't call filter", f.wasCalled());
173     }
174
175     public void testRemoveFilter() throws Exception JavaDoc
176     {
177         DebugChecker c= new DebugChecker();
178         DebugFilter f = new DebugFilter();
179         DebugFilter f2 = new DebugFilter();
180         c.addFilter(f);
181         c.addFilter(f2);
182         c.removeFilter(f);
183
184         // Let's try fire some events
185
// we do call filter in fireErrors() only (fix for 1080343)
186
// c.fireAuditStarted();
187
// assertTrue("Checker.fireAuditStarted() doesn't call filter", f2.wasCalled());
188
// assertFalse("Checker.fireAuditStarted() does call removed filter", f.wasCalled());
189

190     // we do call filter in fireErrors() only (fix for 1080343)
191
// f2.resetFilter();
192
// c.fireAuditFinished();
193
// assertTrue("Checker.fireAuditFinished() doesn't call filter", f2.wasCalled());
194
// assertFalse("Checker.fireAuditFinished() does call removed filter", f.wasCalled());
195

196     // we do call filter in fireErrors() only (fix for 1080343)
197
// f2.resetFilter();
198
// c.fireFileStarted("Some File Name");
199
// assertTrue("Checker.fireFileStarted() doesn't call filter", f2.wasCalled());
200
// assertFalse("Checker.fireFileStarted() does call removed filter", f.wasCalled());
201

202     // we do call filter in fireErrors() only (fix for 1080343)
203
// f2.resetFilter();
204
// c.fireFileFinished("Some File Name");
205
// assertTrue("Checker.fireFileFinished() doesn't call filter", f2.wasCalled());
206
// assertFalse("Checker.fireFileFinished() does call removed filter", f.wasCalled());
207

208         f2.resetFilter();
209         LocalizedMessage[] msgs = new LocalizedMessage[1];
210         msgs[0] = new LocalizedMessage(0, 0, "a Bundle", "message.key",
211                                        new Object JavaDoc[] {"arg"}, null,
212                                        getClass());
213         c.fireErrors("Some File Name", msgs);
214         assertTrue("Checker.fireErrors() doesn't call filter", f2.wasCalled());
215         assertFalse("Checker.fireErrors() does call removed filter", f.wasCalled());
216
217     }
218 }
219
220 // Utility classes
221
class DebugChecker extends Checker
222 {
223     public DebugChecker() throws CheckstyleException
224     {
225         super();
226     }
227
228     public void fireAuditFinished()
229     {
230         super.fireAuditFinished();
231     }
232
233     public void fireAuditStarted()
234     {
235         super.fireAuditStarted();
236     }
237 }
238 class DebugAuditAdapter implements AuditListener
239 {
240     /** keeps track of the number of errors */
241     private boolean mCalled;
242
243     public boolean wasCalled()
244     {
245         return mCalled;
246     }
247
248     public void resetListener()
249     {
250         mCalled = false;
251     }
252
253     /** @see AuditListener */
254     public void addError(AuditEvent aEvt)
255     {
256         mCalled = true;
257     }
258
259     /** @see AuditListener */
260     public void addException(AuditEvent aEvt, Throwable JavaDoc aThrowable)
261     {
262         mCalled = true;
263     }
264
265     /** @see AuditListener */
266     public void auditStarted(AuditEvent aEvt)
267     {
268         mCalled = true;
269     }
270
271     /** @see AuditListener */
272     public void fileStarted(AuditEvent aEvt)
273     {
274         mCalled = true;
275     }
276
277     /** @see AuditListener */
278     public void auditFinished(AuditEvent aEvt)
279     {
280         mCalled = true;
281     }
282
283     /** @see AuditListener */
284     public void fileFinished(AuditEvent aEvt)
285     {
286         mCalled = true;
287     }
288 }
289
290 class DebugFilter implements Filter
291 {
292     private boolean mCalled;
293
294     public boolean accept(AuditEvent aEvent)
295     {
296         mCalled = true;
297         return true;
298     }
299
300     public boolean wasCalled()
301     {
302         return mCalled;
303     }
304
305     public void resetFilter()
306     {
307         mCalled = false;
308     }
309 }
310
Popular Tags