package org.junit.internal;

import java.io.PrintStream;
import java.text.NumberFormat;

import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.Ignorance;
import org.junit.runner.notification.RunListener;

public class TextListener extends RunListener {

	private final PrintStream fWriter;

	public TextListener(JUnitSystem system) {
		this(system.out());
	}

	public TextListener(PrintStream writer) {
		this.fWriter= writer;
	}

	@Override
	public void testRunFinished(Result result) {
		printHeader(result.getRunTime());
		printFailures(result);
		printIgnorances(result);
		printFooter(result);
	}

	@Override
	public void testStarted(Description description) {
		fWriter.append('.');
	}

	@Override
	public void testFailure(Failure failure) {
		fWriter.append('E');
	}
	
	@Override
	public void testIgnored(Description description) {
		fWriter.append('I');
	}
	
	/*
	 * Internal methods
	 */

	private PrintStream getWriter() {
		return fWriter;
	}

	protected void printHeader(long runTime) {
		getWriter().println();
		getWriter().println("Time: " + elapsedTimeAsString(runTime));
	}

	protected void printFailures(Result result) {
		if (result.getFailureCount() == 0)
			return;
		if (result.getFailureCount() == 1)
			getWriter().println("There was " + result.getFailureCount() + " failure:");
		else
			getWriter().println("There were " + result.getFailureCount() + " failures:");
		int i= 1;
		for (Failure each : result.getFailures())
			printFailure(each, i++);
	}
	
	private void printIgnorances(Result result) {
		// TODO: (Dec 7, 2007 10:23:17 AM) DUP

		if (result.getIgnoreCount() == 0)
			return;
		if (result.getIgnoreCount() == 1)
			getWriter().println("There was " + result.getIgnoreCount() + " ignored test:");
		else
			getWriter().println("There were " + result.getIgnoreCount() + " ignored tests:");
		int i= 1;
		for (Ignorance each : result.getIgnorances())
			printFailure(each, i++);
	}

	protected void printFailure(Failure failure, int count) {
		printFailureHeader(failure, count);
		printFailureTrace(failure);
	}

	protected void printFailureHeader(Failure failure, int count) {
		// TODO: (Dec 7, 2007 10:22:47 AM) indicate if this is an ignorance, somehow

		getWriter().println(count + ") " + failure.getTestHeader());
	}

	protected void printFailureTrace(Failure failure) {
		getWriter().print(failure.getTrace());
	}

	protected void printFooter(Result result) {
		if (result.wasSuccessful()) {
			getWriter().println();
			getWriter().print("OK");
			getWriter().println(" (" + result.getRunCount() + " test" + (result.getRunCount() == 1 ? "" : "s") + ")");

		} else {
			getWriter().println();
			getWriter().println("FAILURES!!!");
			getWriter().println("Tests run: " + result.getRunCount() + ",  Failures: " + result.getFailureCount());
		}
		getWriter().println();
	}

	/**
	 * Returns the formatted string of the elapsed time. Duplicated from
	 * BaseTestRunner. Fix it.
	 */
	protected String elapsedTimeAsString(long runTime) {
		return NumberFormat.getInstance().format((double) runTime / 1000);
	}
}

@@ -80,16 +80,39 @@
 rFormat;
+%0Aimport java.util.List;
 %0A%0Aimport
@@ -224,56 +224,8 @@
 re;%0A
-import org.junit.runner.notification.Ignorance;%0A
 impo
@@ -1211,346 +1211,193 @@
 %7B%0A%09%09
-if (result.getFailureCount() == 0)%0A%09%09%09return;%0A%09%09if (result.getFailureCount() == 1)%0A%09%09%09getWriter().println(%22There was %22 + result.getFailureCount() + %22 failure:%22);%0A%09%09else%0A%09%09%09getWriter().println(%22There were %22 + result.getFailureCount() + %22 failures:%22);%0A%09%09int i= 1;%0A%09%09for (Failure each : result.getFailures())%0A%09%09%09printFailure(each, i++
+printExceptions(%22failure%22, %22%22, result.getFailures());%0A%09%7D%0A%09%0A%09private void printIgnorances(Result result) %7B%0A%09%09printExceptions(%22ignored test%22, %22IGNORED TEST %22, result.getIgnorances()
 );%0A%09%7D%0A
-%09
 %0A%09pr
@@ -1416,105 +1416,126 @@
 rint
-Ignorances(Result result) %7B%0A%09%09// TODO: (Dec 7, 2007 10:23:17 AM) DUP%0A%0A%09%09if (result.getIgnoreCount
+Exceptions(String exceptionTypeName, String listPrefix,%0A%09%09%09List%3C? extends Failure%3E exceptions) %7B%0A%09%09if (exceptions.size
 () =
@@ -1556,37 +1556,31 @@
 ;%0A%09%09if (
-result.getIgnoreCount
+exceptions.size
 () == 1)
@@ -1622,48 +1622,55 @@
 %22 + 
-result.getIgnoreCount() + %22 ignored test
+exceptions.size() + %22 %22 + exceptionTypeName + %22
 :%22);
@@ -1720,48 +1720,55 @@
 %22 + 
-result.getIgnoreCount
+exceptions.size
 () + %22 
+%22 
-ignored test
++ exceptionTypeName + %22
 s:%22)
@@ -1788,24 +1788,22 @@
 %0A%09%09for (
-Ignoranc
+Failur
 e each :
@@ -1803,38 +1803,26 @@
  each : 
-result.getIgnorances()
+exceptions
 )%0A%09%09%09pri
@@ -1836,16 +1836,29 @@
 re(each,
+ listPrefix +
  i++);%0A%09
@@ -1910,237 +1910,24 @@
 re, 
-int count) %7B%0A%09%09printFailureHeader(failure, count);%0A%09%09printFailureTrace(failure);%0A%09%7D%0A%0A%09protected void printFailureHeader(Failure failure, int count) %7B%0A%09%09// TODO: (Dec 7, 2007 10:22:47 AM) indicate if this is an ignorance, somehow%0A
+String prefix) %7B
 %0A%09%09g
@@ -1949,13 +1949,14 @@
 tln(
-count
+prefix
  + %22
@@ -1991,65 +1991,8 @@
 ));%0A
-%09%7D%0A%0A%09protected void printFailureTrace(Failure failure) %7B%0A
 %09%09ge

Remove duplication between printing failures and ignorances