package de.ugoe.cs.util.console;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import org.junit.*;
import static org.junit.Assert.*;

/**
 * The class <code>TextConsoleTest</code> contains tests for the class
 * <code>{@link TextConsole}</code>.
 * 
 * @author Steffen Herbold
 * @version 1.0
 */
public class TextConsoleTest {

	private final static String ENDLINE = System.getProperty("line.separator");

	private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
	private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();

	@Test
	public void testTextConsole_1() throws Exception {

		Console.reset();
		TextConsole result = new TextConsole();

		assertNotNull(result);
		assertTrue(Console.getInstance().hasErrorListener(result));
		assertTrue(Console.getInstance().hasExceptionListener(result));
		assertTrue(Console.getInstance().hasOutputListener(result));
		assertTrue(Console.getInstance().hasTraceListener(result));
	}

	@Test
	public void testErrorMsg_1() throws Exception {
		TextConsole fixture = new TextConsole();
		String errMessage = "test";

		fixture.errorMsg(errMessage);

		assertEquals(errMessage, errContent.toString());
	}

	@Test
	public void testLogException_1() throws Exception {
		TextConsole fixture = new TextConsole();
		Exception e = new Exception("test");
		;

		fixture.logException(e);
		assertEquals(e.getMessage() + ENDLINE, errContent.toString());
	}

	@Test
	public void testOutputMsg_1() throws Exception {
		TextConsole fixture = new TextConsole();
		String newMessage = "test";

		fixture.outputMsg(newMessage);

		assertEquals(newMessage, outContent.toString());
	}

	@Test
	public void testTraceMsg_1() throws Exception {
		TextConsole fixture = new TextConsole();
		fixture.setDebug(true);
		String traceMessage = "test";

		fixture.traceMsg(traceMessage);

		assertEquals(traceMessage, outContent.toString());
	}

	@Test
	public void testTraceMsg_2() throws Exception {
		TextConsole fixture = new TextConsole();
		fixture.setDebug(false);
		String traceMessage = "test";

		fixture.traceMsg(traceMessage);

		assertEquals("", outContent.toString());
	}

	@Before
	public void setUp() throws Exception {
		System.setOut(new PrintStream(outContent));
		System.setErr(new PrintStream(errContent));
	}

	@After
	public void tearDown() throws Exception {
		System.setOut(null);
		System.setErr(null);
	}

	public static void main(String[] args) {
		new org.junit.runner.JUnitCore().run(TextConsoleTest.class);
	}
}