/*
 * Copyright 2015 the original author or authors.
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution and is available at
 *
 * http://www.eclipse.org/legal/epl-v10.html
 */

package org.junit.gen5.api;

import java.util.function.Supplier;

import org.opentestalliance.TestAbortedException;

/**
 * @author Sam Brannen
 * @since 5.0
 */
public final class Assumptions {

	private Assumptions() {
		/* no-op */
	}

	public static void assumeTrue(boolean condition) {
		if (!condition) {
			throw new TestAbortedException("Assumption failed: condition is not true");
		}
	}

	public static void assumeTrue(boolean condition, String message) {
		if (!condition) {
			throw new TestAbortedException("Assumption failed: " + message);
		}
	}

	public static void assumeTrue(boolean condition, Supplier<String> messageSupplier) {
		if (!condition) {
			throw new TestAbortedException(messageSupplier.get());
		}
	}

	public static void assumeFalse(boolean condition) {
		if (condition) {
			throw new TestAbortedException("Assumption failed: condition is not false");
		}
	}

	public static void assumeFalse(boolean condition, String message) {
		if (condition) {
			throw new TestAbortedException(message);
		}
	}

	public static void assumeFalse(boolean condition, Supplier<String> messageSupplier) {
		if (condition) {
			throw new TestAbortedException("Assumption failed: " + messageSupplier.get());
		}
	}

	public static void assumingThat(boolean condition, Executable executable) {
		if (condition) {
			try {
				executable.execute();
			}
			catch (AssertionError | RuntimeException e) {
				// rethrow
				throw e;
			}
			catch (Throwable e) {
				// TODO Don't wrap Throwables such as OutOfMemoryError, etc.
				throw new RuntimeException("Wrapped exception thrown from Executable", e);
			}
		}
	}

}

@@ -383,50 +383,194 @@
 org.
-opentestalliance.TestAbortedException;%0A%0A/*
+junit.gen5.commons.util.StringUtils;%0Aimport org.opentestalliance.TestAbortedException;%0A%0A/**%0A * Collection of utility methods for aborting test execution based on failed%0A * assumptions.%0A 
 *%0A *
@@ -604,16 +604,45 @@
 nce 5.0%0A
+ * @see TestAbortedException%0A
  */%0Apubl
@@ -786,37 +786,32 @@
 tion) %7B%0A%09%09%09throw
- new 
 TestAbortedExcep
@@ -816,35 +816,16 @@
 eption(%22
-Assumption failed: 
 conditio
@@ -937,37 +937,32 @@
 tion) %7B%0A%09%09%09throw
- new 
 TestAbortedExcep
@@ -970,32 +970,8 @@
 ion(
-%22Assumption failed: %22 + 
 mess
@@ -1090,37 +1090,32 @@
 tion) %7B%0A%09%09%09throw
- new 
 TestAbortedExcep
@@ -1223,37 +1223,32 @@
 tion) %7B%0A%09%09%09throw
- new 
 TestAbortedExcep
@@ -1253,35 +1253,16 @@
 eption(%22
-Assumption failed: 
 conditio
@@ -1375,37 +1375,32 @@
 tion) %7B%0A%09%09%09throw
- new 
 TestAbortedExcep
@@ -1536,21 +1536,16 @@
 %09%09%09throw
- new 
 TestAbor
@@ -1561,32 +1561,8 @@
 ion(
-%22Assumption failed: %22 + 
 mess
@@ -1739,17 +1739,8 @@
 ch (
-Assertion
 Erro
@@ -1761,16 +1761,17 @@
 eption e
+x
 ) %7B%0A%09%09%09%09
@@ -1792,16 +1792,17 @@
 %09throw e
+x
 ;%0A%09%09%09%7D%0A%09
@@ -1825,77 +1825,13 @@
 le e
+x
 ) %7B%0A
-%09%09%09%09// TODO Don't wrap Throwables such as OutOfMemoryError, etc.%0A
 %09%09%09%09
@@ -1866,16 +1866,24 @@
 Wrapped 
+checked 
 exceptio
@@ -1910,16 +1910,17 @@
 able%22, e
+x
 );%0A%09%09%09%7D%0A
@@ -1927,10 +1927,208 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09private static void throwTestAbortedException(String message) %7B%0A%09%09throw new TestAbortedException(%0A%09%09%09StringUtils.isNotBlank(message) ? (%22Assumption failed: %22 + message) : %22Assumption failed%22);%0A%09%7D%0A%0A
 %7D%0A

Document and polish Assumptions