diff --git a/core/src/test/java/org/itsallcode/openfasttrace/core/importer/TestImporterService.java b/core/src/test/java/org/itsallcode/openfasttrace/core/importer/TestImporterService.java index 0047f4a7..cb67cc9a 100644 --- a/core/src/test/java/org/itsallcode/openfasttrace/core/importer/TestImporterService.java +++ b/core/src/test/java/org/itsallcode/openfasttrace/core/importer/TestImporterService.java @@ -15,9 +15,11 @@ import org.itsallcode.openfasttrace.api.importer.*; import org.itsallcode.openfasttrace.api.importer.input.InputFile; import org.itsallcode.openfasttrace.api.importer.input.RealFileInput; -import org.itsallcode.openfasttrace.testutil.OsDetector; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.api.condition.EnabledOnOs; +import org.junit.jupiter.api.condition.OS; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; import org.mockito.Captor; @@ -59,17 +61,17 @@ void beforeEach() } @Test + @EnabledOnOs(OS.WINDOWS) void testImportWindows() { - OsDetector.assumeRunningOnWindows(); runImporter(); assertThat(this.fileArg.getValue().getPath(), equalTo("dir\\file")); } @Test + @DisabledOnOs(OS.WINDOWS) void testImportUnix() { - OsDetector.assumeRunningOnUnix(); runImporter(); assertThat(this.fileArg.getValue().getPath(), equalTo("dir/file")); } diff --git a/core/src/test/java/org/itsallcode/openfasttrace/core/importer/tag/config/DescribedPathMatcherTest.java b/core/src/test/java/org/itsallcode/openfasttrace/core/importer/tag/config/DescribedPathMatcherTest.java index 682dc092..7e107ac1 100644 --- a/core/src/test/java/org/itsallcode/openfasttrace/core/importer/tag/config/DescribedPathMatcherTest.java +++ b/core/src/test/java/org/itsallcode/openfasttrace/core/importer/tag/config/DescribedPathMatcherTest.java @@ -12,9 +12,11 @@ import org.itsallcode.openfasttrace.api.importer.input.InputFile; import org.itsallcode.openfasttrace.api.importer.input.RealFileInput; import org.itsallcode.openfasttrace.api.importer.tag.config.DescribedPathMatcher; -import org.itsallcode.openfasttrace.testutil.OsDetector; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.api.condition.EnabledOnOs; +import org.junit.jupiter.api.condition.OS; class DescribedPathMatcherTest { @@ -47,18 +49,18 @@ void testListBasedMatcherMatchesRelativePath() } @Test + @EnabledOnOs(OS.WINDOWS) void testListBasedMatcherIsCaseInsensitiveUnderWindows() { - OsDetector.assumeRunningOnWindows(); createListMatcher("rel/path"); assertMatches("REL/path", true); assertMatches("rel/PATH", true); } @Test + @DisabledOnOs(OS.WINDOWS) void testListBasedMatcherIsCaseSensitiveUnderUnix() { - OsDetector.assumeRunningOnUnix(); createListMatcher("rel/path"); assertMatches("REL/path", false); assertMatches("rel/PATH", false); @@ -83,17 +85,17 @@ void testListBasedMatcherMatchesAbsoluteWindowsStylePathWithForwardSlash() } @Test + @EnabledOnOs(OS.WINDOWS) void testListBasedMatcherMatchesAbsoluteWindowsStylePathWithForwardSlashWindows() { - OsDetector.assumeRunningOnWindows(); createListMatcher("C:/abs/path"); assertMatches("C:\\abs\\path", true); } @Test + @DisabledOnOs(OS.WINDOWS) void testListBasedMatcherMatchesAbsoluteWindowsStylePathWithForwardSlashUnix() { - OsDetector.assumeRunningOnUnix(); createListMatcher("C:/abs/path"); assertMatches("C:\\abs\\path", false); } @@ -107,18 +109,18 @@ void testListBasedMatcherMatchesAbsoluteWindowsStylePathWithBackslash() } @Test + @DisabledOnOs(OS.WINDOWS) void testListBasedMatcherMatchesAbsoluteWindowsStylePathWithBackslashUnix() { - OsDetector.assumeRunningOnUnix(); createListMatcher("C:\\abs\\path"); assertMatches("C:/abs/path", false); assertMatches("C:\\abs\\path\\", false); } @Test + @EnabledOnOs(OS.WINDOWS) void testListBasedMatcherMatchesAbsoluteWindowsStylePathWithBackslashWindows() { - OsDetector.assumeRunningOnWindows(); createListMatcher("C:\\abs\\path"); assertMatches("C:/abs/path", true); assertMatches("C:\\abs\\path\\", true); diff --git a/doc/changes/changes_4.6.0.md b/doc/changes/changes_4.6.0.md index b5e30ec4..a37ce33e 100644 --- a/doc/changes/changes_4.6.0.md +++ b/doc/changes/changes_4.6.0.md @@ -12,4 +12,5 @@ We moved some GitHub action permissions from workflow-level to job-level. ## Refactoring -* #543: Made `CliException` a `RuntimeException` \ No newline at end of file +* #546: Replaced `OsDetector` with JUnit5's `EnabledOnOs` annotation. +* #543: Made `CliException` a `RuntimeException` diff --git a/product/src/test/java/org/itsallcode/openfasttrace/report/html/TestHtmlReportCssInlining.java b/product/src/test/java/org/itsallcode/openfasttrace/report/html/TestHtmlReportCssInlining.java index 8c0ce891..57afca09 100644 --- a/product/src/test/java/org/itsallcode/openfasttrace/report/html/TestHtmlReportCssInlining.java +++ b/product/src/test/java/org/itsallcode/openfasttrace/report/html/TestHtmlReportCssInlining.java @@ -14,8 +14,9 @@ import org.itsallcode.openfasttrace.report.html.view.ViewableContainer; import org.itsallcode.openfasttrace.report.html.view.html.HtmlViewFactory; import org.itsallcode.openfasttrace.testutil.AbstractFileBasedTest; -import org.itsallcode.openfasttrace.testutil.OsDetector; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.api.condition.OS; import org.junit.jupiter.api.io.TempDir; class TestHtmlReportCssInlining extends AbstractFileBasedTest @@ -48,9 +49,9 @@ void testInliningNonExistentCssThrowsException() throws URISyntaxException, Malf } @Test + @DisabledOnOs(OS.WINDOWS) void testInliningUnreadableCssThrowsException(@TempDir final Path tempDir) throws IOException { - OsDetector.assumeRunningOnUnix(); final File cssFile = tempDir.resolve("test.css").toFile(); cssFile.setReadable(false); final HtmlViewFactory factory = HtmlViewFactory.create(new ByteArrayOutputStream(), cssFile.toURI().toURL(), diff --git a/reporter/html/src/test/java/org/itsallcode/openfasttrace/report/html/view/html/TestOriginLinkFormatter.java b/reporter/html/src/test/java/org/itsallcode/openfasttrace/report/html/view/html/TestOriginLinkFormatter.java index 0c895b52..89398ef6 100644 --- a/reporter/html/src/test/java/org/itsallcode/openfasttrace/report/html/view/html/TestOriginLinkFormatter.java +++ b/reporter/html/src/test/java/org/itsallcode/openfasttrace/report/html/view/html/TestOriginLinkFormatter.java @@ -6,8 +6,10 @@ import static org.itsallcode.openfasttrace.report.html.view.html.OriginLinkFormatter.formatAsSpan; import org.itsallcode.openfasttrace.api.core.Location; -import org.itsallcode.openfasttrace.testutil.OsDetector; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.api.condition.EnabledOnOs; +import org.junit.jupiter.api.condition.OS; class TestOriginLinkFormatter { @@ -66,33 +68,33 @@ void testLongerHttpLink() } @Test + @DisabledOnOs(OS.WINDOWS) void testFormatRegularAbsoluteUnixPath() { - OsDetector.assumeRunningOnUnix(); assertPathAndLineRenderedToSpan("/foo/bar/baz", 1111, "/foo/bar/baz:1111"); } @Test + @EnabledOnOs(OS.WINDOWS) void testFormatRegularAbsoluteWindowsPath() { - OsDetector.assumeRunningOnWindows(); assertPathAndLineRenderedToSpan("C:\\foo\\bar\\baz", 1111, "C:\\foo\\bar\\baz:1111"); } @Test + @EnabledOnOs(OS.LINUX) void testFormatAbsoluteUnixPathWithSpecialCharactersLinux() { - OsDetector.assumeRunningOnLinux(); assertPathAndLineRenderedToSpan("/fo o/bär/baz", 12345678, "/fo o/bär/baz:12345678"); } @Test + @EnabledOnOs(OS.MAC) void testFormatAbsoluteUnixPathWithSpecialCharactersMacOs() { - OsDetector.assumeRunningOnMacOs(); assertPathAndLineRenderedToSpan("/fo o/bär/baz", 12345678, "/fo o/bär/baz:12345678"); } diff --git a/testutil/src/main/java/org/itsallcode/openfasttrace/testutil/OsCheck.java b/testutil/src/main/java/org/itsallcode/openfasttrace/testutil/OsCheck.java deleted file mode 100644 index e2712dd3..00000000 --- a/testutil/src/main/java/org/itsallcode/openfasttrace/testutil/OsCheck.java +++ /dev/null @@ -1,76 +0,0 @@ -package org.itsallcode.openfasttrace.testutil; - -import java.util.Locale; - -/** - * Helper class to check the operating system this Java VM runs in. - *

- * please keep the notes below as a pseudo-license: - *

- *

- * http://stackoverflow.com/questions/228477/how-do-i-programmatically-determine-operating-system-in-java - *

- *

- * compare to: - *

- * - */ -public class OsCheck -{ - /** - * Create a new instance of the operating system check. - */ - public OsCheck() - { - // Default constructor to fix compiler warning "missing-explicit-ctor" - } - - /** - * Types of Operating Systems - */ - public enum OSType - { - /** Windows OS */ - WINDOWS, - /** macOS */ - MACOS, - /** Linux */ - LINUX, - /** Any other operating system */ - OTHER - } - - /** - * Detect the operating system from the {@code os.name} System property and - * cache the result. - * - * @return the operating system detected - */ - public OSType getOperatingSystemType() - { - return detectOperatingSystemType(); - } - - private static OSType detectOperatingSystemType() - { - final String os = System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH); - if (os.contains("mac") || os.contains("darwin")) - { - return OSType.MACOS; - } - else if (os.contains("win")) - { - return OSType.WINDOWS; - } - else if (os.contains("linux")) - { - return OSType.LINUX; - } - else { - return OSType.OTHER; - } - } -} diff --git a/testutil/src/main/java/org/itsallcode/openfasttrace/testutil/OsDetector.java b/testutil/src/main/java/org/itsallcode/openfasttrace/testutil/OsDetector.java deleted file mode 100644 index 85686366..00000000 --- a/testutil/src/main/java/org/itsallcode/openfasttrace/testutil/OsDetector.java +++ /dev/null @@ -1,75 +0,0 @@ -package org.itsallcode.openfasttrace.testutil; - -import static org.junit.jupiter.api.Assumptions.assumeFalse; -import static org.junit.jupiter.api.Assumptions.assumeTrue; - -import org.itsallcode.openfasttrace.testutil.OsCheck.OSType; - -/** - * Helper class to detect the operating system. Contains assumption methods for - * JUnit. - */ -public final class OsDetector -{ - private static final OsCheck OS_CHECK = new OsCheck(); - - private OsDetector() - { - // not instantiable - } - - /** - * Assumes that the current operating system is Windows. If the application - * is not running on Windows, the assumption will fail, and the test will be - * aborted. - */ - public static void assumeRunningOnWindows() - { - assumeTrue(OsDetector::runningOnWindows, "not running on windows"); - } - - /** - * Assumes that the current operating system is Unix-like OS. If the - * application is not running on a Unix-like OS, the assumption will fail, - * and the test will be aborted. - */ - public static void assumeRunningOnUnix() - { - assumeFalse(OsDetector::runningOnWindows, "not running on unix"); - } - - /** - * Assumes that the current operating system is Linux. If the application - * is not running on Linux, the assumption will fail, and the test will be - * aborted. - */ - public static void assumeRunningOnLinux() - { - assumeTrue(OsDetector::runningOnLinux, "not running on linux"); - } - - /** - * Assumes that the current operating system is macOS. If the application - * is not running on macOS, the assumption will fail, and the test will be - * aborted. - */ - public static void assumeRunningOnMacOs() - { - assumeTrue(OsDetector::runningOnMac, "not running on macOS"); - } - - private static boolean runningOnWindows() - { - return OS_CHECK.getOperatingSystemType() == OSType.WINDOWS; - } - - private static boolean runningOnLinux() - { - return OS_CHECK.getOperatingSystemType() == OSType.LINUX; - } - - private static boolean runningOnMac() - { - return OS_CHECK.getOperatingSystemType() == OSType.MACOS; - } -} diff --git a/testutil/src/test/java/org/itsallcode/openfasttrace/testutil/OsCheckTest.java b/testutil/src/test/java/org/itsallcode/openfasttrace/testutil/OsCheckTest.java deleted file mode 100644 index f95f562c..00000000 --- a/testutil/src/test/java/org/itsallcode/openfasttrace/testutil/OsCheckTest.java +++ /dev/null @@ -1,72 +0,0 @@ -package org.itsallcode.openfasttrace.testutil; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.*; - -import org.itsallcode.openfasttrace.testutil.OsCheck.OSType; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.CsvSource; - -class OsCheckTest { - - private static final String OS_NAME_SYSTEM_PROPERTY = "os.name"; - private OsCheck osCheck; - - @BeforeEach - void setup() - { - osCheck = new OsCheck(); - } - - @Test - void getOperatingSystemType() - { - assertThat(osCheck.getOperatingSystemType(), allOf(notNullValue(), not(equalTo(OSType.OTHER)))); - } - - @ParameterizedTest - @CsvSource(nullValues = - { "NULL" }, value = { - "Mac OS X, MACOS", - "MAC OS X, MACOS", - "Mac, MACOS", - "_Mac_, MACOS", - "Darwin, MACOS", - "Windows 10, WINDOWS", - "Windows 11, WINDOWS", - "Win, WINDOWS", - "_Win_, WINDOWS", - "linux, LINUX", - "LINUX DEBIAN, LINUX", - "unix, OTHER", - "NULL, OTHER" - }) - - void detectOperatingSystemType(final String osNameSystemProperty, final OSType expectedType) - { - final String orgValue = System.getProperty(OS_NAME_SYSTEM_PROPERTY); - try - { - setSystemProperty(osNameSystemProperty); - assertThat(osCheck.getOperatingSystemType(), equalTo(expectedType)); - } - finally - { - setSystemProperty(orgValue); - } - } - - private void setSystemProperty(final String value) - { - if (value == null) - { - System.clearProperty(OS_NAME_SYSTEM_PROPERTY); - } - else - { - System.setProperty(OS_NAME_SYSTEM_PROPERTY, value); - } - } -}