/*
 * Copyright 2015-2016 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.engine;

import static org.junit.gen5.engine.TestExecutionResult.Status.*;

import java.util.Optional;

import org.junit.gen5.commons.util.Preconditions;

/**
 * @since 5.0
 */
public class TestExecutionResult {

	private static final TestExecutionResult SUCCESSFUL_RESULT = new TestExecutionResult(SUCCESSFUL, null);

	public enum Status {
		SUCCESSFUL, ABORTED, FAILED
	}

	private final Status status;
	private final Throwable throwable;

	public static TestExecutionResult successful() {
		return SUCCESSFUL_RESULT;
	}

	public static TestExecutionResult aborted(Throwable throwable) {
		return new TestExecutionResult(ABORTED, throwable);
	}

	public static TestExecutionResult failed(Throwable throwable) {
		return new TestExecutionResult(FAILED, throwable);
	}

	public TestExecutionResult(Status status, Throwable throwable) {
		Preconditions.notNull(status, "status must not be null");
		this.status = status;
		this.throwable = throwable;
	}

	public Status getStatus() {
		return status;
	}

	public Optional<Throwable> getThrowable() {
		return Optional.ofNullable(throwable);
	}

	@Override
	public String toString() {
		StringBuilder result = new StringBuilder();
		result.append(status);
		if (throwable != null) {
			result.append(": ").append(throwable.getMessage()).append(" [").append(
				throwable.getClass().getName()).append(']');
		}
		return result.toString();
	}

}

@@ -483,16 +483,68 @@
 ditions;
+%0Aimport org.junit.gen5.commons.util.ToStringBuilder;
 %0A%0A/**%0A *
@@ -1517,16 +1517,18 @@
 g() %7B%0A%09%09
+To
 StringBu
@@ -1537,21 +1537,24 @@
 der 
-result
+builder
  = new 
+To
 Stri
@@ -1567,27 +1567,32 @@
 der(
+this
 );%0A%09%09
-result
+builder
 .append(
 stat
@@ -1591,74 +1591,44 @@
 end(
+%22
 status
-);%0A%09%09if (throwable != null) %7B%0A%09%09%09result.append(%22: %22)
+%22, status);%0A%09%09builder
 .append(
 thro
@@ -1623,16 +1623,17 @@
 .append(
+%22
 throwabl
@@ -1637,97 +1637,23 @@
 able
-.getMessage()).append(%22 %5B%22).append(%0A%09%09%09%09throwable.getClass().getName()).append('%5D'
+%22, throwable
 );%0A
-%09%09%7D%0A
 %09%09re
@@ -1661,14 +1661,15 @@
 urn 
-result
+builder
 .toS

Use ToStringBuilder in TestExecutionResult