KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > util > ConvertSingleLogUTest


1 /*
2  * @(#)ConvertSingleLogUTest.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.util;
28
29 import net.sourceforge.groboutils.codecoverage.v2.IChannelLogger;
30 import java.io.Reader JavaDoc;
31 import java.io.BufferedReader JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.util.*;
34
35 import junit.framework.Test;
36 import junit.framework.TestCase;
37 import junit.framework.TestSuite;
38 import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
39
40
41 /**
42  * Tests the ConvertSingleLog class.
43  *
44  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
45  * @version $Date: 2004/07/07 09:39:14 $
46  * @since April 16, 2004
47  */

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

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

66     
67     public void testConstructor1()
68     {
69         try
70         {
71             new ConvertSingleLog( null );
72             fail( "Didn't throw IllegalArgumentException" );
73         }
74         catch (IllegalArgumentException JavaDoc e)
75         {
76             // check exception
77
}
78     }
79     
80     
81     public void testConstructor2()
82     {
83         try
84         {
85             new ConvertSingleLog( new IChannelLogger[0] );
86             fail( "Didn't throw IllegalArgumentException" );
87         }
88         catch (IllegalArgumentException JavaDoc e)
89         {
90             // check exception
91
}
92     }
93     
94     
95     public void testConstructor3()
96     {
97         try
98         {
99             new ConvertSingleLog( new IChannelLogger[1] );
100             fail( "Didn't throw IllegalArgumentException" );
101         }
102         catch (IllegalArgumentException JavaDoc e)
103         {
104             // check exception
105
}
106     }
107     
108     
109     public void testParseShort1()
110     {
111         MyChannelLogger mcl = new MyChannelLogger();
112         ConvertSingleLog csl = new ConvertSingleLog( new IChannelLogger[] { mcl } );
113         
114         try
115         {
116             csl.parseShort( "1,", "0,1,2" );
117             fail( "Did not throw IOE" );
118         }
119         catch (IOException JavaDoc e)
120         {
121             // check output
122
}
123     }
124     
125     
126     public void testParseShort2()
127     {
128         MyChannelLogger mcl = new MyChannelLogger();
129         ConvertSingleLog csl = new ConvertSingleLog( new IChannelLogger[] { mcl } );
130         
131         try
132         {
133             csl.parseShort( "asdfasdf", "0,1,2" );
134             fail( "Did not throw IOE" );
135         }
136         catch (IOException JavaDoc e)
137         {
138             // check output
139
}
140     }
141     
142     
143     public void testParseShort3() throws IOException JavaDoc
144     {
145         MyChannelLogger mcl = new MyChannelLogger();
146         ConvertSingleLog csl = new ConvertSingleLog( new IChannelLogger[] { mcl } );
147         
148         assertEquals(
149             "Didn't convert right",
150             12,
151             csl.parseShort( "12", "0,1,2" ) );
152     }
153     
154     
155     public void testNextElement1()
156     {
157         MyChannelLogger mcl = new MyChannelLogger();
158         ConvertSingleLog csl = new ConvertSingleLog( new IChannelLogger[] { mcl } );
159         
160         try
161         {
162             int pos[] = { 0, 0 };
163             csl.nextElement( pos, "asdf" );
164             fail("Did not throw IOE.");
165         }
166         catch (IOException JavaDoc e)
167         {
168             // check exception
169
}
170     }
171     
172     
173     public void testProcessLine1()
174     {
175         MyChannelLogger mcl = new MyChannelLogger();
176         ConvertSingleLog csl = new ConvertSingleLog( new IChannelLogger[] { mcl } );
177         
178         try
179         {
180             csl.processLine( null, true );
181             fail("Did not throw IOE");
182         }
183         catch (IOException JavaDoc e)
184         {
185             assertTrue(
186                 "Bad exception text",
187                 e.getMessage().indexOf("End of stream: line is null") >= 0 );
188         }
189     }
190     
191     
192     public void testProcessLine2()
193     {
194         MyChannelLogger mcl = new MyChannelLogger();
195         ConvertSingleLog csl = new ConvertSingleLog( new IChannelLogger[] { mcl } );
196         
197         try
198         {
199             csl.processLine( "1,asdf,1,1", false );
200             fail("Did not throw IOE");
201         }
202         catch (IOException JavaDoc e)
203         {
204             assertTrue(
205                 "Bad exception text",
206                 e.getMessage().indexOf("Invalid channel:") >= 0 );
207         }
208     }
209     
210     
211     public void testProcessLine3() throws IOException JavaDoc
212     {
213         MyChannelLogger mclL[] = {
214                 new MyChannelLogger(),
215                 new MyChannelLogger(),
216                 new MyChannelLogger()
217         };
218         ConvertSingleLog csl = new ConvertSingleLog( mclL );
219         
220         csl.processLine( "1,asdf1,1 2", false );
221         csl.processLine( "0,asdf2,0006 000C", false );
222         csl.processLine( "2,asdf3,9 3", false );
223         
224         assertEquals( "did not store all class IDs for c0",
225             new String JavaDoc[] { "asdf2" },
226             mclL[0].getClassIDs() );
227         assertEquals( "did not store all marks for c0",
228             new String JavaDoc[] { "6,12" },
229             mclL[0].getMarks("asdf2") );
230         
231         assertEquals( "did not store all class IDs for c1",
232             new String JavaDoc[] { "asdf1" },
233             mclL[1].getClassIDs() );
234         assertEquals( "did not store all marks for c1",
235             new String JavaDoc[] { "1,2" },
236             mclL[1].getMarks("asdf1") );
237         
238         assertEquals( "did not store all class IDs for c2",
239             new String JavaDoc[] { "asdf3" },
240             mclL[2].getClassIDs() );
241         assertEquals( "did not store all marks for c2",
242             new String JavaDoc[] { "9,3" },
243             mclL[2].getMarks("asdf3") );
244     }
245     
246     
247     public void testProcessLine4() throws IOException JavaDoc
248     {
249         MyChannelLogger mcl = new MyChannelLogger();
250         ConvertSingleLog csl = new ConvertSingleLog( new IChannelLogger[] { mcl } );
251         
252         csl.processLine( "0,A,0001 0002", false );
253         csl.processLine( "0,B,0005 000C", false );
254         csl.processLine( "0,C,03E7 0006", false );
255         csl.processLine( "0,D,0012 0004", false );
256         csl.processLine( "0,D,0017 004E", false );
257         csl.processLine( "0,D,0017 0000", false );
258         
259         assertEquals( "did not store all class IDs",
260             new String JavaDoc[] { "A", "B", "C", "D" },
261             mcl.getClassIDs() );
262         assertEquals( "did not store all marks for A",
263             new String JavaDoc[] { "1,2" },
264             mcl.getMarks("A") );
265         assertEquals( "did not store all marks for B",
266             new String JavaDoc[] { "5,12" },
267             mcl.getMarks("B") );
268         assertEquals( "did not store all marks for C",
269             new String JavaDoc[] { "999,6" },
270             mcl.getMarks("C") );
271         assertEquals( "did not store all marks",
272             new String JavaDoc[] { "23,78", "23,0", "18,4" },
273             mcl.getMarks("D") );
274     }
275     
276     
277     
278     //-------------------------------------------------------------------------
279
// Helpers
280

281     private static class MyChannelLogger implements IChannelLogger
282     {
283         private Map classToMark = new HashMap();
284         
285         public void cover( String JavaDoc classID, short methodIndex, short markIndex )
286         {
287             List s = (List)this.classToMark.get( classID );
288             if (s == null)
289             {
290                 s = new ArrayList();
291                 this.classToMark.put( classID, s );
292             }
293             s.add( ""+methodIndex+','+markIndex );
294         }
295         
296         
297         public String JavaDoc[] getClassIDs()
298         {
299             Set s = this.classToMark.keySet();
300             return (String JavaDoc[])s.toArray( new String JavaDoc[ s.size() ] );
301         }
302         
303         public String JavaDoc[] getMarks( String JavaDoc classID )
304         {
305             List s = (List)this.classToMark.get( classID );
306             if (s == null)
307             {
308                 return new String JavaDoc[0];
309             }
310             return (String JavaDoc[])s.toArray( new String JavaDoc[ s.size() ] );
311         }
312     }
313     
314     
315     private static void assertEquals( String JavaDoc msg, String JavaDoc[] a, String JavaDoc[] b )
316     {
317         if (a == null)
318         {
319             assertNull(
320                 msg, b );
321             return;
322         }
323         if (b == null)
324         {
325             fail(msg+": actual array is null, but expected isn't.");
326             return;
327         }
328         
329         assertEquals(
330             msg+": lengths don't match;",
331             a.length,
332             b.length );
333         Arrays.sort( a );
334         Arrays.sort( b );
335         for (int i = 0; i < a.length; ++i)
336         {
337             assertEquals(
338                 msg+": contents aren't equal",
339                 a[i], b[i] );
340         }
341     }
342     
343     
344     //-------------------------------------------------------------------------
345
// Standard JUnit declarations
346

347     
348     public static Test suite()
349     {
350         TestSuite suite = new TestSuite( THIS_CLASS );
351         
352         return suite;
353     }
354     
355     public static void main( String JavaDoc[] args )
356     {
357         String JavaDoc[] name = { THIS_CLASS.getName() };
358         
359         // junit.textui.TestRunner.main( name );
360
// junit.swingui.TestRunner.main( name );
361

362         junit.textui.TestRunner.main( name );
363     }
364     
365     
366     /**
367      *
368      * @exception Exception thrown under any exceptional condition.
369      */

370     protected void setUp() throws Exception JavaDoc
371     {
372         super.setUp();
373         
374         // set ourself up
375
}
376     
377     
378     /**
379      *
380      * @exception Exception thrown under any exceptional condition.
381      */

382     protected void tearDown() throws Exception JavaDoc
383     {
384         // tear ourself down
385

386         
387         super.tearDown();
388     }
389 }
390
391
Popular Tags