/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.flink.streaming.util;

import org.apache.flink.test.util.AbstractTestBase;

import org.junit.Test;

import static org.junit.Assert.fail;

/**
 * Base class for unit tests that run a single test.
 *
 * <p>To write a unit test against this test base, simply extend it and implement the {@link #testProgram()} method.
 */
public abstract class StreamingProgramTestBase extends AbstractTestBase {

	// --------------------------------------------------------------------------------------------
	//  Methods to create the test program and for pre- and post- test work
	// --------------------------------------------------------------------------------------------

	protected abstract void testProgram() throws Exception;

	protected void preSubmit() throws Exception {}

	protected void postSubmit() throws Exception {}

	// --------------------------------------------------------------------------------------------
	//  Test entry point
	// --------------------------------------------------------------------------------------------

	@Test
	public void testJob() throws Exception {
		// pre-submit
		try {
			preSubmit();
		}
		catch (Exception e) {
			System.err.println(e.getMessage());
			e.printStackTrace();
			fail("Pre-submit work caused an error: " + e.getMessage());
		}

		// call the test program
		try {
			testProgram();
		}
		catch (Exception e) {
			System.err.println(e.getMessage());
			e.printStackTrace();
			fail("Error while calling the test program: " + e.getMessage());
		}

		// post-submit
		try {
			postSubmit();
		}
		catch (Exception e) {
			System.err.println(e.getMessage());
			e.printStackTrace();
			fail("Post-submit work caused an error: " + e.getMessage());
		}
	}
}

@@ -923,46 +923,8 @@
 t;%0A%0A
-import static org.junit.Assert.fail;%0A%0A
 /**%0A
@@ -1882,25 +1882,16 @@
 -submit%0A
-%09%09try %7B%0A%09
 %09%09preSub
@@ -1900,166 +1900,8 @@
 t();
-%0A%09%09%7D%0A%09%09catch (Exception e) %7B%0A%09%09%09System.err.println(e.getMessage());%0A%09%09%09e.printStackTrace();%0A%09%09%09fail(%22Pre-submit work caused an error: %22 + e.getMessage());%0A%09%09%7D
 %0A%0A%09%09
@@ -1925,25 +1925,16 @@
 program%0A
-%09%09try %7B%0A%09
 %09%09testPr
@@ -1945,171 +1945,8 @@
 m();
-%0A%09%09%7D%0A%09%09catch (Exception e) %7B%0A%09%09%09System.err.println(e.getMessage());%0A%09%09%09e.printStackTrace();%0A%09%09%09fail(%22Error while calling the test program: %22 + e.getMessage());%0A%09%09%7D
 %0A%0A%09%09
@@ -1964,17 +1964,8 @@
 mit%0A
-%09%09try %7B%0A%09
 %09%09po
@@ -1980,167 +1980,8 @@
 ();%0A
-%09%09%7D%0A%09%09catch (Exception e) %7B%0A%09%09%09System.err.println(e.getMessage());%0A%09%09%09e.printStackTrace();%0A%09%09%09fail(%22Post-submit work caused an error: %22 + e.getMessage());%0A%09%09%7D%0A
 %09%7D%0A%7D

Remove unnecessary exception catching in StreamingProgramTestBase