KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hanseltest > TestBug758304


1 package org.hanseltest;
2
3 import java.io.PrintWriter JavaDoc;
4 import java.io.StringWriter JavaDoc;
5
6 import junit.framework.Test;
7 import junit.framework.TestCase;
8 import junit.framework.TestFailure;
9 import junit.framework.TestResult;
10
11 import org.hansel.CoverageDecorator;
12
13 public class TestBug758304 extends TestCase {
14
15     private String JavaDoc testToString(Test test) {
16         TestResult result = new TestResult();
17         test.run(result);
18
19         assertEquals(1, result.failureCount());
20
21         TestFailure failure = (TestFailure) result.failures().nextElement();
22
23         StringWriter JavaDoc sw = new StringWriter JavaDoc();
24         failure.thrownException().printStackTrace(new PrintWriter JavaDoc(sw));
25
26         return sw.toString();
27     }
28
29     /**
30      * Try to reproduce the bug.
31      */

32     public void testBug1() {
33         String JavaDoc failure = testToString(BugTest1.suite());
34         assertTrue(failure.indexOf("Condition 'a >= b' is not fulfilled.") > 0);
35     }
36
37    /**
38      * Try to reproduce the bug.
39      */

40     public void testBug2() {
41         String JavaDoc failure = testToString(BugTest2.suite());
42         assertTrue(failure.indexOf("Condition 'a < b' is not fulfilled.") > 0);
43     }
44
45     public static class BugTest1 extends TestCase {
46         public static Test suite() {
47             return new CoverageDecorator(BugTest1.class,
48                                          new Class JavaDoc[] {BugExample.class});
49         }
50         
51         /** The test will fail, if no tests are provided. */
52         public void testSomething() {
53             assertEquals(1, new BugExample().cmp(1, 2));
54         }
55     }
56
57     public static class BugTest2 extends TestCase {
58         public static Test suite() {
59             return new CoverageDecorator(BugTest2.class,
60                                          new Class JavaDoc[] {BugExample.class});
61         }
62         
63         /** The test will fail, if no tests are provided. */
64         public void testSomething() {
65             assertEquals(0, new BugExample().cmp(2, 1));
66         }
67     }
68
69     /** Example class to be covered. */
70     public static class BugExample {
71         public int cmp(int a, int b) {
72             if (a < b) {
73                 return 1;
74             } else {
75                 return 0;
76             }
77         }
78     }
79 }
80
Popular Tags