From c81922a615e70de6003ac66f510da40c3f49af68 Mon Sep 17 00:00:00 2001 From: markiantorno Date: Tue, 21 Apr 2020 11:47:49 -0400 Subject: [PATCH] Fixing utilities test so it runs on all OS --- .../fhir/utilities/tests/UtilitiesTests.java | 95 +++++++++++++++++-- 1 file changed, 89 insertions(+), 6 deletions(-) diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/UtilitiesTests.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/UtilitiesTests.java index a7cc47ca1..9d1689a81 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/UtilitiesTests.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/tests/UtilitiesTests.java @@ -1,18 +1,101 @@ package org.hl7.fhir.utilities.tests; +import java.io.File; import java.io.IOException; +import org.apache.commons.lang3.SystemUtils; import org.hl7.fhir.utilities.Utilities; -import org.junit.Test; -import junit.framework.Assert; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; public class UtilitiesTests { + public static final String OSX = "OS X"; + public static final String MAC = "MAC"; + public static final String WINDOWS = "WINDOWS"; + public static final String LINUX = "Linux"; + + public static final String TEST_TXT = "test.txt"; + + public static final String LINUX_TEMP_DIR = "/tmp/"; + public static final String LINUX_USER_DIR = System.getProperty("user.home") + "/"; + public static final String LINUX_JAVA_HOME = System.getenv("JAVA_HOME") + "/"; + + public static final String WIN_TEMP_DIR = "c:\\temp\\"; + public static final String WIN_USER_DIR = System.getProperty("user.home") + "\\"; + public static final String WIN_JAVA_HOME = System.getenv("JAVA_HOME") + "\\"; + + public static final String OSX_USER_DIR = System.getProperty("user.home") + "/"; + public static final String OSX_JAVA_HOME = System.getenv("JAVA_HOME") + "/"; + @Test - public void testPath() throws IOException { - Assert.assertEquals(Utilities.path("[tmp]", "test.txt"), "c:\\temp\\test.txt"); - Assert.assertEquals(Utilities.path("[user]", "test.txt"), System.getProperty("user.home")+"\\test.txt"); - Assert.assertEquals(Utilities.path("[JAVA_HOME]", "test.txt"), System.getenv("JAVA_HOME")+"\\test.txt"); + @DisplayName("Test Utilities.path maps temp directory correctly") + public void testTempDirPath() throws IOException { + Assertions.assertEquals(Utilities.path("[tmp]", TEST_TXT), getTempDirectory() + TEST_TXT); + } + + @Test + @DisplayName("Test Utilities.path maps user directory correctly") + public void testUserDirPath() throws IOException { + Assertions.assertEquals(Utilities.path("[user]", TEST_TXT), getUserDirectory() + TEST_TXT); + } + + @Test + @DisplayName("Test Utilities.path maps JAVA_HOME correctly") + public void testJavaHomeDirPath() throws IOException { + Assertions.assertEquals(Utilities.path("[JAVA_HOME]", TEST_TXT), getJavaHomeDirectory() + TEST_TXT); + } + + private String getJavaHomeDirectory() { + String os = SystemUtils.OS_NAME; + if (os.contains(OSX) || os.contains(MAC)) { + return OSX_JAVA_HOME; + } else if (os.contains(LINUX)) { + return LINUX_JAVA_HOME; + } else if (os.contains(WINDOWS)) { + return WIN_JAVA_HOME; + } else { + throw new IllegalStateException("OS not recognized...cannot verify created directories."); + } + } + + private String getUserDirectory() { + String os = SystemUtils.OS_NAME; + if (os.contains(OSX) || os.contains(MAC)) { + return OSX_USER_DIR; + } else if (os.contains(LINUX)) { + return LINUX_USER_DIR; + } else if (os.contains(WINDOWS)) { + return WIN_USER_DIR; + } else { + throw new IllegalStateException("OS not recognized...cannot verify created directories."); + } + } + + private String getTempDirectory() throws IOException { + String os = SystemUtils.OS_NAME; + if (os.contains(OSX) || os.contains(MAC)) { + return getOsxTempDir(); + } else if (os.contains(LINUX)) { + return LINUX_TEMP_DIR; + } else if (os.contains(WINDOWS)) { + return WIN_TEMP_DIR; + } else { + throw new IllegalStateException("OS not recognized...cannot verify created directories."); + } + } + + /** + * Getting the temporary directory in OSX is a little different from Linux and Windows. We need to create a temporary + * file and then extract the directory path from it. + * + * @return Full path to tmp directory on OSX machines. + * @throws IOException + */ + public static String getOsxTempDir() throws IOException { + File file = File.createTempFile("throwaway", ".file"); + return file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf('/')) + '/'; } }