KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > ant > GroboInstrumentTaskUTest


1 /*
2  * @(#)GroboInstrumentTaskUTest.java
3  *
4  * Copyright (C) 2004 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.codecoverage.v2.ant;
28
29 import java.io.File JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.util.Properties JavaDoc;
32
33 import junit.framework.Test;
34 import junit.framework.TestSuite;
35 import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
36
37
38 /**
39  * Tests the GroboInstrumentTask class.
40  *
41  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
42  * @version $Date: 2004/04/15 05:48:27 $
43  * @since March 9, 2004
44  */

45 public class GroboInstrumentTaskUTest extends AntTestA
46 {
47     //-------------------------------------------------------------------------
48
// Standard JUnit Class-specific declarations
49

50     private static final Class JavaDoc THIS_CLASS = GroboInstrumentTaskUTest.class;
51     private static final AutoDoc DOC = new AutoDoc( THIS_CLASS );
52     
53     public GroboInstrumentTaskUTest( String JavaDoc name )
54     {
55         super( name );
56     }
57
58
59     //-------------------------------------------------------------------------
60
// Tests
61

62     //...........................
63
// Tests that pass
64

65     private static final Properties JavaDoc EXPECTED_1 = new Properties JavaDoc();
66     static {
67         EXPECTED_1.setProperty( "factory",
68             "net.sourceforge.groboutils.codecoverage.v2.logger.DirectoryChannelLoggerFactory" );
69         EXPECTED_1.setProperty( "channel-count", "0" );
70     }
71     public void testInstrument1() throws Exception JavaDoc
72     {
73         executeTarget( "instrument-1" );
74         
75         EXPECTED_1.setProperty( "logger.dir",
76             (new File JavaDoc( getCoverageDir(),
77             "logs" + File.separator + "logs" )).getAbsolutePath() );
78         
79         assertEquals(
80             "Did not create the grobocoverage.properties file correctly",
81             EXPECTED_1,
82             loadGroboProperties() );
83     }
84     
85     private static final Properties JavaDoc EXPECTED_2 = new Properties JavaDoc();
86     static {
87         EXPECTED_2.setProperty( "factory",
88             "net.sourceforge.groboutils.codecoverage.v2.logger.DirectoryChannelLoggerFactory" );
89         EXPECTED_2.setProperty( "channel-count", "0" );
90     }
91     public void testInstrument2() throws Exception JavaDoc
92     {
93         executeTarget( "instrument-2" );
94         
95         EXPECTED_2.setProperty( "logger.dir",
96             (new File JavaDoc( getCoverageDir(),
97             "logs" + File.separator + "logs" )).getAbsolutePath() );
98         
99         assertEquals(
100             "Did not create the grobocoverage.properties file correctly",
101             EXPECTED_2,
102             loadGroboProperties() );
103         
104         // Also, ensure that the data files were created...
105
assertModuleData(
106             "Did not create the module data correctly:",
107             "LineCount",
108             "a.A" );
109     }
110     
111     
112     public void testInstrument3() throws Exception JavaDoc
113     {
114         executeTarget( "instrument-3" );
115         
116         // Ensure that only one data file type for each class was created...
117
assertModuleData(
118             "Did not create the module data correctly:",
119             "LineCount",
120             "a.A" );
121         assertModuleData(
122             "Did not create the module data correctly:",
123             "LineCount",
124             "a.A$X" );
125         assertModuleData(
126             "Did not create the module data correctly:",
127             "LineCount",
128             "a.B" );
129         assertModuleData(
130             "Did not create the module data correctly:",
131             "LineCount",
132             "a.B$X" );
133     }
134     
135     
136     public void testInstrument3a() throws Exception JavaDoc
137     {
138         executeTarget( "instrument-3a" );
139         
140         // Ensure that only one data file type for each class was created...
141
assertModuleData(
142             "Did not create the module data correctly:",
143             "LineCount",
144             "a.A" );
145         assertModuleData(
146             "Did not create the module data correctly:",
147             "LineCount",
148             "a.A$X" );
149         assertModuleData(
150             "Did not remove the module data correctly:",
151             "LineCount",
152             "a.B", 0 );
153     }
154     
155     
156     public void testInstrument4() throws Exception JavaDoc
157     {
158         executeTarget( "instrument-4" );
159         
160         // Ensure that only one data file type for each class was created...
161

162         //why is this failing?
163
/*
164         assertModuleData(
165             "Did not create the module data correctly:",
166             "LineCount",
167             "a.A", 2 );
168         */

169         assertModuleData(
170             "Did not create the module data correctly:",
171             "LineCount",
172             "a.A$X" );
173         assertModuleData(
174             "Did not create the module data correctly:",
175             "LineCount",
176             "a.B", 2 );
177         assertModuleData(
178             "Did not create the module data correctly:",
179             "LineCount",
180             "a.B$X" );
181     }
182     
183     
184     public void testInstrument5() throws Exception JavaDoc
185     {
186         executeTarget( "instrument-5" );
187         
188         // Ensure that only one data file type for each class was created...
189
assertModuleData(
190             "Did not create the module data correctly:",
191             "LineCount",
192             "a.A" );
193         assertModuleData(
194             "Did not keep the module data correctly:",
195             "LineCount",
196             "a.B" );
197         assertModuleData(
198             "Did not create the module data correctly:",
199             "LineCount",
200             "a.A$X" );
201     }
202     
203     
204     private static final Properties JavaDoc EXPECTED_6 = new Properties JavaDoc();
205     static {
206         EXPECTED_6.setProperty( "factory",
207             "net.sourceforge.groboutils.codecoverage.v2.logger.DirectoryChannelLoggerFactory" );
208         EXPECTED_6.setProperty( "channel-count", "0" );
209         EXPECTED_6.setProperty( "logger.1", "A" );
210     }
211     public void testInstrument6() throws Exception JavaDoc
212     {
213         executeTarget( "instrument-6" );
214         
215         EXPECTED_6.setProperty( "logger.dir",
216             (new File JavaDoc( getCoverageDir(),
217             "logs" + File.separator + "logs" )).getAbsolutePath() );
218         EXPECTED_6.setProperty( "logger.2",
219             getCoverageDir().getAbsolutePath() );
220         
221         assertEquals(
222             "Did not create the grobocoverage.properties file correctly",
223             EXPECTED_6,
224             loadGroboProperties() );
225     }
226     
227     
228     public void testInstrument7() throws Exception JavaDoc
229     {
230         // this does all of its own tests
231
executeTarget( "instrument-7" );
232     }
233     
234     
235     private static final Properties JavaDoc EXPECTED_8 = new Properties JavaDoc();
236     static {
237         EXPECTED_8.setProperty( "factory",
238             "net.sourceforge.groboutils.codecoverage.v2.logger.MinDirChannelLoggerFactory" );
239         EXPECTED_8.setProperty( "channel-count", "0" );
240     }
241     public void testInstrument8() throws Exception JavaDoc
242     {
243         executeTarget( "instrument-8" );
244         
245         EXPECTED_8.setProperty( "logger.dir",
246             (new File JavaDoc( getCoverageDir(),
247             "logs" + File.separator + "logs" )).getAbsolutePath() );
248         
249         assertEquals(
250             "Did not create the grobocoverage.properties file correctly",
251             EXPECTED_8,
252             loadGroboProperties() );
253     }
254     
255     
256     private static final Properties JavaDoc EXPECTED_9 = new Properties JavaDoc();
257     static {
258         EXPECTED_9.setProperty( "factory",
259             "net.sourceforge.groboutils.codecoverage.v2.logger.X" );
260         EXPECTED_9.setProperty( "channel-count", "0" );
261     }
262     public void testInstrument9() throws Exception JavaDoc
263     {
264         executeTarget( "instrument-9" );
265         
266         EXPECTED_9.setProperty( "logger.dir",
267             (new File JavaDoc( getCoverageDir(),
268             "logs" + File.separator + "logs" )).getAbsolutePath() );
269         
270         assertEquals(
271             "Did not create the grobocoverage.properties file correctly",
272             EXPECTED_9,
273             loadGroboProperties() );
274     }
275     
276     
277     private static final Properties JavaDoc EXPECTED_10 = new Properties JavaDoc();
278     static {
279         EXPECTED_10.setProperty( "factory",
280             "a.X" );
281         EXPECTED_10.setProperty( "channel-count", "0" );
282     }
283     public void testInstrument10() throws Exception JavaDoc
284     {
285         executeTarget( "instrument-9" );
286         
287         EXPECTED_10.setProperty( "logger.dir",
288             (new File JavaDoc( getCoverageDir(),
289             "logs" + File.separator + "logs" )).getAbsolutePath() );
290         
291         assertEquals(
292             "Did not create the grobocoverage.properties file correctly",
293             EXPECTED_10,
294             loadGroboProperties() );
295     }
296     
297     
298     
299     //...........................
300
// Tests that fail
301

302     public void testFInstrument1() throws Exception JavaDoc
303     {
304         expectBuildExceptionContaining( "f-instrument-1",
305             "did not fail on no logdir specified",
306             "Attribute 'logdir' was never set.");
307     }
308     
309     
310     public void testFInstrument2() throws Exception JavaDoc
311     {
312         expectBuildExceptionContaining( "f-instrument-2",
313             "did not fail on no destdir specified",
314             "Attribute 'destdir' was never set.");
315     }
316     
317     
318     public void testFInstrument3() throws Exception JavaDoc
319     {
320         expectBuildExceptionContaining( "f-instrument-3",
321             "did not fail on no loggerprop value specified",
322             "No value or location given for loggerprop key \"3\".");
323     }
324     
325     
326     public void testFInstrument4() throws Exception JavaDoc
327     {
328         expectBuildExceptionContaining( "f-instrument-4",
329             "did not fail on no loggerprop key specified",
330             "No key given for loggerprop.");
331     }
332     
333     
334     public void testFInstrument5() throws Exception JavaDoc
335     {
336         expectBuildExceptionContaining( "f-instrument-5",
337             "did not fail on no loggerprop key specified",
338             "No key given for loggerprop.");
339     }
340     
341     
342     public void testFInstrument6() throws Exception JavaDoc
343     {
344         expectBuildExceptionContaining( "f-instrument-6",
345             "did not fail on no loggerprop key specified",
346             "No key given for loggerprop.");
347     }
348     
349     
350     //-------------------------------------------------------------------------
351
// Helpers
352

353     
354     protected void assertEquals( String JavaDoc text, Properties JavaDoc expected,
355             Properties JavaDoc actual )
356     {
357         PropertyCheckUtil.assertEquals( text, expected, actual );
358     }
359     
360
361     protected Properties JavaDoc loadGroboProperties()
362             throws IOException JavaDoc
363     {
364         return loadProperties(
365             (new File JavaDoc( getCoverageDir(),
366                 "classes" + File.separator + "grobocoverage.properties")).
367             getAbsolutePath() );
368     }
369     
370     
371     protected Properties JavaDoc loadProperties( String JavaDoc file )
372             throws IOException JavaDoc
373     {
374         return PropertyCheckUtil.loadProperties( file );
375     }
376     
377     
378     protected File JavaDoc getCoverageDir()
379     {
380         return new File JavaDoc( getProjectDir(), "instrument" +
381             File.separator + "coverage" );
382     }
383     
384     
385     protected void assertModuleData( String JavaDoc text, String JavaDoc moduleName,
386             String JavaDoc className )
387     {
388         StringBuffer JavaDoc errors = new StringBuffer JavaDoc( text );
389         errors.append( "(for class " ).append( className ).
390             append( " in module " ).append( moduleName ).
391             append( ")" );
392         boolean hasErrors = false;
393         String JavaDoc prefix = className + "-";
394         File JavaDoc parent = new File JavaDoc( getCoverageDir(), "logs" +
395             File.separator + "data" + File.separator + moduleName );
396         String JavaDoc contents[] = parent.list();
397         boolean classdata = false;
398         boolean classmarks = false;
399         for (int i = 0; i < contents.length; ++i)
400         {
401             if (contents[i].startsWith( prefix ))
402             {
403                 if (contents[i].endsWith( ".classdata.txt" ))
404                 {
405                     System.out.println("Found class data file "+contents[i]);
406                     if (classdata)
407                     {
408                         errors.append( "; too many classdata files" );
409                         hasErrors = true;
410                     }
411                     else
412                     {
413                         classdata = true;
414                     }
415                 }
416                 if (contents[i].endsWith( ".classmarks.txt" ))
417                 {
418                     System.out.println("Found class marks file "+contents[i]);
419                     if (classmarks)
420                     {
421                         errors.append( "; too many classmark files" );
422                         hasErrors = true;
423                     }
424                     else
425                     {
426                         classmarks = true;
427                     }
428                 }
429             }
430         }
431         
432         if (!classdata)
433         {
434             errors.append( "; did not create classdata file" );
435             hasErrors = true;
436         }
437         if (!classmarks)
438         {
439             errors.append( "; did not create classmarks file" );
440             hasErrors = true;
441         }
442         assertFalse( errors.toString(), hasErrors );
443     }
444     
445     
446     /**
447      * Yes, it's duplication of code, but the first one will be used
448      * more often, and gives a clearer error message set.
449      */

450     protected void assertModuleData( String JavaDoc text, String JavaDoc moduleName,
451             String JavaDoc className, int count )
452     {
453         String JavaDoc baseErrorText = text + "(for class " + className +
454             " in module " + moduleName + ") ";
455         String JavaDoc prefix = className + "-";
456         File JavaDoc parent = new File JavaDoc( getCoverageDir(), "logs" +
457             File.separator + "data" + File.separator + moduleName );
458         String JavaDoc contents[] = parent.list();
459         int classdata = 0;
460         int classmarks = 0;
461         for (int i = 0; i < contents.length; ++i)
462         {
463             if (contents[i].startsWith( prefix ))
464             {
465                 if (contents[i].endsWith( ".classdata.txt" ))
466                 {
467                     ++classdata;
468                 }
469                 if (contents[i].endsWith( ".classmarks.txt" ))
470                 {
471                     ++classmarks;
472                 }
473             }
474         }
475         
476         assertEquals(
477             baseErrorText + "incorrect number of classdata files.",
478             count,
479             classdata );
480         assertEquals(
481             baseErrorText + "incorrect number of classmarks files.",
482             count,
483             classmarks );
484     }
485     
486     
487     //-------------------------------------------------------------------------
488
// Standard JUnit declarations
489

490     
491     public static Test suite()
492     {
493         TestSuite suite = new TestSuite( THIS_CLASS );
494         
495         return suite;
496     }
497     
498     public static void main( String JavaDoc[] args )
499     {
500         String JavaDoc[] name = { THIS_CLASS.getName() };
501         
502         // junit.textui.TestRunner.main( name );
503
// junit.swingui.TestRunner.main( name );
504

505         junit.textui.TestRunner.main( name );
506     }
507     
508     
509     /**
510      *
511      * @exception Exception thrown under any exceptional condition.
512      */

513     protected void setUp() throws Exception JavaDoc
514     {
515         super.setUp();
516         
517         // set ourself up
518
configureProject( "instrument.xml" );
519     }
520     
521     
522     /**
523      *
524      * @exception Exception thrown under any exceptional condition.
525      */

526     protected void tearDown() throws Exception JavaDoc
527     {
528         // tear ourself down
529
executeTarget( "test-teardown" );
530         
531         super.tearDown();
532     }
533 }
534
535
Popular Tags