KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > datastore > ClassRecordUTest


1 /*
2  * @(#)ClassRecordUTest.java
3  *
4  * Copyright (C) 2003 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.datastore;
28
29 import junit.framework.Test;
30 import junit.framework.TestCase;
31 import junit.framework.TestSuite;
32 import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
33 import net.sourceforge.groboutils.codecoverage.v2.CCCreatorUtil;
34 import net.sourceforge.groboutils.codecoverage.v2.IAnalysisMetaData;
35 import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModule;
36
37
38 /**
39  * Tests the ClassRecord 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:28 $
43  * @since January 22, 2003
44  */

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

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

62     
63     public void testConstructor1()
64     {
65         try
66         {
67             new ClassRecord( null, 0L, null, null, null );
68         }
69         catch (IllegalArgumentException JavaDoc ex)
70         {
71             // test exception
72
}
73     }
74     
75     
76     public void testConstructor2()
77     {
78         try
79         {
80             new ClassRecord( "A", 0L, null, null, null );
81         }
82         catch (IllegalArgumentException JavaDoc ex)
83         {
84             // test exception
85
}
86     }
87     
88     
89     public void testConstructor3()
90     {
91         try
92         {
93             new ClassRecord( null, 0L, null, new String JavaDoc[0], null );
94         }
95         catch (IllegalArgumentException JavaDoc ex)
96         {
97             // test exception
98
}
99     }
100     
101     
102     public void testConstructor4()
103     {
104         try
105         {
106             new ClassRecord( null, 0L, null, null, createAnalysisModuleSet( 4 ) );
107         }
108         catch (IllegalArgumentException JavaDoc ex)
109         {
110             // test exception
111
}
112     }
113     
114     
115     public void testConstructor5()
116     {
117         try
118         {
119             new ClassRecord( "A", 0L, null, null, createAnalysisModuleSet( 4 ) );
120         }
121         catch (IllegalArgumentException JavaDoc ex)
122         {
123             // test exception
124
}
125     }
126     
127     
128     public void testConstructor6()
129     {
130         try
131         {
132             new ClassRecord( null, 0L, "a", new String JavaDoc[0],
133                 createAnalysisModuleSet( 4 ) );
134         }
135         catch (IllegalArgumentException JavaDoc ex)
136         {
137             // test exception
138
}
139     }
140     
141     
142     public void testConstructor7()
143     {
144         try
145         {
146             new ClassRecord( "A", 0L, "a", new String JavaDoc[1],
147                 createAnalysisModuleSet( 4 ) );
148         }
149         catch (IllegalArgumentException JavaDoc ex)
150         {
151             // test exception
152
}
153     }
154     
155     
156     public void testConstructor8()
157     {
158         new ClassRecord( "A", 0L, "a", new String JavaDoc[0],
159             createAnalysisModuleSet( 4 ) );
160     }
161     
162     
163     public void testConstructor9()
164     {
165         new ClassRecord( "A", 0L, "a", new String JavaDoc[] { "m()V" },
166             createAnalysisModuleSet( 4 ) );
167     }
168     
169     
170     public void testConstructor10()
171     {
172         try
173         {
174             new ClassRecord( "A", 0L, "a", new String JavaDoc[ Short.MAX_VALUE + 1 ],
175                 createAnalysisModuleSet( 4 ) );
176             fail( "Did not throw IllegalStateException." );
177         }
178         catch (IllegalStateException JavaDoc ex)
179         {
180             assertTrue(
181                 "Did not raise exception for the correct reason.",
182                 ex.getMessage().toLowerCase().
183                     indexOf( "too many methods" ) >= 0 );
184         }
185         
186     }
187     
188     
189     public void testGetClassName1()
190     {
191         ClassRecord cr = new ClassRecord( "A", 0L, "a", new String JavaDoc[] { "m()V" },
192             createAnalysisModuleSet( 4 ) );
193         assertEquals(
194             "Did not return correct class name.",
195             "A",
196             cr.getClassName() );
197     }
198     
199     
200     public void testGetClassCRC1()
201     {
202         ClassRecord cr = new ClassRecord( "A", 100L, "a",
203             new String JavaDoc[] { "m()V" }, createAnalysisModuleSet( 4 ) );
204         assertEquals(
205             "Did not return correct class crc.",
206             100L,
207             cr.getClassCRC() );
208     }
209     
210     
211     public void testGetClassSignature1()
212     {
213         ClassRecord cr = new ClassRecord( "A", -10L, "a",
214             new String JavaDoc[] { "m()V" }, createAnalysisModuleSet( 4 ) );
215         assertEquals(
216             "Did not return correct class name.",
217             "A--10",
218             cr.getClassSignature() );
219     }
220     
221     
222     public void testGetAnalysisModuleSet1()
223     {
224         ClassRecord cr = new ClassRecord( "A", -10L, "a",
225             new String JavaDoc[] { "m()V" }, createAnalysisModuleSet( 4 ) );
226         AnalysisModuleSet ams = cr.getAnalysisModuleSet();
227         assertEquals(
228             "Did not return the correct number of modules.",
229             4,
230             ams.getAnalysisModuleCount() );
231     }
232     
233     
234     public void testGetMethods1()
235     {
236         ClassRecord cr = new ClassRecord( "A", -10L, "a",
237             new String JavaDoc[0], createAnalysisModuleSet( 4 ) );
238         String JavaDoc m[] = cr.getMethods();
239         assertEquals(
240             "Did not return correct method set.",
241             0,
242             m.length );
243     }
244     
245     
246     public void testGetMethods2()
247     {
248         ClassRecord cr = new ClassRecord( "A", -10L, "a", new String JavaDoc[] {
249             "m1()V", "m2()V", "m3()V" },
250             createAnalysisModuleSet( 4 ) );
251         String JavaDoc m[] = cr.getMethods();
252         assertEquals(
253             "Did not return correct method set length.",
254             3,
255             m.length );
256         assertEquals(
257             "Did not return correct method [0], or resorted the list.",
258             "m1()V",
259             m[0] );
260         assertEquals(
261             "Did not return correct method [1], or resorted the list.",
262             "m2()V",
263             m[1] );
264         assertEquals(
265             "Did not return correct method [2], or resorted the list.",
266             "m3()V",
267             m[2] );
268     }
269     
270     
271     public void testGetMethodIndex1()
272     {
273         ClassRecord cr = new ClassRecord( "A", -10L, "a", new String JavaDoc[] {
274             "m1()V", "m2()V", "m3()V" },
275             createAnalysisModuleSet( 4 ) );
276         try
277         {
278             cr.getMethodIndex( null );
279             fail( "Did not throw IllegalArgumentException." );
280         }
281         catch (IllegalArgumentException JavaDoc ex)
282         {
283             // test exception
284
}
285     }
286     
287     
288     public void testGetMethodIndex2()
289     {
290         ClassRecord cr = new ClassRecord( "A", -10L, "a", new String JavaDoc[] {
291             "m1()V", "m2()V", "m3()V" },
292             createAnalysisModuleSet( 4 ) );
293         assertEquals(
294             "Did not identify unknown method as not found.",
295             (short)-1,
296             cr.getMethodIndex( "m0()V" ) );
297     }
298     
299     
300     public void testGetMethodIndex3()
301     {
302         ClassRecord cr = new ClassRecord( "A", -10L, "a", new String JavaDoc[] {
303             "m1()V", "m2()V", "m3()V" },
304             createAnalysisModuleSet( 4 ) );
305         assertEquals(
306             "Did not keep the original order, or couldn't find the index [0].",
307             (short)0,
308             cr.getMethodIndex( "m1()V" ) );
309         assertEquals(
310             "Did not keep the original order, or couldn't find the index [1].",
311             (short)1,
312             cr.getMethodIndex( "m2()V" ) );
313         assertEquals(
314             "Did not keep the original order, or couldn't find the index [2].",
315             (short)2,
316             cr.getMethodIndex( "m3()V" ) );
317     }
318     
319     
320     public void testGetMethodCount1()
321     {
322         ClassRecord cr = new ClassRecord( "A", -10L, "a", new String JavaDoc[] {
323             "m1()V", "m2()V", "m3()V" },
324             createAnalysisModuleSet( 4 ) );
325         assertEquals(
326             "Did not return correct count.",
327             3,
328             cr.getMethodCount() );
329     }
330     
331     
332     public void testGetMethodCount2()
333     {
334         ClassRecord cr = new ClassRecord( "A", -10L, "a", new String JavaDoc[0],
335             createAnalysisModuleSet( 4 ) );
336         assertEquals(
337             "Did not return correct count.",
338             0,
339             cr.getMethodCount() );
340     }
341     
342     
343     public void testGetMethodAt1()
344     {
345         ClassRecord cr = new ClassRecord( "A", -10L, "a", new String JavaDoc[0],
346             createAnalysisModuleSet( 4 ) );
347         try
348         {
349             cr.getMethodAt( (short)0 );
350             fail( "Did not throw IllegalArgumentException." );
351         }
352         catch (IllegalArgumentException JavaDoc ex)
353         {
354             // test exception
355
}
356     }
357     
358     
359     public void testGetMethodAt2a()
360     {
361         ClassRecord cr = new ClassRecord( "A", -10L, "a", new String JavaDoc[] {
362             "m1()V", "m2()V", "m3()V" },
363             createAnalysisModuleSet( 4 ) );
364         try
365         {
366             cr.getMethodAt( (short)3 );
367             fail( "Did not throw IllegalArgumentException." );
368         }
369         catch (IllegalArgumentException JavaDoc ex)
370         {
371             // test exception
372
}
373     }
374     
375     
376     public void testGetMethodAt2b()
377     {
378         ClassRecord cr = new ClassRecord( "A", -10L, "a", new String JavaDoc[] {
379             "m1()V", "m2()V", "m3()V" },
380             createAnalysisModuleSet( 4 ) );
381         try
382         {
383             cr.getMethodAt( (short)-1 );
384             fail( "Did not throw IllegalArgumentException." );
385         }
386         catch (IllegalArgumentException JavaDoc ex)
387         {
388             // test exception
389
}
390     }
391     
392     
393     public void testGetMethodAt2c()
394     {
395         ClassRecord cr = new ClassRecord( "A", -10L, "a", new String JavaDoc[] {
396             "m1()V", "m2()V", "m3()V" },
397             createAnalysisModuleSet( 4 ) );
398         try
399         {
400             cr.getMethodAt( (short)4 );
401             fail( "Did not throw IllegalArgumentException." );
402         }
403         catch (IllegalArgumentException JavaDoc ex)
404         {
405             // test exception
406
}
407     }
408     
409     
410     public void testGetMethodAt3()
411     {
412         ClassRecord cr = new ClassRecord( "A", -10L, "a", new String JavaDoc[] {
413             "m1()V", "m2()V", "m3()V" },
414             createAnalysisModuleSet( 4 ) );
415         assertEquals(
416             "Did not return correct method name [0].",
417             "m1()V",
418             cr.getMethodAt( (short)0 ) );
419         assertEquals(
420             "Did not return correct method name [1].",
421             "m2()V",
422             cr.getMethodAt( (short)1 ) );
423         assertEquals(
424             "Did not return correct method name [2].",
425             "m3()V",
426             cr.getMethodAt( (short)2 ) );
427     }
428     
429     
430     public void testAddMark1()
431     {
432         ClassRecord cr = new ClassRecord( "A", -10L, "a", new String JavaDoc[] {
433             "m1()V", "m2()V", "m3()V" },
434             createAnalysisModuleSet( 4 ) );
435         try
436         {
437             cr.addMark( null );
438         }
439         catch (IllegalArgumentException JavaDoc ex)
440         {
441             // test exception
442
}
443     }
444     
445     
446     public void testMarks1a()
447     {
448         AnalysisModuleSet ams = createAnalysisModuleSet( 4 );
449         ClassRecord cr = new ClassRecord( "A", -10L, "a", new String JavaDoc[] {
450             "m1()V", "m2()V", "m3()V" }, ams );
451         String JavaDoc moduleN = ams.getAnalysisModuleAt( (short)0 ).getMeasureName();
452         cr.addMark( createMarkRecord( 0, moduleN, "m0()V", 1 ) );
453         MarkRecord mr[] = cr.getMarksForAnalysisModule( moduleN );
454         assertEquals(
455             "Mark record length is wrong.",
456             1,
457             mr.length );
458         try
459         {
460             mr[0].getMethodIndex();
461             fail( "Did not throw IllegalStateException." );
462         }
463         catch (IllegalStateException JavaDoc ex)
464         {
465             // test exception
466
}
467     }
468     
469     
470     public void testMarks1b()
471     {
472         AnalysisModuleSet ams = createAnalysisModuleSet( 4 );
473         ClassRecord cr = new ClassRecord( "A", -10L, "a", new String JavaDoc[] {
474             "m1()V", "m2()V", "m3()V" }, ams );
475         IAnalysisModule am = ams.getAnalysisModuleAt( (short)0 );
476         String JavaDoc moduleN = am.getMeasureName();
477         cr.addMark( createMarkRecord( 0, moduleN, "m0()V", 1 ) );
478         MarkRecord mr[] = cr.getMarksForAnalysisModule( am );
479         assertEquals(
480             "Mark record length is wrong.",
481             1,
482             mr.length );
483         try
484         {
485             mr[0].getMethodIndex();
486             fail( "Did not throw IllegalStateException." );
487         }
488         catch (IllegalStateException JavaDoc ex)
489         {
490             // test exception
491
}
492     }
493     
494     
495     public void testMarks2a()
496     {
497         AnalysisModuleSet ams = createAnalysisModuleSet( 4 );
498         ClassRecord cr = new ClassRecord( "A", -10L, "a", new String JavaDoc[] {
499             "m1()V", "m2()V", "m3()V" }, ams );
500         String JavaDoc moduleN1 = ams.getAnalysisModuleAt( (short)0 ).getMeasureName();
501         String JavaDoc moduleN2 = ams.getAnalysisModuleAt( (short)1 ).getMeasureName();
502         cr.addMark( createMarkRecord( 0, moduleN1, "m1()V", 1 ) );
503         cr.addMark( createMarkRecord( 0, moduleN2, "m1()V", 2 ) );
504         MarkRecord mr[] = cr.getMarksForAnalysisModule( moduleN1 );
505         assertEquals(
506             "Mark record length is wrong.",
507             1,
508             mr.length );
509         assertEquals(
510             "Wrong method index.",
511             (short)0,
512             mr[0].getMethodIndex() );
513         assertEquals(
514             "Wrong mark index.",
515             (short)1,
516             mr[0].getMarkIndex() );
517         
518         mr = cr.getMarksForAnalysisModule( moduleN2 );
519         assertEquals(
520             "Mark record length is wrong.",
521             1,
522             mr.length );
523         assertEquals(
524             "Wrong method index.",
525             (short)0,
526             mr[0].getMethodIndex() );
527         assertEquals(
528             "Wrong mark index.",
529             (short)2,
530             mr[0].getMarkIndex() );
531     }
532     
533     
534     public void testMarks2b()
535     {
536         AnalysisModuleSet ams = createAnalysisModuleSet( 4 );
537         ClassRecord cr = new ClassRecord( "A", -10L, "a", new String JavaDoc[] {
538             "m1()V", "m2()V", "m3()V" }, ams );
539         IAnalysisModule am1 = ams.getAnalysisModuleAt( (short)0 );
540         IAnalysisModule am2 = ams.getAnalysisModuleAt( (short)1 );
541         String JavaDoc moduleN1 = am1.getMeasureName();
542         String JavaDoc moduleN2 = am2.getMeasureName();
543         cr.addMark( createMarkRecord( 0, moduleN1, "m1()V", 1 ) );
544         cr.addMark( createMarkRecord( 0, moduleN2, "m1()V", 2 ) );
545         MarkRecord mr[] = cr.getMarksForAnalysisModule( am1 );
546         assertEquals(
547             "Mark record length is wrong.",
548             1,
549             mr.length );
550         assertEquals(
551             "Wrong method index.",
552             (short)0,
553             mr[0].getMethodIndex() );
554         assertEquals(
555             "Wrong mark index.",
556             (short)1,
557             mr[0].getMarkIndex() );
558         
559         mr = cr.getMarksForAnalysisModule( am2 );
560         assertEquals(
561             "Mark record length is wrong.",
562             1,
563             mr.length );
564         assertEquals(
565             "Wrong method index.",
566             (short)0,
567             mr[0].getMethodIndex() );
568         assertEquals(
569             "Wrong mark index.",
570             (short)2,
571             mr[0].getMarkIndex() );
572     }
573     
574     
575     public void testMarks3a()
576     {
577         AnalysisModuleSet ams = createAnalysisModuleSet( 4 );
578         ClassRecord cr = new ClassRecord( "A", -10L, "a", new String JavaDoc[] {
579             "m1()V", "m2()V", "m3()V" }, ams );
580         String JavaDoc moduleN = ams.getAnalysisModuleAt( (short)0 ).getMeasureName();
581         cr.addMark( createMarkRecord( 0, moduleN, "m1()V", 1 ) );
582         cr.addMark( createMarkRecord( 1, moduleN, "m1()V", 1 ) );
583         MarkRecord mr[] = cr.getMarksForAnalysisModule( moduleN );
584         assertEquals(
585             "Mark record length is wrong.",
586             1,
587             mr.length );
588     }
589     
590     
591     public void testMarks3b()
592     {
593         AnalysisModuleSet ams = createAnalysisModuleSet( 4 );
594         ClassRecord cr = new ClassRecord( "A", -10L, "a", new String JavaDoc[] {
595             "m1()V", "m2()V", "m3()V" }, ams );
596         IAnalysisModule am = ams.getAnalysisModuleAt( (short)0 );
597         String JavaDoc moduleN = am.getMeasureName();
598         cr.addMark( createMarkRecord( 0, moduleN, "m1()V", 1 ) );
599         cr.addMark( createMarkRecord( 1, moduleN, "m1()V", 1 ) );
600         MarkRecord mr[] = cr.getMarksForAnalysisModule( am );
601         assertEquals(
602             "Mark record length is wrong.",
603             1,
604             mr.length );
605     }
606     
607     
608     public void testMarks4()
609     {
610         AnalysisModuleSet ams = createAnalysisModuleSet( 4 );
611         ClassRecord cr = new ClassRecord( "A", -10L, "a", new String JavaDoc[] {
612             "m1()V", "m2()V", "m3()V" }, ams );
613         IAnalysisModule am = ams.getAnalysisModuleAt( (short)0 );
614         String JavaDoc moduleN = am.getMeasureName();
615         cr.addMark( createMarkRecord( 0, moduleN, "m1()V", 1 ) );
616         MarkRecord mr1[] = cr.getMarksForAnalysisModule( moduleN );
617         MarkRecord mr2[] = cr.getMarksForAnalysisModule( am );
618         assertSame(
619             "Marks are wrong.",
620             mr1[0],
621             mr2[0] );
622     }
623     
624     
625     //-------------------------------------------------------------------------
626
// Helpers
627

628     
629     
630     protected IAnalysisMetaData createAnalysisMetaData( int weight )
631     {
632         return CCCreatorUtil.createIAnalysisMetaData( "a", "b", (byte)weight );
633     }
634     
635     
636     protected AnalysisModuleSet createAnalysisModuleSet( int count )
637     {
638         return CCCreatorUtil.createAnalysisModuleSet( count );
639     }
640     
641     
642     protected MarkRecord createMarkRecord( int metaDataWeight, String JavaDoc am,
643             String JavaDoc methSig, int markId )
644     {
645         return new MarkRecord( createAnalysisMetaData( metaDataWeight ),
646             am, methSig, (short)markId, 1 );
647     }
648     
649     
650     //-------------------------------------------------------------------------
651
// Standard JUnit declarations
652

653     
654     public static Test suite()
655     {
656         TestSuite suite = new TestSuite( THIS_CLASS );
657         
658         return suite;
659     }
660     
661     public static void main( String JavaDoc[] args )
662     {
663         String JavaDoc[] name = { THIS_CLASS.getName() };
664         
665         // junit.textui.TestRunner.main( name );
666
// junit.swingui.TestRunner.main( name );
667

668         junit.textui.TestRunner.main( name );
669     }
670     
671     
672     /**
673      *
674      * @exception Exception thrown under any exceptional condition.
675      */

676     protected void setUp() throws Exception JavaDoc
677     {
678         super.setUp();
679
680        
681         // set ourself up
682
}
683     
684     
685     /**
686      *
687      * @exception Exception thrown under any exceptional condition.
688      */

689     protected void tearDown() throws Exception JavaDoc
690     {
691         // tear ourself down
692

693         
694         super.tearDown();
695     }
696 }
697
698
Popular Tags