Skip to content

Commit 0f46156

Browse files
authored
Merge pull request #799 from sk1418/os-name
[os-name] determine OS
2 parents 420ebc3 + bab0beb commit 0f46156

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## Relevant Articles
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
7+
<parent>
8+
<groupId>com.baeldung</groupId>
9+
<artifactId>core-kotlin-modules</artifactId>
10+
<version>1.0.0-SNAPSHOT</version>
11+
</parent>
12+
13+
<modelVersion>4.0.0</modelVersion>
14+
<artifactId>core-kotlin-10</artifactId>
15+
16+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.obtainTheOsName
2+
3+
import org.apache.commons.lang3.SystemUtils
4+
import org.junit.jupiter.api.Assertions.assertTrue
5+
import org.junit.jupiter.api.Test
6+
import org.slf4j.LoggerFactory
7+
8+
class ObtainTheOsNameUnitTest {
9+
private val log = LoggerFactory.getLogger(ObtainTheOsNameUnitTest::class.java)
10+
11+
@Test
12+
fun `when read system env properties then get the OS name and version`() {
13+
val osName = System.getProperty("os.name")
14+
val osVersion = System.getProperty("os.version")
15+
assertTrue { osName.isNotBlank() }
16+
assertTrue { osVersion.isNotBlank() }
17+
18+
log.info(
19+
"""|
20+
|OS Information
21+
|-----------------
22+
|OS_Name: $osName
23+
|Version: $osVersion
24+
|""".trimMargin()
25+
)
26+
}
27+
28+
@Test
29+
fun `when using when block then the OS gets determined`() {
30+
val osName = System.getProperty("os.name").lowercase()
31+
assertTrue { osName.isNotBlank() }
32+
33+
val result = when {
34+
"windows" in osName -> "Windows"
35+
listOf("mac", "nix", "sunos", "solaris", "bsd").any { it in osName } -> "*nix"
36+
else -> "Other"
37+
}
38+
39+
log.info(">>> $osName -> $result")
40+
}
41+
42+
@Test
43+
fun `when using SystemUtils from commons-lang then get the OS name and version`() {
44+
val osName = SystemUtils.OS_NAME
45+
val osVersion = SystemUtils.OS_VERSION
46+
assertTrue { osName.isNotBlank() }
47+
assertTrue { osVersion.isNotBlank() }
48+
49+
log.info(
50+
"""|
51+
|OS_Name : $osName
52+
|Version : $osVersion
53+
|Is Windows: ${SystemUtils.IS_OS_WINDOWS}
54+
|Is *nix : ${SystemUtils.IS_OS_UNIX}
55+
|Is Mac OS : ${SystemUtils.IS_OS_MAC}
56+
|""".trimMargin()
57+
)
58+
}
59+
60+
}

core-kotlin-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<module>core-kotlin-7</module>
2525
<module>core-kotlin-8</module>
2626
<module>core-kotlin-9</module>
27+
<module>core-kotlin-10</module>
2728
<module>core-kotlin-advanced</module>
2829
<module>core-kotlin-advanced-2</module>
2930
<module>core-kotlin-annotations</module>

0 commit comments

Comments
 (0)