/*
 * Copyright 2015-2018 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 v2.0 which
 * accompanies this distribution and is available at
 *
 * http://www.eclipse.org/legal/epl-v20.html
 */

package org.junit.jupiter.api;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

class DynamicTestTests {

	private final List<String> assertedValues = new ArrayList<>();

	@Test
	void streamFromIterator() throws Throwable {
		Stream<DynamicTest> stream = DynamicTest.stream(Arrays.asList("foo", "bar", "baz").iterator(),
			String::toUpperCase, this::throwingConsumer);
		List<DynamicTest> dynamicTests = stream.collect(Collectors.toList());

		assertThat(dynamicTests).hasSize(3).extracting(DynamicTest::getDisplayName).containsExactly("FOO", "BAR",
			"BAZ");

		assertThat(assertedValues).isEmpty();

		dynamicTests.get(0).getExecutable().execute();
		assertThat(assertedValues).containsExactly("foo");

		dynamicTests.get(1).getExecutable().execute();
		assertThat(assertedValues).containsExactly("foo", "bar");

		Throwable t = assertThrows(Throwable.class, () -> dynamicTests.get(2).getExecutable().execute());
		assertThat(t).hasMessage("Baz!");
		assertThat(assertedValues).containsExactly("foo", "bar");
	}

	private void throwingConsumer(String str) throws Throwable {
		if ("baz".equals(str)) {
			throw new Throwable("Baz!");
		}
		this.assertedValues.add(str);
	}

}

@@ -458,16 +458,162 @@
 Throws;%0A
+import static org.junit.jupiter.api.DynamicTest.dynamicTest;%0A%0Aimport java.lang.reflect.InvocationTargetException;%0Aimport java.lang.reflect.Method;
 %0Aimport 
@@ -750,16 +750,61 @@
 tream;%0A%0A
+import org.opentest4j.AssertionFailedError;%0A%0A
 class Dy
@@ -1893,10 +1893,1506 @@
 r);%0A%09%7D%0A%0A
+%09@Test%0A%09void reflectiveOperationThrowingAssertionFailedError() %7B%0A%09%09Throwable t48 = assertThrows(AssertionFailedError.class,%0A%09%09%09() -%3E dynamicTest(%221 == 48%22, this::assertOneEquals48).getExecutable().execute());%0A%09%09assertThat(t48).hasMessage(%22expected: %3C1%3E but was: %3C48%3E%22);%0A%0A%09%09Throwable t49 = assertThrows(AssertionFailedError.class,%0A%09%09%09() -%3E dynamicTest(%221 == 49%22, this::assertOnEquals49).getExecutable().execute());%0A%09%09assertThat(t49).hasMessage(%22expected: %3C1%3E but was: %3C49%3E%22);%0A%09%7D%0A%0A%09@Test%0A%09@Disabled(%22https://github.com/junit-team/junit5/issues/1322%22)%0A%09void reflectiveOperationThrowingUnexpectedException() %7B%0A%09%09// Fails with: Unexpected exception type thrown%0A%09%09//   ==%3E expected: %3Corg.opentest4j.AssertionFailedError%3E%0A%09%09//        but was: %3Cjava.lang.reflect.InvocationTargetException%3E%0A%09%09Throwable t50 = assertThrows(AssertionFailedError.class,%0A%09%09%09() -%3E dynamicTest(%221 == 50%22, this::assertOneEquals50).getExecutable().execute());%0A%09%09assertThat(t50).hasMessage(%22expected: %3C1%3E but was: %3C50%3E%22);%0A%09%7D%0A%0A%09private void assertOneEquals48() %7B%0A%09%09Assertions.assertEquals(1, 48);%0A%09%7D%0A%0A%09private void assertOnEquals49() throws Throwable %7B%0A%09%09Method method = Assertions.class.getMethod(%22assertEquals%22, int.class, int.class);%0A%09%09try %7B%0A%09%09%09method.invoke(null, 1, 49);%0A%09%09%7D%0A%09%09catch (InvocationTargetException e) %7B%0A%09%09%09throw e.getTargetException();%0A%09%09%7D%0A%09%7D%0A%0A%09private void assertOneEquals50() throws Throwable %7B%0A%09%09Method method = Assertions.class.getMethod(%22assertEquals%22, int.class, int.class);%0A%09%09method.invoke(null, 1, 50);%0A%09%7D%0A%0A
 %7D%0A

Add test cases for failing reflective operations in dynamic tests