KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)ClassRecordIOUTest.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 java.io.StringReader JavaDoc;
30 import java.io.StringWriter JavaDoc;
31
32 import junit.framework.Test;
33 import junit.framework.TestCase;
34 import junit.framework.TestSuite;
35 import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
36 import net.sourceforge.groboutils.codecoverage.v2.CCCreatorUtil;
37 import net.sourceforge.groboutils.codecoverage.v2.compiler.ModifiedMethod;
38
39
40 /**
41  * Tests the ClassRecordIO class.
42  *
43  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
44  * @version $Date: 2004/04/15 05:48:28 $
45  * @since January 22, 2003
46  */

47 public class ClassRecordIOUTest extends TestCase
48 {
49     //-------------------------------------------------------------------------
50
// Standard JUnit Class-specific declarations
51

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

64     
65     public void testWriteClass1() throws Exception JavaDoc
66     {
67         StringWriter JavaDoc sw = new StringWriter JavaDoc();
68         ModifiedMethod mm = createModifiedMethod( THIS_CLASS, 0 );
69         ClassRecord cr = createClassRecord( THIS_CLASS, mm );
70         ClassRecordIO crio = new ClassRecordIO();
71         
72         crio.writeClass( cr, sw );
73         String JavaDoc out = sw.toString();
74         
75         assertTrue(
76             "Did not write any data.",
77             out.length() > 0 );
78         assertEquals(
79             "Did not write the correct data.",
80             THIS_CLASS.getName().length()+":"+THIS_CLASS.getName()+
81             ","+cr.getClassCRC()+","+cr.getSourceFileName().length()+
82             ":"+cr.getSourceFileName()+",1[("+mm.getMethodName().length()+":"+
83             mm.getMethodName()+")]",
84             out );
85     }
86     
87     
88     public void testWriteClass2() throws Exception JavaDoc
89     {
90         ClassRecord cr = new ClassRecord( "a.MyClass", 100, "MyClass.java",
91             new String JavaDoc[ 0 ], createAnalysisModuleSet() );
92         StringWriter JavaDoc sw = new StringWriter JavaDoc();
93         ClassRecordIO crio = new ClassRecordIO();
94         
95         crio.writeClass( cr, sw );
96         String JavaDoc out = sw.toString();
97         
98         assertEquals(
99             "Did not write the correct data.",
100             "9:a.MyClass,100,12:MyClass.java,0[]",
101             out );
102     }
103     
104     
105     public void testReadClass1() throws Exception JavaDoc
106     {
107         StringReader JavaDoc sr = new StringReader JavaDoc(
108             "9:a.MyClass,100,12:MyClass.java,2[(4:a()V)(6:b([S)V)]"
109             );
110         ClassRecordIO crio = new ClassRecordIO();
111         
112         ClassRecord cr = crio.readClass( createAnalysisModuleSet(), sr );
113         assertNotNull(
114             "Returned null class record.",
115             cr );
116         assertEquals(
117             "Did not return correct class name.",
118             "a.MyClass",
119             cr.getClassName() );
120         assertEquals(
121             "Did not return correct class check-sum.",
122             100L,
123             cr.getClassCRC() );
124         assertEquals(
125             "Did not return correct source file name.",
126             "MyClass.java",
127             cr.getSourceFileName() );
128         String JavaDoc methods[] = cr.getMethods();
129         assertEquals(
130             "Did not find correct number of methods.",
131             2,
132             methods.length );
133         assertEquals(
134             "First method isn't correct signature.",
135             "a()V",
136             methods[0] );
137         assertEquals(
138             "Second method isn't correct signature.",
139             "b([S)V",
140             methods[1] );
141     }
142     
143     
144     public void testReadClass2() throws Exception JavaDoc
145     {
146         StringReader JavaDoc sr = new StringReader JavaDoc(
147             "9:b.MyClass,100,13:MyClass2.java,0[]"
148             );
149         ClassRecordIO crio = new ClassRecordIO();
150         
151         ClassRecord cr = crio.readClass( createAnalysisModuleSet(), sr );
152         assertNotNull(
153             "Returned null class record.",
154             cr );
155         assertEquals(
156             "Did not return correct class name.",
157             "b.MyClass",
158             cr.getClassName() );
159         assertEquals(
160             "Did not return correct class check-sum.",
161             100L,
162             cr.getClassCRC() );
163         assertEquals(
164             "Did not return correct source file name.",
165             "MyClass2.java",
166             cr.getSourceFileName() );
167         assertEquals(
168             "Created methods out of nothing.",
169             0,
170             cr.getMethods().length );
171     }
172     
173     
174     public void testReadWriteClass1() throws Exception JavaDoc
175     {
176         StringWriter JavaDoc sw = new StringWriter JavaDoc();
177         ModifiedMethod mm = createModifiedMethod( THIS_CLASS, 0 );
178         ClassRecord crIn = createClassRecord( THIS_CLASS, mm );
179         ClassRecordIO crio = new ClassRecordIO();
180         
181         crio.writeClass( crIn, sw );
182         
183         String JavaDoc out = sw.toString();
184         StringReader JavaDoc sr = new StringReader JavaDoc( out );
185         ClassRecord crOut = crio.readClass( createAnalysisModuleSet(), sr );
186
187         assertNotNull(
188             "Returned null class record.",
189             crOut );
190         assertEquals(
191             "Did not return correct class name.",
192             crIn.getClassName(),
193             crOut.getClassName() );
194         assertEquals(
195             "Did not return correct class check-sum.",
196             crIn.getClassCRC(),
197             crOut.getClassCRC() );
198         assertEquals(
199             "Did not return correct method count.",
200             crIn.getMethodCount(),
201             crOut.getMethodCount() );
202         for (int i = 0; i < crIn.getMethodCount(); ++i)
203         {
204             assertEquals(
205                 "Did not return same method for index "+i+".",
206                 crIn.getMethodAt( (short)i ),
207                 crOut.getMethodAt( (short)i ) );
208         }
209     }
210     
211     
212     //-------------------------------------------------------------------------
213
// Helpers
214

215     
216     protected ModifiedMethod createModifiedMethod( Class JavaDoc c, int index )
217             throws Exception JavaDoc
218     {
219         return CCCreatorUtil.createModifiedMethod( c, 0 );
220     }
221     
222     
223     protected AnalysisModuleSet createAnalysisModuleSet()
224     {
225         return CCCreatorUtil.createAnalysisModuleSet( 3 );
226     }
227     
228     
229     protected ClassRecord createClassRecord( Class JavaDoc c, ModifiedMethod mm )
230             throws Exception JavaDoc
231     {
232         return CCCreatorUtil.createClassRecord( c, mm,
233             createAnalysisModuleSet() );
234     }
235     
236     
237     //-------------------------------------------------------------------------
238
// Standard JUnit declarations
239

240     
241     public static Test suite()
242     {
243         TestSuite suite = new TestSuite( THIS_CLASS );
244         
245         return suite;
246     }
247     
248     public static void main( String JavaDoc[] args )
249     {
250         String JavaDoc[] name = { THIS_CLASS.getName() };
251         
252         // junit.textui.TestRunner.main( name );
253
// junit.swingui.TestRunner.main( name );
254

255         junit.textui.TestRunner.main( name );
256     }
257     
258     
259     /**
260      *
261      * @exception Exception thrown under any exceptional condition.
262      */

263     protected void setUp() throws Exception JavaDoc
264     {
265         super.setUp();
266
267        
268         // set ourself up
269
}
270     
271     
272     /**
273      *
274      * @exception Exception thrown under any exceptional condition.
275      */

276     protected void tearDown() throws Exception JavaDoc
277     {
278         // tear ourself down
279

280         
281         super.tearDown();
282     }
283 }
284
285
Popular Tags