1b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotpackage org.junit.experimental.results; 2b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 3b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport java.io.ByteArrayOutputStream; 4b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport java.io.PrintStream; 5b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport java.util.List; 6b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 7b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.junit.internal.TextListener; 8b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.junit.runner.JUnitCore; 9b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.junit.runner.Request; 10b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.junit.runner.Result; 11b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.junit.runner.notification.Failure; 12b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 13b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot/** 14b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * A test result that prints nicely in error messages. 15b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * This is only intended to be used in JUnit self-tests. 16b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * For example: 17b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * 18b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * <pre> 19b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * assertThat(testResult(HasExpectedException.class), isSuccessful()); 20b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * </pre> 21b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot */ 22b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotpublic class PrintableResult { 23b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot /** 24b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * The result of running JUnit on {@code type} 25b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot */ 26b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot public static PrintableResult testResult(Class<?> type) { 27b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot return testResult(Request.aClass(type)); 28b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot } 29b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 30b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot /** 31b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * The result of running JUnit on Request {@code request} 32b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot */ 33b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot public static PrintableResult testResult(Request request) { 34b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot return new PrintableResult(new JUnitCore().run(request)); 35b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot } 36b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 37b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot private Result result; 38b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 39b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot /** 40b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * A result that includes the given {@code failures} 41b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot */ 42b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot public PrintableResult(List<Failure> failures) { 43b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot this(new FailureList(failures).result()); 44b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot } 45b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 46b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot private PrintableResult(Result result) { 47b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot this.result = result; 48b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot } 49b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 50b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot @Override 51b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot public String toString() { 52b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot ByteArrayOutputStream stream = new ByteArrayOutputStream(); 53b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot new TextListener(new PrintStream(stream)).testRunFinished(result); 54b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot return stream.toString(); 55b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot } 56b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 57b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot /** 58b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * Returns the number of failures in this result. 59b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot */ 60b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot public int failureCount() { 61b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot return result.getFailures().size(); 62b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot } 63b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot}