Skip to content

Commit

Permalink
Introduce unit tests for operating system conditions
Browse files Browse the repository at this point in the history
This commit introduces dedicated unit tests for
EnabledOnOsCondition and DisabledOnOsCondition.

Issue: junit-team#1280
  • Loading branch information
sbrannen committed Feb 8, 2018
1 parent 8ec227a commit 3a21c00
Show file tree
Hide file tree
Showing 4 changed files with 356 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* 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.condition;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.condition.EnabledOnOsIntegrationTests.onLinux;
import static org.junit.jupiter.api.condition.EnabledOnOsIntegrationTests.onMac;
import static org.junit.jupiter.api.condition.EnabledOnOsIntegrationTests.onSolaris;
import static org.junit.jupiter.api.condition.EnabledOnOsIntegrationTests.onWindows;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.platform.commons.util.PreconditionViolationException;

/**
* Unit tests for {@link DisabledOnOsCondition}.
*
* <p>Note that test method names MUST match the test method names in
* {@link DisabledOnOsIntegrationTests}.
*
* @since 5.1
*/
class DisabledOnOsConditionTests extends AbstractExecutionConditionTests {

@Override
protected ExecutionCondition getExecutionCondition() {
return new DisabledOnOsCondition();
}

@Override
protected Class<?> getTestClass() {
return DisabledOnOsIntegrationTests.class;
}

/**
* @see DisabledOnOsIntegrationTests#enabledBecauseAnnotationIsNotPresent()
*/
@Test
void enabledBecauseAnnotationIsNotPresent() {
evaluateCondition();
assertEnabled();
assertReasonContains("@DisabledOnOs is not present");
}

/**
* @see DisabledOnOsIntegrationTests#missingOsDeclaration()
*/
@Test
void missingOsDeclaration() {
Exception exception = assertThrows(PreconditionViolationException.class, this::evaluateCondition);
assertThat(exception).hasMessageContaining("You must declare at least one OS");
}

/**
* @see DisabledOnOsIntegrationTests#disabledOnEveryOs()
*/
@Test
void disabledOnEveryOs() {
evaluateCondition();
assertDisabledOnCurrentOs();
}

/**
* @see DisabledOnOsIntegrationTests#linux()
*/
@Test
void linux() {
evaluateCondition();
if (onLinux()) {
assertDisabledOnCurrentOs();
}
else {
assertEnabledOnCurrentOs();
}
}

/**
* @see DisabledOnOsIntegrationTests#macOs()
*/
@Test
void macOs() {
evaluateCondition();
if (onMac()) {
assertDisabledOnCurrentOs();
}
else {
assertEnabledOnCurrentOs();
}
}

/**
* @see DisabledOnOsIntegrationTests#macOsWithComposedAnnotation()
*/
@Test
void macOsWithComposedAnnotation() {
evaluateCondition();
if (onMac()) {
assertDisabledOnCurrentOs();
}
else {
assertEnabledOnCurrentOs();
}
}

/**
* @see DisabledOnOsIntegrationTests#windows()
*/
@Test
void windows() {
evaluateCondition();
if (onWindows()) {
assertDisabledOnCurrentOs();
}
else {
assertEnabledOnCurrentOs();
}
}

/**
* @see DisabledOnOsIntegrationTests#solaris()
*/
@Test
void solaris() {
evaluateCondition();
if (onSolaris()) {
assertDisabledOnCurrentOs();
}
else {
assertEnabledOnCurrentOs();
}
}

/**
* @see DisabledOnOsIntegrationTests#other()
*/
@Test
void other() {
evaluateCondition();
if (onLinux() || onMac() || onSolaris() || onWindows()) {
assertEnabledOnCurrentOs();
}
else {
assertDisabledOnCurrentOs();
}
}

private void assertEnabledOnCurrentOs() {
assertEnabled();
assertReasonContains("Enabled on operating system: " + System.getProperty("os.name"));
}

private void assertDisabledOnCurrentOs() {
assertDisabled();
assertReasonContains("Disabled on operating system: " + System.getProperty("os.name"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

/**
Expand All @@ -37,6 +38,17 @@
*/
class DisabledOnOsIntegrationTests {

@Test
@Disabled("Only used in a unit test via reflection")
void enabledBecauseAnnotationIsNotPresent() {
}

@Test
@Disabled("Only used in a unit test via reflection")
@DisabledOnOs({})
void missingOsDeclaration() {
}

@Test
@DisabledOnOs({ LINUX, MAC, WINDOWS, SOLARIS, OTHER })
void disabledOnEveryOs() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* 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.condition;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.condition.EnabledOnOsIntegrationTests.onLinux;
import static org.junit.jupiter.api.condition.EnabledOnOsIntegrationTests.onMac;
import static org.junit.jupiter.api.condition.EnabledOnOsIntegrationTests.onSolaris;
import static org.junit.jupiter.api.condition.EnabledOnOsIntegrationTests.onWindows;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.platform.commons.util.PreconditionViolationException;

/**
* Unit tests for {@link EnabledOnOsCondition}.
*
* <p>Note that test method names MUST match the test method names in
* {@link EnabledOnOsIntegrationTests}.
*
* @since 5.1
*/
class EnabledOnOsConditionTests extends AbstractExecutionConditionTests {

@Override
protected ExecutionCondition getExecutionCondition() {
return new EnabledOnOsCondition();
}

@Override
protected Class<?> getTestClass() {
return EnabledOnOsIntegrationTests.class;
}

/**
* @see EnabledOnOsIntegrationTests#enabledBecauseAnnotationIsNotPresent()
*/
@Test
void enabledBecauseAnnotationIsNotPresent() {
evaluateCondition();
assertEnabled();
assertReasonContains("@EnabledOnOs is not present");
}

/**
* @see EnabledOnOsIntegrationTests#missingOsDeclaration()
*/
@Test
void missingOsDeclaration() {
Exception exception = assertThrows(PreconditionViolationException.class, this::evaluateCondition);
assertThat(exception).hasMessageContaining("You must declare at least one OS");
}

/**
* @see EnabledOnOsIntegrationTests#enabledOnEveryOs()
*/
@Test
void enabledOnEveryOs() {
evaluateCondition();
assertEnabledOnCurrentOs();
}

/**
* @see EnabledOnOsIntegrationTests#linux()
*/
@Test
void linux() {
evaluateCondition();
if (onLinux()) {
assertEnabledOnCurrentOs();
}
else {
assertDisabledOnCurrentOs();
}
}

/**
* @see EnabledOnOsIntegrationTests#macOs()
*/
@Test
void macOs() {
evaluateCondition();
if (onMac()) {
assertEnabledOnCurrentOs();
}
else {
assertDisabledOnCurrentOs();
}
}

/**
* @see EnabledOnOsIntegrationTests#macOsWithComposedAnnotation()
*/
@Test
void macOsWithComposedAnnotation() {
evaluateCondition();
if (onMac()) {
assertEnabledOnCurrentOs();
}
else {
assertDisabledOnCurrentOs();
}
}

/**
* @see EnabledOnOsIntegrationTests#windows()
*/
@Test
void windows() {
evaluateCondition();
if (onWindows()) {
assertEnabledOnCurrentOs();
}
else {
assertDisabledOnCurrentOs();
}
}

/**
* @see EnabledOnOsIntegrationTests#solaris()
*/
@Test
void solaris() {
evaluateCondition();
if (onSolaris()) {
assertEnabledOnCurrentOs();
}
else {
assertDisabledOnCurrentOs();
}
}

/**
* @see EnabledOnOsIntegrationTests#other()
*/
@Test
void other() {
evaluateCondition();
if (onLinux() || onMac() || onSolaris() || onWindows()) {
assertDisabledOnCurrentOs();
}
else {
assertEnabledOnCurrentOs();
}
}

private void assertEnabledOnCurrentOs() {
assertEnabled();
assertReasonContains("Enabled on operating system: " + System.getProperty("os.name"));
}

private void assertDisabledOnCurrentOs() {
assertDisabled();
assertReasonContains("Disabled on operating system: " + System.getProperty("os.name"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.lang.annotation.Target;
import java.util.Locale;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

/**
Expand All @@ -35,6 +36,17 @@ class EnabledOnOsIntegrationTests {

private static final String OS_NAME = System.getProperty("os.name").toLowerCase(Locale.ENGLISH);

@Test
@Disabled("Only used in a unit test via reflection")
void enabledBecauseAnnotationIsNotPresent() {
}

@Test
@Disabled("Only used in a unit test via reflection")
@EnabledOnOs({})
void missingOsDeclaration() {
}

@Test
@EnabledOnOs({ LINUX, MAC, WINDOWS, SOLARIS, OTHER })
void enabledOnEveryOs() {
Expand Down

0 comments on commit 3a21c00

Please sign in to comment.