Skip to content

Commit

Permalink
Add test for SystemUtils.USER_NAME.
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Mar 28, 2021
1 parent 5e5af7a commit d1e9e59
Showing 1 changed file with 95 additions and 87 deletions.
182 changes: 95 additions & 87 deletions src/test/java/org/apache/commons/lang3/SystemUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,93 +52,9 @@
*/
public class SystemUtilsTest {

@Test
public void testConstructor() {
assertNotNull(new SystemUtils());
final Constructor<?>[] cons = SystemUtils.class.getDeclaredConstructors();
assertEquals(1, cons.length);
assertTrue(Modifier.isPublic(cons[0].getModifiers()));
assertTrue(Modifier.isPublic(SystemUtils.class.getModifiers()));
assertFalse(Modifier.isFinal(SystemUtils.class.getModifiers()));
}

@Test
public void testGetEnvironmentVariableAbsent() {
final String name = "THIS_ENV_VAR_SHOULD_NOT_EXIST_FOR_THIS_TEST_TO_PASS";
final String expected = System.getenv(name);
assertNull(expected);
final String value = SystemUtils.getEnvironmentVariable(name, "DEFAULT");
assertEquals("DEFAULT", value);
}

@Test
public void testGetEnvironmentVariablePresent() {
final String name = "PATH";
final String expected = System.getenv(name);
final String value = SystemUtils.getEnvironmentVariable(name, null);
assertEquals(expected, value);
}

@Test
public void testGetHostName() {
final String hostName = SystemUtils.getHostName();
final String expected = SystemUtils.IS_OS_WINDOWS ? System.getenv("COMPUTERNAME") : System.getenv("HOSTNAME");
assertEquals(expected, hostName);
}

/**
* Assumes no security manager exists.
*/
@Test
public void testGetJavaHome() {
final File dir = SystemUtils.getJavaHome();
assertNotNull(dir);
assertTrue(dir.exists());
}

/**
* Assumes no security manager exists.
*/
@Test
public void testGetJavaIoTmpDir() {
final File dir = SystemUtils.getJavaIoTmpDir();
assertNotNull(dir);
assertTrue(dir.exists());
}

/**
* Assumes no security manager exists.
*/
@Test
public void testGetUserDir() {
final File dir = SystemUtils.getUserDir();
assertNotNull(dir);
assertTrue(dir.exists());
}

/**
* Assumes no security manager exists.
*/
@Test
public void testGetUserHome() {
final File dir = SystemUtils.getUserHome();
assertNotNull(dir);
assertTrue(dir.exists());
}

/**
* Assumes no security manager exists.
*/
@Test
public void testGetUserName() {
assertEquals(System.getProperty("user.name"), SystemUtils.getUserName());
// Don't overwrite the system property in this test in case something goes awfully wrong.
assertEquals(System.getProperty("user.name", "foo"), SystemUtils.getUserName("foo"));
}

@Test
@SuppressWarnings("deprecation")
public void testIS_JAVA() {
public void test_IS_JAVA() {
final String javaVersion = SystemUtils.JAVA_VERSION;
if (javaVersion == null) {
assertFalse(SystemUtils.IS_JAVA_1_1);
Expand Down Expand Up @@ -297,7 +213,7 @@ public void testIS_JAVA() {
}

@Test
public void testIS_OS() {
public void test_IS_OS() {
final String osName = System.getProperty("os.name");
if (osName == null) {
assertFalse(SystemUtils.IS_OS_WINDOWS);
Expand Down Expand Up @@ -338,7 +254,7 @@ public void testIS_OS() {
}

@Test
public void testIS_zOS() {
public void test_IS_zOS() {
final String osName = System.getProperty("os.name");
if (osName == null) {
assertFalse(SystemUtils.IS_OS_ZOS);
Expand All @@ -348,6 +264,98 @@ public void testIS_zOS() {
}
}

/**
* Assumes no security manager exists.
*/
@Test
public void test_USER_NAME() {
assertEquals(System.getProperty("user.name"), SystemUtils.USER_NAME);
}

@Test
public void testConstructor() {
assertNotNull(new SystemUtils());
final Constructor<?>[] cons = SystemUtils.class.getDeclaredConstructors();
assertEquals(1, cons.length);
assertTrue(Modifier.isPublic(cons[0].getModifiers()));
assertTrue(Modifier.isPublic(SystemUtils.class.getModifiers()));
assertFalse(Modifier.isFinal(SystemUtils.class.getModifiers()));
}

@Test
public void testGetEnvironmentVariableAbsent() {
final String name = "THIS_ENV_VAR_SHOULD_NOT_EXIST_FOR_THIS_TEST_TO_PASS";
final String expected = System.getenv(name);
assertNull(expected);
final String value = SystemUtils.getEnvironmentVariable(name, "DEFAULT");
assertEquals("DEFAULT", value);
}

@Test
public void testGetEnvironmentVariablePresent() {
final String name = "PATH";
final String expected = System.getenv(name);
final String value = SystemUtils.getEnvironmentVariable(name, null);
assertEquals(expected, value);
}

@Test
public void testGetHostName() {
final String hostName = SystemUtils.getHostName();
final String expected = SystemUtils.IS_OS_WINDOWS ? System.getenv("COMPUTERNAME") : System.getenv("HOSTNAME");
assertEquals(expected, hostName);
}

/**
* Assumes no security manager exists.
*/
@Test
public void testGetJavaHome() {
final File dir = SystemUtils.getJavaHome();
assertNotNull(dir);
assertTrue(dir.exists());
}

/**
* Assumes no security manager exists.
*/
@Test
public void testGetJavaIoTmpDir() {
final File dir = SystemUtils.getJavaIoTmpDir();
assertNotNull(dir);
assertTrue(dir.exists());
}

/**
* Assumes no security manager exists.
*/
@Test
public void testGetUserDir() {
final File dir = SystemUtils.getUserDir();
assertNotNull(dir);
assertTrue(dir.exists());
}

/**
* Assumes no security manager exists.
*/
@Test
public void testGetUserHome() {
final File dir = SystemUtils.getUserHome();
assertNotNull(dir);
assertTrue(dir.exists());
}

/**
* Assumes no security manager exists.
*/
@Test
public void testGetUserName() {
assertEquals(System.getProperty("user.name"), SystemUtils.getUserName());
// Don't overwrite the system property in this test in case something goes awfully wrong.
assertEquals(System.getProperty("user.name", "foo"), SystemUtils.getUserName("foo"));
}

@Test
public void testIsJavaVersionAtLeast() {
if (SystemUtils.IS_JAVA_1_8) {
Expand Down

0 comments on commit d1e9e59

Please sign in to comment.