KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hanseltest > TestBug755797


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 TestBug755797 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 testBug() {
33         String JavaDoc failure = testToString(BugTest.suite());
34         assertTrue(failure.indexOf("Condition '!(obj1 instanceof java.lang.String)' is not fulfilled.") > 0);
35     }
36
37     public static class BugTest extends TestCase {
38         public static Test suite() {
39             return new CoverageDecorator(BugTest.class,
40                                          new Class JavaDoc[] {BugExample.class});
41         }
42         
43         /** The test will fail, if no tests are provided. */
44         public void testSomething() {
45             assertEquals(1, new BugExample().instanceOf("test"));
46         }
47     }
48
49     /** Example class to be covered. */
50     public static class BugExample {
51         public int instanceOf(Object JavaDoc obj1) {
52             if (obj1 instanceof java.lang.String JavaDoc) {
53                 return 1;
54             } else {
55                 return 0;
56             }
57         }
58     }
59 }
60
Popular Tags