BAEL-6773: Get the OS Username in Java (#14848)

* BAEL-6773: Removed Ignored Tests

* BAEL-6773: Initializing a New Module

* BAEL-6773: user.name Property Example

* BAEL-6773: Removed Unused Dependencies
This commit is contained in:
Eugene Kovko 2023-09-28 18:04:54 +02:00 committed by GitHub
parent 2dc953dcad
commit eabaf2764d
7 changed files with 103 additions and 89 deletions

View File

@ -0,0 +1,26 @@
*.class
0.*
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
.resourceCache
# Packaged files #
*.jar
*.war
*.ear
# Files generated by integration tests
*.txt
backup-pom.xml
/bin/
/temp
#IntelliJ specific
.idea/
*.iml

View File

@ -0,0 +1,7 @@
## Core Java OS
This module contains articles about working with the operating system (OS) in Java
### Relevant Articles:

View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-os</artifactId>
<name>core-java-os</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<build>
<finalName>core-java-os</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
</properties>
</project>

View File

@ -0,0 +1,9 @@
package com.baeldung.system;
public class EnvironmentExample {
public void getUserName() {
String username = System.getenv("USERNAME");
System.out.println("User: " + username);
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.system;
public class PropertiesExample {
public void getUserName() {
String username = System.getProperty("user.name");
System.out.println("User: " + username);
}
public void getCustomProp() {
String customProperty = System.getProperty("custom.prop");
System.out.println("Custom property: " + customProperty);
}
public void getCustomPropWithFallback() {
String customProperty = System.getProperty("non-existent-property", "default value");
System.out.println("Custom property: " + customProperty);
}
}

View File

@ -1,25 +0,0 @@
package com.baeldung.system;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
@Ignore
public class WhenDetectingOSUnitTest {
private DetectOS os = new DetectOS();
@Test
public void whenUsingSystemProperty_shouldReturnOS() {
String expected = "Windows 10";
String actual = os.getOperatingSystem();
Assert.assertEquals(expected, actual);
}
@Test
public void whenUsingSystemUtils_shouldReturnOS() {
String expected = "Windows 10";
String actual = os.getOperatingSystemSystemUtils();
Assert.assertEquals(expected, actual);
}
}

View File

@ -1,64 +0,0 @@
package com.baeldung.system.exit;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import java.security.Permission;
import static org.junit.Assert.assertEquals;
@Ignore("This test is ignored because it tests deprecated code")
public class SystemExitUnitTest {
private SecurityManager securityManager;
private SystemExitExample example;
@Before
public void setUp() {
example = new SystemExitExample();
securityManager = System.getSecurityManager();
System.setSecurityManager(new NoExitSecurityManager());
}
@After
public void tearDown() throws Exception {
System.setSecurityManager(securityManager);
}
@Test
public void testExit() throws Exception {
try {
example.readFile();
} catch (ExitException e) {
assertEquals("Exit status", 2, e.status);
}
}
protected static class ExitException extends SecurityException {
private static final long serialVersionUID = 1L;
public final int status;
public ExitException(int status) {
this.status = status;
}
}
private static class NoExitSecurityManager extends SecurityManager {
@Override
public void checkPermission(Permission perm) {
}
@Override
public void checkPermission(Permission perm, Object context) {
}
@Override
public void checkExit(int status) {
super.checkExit(status);
throw new ExitException(status);
}
}
}