Skip to content

Commit

Permalink
Extracting extension point tests in subpackage as prep for #103
Browse files Browse the repository at this point in the history
  • Loading branch information
jlink committed Jan 21, 2016
1 parent de1d978 commit a6e5159
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 85 deletions.
Expand Up @@ -13,25 +13,20 @@
import static org.junit.gen5.engine.discovery.ClassSelector.forClass; import static org.junit.gen5.engine.discovery.ClassSelector.forClass;
import static org.junit.gen5.launcher.main.TestDiscoveryRequestBuilder.request; import static org.junit.gen5.launcher.main.TestDiscoveryRequestBuilder.request;


import org.junit.gen5.api.BeforeEach;
import org.junit.gen5.engine.ExecutionEventRecorder; import org.junit.gen5.engine.ExecutionEventRecorder;
import org.junit.gen5.engine.ExecutionRequest; import org.junit.gen5.engine.ExecutionRequest;
import org.junit.gen5.engine.TestDescriptor; import org.junit.gen5.engine.TestDescriptor;
import org.junit.gen5.launcher.*; import org.junit.gen5.launcher.TestDiscoveryRequest;


/** /**
* Abstract base class for tests involving the {@link JUnit5TestEngine}. * Abstract base class for tests involving the {@link JUnit5TestEngine}.
* *
* @since 5.0 * @since 5.0
*/ */
abstract class AbstractJUnit5TestEngineTests { public abstract class AbstractJUnit5TestEngineTests {


private final JUnit5TestEngine engine = new JUnit5TestEngine(); private final JUnit5TestEngine engine = new JUnit5TestEngine();


@BeforeEach
void initListeners() {
}

protected ExecutionEventRecorder executeTestsForClass(Class<?> testClass) { protected ExecutionEventRecorder executeTestsForClass(Class<?> testClass) {
return executeTests(request().select(forClass(testClass)).build()); return executeTests(request().select(forClass(testClass)).build());
} }
Expand Down
Expand Up @@ -10,33 +10,19 @@


package org.junit.gen5.engine.junit5; package org.junit.gen5.engine.junit5;


import static org.junit.gen5.api.Assertions.assertEquals; import static org.junit.gen5.api.Assertions.*;
import static org.junit.gen5.api.Assertions.fail;
import static org.junit.gen5.commons.util.AnnotationUtils.findAnnotation;
import static org.junit.gen5.engine.discovery.ClassSelector.forClass; import static org.junit.gen5.engine.discovery.ClassSelector.forClass;
import static org.junit.gen5.launcher.main.TestDiscoveryRequestBuilder.request; import static org.junit.gen5.launcher.main.TestDiscoveryRequestBuilder.request;


import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Objects;
import java.util.Optional;

import org.junit.gen5.api.AfterEach; import org.junit.gen5.api.AfterEach;
import org.junit.gen5.api.BeforeEach; import org.junit.gen5.api.BeforeEach;
import org.junit.gen5.api.Disabled; import org.junit.gen5.api.Disabled;
import org.junit.gen5.api.Test; import org.junit.gen5.api.Test;
import org.junit.gen5.api.extension.ConditionEvaluationResult;
import org.junit.gen5.api.extension.ExtendWith;
import org.junit.gen5.api.extension.TestExecutionCondition;
import org.junit.gen5.api.extension.TestExtensionContext;
import org.junit.gen5.engine.ExecutionEventRecorder; import org.junit.gen5.engine.ExecutionEventRecorder;
import org.junit.gen5.launcher.TestDiscoveryRequest; import org.junit.gen5.launcher.TestDiscoveryRequest;


/** /**
* Integration tests that verify support for {@link Disabled @Disabled} and * Integration tests that verify support for {@link Disabled @Disabled} in the {@link JUnit5TestEngine}.
* custom {@link TestExecutionCondition Conditions} in the {@link JUnit5TestEngine}.
* *
* @since 5.0 * @since 5.0
*/ */
Expand Down Expand Up @@ -70,11 +56,9 @@ public void executeTestsWithDisabledTestMethods() {
TestDiscoveryRequest request = request().select(forClass(DisabledTestMethodsTestCase.class)).build(); TestDiscoveryRequest request = request().select(forClass(DisabledTestMethodsTestCase.class)).build();
ExecutionEventRecorder eventRecorder = executeTests(request); ExecutionEventRecorder eventRecorder = executeTests(request);


assertEquals(2L, eventRecorder.getTestStartedCount(), "# tests started"); assertEquals(1L, eventRecorder.getTestStartedCount(), "# tests started");
assertEquals(2L, eventRecorder.getTestSuccessfulCount(), "# tests succeeded"); assertEquals(1L, eventRecorder.getTestSuccessfulCount(), "# tests succeeded");
assertEquals(3L, eventRecorder.getTestSkippedCount(), "# tests skipped"); assertEquals(1L, eventRecorder.getTestSkippedCount(), "# tests skipped");
assertEquals(0L, eventRecorder.getTestAbortedCount(), "# tests aborted");
assertEquals(0L, eventRecorder.getTestFailedCount(), "# tests failed");
} }


// ------------------------------------------------------------------- // -------------------------------------------------------------------
Expand All @@ -100,56 +84,6 @@ void disabledTest() {
fail("this should be @Disabled"); fail("this should be @Disabled");
} }


@Test
@SystemProperty(key = FOO, value = BAR)
void systemPropertyEnabledTest() {
}

@Test
@SystemProperty(key = FOO, value = BOGUS)
void systemPropertyWithIncorrectValueTest() {
fail("this should be disabled");
}

@Test
@SystemProperty(key = BOGUS, value = "doesn't matter")
void systemPropertyNotSetTest() {
fail("this should be disabled");
}

}

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith(SystemPropertyCondition.class)
@interface SystemProperty {

String key();

String value();
}

private static class SystemPropertyCondition implements TestExecutionCondition {

@Override
public ConditionEvaluationResult evaluate(TestExtensionContext context) {
Optional<SystemProperty> optional = findAnnotation(context.getElement(), SystemProperty.class);

if (optional.isPresent()) {
SystemProperty systemProperty = optional.get();
String key = systemProperty.key();
String expected = systemProperty.value();
String actual = System.getProperty(key);

if (!Objects.equals(expected, actual)) {
return ConditionEvaluationResult.disabled(String.format(
"System property [%s] has a value of [%s] instead of [%s]", key, actual, expected));
}
}

return ConditionEvaluationResult.enabled("@SystemProperty is not present");
}

} }


} }
Expand Up @@ -8,7 +8,7 @@
* http://www.eclipse.org/legal/epl-v10.html * http://www.eclipse.org/legal/epl-v10.html
*/ */


package org.junit.gen5.engine.junit5; package org.junit.gen5.engine.junit5.extension;


import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static org.junit.gen5.api.Assertions.assertEquals; import static org.junit.gen5.api.Assertions.assertEquals;
Expand All @@ -29,15 +29,17 @@
import org.junit.gen5.api.extension.ExtensionPointRegistry.Position; import org.junit.gen5.api.extension.ExtensionPointRegistry.Position;
import org.junit.gen5.api.extension.ExtensionRegistrar; import org.junit.gen5.api.extension.ExtensionRegistrar;
import org.junit.gen5.engine.ExecutionEventRecorder; import org.junit.gen5.engine.ExecutionEventRecorder;
import org.junit.gen5.engine.junit5.AbstractJUnit5TestEngineTests;
import org.junit.gen5.engine.junit5.JUnit5TestEngine;
import org.junit.gen5.launcher.TestDiscoveryRequest; import org.junit.gen5.launcher.TestDiscoveryRequest;


/** /**
* Integration tests that verify support of {@link BeforeAll}, {@link AfterAll}, * Integration tests that verify support of {@link BeforeAll}, {@link AfterAll},
* and {@link BeforeAllExtensionPoint} in the {@link JUnit5TestEngine}. * {@link BeforeAllExtensionPoint}, and {@link AfterAllExtensionPoint} in the {@link JUnit5TestEngine}.
* *
* @since 5.0 * @since 5.0
*/ */
public class ClassLevelCallbackTests extends AbstractJUnit5TestEngineTests { public class BeforeAndAfterAllTests extends AbstractJUnit5TestEngineTests {


@Test @Test
public void beforeAllAndAfterAllCallbacks() { public void beforeAllAndAfterAllCallbacks() {
Expand Down
Expand Up @@ -8,7 +8,7 @@
* http://www.eclipse.org/legal/epl-v10.html * http://www.eclipse.org/legal/epl-v10.html
*/ */


package org.junit.gen5.engine.junit5; package org.junit.gen5.engine.junit5.extension;


import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static org.junit.gen5.api.Assertions.assertEquals; import static org.junit.gen5.api.Assertions.assertEquals;
Expand All @@ -30,6 +30,8 @@
import org.junit.gen5.api.extension.ExtensionRegistrar; import org.junit.gen5.api.extension.ExtensionRegistrar;
import org.junit.gen5.api.extension.TestExtensionContext; import org.junit.gen5.api.extension.TestExtensionContext;
import org.junit.gen5.engine.ExecutionEventRecorder; import org.junit.gen5.engine.ExecutionEventRecorder;
import org.junit.gen5.engine.junit5.AbstractJUnit5TestEngineTests;
import org.junit.gen5.engine.junit5.JUnit5TestEngine;
import org.junit.gen5.launcher.TestDiscoveryRequest; import org.junit.gen5.launcher.TestDiscoveryRequest;


/** /**
Expand All @@ -38,7 +40,7 @@
* *
* @since 5.0 * @since 5.0
*/ */
public class MethodLevelCallbackTests extends AbstractJUnit5TestEngineTests { public class BeforeAndAfterEachTests extends AbstractJUnit5TestEngineTests {


@Test @Test
public void beforeEachAndAfterEachCallbacks() { public void beforeEachAndAfterEachCallbacks() {
Expand Down
@@ -0,0 +1,159 @@
/*
* 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.junit5.extension;

import static org.junit.gen5.api.Assertions.*;
import static org.junit.gen5.commons.util.AnnotationUtils.findAnnotation;
import static org.junit.gen5.engine.discovery.ClassSelector.forClass;
import static org.junit.gen5.launcher.main.TestDiscoveryRequestBuilder.request;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Objects;
import java.util.Optional;

import org.junit.gen5.api.AfterEach;
import org.junit.gen5.api.BeforeEach;
import org.junit.gen5.api.Test;
import org.junit.gen5.api.extension.ConditionEvaluationResult;
import org.junit.gen5.api.extension.ContainerExecutionCondition;
import org.junit.gen5.api.extension.ContainerExtensionContext;
import org.junit.gen5.api.extension.ExtendWith;
import org.junit.gen5.api.extension.ExtensionContext;
import org.junit.gen5.api.extension.TestExecutionCondition;
import org.junit.gen5.api.extension.TestExtensionContext;
import org.junit.gen5.engine.ExecutionEventRecorder;
import org.junit.gen5.engine.junit5.AbstractJUnit5TestEngineTests;
import org.junit.gen5.engine.junit5.JUnit5TestEngine;
import org.junit.gen5.launcher.TestDiscoveryRequest;

/**
* Integration tests that verify support for {@link TestExecutionCondition} and
* {@link ContainerExecutionCondition} extension points in the {@link JUnit5TestEngine}.
*
* @since 5.0
*/
public class ExecutionConditionTests extends AbstractJUnit5TestEngineTests {

private static final String FOO = "DisabledTests.foo";
private static final String BAR = "DisabledTests.bar";
private static final String BOGUS = "DisabledTests.bogus";

@BeforeEach
public void setUp() {
System.setProperty(FOO, BAR);
}

@AfterEach
public void tearDown() {
System.clearProperty(FOO);
}

@Test
public void executionConditionWorksOnContainer() {
TestDiscoveryRequest request = request().select(
forClass(TestCaseWithContainerExecutionCondition.class)).build();
ExecutionEventRecorder eventRecorder = executeTests(request);

assertEquals(1L, eventRecorder.getContainerSkippedCount(), "# container skipped");
assertEquals(0L, eventRecorder.getTestStartedCount(), "# tests started");
}

@Test
public void executionConditionWorksOnTest() {
TestDiscoveryRequest request = request().select(forClass(TestCaseWithTestExecutionCondition.class)).build();
ExecutionEventRecorder eventRecorder = executeTests(request);

assertEquals(2L, eventRecorder.getTestStartedCount(), "# tests started");
assertEquals(2L, eventRecorder.getTestSuccessfulCount(), "# tests succeeded");
assertEquals(2L, eventRecorder.getTestSkippedCount(), "# tests skipped");
}

// -------------------------------------------------------------------

@SystemProperty(key = FOO, value = BOGUS)
private static class TestCaseWithContainerExecutionCondition {

@Test
void disabledTest() {
fail("this should be @Disabled");
}
}

private static class TestCaseWithTestExecutionCondition {

@Test
void enabledTest() {
}

@Test
@SystemProperty(key = FOO, value = BAR)
void systemPropertyEnabledTest() {
}

@Test
@SystemProperty(key = FOO, value = BOGUS)
void systemPropertyWithIncorrectValueTest() {
fail("this should be disabled");
}

@Test
@SystemProperty(key = BOGUS, value = "doesn't matter")
void systemPropertyNotSetTest() {
fail("this should be disabled");
}

}

@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith(SystemPropertyCondition.class)
@interface SystemProperty {

String key();

String value();
}

private static class SystemPropertyCondition implements TestExecutionCondition, ContainerExecutionCondition {

@Override
public ConditionEvaluationResult evaluate(ContainerExtensionContext context) {
return evaluate((ExtensionContext) context);
}

@Override
public ConditionEvaluationResult evaluate(TestExtensionContext context) {
return evaluate((ExtensionContext) context);
}

private ConditionEvaluationResult evaluate(ExtensionContext context) {
Optional<SystemProperty> optional = findAnnotation(context.getElement(), SystemProperty.class);

if (optional.isPresent()) {
SystemProperty systemProperty = optional.get();
String key = systemProperty.key();
String expected = systemProperty.value();
String actual = System.getProperty(key);

if (!Objects.equals(expected, actual)) {
return ConditionEvaluationResult.disabled(String.format(
"System property [%s] has a value of [%s] instead of [%s]", key, actual, expected));
}
}

return ConditionEvaluationResult.enabled("@SystemProperty is not present");
}
}

}
Expand Up @@ -8,7 +8,7 @@
* http://www.eclipse.org/legal/epl-v10.html * http://www.eclipse.org/legal/epl-v10.html
*/ */


package org.junit.gen5.engine.junit5; package org.junit.gen5.engine.junit5.extension;


import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static org.junit.gen5.api.Assertions.assertEquals; import static org.junit.gen5.api.Assertions.assertEquals;
Expand All @@ -25,6 +25,7 @@
import org.junit.gen5.api.extension.InstancePostProcessor; import org.junit.gen5.api.extension.InstancePostProcessor;
import org.junit.gen5.api.extension.TestExtensionContext; import org.junit.gen5.api.extension.TestExtensionContext;
import org.junit.gen5.engine.ExecutionEventRecorder; import org.junit.gen5.engine.ExecutionEventRecorder;
import org.junit.gen5.engine.junit5.AbstractJUnit5TestEngineTests;
import org.junit.gen5.launcher.TestDiscoveryRequest; import org.junit.gen5.launcher.TestDiscoveryRequest;


/** /**
Expand Down
Expand Up @@ -8,7 +8,7 @@
* http://www.eclipse.org/legal/epl-v10.html * http://www.eclipse.org/legal/epl-v10.html
*/ */


package org.junit.gen5.engine.junit5; package org.junit.gen5.engine.junit5.extension;


import static org.junit.gen5.api.Assertions.*; import static org.junit.gen5.api.Assertions.*;


Expand All @@ -22,6 +22,8 @@
import org.junit.gen5.api.extension.ExtendWith; import org.junit.gen5.api.extension.ExtendWith;
import org.junit.gen5.api.extension.MethodParameterResolver; import org.junit.gen5.api.extension.MethodParameterResolver;
import org.junit.gen5.engine.ExecutionEventRecorder; import org.junit.gen5.engine.ExecutionEventRecorder;
import org.junit.gen5.engine.junit5.AbstractJUnit5TestEngineTests;
import org.junit.gen5.engine.junit5.JUnit5TestEngine;
import org.junit.gen5.engine.junit5.execution.injection.sample.CustomAnnotation; import org.junit.gen5.engine.junit5.execution.injection.sample.CustomAnnotation;
import org.junit.gen5.engine.junit5.execution.injection.sample.CustomAnnotationParameterResolver; import org.junit.gen5.engine.junit5.execution.injection.sample.CustomAnnotationParameterResolver;
import org.junit.gen5.engine.junit5.execution.injection.sample.CustomType; import org.junit.gen5.engine.junit5.execution.injection.sample.CustomType;
Expand Down

2 comments on commit a6e5159

@sbrannen
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you get a chance, please delete the FOO, BAR, and BOGUS constants as well as the setUp() and tearDown() methods in DisabledTests, since this has all been moved to ExecutionConditionTests.

@jlink
Copy link
Contributor Author

@jlink jlink commented on a6e5159 Jan 21, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.