package de.ugoe.cs.util.console;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.logging.Level;

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 PrintStream sysOut = null;
	private PrintStream sysErr = null;

	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();
		String traceMessage = "test";
		Level traceLevel = Level.WARNING;
		String expectedMessage = "[WARNING] test";

		fixture.traceMsg(traceMessage, traceLevel);

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

	@Test
	public void testTraceMsg_2() throws Exception {
		TextConsole fixture = new TextConsole(Level.INFO);
		String traceMessage = "test";
		Level traceLevel = Level.INFO;
		String expectedMessage = "[INFO] test";

		fixture.traceMsg(traceMessage, traceLevel);

		assertEquals(expectedMessage, outContent.toString());
	}
	
	       @Test
	        public void testTraceMsg_3() throws Exception {
	                TextConsole fixture = new TextConsole(Level.WARNING);
	                String traceMessage = "[INFO] test";
	                Level traceLevel = Level.INFO;
	                

	                fixture.traceMsg(traceMessage, traceLevel);

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

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

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

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