001: /*
002: * @(#)AssertTestFactory.java
003: *
004: * Copyright (C) 2002-2003 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.junit.v1;
028:
029: import org.apache.log4j.Logger;
030: import junit.framework.TestCase;
031: import junit.framework.TestResult;
032: import junit.framework.AssertionFailedError;
033: import junit.framework.Assert;
034:
035: /**
036: * A factory that creates test instances for the standard set of assert
037: * methods. The created test instances should have their <tt>setName()</tt>
038: * method invoked to properly set the name of the test. Alternatively, the
039: * factory instance can have the name set so that all tests will have the
040: * same name.
041: * <P>
042: * To support JUnit 3.8 functionality, but remain backwards compatible with
043: * earlier JUnit libraries, the names for the JUnit 3.8 methods will be
044: * allowed, but they will call JUnit 3.7 compatible methods.
045: * <P>
046: * As of Dec 8, 2002, the factory can uniquely (per instance) name each
047: * generated test via an index. This can help traceability in identifying
048: * each created test. Alternatively, the user can set the factory's name
049: * before invoking a create method.
050: *
051: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
052: * @version $Date: 2004/01/09 20:32:26 $
053: * @since July 26, 2002
054: */
055: public class AssertTestFactory {
056: /**
057: * Inner test instance which specializes in generating a message with
058: * the test's instance's specific name.
059: */
060: public static abstract class InnerTest extends TestCase {
061: private String message;
062:
063: /**
064: * JUnit 3.8 allows for test constructors to not have to specify
065: * a name in the super() call, but for JUnit 3.7 compatibility,
066: * we will only support the original usage.
067: */
068: public InnerTest(String name, String msg) {
069: super (name);
070: this .message = msg;
071: }
072:
073: public void setMessage(String msg) {
074: this .message = msg;
075: }
076:
077: public String getMessage() {
078: return this .message;
079: }
080:
081: public String getFullMessage() {
082: String msg = getMessage();
083: String name = this .getName();
084: if (name != null) {
085: msg = name + ": " + msg;
086: }
087: return msg;
088: }
089:
090: protected final void runTest() {
091: callAssert(getFullMessage());
092: }
093:
094: public abstract void callAssert(String message);
095: }
096:
097: private String name;
098: private int index = 0;
099: private boolean useIndex = false;
100:
101: /**
102: * Creates a new factory that can generate assertions as independent
103: * test objects.
104: */
105: public AssertTestFactory() {
106: // do nothing
107: }
108:
109: /**
110: * Creates a new factory with a specific name for each generated test,
111: * but will not add an index to each generated test's name.
112: *
113: * @param name default name shared by all generated assertion tests.
114: */
115: public AssertTestFactory(String name) {
116: this (name, false);
117: }
118:
119: /**
120: * Creates a new factory with a specific name for each generated test,
121: * and can optionally add an index to each generated test's name.
122: *
123: * @param name default name shared by all generated assertion tests.
124: * @param useIndexWithName <tt>true</tt> if indecies will be appended
125: * to each generated test's name, or <tt>false</tt> if they
126: * will use the passed-in name exactly.
127: * @since 08-Dec-2002
128: */
129: public AssertTestFactory(String name, boolean useIndexWithName) {
130: setName(name);
131: setUseIndexWithName(useIndexWithName);
132: }
133:
134: /**
135: * Sets the default test name. This will not reset the generated index.
136: *
137: * @param name default name shared by all generated assertion tests.
138: */
139: public void setName(String name) {
140: this .name = name;
141: }
142:
143: /**
144: * Returns the default test name. If the name has never been set, then
145: * this will return <tt>null</tt>.
146: *
147: * @return default name shared by all generated assertion tests.
148: */
149: public String getName() {
150: return this .name;
151: }
152:
153: /**
154: * Sets whether each generated test will add a unique (for this instance)
155: * index to the test's name. Reseting this value will not affect the
156: * index's value.
157: *
158: * @param useIndexWithName <tt>true</tt> if indecies will be appended
159: * to each generated test's name, or <tt>false</tt> if they
160: * will use the passed-in name exactly.
161: * @since 08-Dec-2002
162: */
163: public void setUseIndexWithName(boolean useIndexWithName) {
164: this .useIndex = useIndexWithName;
165: }
166:
167: /**
168: * Returns whether each generated test will add a unique (for this
169: * instance) index to the test's name.
170: *
171: * @return <tt>true</tt> if an index is appended to the name, or
172: * <tt>false</tt> if the test's name is exactly the factory's name.
173: * @since 08-Dec-2002
174: */
175: public boolean getUseIndexWithName() {
176: return this .useIndex;
177: }
178:
179: //-----------------------------------------------------------------------
180:
181: private static class AssertTrue1 extends InnerTest {
182: boolean condition;
183:
184: public AssertTrue1(String n, String m, boolean c) {
185: super (n, m);
186: this .condition = c;
187: }
188:
189: public void callAssert(String msg) {
190: Assert.assertTrue(msg, this .condition);
191: }
192: }
193:
194: /**
195: * Asserts that a condition is true. If it isn't it throws an
196: * AssertionFailedError with the given message.
197: *
198: * @param message message that describes what failed if the assertion
199: * fails.
200: * @param condition boolean to check for failure
201: */
202: public InnerTest createAssertTrue(String message, boolean condition) {
203: return new AssertTrue1(getNextTestName(), message, condition);
204: }
205:
206: /**
207: * Asserts that a condition is true. If it isn't it throws an
208: * AssertionFailedError.
209: *
210: * @param condition boolean to check for failure
211: */
212: public InnerTest createAssertTrue(boolean condition) {
213: return createAssertTrue(null, condition);
214: }
215:
216: /**
217: * Asserts that a condition is false. If it isn't it throws an
218: * AssertionFailedError with the given message.
219: *
220: * @since 30-Oct-2002
221: * @param message message that describes what failed if the assertion
222: * fails.
223: * @param condition boolean to check for failure
224: */
225: public InnerTest createAssertFalse(String message, boolean condition) {
226: return new AssertTrue1(getNextTestName(), message, !condition);
227: }
228:
229: /**
230: * Asserts that a condition is true. If it isn't it throws an
231: * AssertionFailedError.
232: *
233: * @since 30-Oct-2002
234: * @param condition boolean to check for failure
235: */
236: public InnerTest createAssertFalse(boolean condition) {
237: // don't 'not' the condition here - it will be done in the
238: // invoked method.
239: return createAssertFalse(null, condition);
240: }
241:
242: //-----------------------------------------------------------------------
243:
244: private static class Fail1 extends InnerTest {
245: public Fail1(String n, String m) {
246: super (n, m);
247: }
248:
249: public void callAssert(String msg) {
250: Assert.fail(msg);
251: }
252: }
253:
254: /**
255: * Fails a test with the given message.
256: *
257: * @param message message that describes what failed if the assertion
258: * fails.
259: */
260: public InnerTest createFail(String message) {
261: return new Fail1(getNextTestName(), message);
262: }
263:
264: /**
265: * Fails a test with no message.
266: */
267: public InnerTest createFail() {
268: return createFail(null);
269: }
270:
271: //-----------------------------------------------------------------------
272:
273: private static class AssertEquals1 extends InnerTest {
274: Object expected;
275: Object actual;
276:
277: public AssertEquals1(String n, String m, Object e, Object a) {
278: super (n, m);
279: this .expected = e;
280: this .actual = a;
281: }
282:
283: public void callAssert(String msg) {
284: Assert.assertEquals(msg, this .expected, this .actual);
285: }
286: }
287:
288: /**
289: * Asserts that two objects are equal. If they are not an
290: * AssertionFailedError is thrown.
291: *
292: * @param message message that describes what failed if the assertion
293: * fails.
294: * @param expected value that the test expects to find from the tested
295: * code.
296: * @param actual actual value generated by tested code.
297: */
298: public InnerTest createAssertEquals(String message,
299: Object expected, Object actual) {
300: return new AssertEquals1(getNextTestName(), message, expected,
301: actual);
302: }
303:
304: /**
305: * Asserts that two objects are equal. If they are not an
306: * AssertionFailedError is thrown.
307: *
308: * @param expected value that the test expects to find from the tested
309: * code.
310: * @param actual actual value generated by tested code.
311: */
312: public InnerTest createAssertEquals(Object expected, Object actual) {
313: return createAssertEquals(null, expected, actual);
314: }
315:
316: /**
317: * Asserts that two objects are equal. If they are not an
318: * AssertionFailedError is thrown.
319: *
320: * @since 30-Oct-2002
321: * @param message message that describes what failed if the assertion
322: * fails.
323: * @param expected value that the test expects to find from the tested
324: * code.
325: * @param actual actual value generated by tested code.
326: */
327: public InnerTest createAssertEquals(String message,
328: String expected, String actual) {
329: return new AssertEquals1(getNextTestName(), message, expected,
330: actual);
331: }
332:
333: /**
334: * Asserts that two objects are equal. If they are not an
335: * AssertionFailedError is thrown.
336: *
337: * @since 30-Oct-2002
338: * @param expected value that the test expects to find from the tested
339: * code.
340: * @param actual actual value generated by tested code.
341: */
342: public InnerTest createAssertEquals(String expected, String actual) {
343: return createAssertEquals(null, expected, actual);
344: }
345:
346: //-----------------------------------------------------------------------
347:
348: private static class AssertEquals2 extends InnerTest {
349: double expected;
350: double actual;
351: double delta;
352:
353: public AssertEquals2(String n, String m, double e, double a,
354: double d) {
355: super (n, m);
356: this .expected = e;
357: this .actual = a;
358: this .delta = d;
359: }
360:
361: public void callAssert(String msg) {
362: Assert.assertEquals(msg, this .expected, this .actual,
363: this .delta);
364: }
365: }
366:
367: /**
368: * Asserts that two doubles are equal concerning a delta. If the expected
369: * value is infinity then the delta value is ignored.
370: *
371: * @param message message that describes what failed if the assertion
372: * fails.
373: * @param expected value that the test expects to find from the tested
374: * code.
375: * @param actual actual value generated by tested code.
376: * @param delta maximum distance between expected and actual such that
377: * the two values are considered equivalent. Necessary since
378: * floating-point numbers on computers are approximations of their
379: * equivalent values; that is, storing <tt>1.1</tt> may actually be
380: * stored as <tt>1.099999999999</tt>.
381: */
382: public InnerTest createAssertEquals(String message,
383: double expected, double actual, double delta) {
384: return new AssertEquals2(getNextTestName(), message, expected,
385: actual, delta);
386: }
387:
388: /**
389: * Asserts that two doubles are equal concerning a delta. If the expected
390: * value is infinity then the delta value is ignored.
391: *
392: * @param expected value that the test expects to find from the tested
393: * code.
394: * @param actual actual value generated by tested code.
395: * @param delta maximum distance between expected and actual such that
396: * the two values are considered equivalent. Necessary since
397: * floating-point numbers on computers are approximations of their
398: * equivalent values; that is, storing <tt>1.1</tt> may actually be
399: * stored as <tt>1.099999999999</tt>.
400: */
401: public InnerTest createAssertEquals(double expected, double actual,
402: double delta) {
403: return createAssertEquals(null, expected, actual, delta);
404: }
405:
406: //-----------------------------------------------------------------------
407:
408: private static class AssertEquals3 extends InnerTest {
409: float expected;
410: float actual;
411: float delta;
412:
413: public AssertEquals3(String n, String m, float e, float a,
414: float d) {
415: super (n, m);
416: this .expected = e;
417: this .actual = a;
418: this .delta = d;
419: }
420:
421: public void callAssert(String msg) {
422: Assert.assertEquals(msg, this .expected, this .actual,
423: this .delta);
424: }
425: }
426:
427: /**
428: * Asserts that two floats are equal concerning a delta. If the expected
429: * value is infinity then the delta value is ignored.
430: *
431: * @param message message that describes what failed if the assertion
432: * fails.
433: * @param expected value that the test expects to find from the tested
434: * code.
435: * @param actual actual value generated by tested code.
436: * @param delta maximum distance between expected and actual such that
437: * the two values are considered equivalent. Necessary since
438: * floating-point numbers on computers are approximations of their
439: * equivalent values; that is, storing <tt>1.1</tt> may actually be
440: * stored as <tt>1.099999999999</tt>.
441: */
442: public InnerTest createAssertEquals(String message, float expected,
443: float actual, float delta) {
444: return new AssertEquals3(getNextTestName(), message, expected,
445: actual, delta);
446: }
447:
448: /**
449: * Asserts that two floats are equal concerning a delta. If the expected
450: * value is infinity then the delta value is ignored.
451: *
452: * @param expected value that the test expects to find from the tested
453: * code.
454: * @param actual actual value generated by tested code.
455: * @param delta maximum distance between expected and actual such that
456: * the two values are considered equivalent. Necessary since
457: * floating-point numbers on computers are approximations of their
458: * equivalent values; that is, storing <tt>1.1</tt> may actually be
459: * stored as <tt>1.099999999999</tt>.
460: */
461: public InnerTest createAssertEquals(float expected, float actual,
462: float delta) {
463: return createAssertEquals(null, expected, actual, delta);
464: }
465:
466: /**
467: * Asserts that two longs are equal.
468: *
469: * @param message message that describes what failed if the assertion
470: * fails.
471: * @param expected value that the test expects to find from the tested
472: * code.
473: * @param actual actual value generated by tested code.
474: */
475: public InnerTest createAssertEquals(String message, long expected,
476: long actual) {
477: return createAssertEquals(message, new Long(expected),
478: new Long(actual));
479: }
480:
481: /**
482: * Asserts that two longs are equal.
483: *
484: * @param expected value that the test expects to find from the tested
485: * code.
486: * @param actual actual value generated by tested code.
487: */
488: public InnerTest createAssertEquals(long expected, long actual) {
489: return createAssertEquals(null, expected, actual);
490: }
491:
492: /**
493: * Asserts that two booleans are equal.
494: *
495: * @param message message that describes what failed if the assertion
496: * fails.
497: * @param expected value that the test expects to find from the tested
498: * code.
499: * @param actual actual value generated by tested code.
500: */
501: public InnerTest createAssertEquals(String message,
502: boolean expected, boolean actual) {
503: return createAssertEquals(message, new Boolean(expected),
504: new Boolean(actual));
505: }
506:
507: /**
508: * Asserts that two booleans are equal.
509: *
510: * @param expected value that the test expects to find from the tested
511: * code.
512: * @param actual actual value generated by tested code.
513: */
514: public InnerTest createAssertEquals(boolean expected, boolean actual) {
515: return createAssertEquals(null, expected, actual);
516: }
517:
518: /**
519: * Asserts that two bytes are equal.
520: *
521: * @param message message that describes what failed if the assertion
522: * fails.
523: * @param expected value that the test expects to find from the tested
524: * code.
525: * @param actual actual value generated by tested code.
526: */
527: public InnerTest createAssertEquals(String message, byte expected,
528: byte actual) {
529: return createAssertEquals(message, new Byte(expected),
530: new Byte(actual));
531: }
532:
533: /**
534: * Asserts that two bytes are equal.
535: *
536: * @param expected value that the test expects to find from the tested
537: * code.
538: * @param actual actual value generated by tested code.
539: */
540: public InnerTest createAssertEquals(byte expected, byte actual) {
541: return createAssertEquals(null, expected, actual);
542: }
543:
544: /**
545: * Asserts that two chars are equal.
546: *
547: * @param message message that describes what failed if the assertion
548: * fails.
549: * @param expected value that the test expects to find from the tested
550: * code.
551: * @param actual actual value generated by tested code.
552: */
553: public InnerTest createAssertEquals(String message, char expected,
554: char actual) {
555: return createAssertEquals(message, new Character(expected),
556: new Character(actual));
557: }
558:
559: /**
560: * Asserts that two chars are equal.
561: *
562: * @param expected value that the test expects to find from the tested
563: * code.
564: * @param actual actual value generated by tested code.
565: */
566: public InnerTest createAssertEquals(char expected, char actual) {
567: return createAssertEquals(null, expected, actual);
568: }
569:
570: /**
571: * Asserts that two shorts are equal.
572: *
573: * @param message message that describes what failed if the assertion
574: * fails.
575: * @param expected value that the test expects to find from the tested
576: * code.
577: * @param actual actual value generated by tested code.
578: */
579: public InnerTest createAssertEquals(String message, short expected,
580: short actual) {
581: return createAssertEquals(message, new Short(expected),
582: new Short(actual));
583: }
584:
585: /**
586: * Asserts that two shorts are equal.
587: *
588: * @param expected value that the test expects to find from the tested
589: * code.
590: * @param actual actual value generated by tested code.
591: */
592: public InnerTest createAssertEquals(short expected, short actual) {
593: return createAssertEquals(null, expected, actual);
594: }
595:
596: /**
597: * Asserts that two ints are equal.
598: *
599: * @param message message that describes what failed if the assertion
600: * fails.
601: * @param expected value that the test expects to find from the tested
602: * code.
603: * @param actual actual value generated by tested code.
604: */
605: public InnerTest createAssertEquals(String message, int expected,
606: int actual) {
607: return createAssertEquals(message, new Integer(expected),
608: new Integer(actual));
609: }
610:
611: /**
612: * Asserts that two ints are equal.
613: *
614: * @param expected value that the test expects to find from the tested
615: * code.
616: * @param actual actual value generated by tested code.
617: */
618: public InnerTest createAssertEquals(int expected, int actual) {
619: return createAssertEquals(null, expected, actual);
620: }
621:
622: //-----------------------------------------------------------------------
623:
624: private static class AssertNotNull1 extends InnerTest {
625: Object object;
626:
627: public AssertNotNull1(String n, String m, Object o) {
628: super (n, m);
629: this .object = o;
630: }
631:
632: public void callAssert(String msg) {
633: Assert.assertNotNull(msg, this .object);
634: }
635: }
636:
637: /**
638: * Asserts that an object isn't null.
639: *
640: * @param message message that describes what failed if the assertion
641: * fails.
642: * @param object test object that must not be null.
643: */
644: public InnerTest createAssertNotNull(String message, Object object) {
645: return new AssertNotNull1(getNextTestName(), message, object);
646: }
647:
648: /**
649: * Asserts that an object isn't null.
650: *
651: * @param object test object that must not be null.
652: */
653: public InnerTest createAssertNotNull(Object object) {
654: return createAssertNotNull(null, object);
655: }
656:
657: //-----------------------------------------------------------------------
658:
659: private static class AssertNull1 extends InnerTest {
660: Object object;
661:
662: public AssertNull1(String n, String m, Object o) {
663: super (n, m);
664: this .object = o;
665: }
666:
667: public void callAssert(String msg) {
668: Assert.assertNull(msg, this .object);
669: }
670: }
671:
672: /**
673: * Asserts that an object is null.
674: *
675: * @param message message that describes what failed if the assertion
676: * fails.
677: * @param object test object that must be null.
678: */
679: public InnerTest createAssertNull(String message, Object object) {
680: return new AssertNull1(getNextTestName(), message, object);
681: }
682:
683: /**
684: * Asserts that an object is null.
685: *
686: * @param object test object that must be null.
687: */
688: public InnerTest createAssertNull(Object object) {
689: return createAssertNull(null, object);
690: }
691:
692: //-----------------------------------------------------------------------
693:
694: private static class AssertSame1 extends InnerTest {
695: Object expected;
696: Object actual;
697:
698: public AssertSame1(String n, String m, Object e, Object a) {
699: super (n, m);
700: this .expected = e;
701: this .actual = a;
702: }
703:
704: public void callAssert(String msg) {
705: Assert.assertSame(msg, this .expected, this .actual);
706: }
707: }
708:
709: /**
710: * Asserts that two objects refer to the same object. If they are not an
711: * AssertionFailedError is thrown.
712: *
713: * @param message message that describes what failed if the assertion
714: * fails.
715: * @param expected value that the test expects to find from the tested
716: * code.
717: * @param actual actual value generated by tested code.
718: */
719: public InnerTest createAssertSame(String message, Object expected,
720: Object actual) {
721: return new AssertSame1(getNextTestName(), message, expected,
722: actual);
723: }
724:
725: /**
726: * Asserts that two objects refer to the same object. If they are not the
727: * same an AssertionFailedError is thrown.
728: *
729: * @param expected value that the test expects to find from the tested
730: * code.
731: * @param actual actual value generated by tested code.
732: */
733: public InnerTest createAssertSame(Object expected, Object actual) {
734: return createAssertSame(null, expected, actual);
735: }
736:
737: /**
738: * Asserts that two objects refer to the same object. If they are not an
739: * AssertionFailedError is thrown.
740: *
741: * @since 30-Oct-2002
742: * @param message message that describes what failed if the assertion
743: * fails.
744: * @param expected value that the test expects to not find from the tested
745: * code.
746: * @param actual actual value generated by tested code.
747: */
748: public InnerTest createAssertNotSame(String message,
749: Object expected, Object actual) {
750: String str = "expected not same";
751: if (message != null) {
752: str = message + ' ' + str;
753: }
754: // manual emulation of JUnit 3.8 functionality
755: return new AssertTrue1(getNextTestName(), str,
756: expected != actual);
757: }
758:
759: /**
760: * Asserts that two objects refer to the same object. If they are not the
761: * same an AssertionFailedError is thrown.
762: *
763: * @since 30-Oct-2002
764: * @param expected value that the test expects to not find from the tested
765: * code.
766: * @param actual actual value generated by tested code.
767: */
768: public InnerTest createAssertNotSame(Object expected, Object actual) {
769: return createAssertNotSame(null, expected, actual);
770: }
771:
772: /**
773: * Generates the next test's name.
774: *
775: * @return the next name for a generated test, possibly appending an
776: * index value.
777: * @since 08-Dec-2002
778: */
779: private String getNextTestName() {
780: String n = getName();
781: if (this.useIndex) {
782: synchronized (this) {
783: ++this.index;
784: n += this.index;
785: }
786: }
787: return n;
788: }
789: }
|