001: /*
002: * @(#)ConvertSingleLogUTest.java
003: *
004: * Copyright (C) 2004 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.codecoverage.v2.util;
028:
029: import net.sourceforge.groboutils.codecoverage.v2.IChannelLogger;
030: import java.io.Reader;
031: import java.io.BufferedReader;
032: import java.io.IOException;
033: import java.util.*;
034:
035: import junit.framework.Test;
036: import junit.framework.TestCase;
037: import junit.framework.TestSuite;
038: import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
039:
040: /**
041: * Tests the ConvertSingleLog class.
042: *
043: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
044: * @version $Date: 2004/07/07 09:39:14 $
045: * @since April 16, 2004
046: */
047: public class ConvertSingleLogUTest extends TestCase {
048: //-------------------------------------------------------------------------
049: // Standard JUnit Class-specific declarations
050:
051: private static final Class THIS_CLASS = ConvertSingleLogUTest.class;
052: private static final AutoDoc DOC = new AutoDoc(THIS_CLASS);
053:
054: public ConvertSingleLogUTest(String name) {
055: super (name);
056: }
057:
058: //-------------------------------------------------------------------------
059: // Tests
060:
061: public void testConstructor1() {
062: try {
063: new ConvertSingleLog(null);
064: fail("Didn't throw IllegalArgumentException");
065: } catch (IllegalArgumentException e) {
066: // check exception
067: }
068: }
069:
070: public void testConstructor2() {
071: try {
072: new ConvertSingleLog(new IChannelLogger[0]);
073: fail("Didn't throw IllegalArgumentException");
074: } catch (IllegalArgumentException e) {
075: // check exception
076: }
077: }
078:
079: public void testConstructor3() {
080: try {
081: new ConvertSingleLog(new IChannelLogger[1]);
082: fail("Didn't throw IllegalArgumentException");
083: } catch (IllegalArgumentException e) {
084: // check exception
085: }
086: }
087:
088: public void testParseShort1() {
089: MyChannelLogger mcl = new MyChannelLogger();
090: ConvertSingleLog csl = new ConvertSingleLog(
091: new IChannelLogger[] { mcl });
092:
093: try {
094: csl.parseShort("1,", "0,1,2");
095: fail("Did not throw IOE");
096: } catch (IOException e) {
097: // check output
098: }
099: }
100:
101: public void testParseShort2() {
102: MyChannelLogger mcl = new MyChannelLogger();
103: ConvertSingleLog csl = new ConvertSingleLog(
104: new IChannelLogger[] { mcl });
105:
106: try {
107: csl.parseShort("asdfasdf", "0,1,2");
108: fail("Did not throw IOE");
109: } catch (IOException e) {
110: // check output
111: }
112: }
113:
114: public void testParseShort3() throws IOException {
115: MyChannelLogger mcl = new MyChannelLogger();
116: ConvertSingleLog csl = new ConvertSingleLog(
117: new IChannelLogger[] { mcl });
118:
119: assertEquals("Didn't convert right", 12, csl.parseShort("12",
120: "0,1,2"));
121: }
122:
123: public void testNextElement1() {
124: MyChannelLogger mcl = new MyChannelLogger();
125: ConvertSingleLog csl = new ConvertSingleLog(
126: new IChannelLogger[] { mcl });
127:
128: try {
129: int pos[] = { 0, 0 };
130: csl.nextElement(pos, "asdf");
131: fail("Did not throw IOE.");
132: } catch (IOException e) {
133: // check exception
134: }
135: }
136:
137: public void testProcessLine1() {
138: MyChannelLogger mcl = new MyChannelLogger();
139: ConvertSingleLog csl = new ConvertSingleLog(
140: new IChannelLogger[] { mcl });
141:
142: try {
143: csl.processLine(null, true);
144: fail("Did not throw IOE");
145: } catch (IOException e) {
146: assertTrue("Bad exception text", e.getMessage().indexOf(
147: "End of stream: line is null") >= 0);
148: }
149: }
150:
151: public void testProcessLine2() {
152: MyChannelLogger mcl = new MyChannelLogger();
153: ConvertSingleLog csl = new ConvertSingleLog(
154: new IChannelLogger[] { mcl });
155:
156: try {
157: csl.processLine("1,asdf,1,1", false);
158: fail("Did not throw IOE");
159: } catch (IOException e) {
160: assertTrue("Bad exception text", e.getMessage().indexOf(
161: "Invalid channel:") >= 0);
162: }
163: }
164:
165: public void testProcessLine3() throws IOException {
166: MyChannelLogger mclL[] = { new MyChannelLogger(),
167: new MyChannelLogger(), new MyChannelLogger() };
168: ConvertSingleLog csl = new ConvertSingleLog(mclL);
169:
170: csl.processLine("1,asdf1,1 2", false);
171: csl.processLine("0,asdf2,0006 000C", false);
172: csl.processLine("2,asdf3,9 3", false);
173:
174: assertEquals("did not store all class IDs for c0",
175: new String[] { "asdf2" }, mclL[0].getClassIDs());
176: assertEquals("did not store all marks for c0",
177: new String[] { "6,12" }, mclL[0].getMarks("asdf2"));
178:
179: assertEquals("did not store all class IDs for c1",
180: new String[] { "asdf1" }, mclL[1].getClassIDs());
181: assertEquals("did not store all marks for c1",
182: new String[] { "1,2" }, mclL[1].getMarks("asdf1"));
183:
184: assertEquals("did not store all class IDs for c2",
185: new String[] { "asdf3" }, mclL[2].getClassIDs());
186: assertEquals("did not store all marks for c2",
187: new String[] { "9,3" }, mclL[2].getMarks("asdf3"));
188: }
189:
190: public void testProcessLine4() throws IOException {
191: MyChannelLogger mcl = new MyChannelLogger();
192: ConvertSingleLog csl = new ConvertSingleLog(
193: new IChannelLogger[] { mcl });
194:
195: csl.processLine("0,A,0001 0002", false);
196: csl.processLine("0,B,0005 000C", false);
197: csl.processLine("0,C,03E7 0006", false);
198: csl.processLine("0,D,0012 0004", false);
199: csl.processLine("0,D,0017 004E", false);
200: csl.processLine("0,D,0017 0000", false);
201:
202: assertEquals("did not store all class IDs", new String[] { "A",
203: "B", "C", "D" }, mcl.getClassIDs());
204: assertEquals("did not store all marks for A",
205: new String[] { "1,2" }, mcl.getMarks("A"));
206: assertEquals("did not store all marks for B",
207: new String[] { "5,12" }, mcl.getMarks("B"));
208: assertEquals("did not store all marks for C",
209: new String[] { "999,6" }, mcl.getMarks("C"));
210: assertEquals("did not store all marks", new String[] { "23,78",
211: "23,0", "18,4" }, mcl.getMarks("D"));
212: }
213:
214: //-------------------------------------------------------------------------
215: // Helpers
216:
217: private static class MyChannelLogger implements IChannelLogger {
218: private Map classToMark = new HashMap();
219:
220: public void cover(String classID, short methodIndex,
221: short markIndex) {
222: List s = (List) this .classToMark.get(classID);
223: if (s == null) {
224: s = new ArrayList();
225: this .classToMark.put(classID, s);
226: }
227: s.add("" + methodIndex + ',' + markIndex);
228: }
229:
230: public String[] getClassIDs() {
231: Set s = this .classToMark.keySet();
232: return (String[]) s.toArray(new String[s.size()]);
233: }
234:
235: public String[] getMarks(String classID) {
236: List s = (List) this .classToMark.get(classID);
237: if (s == null) {
238: return new String[0];
239: }
240: return (String[]) s.toArray(new String[s.size()]);
241: }
242: }
243:
244: private static void assertEquals(String msg, String[] a, String[] b) {
245: if (a == null) {
246: assertNull(msg, b);
247: return;
248: }
249: if (b == null) {
250: fail(msg + ": actual array is null, but expected isn't.");
251: return;
252: }
253:
254: assertEquals(msg + ": lengths don't match;", a.length, b.length);
255: Arrays.sort(a);
256: Arrays.sort(b);
257: for (int i = 0; i < a.length; ++i) {
258: assertEquals(msg + ": contents aren't equal", a[i], b[i]);
259: }
260: }
261:
262: //-------------------------------------------------------------------------
263: // Standard JUnit declarations
264:
265: public static Test suite() {
266: TestSuite suite = new TestSuite(THIS_CLASS);
267:
268: return suite;
269: }
270:
271: public static void main(String[] args) {
272: String[] name = { THIS_CLASS.getName() };
273:
274: // junit.textui.TestRunner.main( name );
275: // junit.swingui.TestRunner.main( name );
276:
277: junit.textui.TestRunner.main(name);
278: }
279:
280: /**
281: *
282: * @exception Exception thrown under any exceptional condition.
283: */
284: protected void setUp() throws Exception {
285: super .setUp();
286:
287: // set ourself up
288: }
289:
290: /**
291: *
292: * @exception Exception thrown under any exceptional condition.
293: */
294: protected void tearDown() throws Exception {
295: // tear ourself down
296:
297: super.tearDown();
298: }
299: }
|