001: /*
002: * @(#)GroboInstrumentTaskUTest.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.ant;
028:
029: import java.io.File;
030: import java.io.IOException;
031: import java.util.Properties;
032:
033: import junit.framework.Test;
034: import junit.framework.TestSuite;
035: import net.sourceforge.groboutils.autodoc.v1.AutoDoc;
036:
037: /**
038: * Tests the GroboInstrumentTask class.
039: *
040: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
041: * @version $Date: 2004/04/15 05:48:27 $
042: * @since March 9, 2004
043: */
044: public class GroboInstrumentTaskUTest extends AntTestA {
045: //-------------------------------------------------------------------------
046: // Standard JUnit Class-specific declarations
047:
048: private static final Class THIS_CLASS = GroboInstrumentTaskUTest.class;
049: private static final AutoDoc DOC = new AutoDoc(THIS_CLASS);
050:
051: public GroboInstrumentTaskUTest(String name) {
052: super (name);
053: }
054:
055: //-------------------------------------------------------------------------
056: // Tests
057:
058: //...........................
059: // Tests that pass
060:
061: private static final Properties EXPECTED_1 = new Properties();
062: static {
063: EXPECTED_1
064: .setProperty(
065: "factory",
066: "net.sourceforge.groboutils.codecoverage.v2.logger.DirectoryChannelLoggerFactory");
067: EXPECTED_1.setProperty("channel-count", "0");
068: }
069:
070: public void testInstrument1() throws Exception {
071: executeTarget("instrument-1");
072:
073: EXPECTED_1.setProperty("logger.dir", (new File(
074: getCoverageDir(), "logs" + File.separator + "logs"))
075: .getAbsolutePath());
076:
077: assertEquals(
078: "Did not create the grobocoverage.properties file correctly",
079: EXPECTED_1, loadGroboProperties());
080: }
081:
082: private static final Properties EXPECTED_2 = new Properties();
083: static {
084: EXPECTED_2
085: .setProperty(
086: "factory",
087: "net.sourceforge.groboutils.codecoverage.v2.logger.DirectoryChannelLoggerFactory");
088: EXPECTED_2.setProperty("channel-count", "0");
089: }
090:
091: public void testInstrument2() throws Exception {
092: executeTarget("instrument-2");
093:
094: EXPECTED_2.setProperty("logger.dir", (new File(
095: getCoverageDir(), "logs" + File.separator + "logs"))
096: .getAbsolutePath());
097:
098: assertEquals(
099: "Did not create the grobocoverage.properties file correctly",
100: EXPECTED_2, loadGroboProperties());
101:
102: // Also, ensure that the data files were created...
103: assertModuleData("Did not create the module data correctly:",
104: "LineCount", "a.A");
105: }
106:
107: public void testInstrument3() throws Exception {
108: executeTarget("instrument-3");
109:
110: // Ensure that only one data file type for each class was created...
111: assertModuleData("Did not create the module data correctly:",
112: "LineCount", "a.A");
113: assertModuleData("Did not create the module data correctly:",
114: "LineCount", "a.A$X");
115: assertModuleData("Did not create the module data correctly:",
116: "LineCount", "a.B");
117: assertModuleData("Did not create the module data correctly:",
118: "LineCount", "a.B$X");
119: }
120:
121: public void testInstrument3a() throws Exception {
122: executeTarget("instrument-3a");
123:
124: // Ensure that only one data file type for each class was created...
125: assertModuleData("Did not create the module data correctly:",
126: "LineCount", "a.A");
127: assertModuleData("Did not create the module data correctly:",
128: "LineCount", "a.A$X");
129: assertModuleData("Did not remove the module data correctly:",
130: "LineCount", "a.B", 0);
131: }
132:
133: public void testInstrument4() throws Exception {
134: executeTarget("instrument-4");
135:
136: // Ensure that only one data file type for each class was created...
137:
138: //why is this failing?
139: /*
140: assertModuleData(
141: "Did not create the module data correctly:",
142: "LineCount",
143: "a.A", 2 );
144: */
145: assertModuleData("Did not create the module data correctly:",
146: "LineCount", "a.A$X");
147: assertModuleData("Did not create the module data correctly:",
148: "LineCount", "a.B", 2);
149: assertModuleData("Did not create the module data correctly:",
150: "LineCount", "a.B$X");
151: }
152:
153: public void testInstrument5() throws Exception {
154: executeTarget("instrument-5");
155:
156: // Ensure that only one data file type for each class was created...
157: assertModuleData("Did not create the module data correctly:",
158: "LineCount", "a.A");
159: assertModuleData("Did not keep the module data correctly:",
160: "LineCount", "a.B");
161: assertModuleData("Did not create the module data correctly:",
162: "LineCount", "a.A$X");
163: }
164:
165: private static final Properties EXPECTED_6 = new Properties();
166: static {
167: EXPECTED_6
168: .setProperty(
169: "factory",
170: "net.sourceforge.groboutils.codecoverage.v2.logger.DirectoryChannelLoggerFactory");
171: EXPECTED_6.setProperty("channel-count", "0");
172: EXPECTED_6.setProperty("logger.1", "A");
173: }
174:
175: public void testInstrument6() throws Exception {
176: executeTarget("instrument-6");
177:
178: EXPECTED_6.setProperty("logger.dir", (new File(
179: getCoverageDir(), "logs" + File.separator + "logs"))
180: .getAbsolutePath());
181: EXPECTED_6.setProperty("logger.2", getCoverageDir()
182: .getAbsolutePath());
183:
184: assertEquals(
185: "Did not create the grobocoverage.properties file correctly",
186: EXPECTED_6, loadGroboProperties());
187: }
188:
189: public void testInstrument7() throws Exception {
190: // this does all of its own tests
191: executeTarget("instrument-7");
192: }
193:
194: private static final Properties EXPECTED_8 = new Properties();
195: static {
196: EXPECTED_8
197: .setProperty("factory",
198: "net.sourceforge.groboutils.codecoverage.v2.logger.MinDirChannelLoggerFactory");
199: EXPECTED_8.setProperty("channel-count", "0");
200: }
201:
202: public void testInstrument8() throws Exception {
203: executeTarget("instrument-8");
204:
205: EXPECTED_8.setProperty("logger.dir", (new File(
206: getCoverageDir(), "logs" + File.separator + "logs"))
207: .getAbsolutePath());
208:
209: assertEquals(
210: "Did not create the grobocoverage.properties file correctly",
211: EXPECTED_8, loadGroboProperties());
212: }
213:
214: private static final Properties EXPECTED_9 = new Properties();
215: static {
216: EXPECTED_9.setProperty("factory",
217: "net.sourceforge.groboutils.codecoverage.v2.logger.X");
218: EXPECTED_9.setProperty("channel-count", "0");
219: }
220:
221: public void testInstrument9() throws Exception {
222: executeTarget("instrument-9");
223:
224: EXPECTED_9.setProperty("logger.dir", (new File(
225: getCoverageDir(), "logs" + File.separator + "logs"))
226: .getAbsolutePath());
227:
228: assertEquals(
229: "Did not create the grobocoverage.properties file correctly",
230: EXPECTED_9, loadGroboProperties());
231: }
232:
233: private static final Properties EXPECTED_10 = new Properties();
234: static {
235: EXPECTED_10.setProperty("factory", "a.X");
236: EXPECTED_10.setProperty("channel-count", "0");
237: }
238:
239: public void testInstrument10() throws Exception {
240: executeTarget("instrument-9");
241:
242: EXPECTED_10.setProperty("logger.dir", (new File(
243: getCoverageDir(), "logs" + File.separator + "logs"))
244: .getAbsolutePath());
245:
246: assertEquals(
247: "Did not create the grobocoverage.properties file correctly",
248: EXPECTED_10, loadGroboProperties());
249: }
250:
251: //...........................
252: // Tests that fail
253:
254: public void testFInstrument1() throws Exception {
255: expectBuildExceptionContaining("f-instrument-1",
256: "did not fail on no logdir specified",
257: "Attribute 'logdir' was never set.");
258: }
259:
260: public void testFInstrument2() throws Exception {
261: expectBuildExceptionContaining("f-instrument-2",
262: "did not fail on no destdir specified",
263: "Attribute 'destdir' was never set.");
264: }
265:
266: public void testFInstrument3() throws Exception {
267: expectBuildExceptionContaining("f-instrument-3",
268: "did not fail on no loggerprop value specified",
269: "No value or location given for loggerprop key \"3\".");
270: }
271:
272: public void testFInstrument4() throws Exception {
273: expectBuildExceptionContaining("f-instrument-4",
274: "did not fail on no loggerprop key specified",
275: "No key given for loggerprop.");
276: }
277:
278: public void testFInstrument5() throws Exception {
279: expectBuildExceptionContaining("f-instrument-5",
280: "did not fail on no loggerprop key specified",
281: "No key given for loggerprop.");
282: }
283:
284: public void testFInstrument6() throws Exception {
285: expectBuildExceptionContaining("f-instrument-6",
286: "did not fail on no loggerprop key specified",
287: "No key given for loggerprop.");
288: }
289:
290: //-------------------------------------------------------------------------
291: // Helpers
292:
293: protected void assertEquals(String text, Properties expected,
294: Properties actual) {
295: PropertyCheckUtil.assertEquals(text, expected, actual);
296: }
297:
298: protected Properties loadGroboProperties() throws IOException {
299: return loadProperties((new File(getCoverageDir(), "classes"
300: + File.separator + "grobocoverage.properties"))
301: .getAbsolutePath());
302: }
303:
304: protected Properties loadProperties(String file) throws IOException {
305: return PropertyCheckUtil.loadProperties(file);
306: }
307:
308: protected File getCoverageDir() {
309: return new File(getProjectDir(), "instrument" + File.separator
310: + "coverage");
311: }
312:
313: protected void assertModuleData(String text, String moduleName,
314: String className) {
315: StringBuffer errors = new StringBuffer(text);
316: errors.append("(for class ").append(className).append(
317: " in module ").append(moduleName).append(")");
318: boolean hasErrors = false;
319: String prefix = className + "-";
320: File parent = new File(getCoverageDir(), "logs"
321: + File.separator + "data" + File.separator + moduleName);
322: String contents[] = parent.list();
323: boolean classdata = false;
324: boolean classmarks = false;
325: for (int i = 0; i < contents.length; ++i) {
326: if (contents[i].startsWith(prefix)) {
327: if (contents[i].endsWith(".classdata.txt")) {
328: System.out.println("Found class data file "
329: + contents[i]);
330: if (classdata) {
331: errors.append("; too many classdata files");
332: hasErrors = true;
333: } else {
334: classdata = true;
335: }
336: }
337: if (contents[i].endsWith(".classmarks.txt")) {
338: System.out.println("Found class marks file "
339: + contents[i]);
340: if (classmarks) {
341: errors.append("; too many classmark files");
342: hasErrors = true;
343: } else {
344: classmarks = true;
345: }
346: }
347: }
348: }
349:
350: if (!classdata) {
351: errors.append("; did not create classdata file");
352: hasErrors = true;
353: }
354: if (!classmarks) {
355: errors.append("; did not create classmarks file");
356: hasErrors = true;
357: }
358: assertFalse(errors.toString(), hasErrors);
359: }
360:
361: /**
362: * Yes, it's duplication of code, but the first one will be used
363: * more often, and gives a clearer error message set.
364: */
365: protected void assertModuleData(String text, String moduleName,
366: String className, int count) {
367: String baseErrorText = text + "(for class " + className
368: + " in module " + moduleName + ") ";
369: String prefix = className + "-";
370: File parent = new File(getCoverageDir(), "logs"
371: + File.separator + "data" + File.separator + moduleName);
372: String contents[] = parent.list();
373: int classdata = 0;
374: int classmarks = 0;
375: for (int i = 0; i < contents.length; ++i) {
376: if (contents[i].startsWith(prefix)) {
377: if (contents[i].endsWith(".classdata.txt")) {
378: ++classdata;
379: }
380: if (contents[i].endsWith(".classmarks.txt")) {
381: ++classmarks;
382: }
383: }
384: }
385:
386: assertEquals(baseErrorText
387: + "incorrect number of classdata files.", count,
388: classdata);
389: assertEquals(baseErrorText
390: + "incorrect number of classmarks files.", count,
391: classmarks);
392: }
393:
394: //-------------------------------------------------------------------------
395: // Standard JUnit declarations
396:
397: public static Test suite() {
398: TestSuite suite = new TestSuite(THIS_CLASS);
399:
400: return suite;
401: }
402:
403: public static void main(String[] args) {
404: String[] name = { THIS_CLASS.getName() };
405:
406: // junit.textui.TestRunner.main( name );
407: // junit.swingui.TestRunner.main( name );
408:
409: junit.textui.TestRunner.main(name);
410: }
411:
412: /**
413: *
414: * @exception Exception thrown under any exceptional condition.
415: */
416: protected void setUp() throws Exception {
417: super .setUp();
418:
419: // set ourself up
420: configureProject("instrument.xml");
421: }
422:
423: /**
424: *
425: * @exception Exception thrown under any exceptional condition.
426: */
427: protected void tearDown() throws Exception {
428: // tear ourself down
429: executeTarget("test-teardown");
430:
431: super.tearDown();
432: }
433: }
|