Fixing utilities test so it runs on all OS
This commit is contained in:
parent
87bcdd36c6
commit
c81922a615
|
@ -1,18 +1,101 @@
|
||||||
package org.hl7.fhir.utilities.tests;
|
package org.hl7.fhir.utilities.tests;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.SystemUtils;
|
||||||
import org.hl7.fhir.utilities.Utilities;
|
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 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
|
@Test
|
||||||
public void testPath() throws IOException {
|
@DisplayName("Test Utilities.path maps temp directory correctly")
|
||||||
Assert.assertEquals(Utilities.path("[tmp]", "test.txt"), "c:\\temp\\test.txt");
|
public void testTempDirPath() throws IOException {
|
||||||
Assert.assertEquals(Utilities.path("[user]", "test.txt"), System.getProperty("user.home")+"\\test.txt");
|
Assertions.assertEquals(Utilities.path("[tmp]", TEST_TXT), getTempDirectory() + TEST_TXT);
|
||||||
Assert.assertEquals(Utilities.path("[JAVA_HOME]", "test.txt"), System.getenv("JAVA_HOME")+"\\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('/')) + '/';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue