forked from junit-team/junit4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kbeck
committed
Aug 19, 2008
1 parent
c4e7dfe
commit 68f1e62
Showing
1 changed file
with
113 additions
and
0 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
src/test/java/org/junit/tests/experimental/logging/LoggingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package org.junit.tests.experimental.logging; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
import org.junit.Test; | ||
import org.junit.runner.Description; | ||
import org.junit.runner.JUnitCore; | ||
import org.junit.runner.Result; | ||
import org.junit.runner.notification.Failure; | ||
import org.junit.runner.notification.RunListener; | ||
|
||
//TODO log success/fail | ||
//TODO log class start/finish | ||
//TODO log timing of single tests | ||
|
||
public class LoggingTest { | ||
public class TestLog { | ||
|
||
private int runCount= 0; | ||
private int failureCount= 0; | ||
|
||
public int getRunCount() { | ||
return runCount; | ||
} | ||
|
||
public void setRunCount(int count) { | ||
runCount= count; | ||
} | ||
|
||
public int getFailureCount() { | ||
return failureCount; | ||
} | ||
|
||
public void setFailureCount(int count) { | ||
this.failureCount= count; | ||
} | ||
|
||
} | ||
|
||
public class LoggingListener extends RunListener { | ||
private StringBuffer writer= new StringBuffer(); | ||
@Override | ||
public void testStarted(Description description) throws Exception { | ||
writer.append("t"); | ||
} | ||
|
||
@Override | ||
public void testFailure(Failure failure) throws Exception { | ||
writer.append("f"); | ||
} | ||
|
||
public String getLogContents() { | ||
return writer.toString(); | ||
} | ||
|
||
} | ||
|
||
public static class Example { | ||
@Test public void uno() {} | ||
@Test public void due() {} | ||
} | ||
|
||
@Test public void logSuccess() { | ||
JUnitCore core= new JUnitCore(); | ||
LoggingListener listener=addLogging(core); | ||
core.run(Example.class); | ||
String log= listener.getLogContents(); | ||
TestLog result = parse(log); | ||
assertEquals(2, result.getRunCount()); | ||
assertEquals(0, result.getFailureCount()); | ||
// some assertions | ||
} | ||
|
||
public static class FailingExample { | ||
@Test public void uno() {} | ||
@Test public void due() { | ||
fail(); | ||
} | ||
} | ||
|
||
@Test public void logFailure() { | ||
JUnitCore core= new JUnitCore(); | ||
LoggingListener listener=addLogging(core); | ||
core.run(FailingExample.class); | ||
String log= listener.getLogContents(); | ||
TestLog result = parse(log); | ||
assertEquals(2, result.getRunCount()); | ||
assertEquals(1, result.getFailureCount()); | ||
// some assertions | ||
} | ||
|
||
private TestLog parse(String log) { | ||
TestLog result= new TestLog(); | ||
result.setRunCount(count(log, 't')); | ||
result.setFailureCount(count(log, 'f')); | ||
return result; | ||
} | ||
|
||
private int count(String log, char pattern) { | ||
int count= 0; | ||
for (int i = 0; i < log.length(); i++) | ||
if (log.charAt(i) == pattern) | ||
count++; | ||
return count; | ||
} | ||
|
||
private LoggingListener addLogging(JUnitCore core) { | ||
LoggingListener listener= new LoggingListener(); | ||
core.addListener(listener); | ||
return listener; | ||
} | ||
} |