/*******************************************************************************
 * Copyright (c) 2016 Microsoft Research. All rights reserved. 
 *
 * The MIT License (MIT)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software. 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Contributors:
 *   Markus Alexander Kuppe - initial API and implementation
 ******************************************************************************/

package tlc2.tool.suite;
import static org.junit.Assert.fail;

import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

import tlc2.output.EC.ExitStatus;
import tlc2.tool.liveness.ModelCheckerTestCase;
import util.ToolIO;

public abstract class SuiteETestCase extends ModelCheckerTestCase {

	private TestPrintStream testPrintStream = new TestPrintStream();

	public SuiteETestCase() {
		this(ExitStatus.SUCCESS);
	}

	public SuiteETestCase(int exitStatus) {
		super("setBySetUp", "suite", exitStatus);
	}
	
	public SuiteETestCase(String[] params) {
		this(params, ExitStatus.SUCCESS);
	}

	public SuiteETestCase(String[] params, int exitStatus) {
		super("setBySetUp", "suite", params, exitStatus);
	}

	/* (non-Javadoc)
	 * @see tlc2.tool.liveness.ModelCheckerTestCase#setUp()
	 */
	public void setUp() {
		// Set spec name to the name of the unit tests
		spec = getClass().getSimpleName().toLowerCase();
		
		// Intercept tool out to check SANY parser errors
		ToolIO.out = testPrintStream;
		ToolIO.err = testPrintStream;
		
		super.setUp();
	}
	
	
	protected void assertSubstring(String substring) {
		for (String string : testPrintStream.strings) {
			if (string.contains(substring)) {
				return;
			}
		}
		fail("Substring not found");
	}

	private static class TestPrintStream extends PrintStream {

		private final List<String> strings = new ArrayList<String>();
		
		public TestPrintStream() {
	        super(new PipedOutputStream());
		}

		/* (non-Javadoc)
		 * @see java.io.PrintStream#println(java.lang.String)
		 */
		public void println(String x) {
			strings.add(x);
			System.out.println(x);
			super.println(x);
		}
	}
}

@@ -1434,160 +1434,8 @@
 te;%0A
-import static org.junit.Assert.fail;%0A%0Aimport java.io.PipedOutputStream;%0Aimport java.io.PrintStream;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 impo
@@ -1464,16 +1464,16 @@
 Status;%0A
+
 import t
@@ -1512,16 +1512,45 @@
 stCase;%0A
+import util.TestPrintStream;%0A
 import u
@@ -1640,16 +1640,22 @@
 private 
+final 
 TestPrin
@@ -2456,536 +2456,51 @@
 %7B%0A%09%09
-for (String string : testPrintStream.strings) %7B%0A%09%09%09if (string.contains(substring)) %7B%0A%09%09%09%09return;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09fail(%22Substring not found%22);%0A%09%7D%0A%0A%09private static class TestPrintStream extends PrintStream %7B%0A%0A%09%09private final List%3CString%3E strings = new ArrayList%3CString%3E();%0A%09%09%0A%09%09public TestPrintStream() %7B%0A%09        super(new PipedOutputStream());%0A%09%09%7D%0A%0A%09%09/* (non-Javadoc)%0A%09%09 * @see java.io.PrintStream#println(java.lang.String)%0A%09%09 */%0A%09%09public void println(String x) %7B%0A%09%09%09strings.add(x);%0A%09%09%09System.out.println(x);%0A%09%09%09super.println(x);%0A%09%09%7D
+testPrintStream.assertSubstring(substring);
 %0A%09%7D%0A

Remove duplicate implementation of TestPrintStream.