01: package org.netbeans.junit;
02:
03: import java.util.logging.Level;
04: import java.util.logging.Logger;
05: import junit.framework.TestFailure;
06: import junit.framework.TestResult;
07:
08: /** Provides an example of randomized test.
09: */
10: public class TimeOutHasToPrintLogTest extends NbTestCase {
11: private Logger LOG = Logger.getLogger("my.log.for.test");
12:
13: public TimeOutHasToPrintLogTest(String testName) {
14: super (testName);
15: }
16:
17: private boolean isSpecial() {
18: return getName().equals("printAhojAndTimeOut")
19: || getName().equals("justTimeOutInOneOfMyMethods");
20: }
21:
22: protected Level logLevel() {
23: if (isSpecial()) {
24: return null;
25: } else {
26: return Level.FINE;
27: }
28: }
29:
30: protected int timeOut() {
31: if (isSpecial()) {
32: return 700;
33: } else {
34: return 0;
35: }
36: }
37:
38: public void printAhojAndTimeOut() throws Exception {
39: LOG.fine("Ahoj");
40: Thread.sleep(10000);
41: }
42:
43: public void justTimeOutInOneOfMyMethods() throws Exception {
44: Thread.sleep(10000);
45: }
46:
47: public void testThatTheTimeOutStillPrintsTheWarning()
48: throws Exception {
49: TimeOutHasToPrintLogTest t = new TimeOutHasToPrintLogTest(
50: "printAhojAndTimeOut");
51:
52: CharSequence seq = Log.enable(LOG.getName(), Level.FINE);
53:
54: TestResult res = t.run();
55:
56: assertEquals("One test has been run", 1, res.runCount());
57:
58: String s = seq.toString();
59:
60: if (s.indexOf("Ahoj") == -1) {
61: fail("Ahoj has to be logged:\n" + s);
62: }
63:
64: assertEquals("No error", 0, res.errorCount());
65: assertEquals("One failure", 1, res.failureCount());
66:
67: TestFailure f = (TestFailure) res.failures().nextElement();
68: s = f.exceptionMessage();
69: if (s.indexOf("Ahoj") == -1) {
70: fail("Ahoj has to be part of the message:\n" + s);
71: }
72: }
73:
74: public void testThreadDumpPrinted() throws Exception {
75: TimeOutHasToPrintLogTest t = new TimeOutHasToPrintLogTest(
76: "justTimeOutInOneOfMyMethods");
77:
78: TestResult res = t.run();
79:
80: assertEquals("One test has been run", 1, res.runCount());
81: TestFailure failure = (TestFailure) res.failures()
82: .nextElement();
83: String s = failure.exceptionMessage();
84:
85: if (s.indexOf("justTimeOutInOneOfMyMethods") == -1) {
86: fail("There should be thread dump reported in case of timeout:\n"
87: + s);
88: }
89:
90: assertEquals("No error", 0, res.errorCount());
91: assertEquals("One failure", 1, res.failureCount());
92: }
93: }
|