Merge pull request #10730 from hmdrzsharifi/master
bael-4909: Getting Java Version at Runtime
This commit is contained in:
commit
e24d787fb1
|
@ -50,6 +50,11 @@
|
|||
<version>${junit.jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -73,6 +78,7 @@
|
|||
<junit.jupiter.version>5.7.0</junit.jupiter.version>
|
||||
<assertj.version>3.17.2</assertj.version>
|
||||
<mockserver.version>5.11.1</mockserver.version>
|
||||
<commons-lang3.version>3.12.0</commons-lang3.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,48 @@
|
|||
package com.baeldung.version;
|
||||
|
||||
import org.apache.commons.lang3.SystemUtils;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class VersionUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenJava_whenUsingRuntime_thenGetVersion() {
|
||||
String expectedVersion = "11";
|
||||
Runtime.Version runtimeVersion = Runtime.version();
|
||||
String version = String.valueOf(runtimeVersion.version().get(0));
|
||||
Assertions.assertThat(version).isEqualTo(expectedVersion);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled("Only valid for Java 8 and lower")
|
||||
public void givenJava_whenUsingCommonsLang_thenGetVersion() {
|
||||
int expectedVersion = 8;
|
||||
String[] versionElements = SystemUtils.JAVA_SPECIFICATION_VERSION.split("\\.");
|
||||
int discard = Integer.parseInt(versionElements[0]);
|
||||
int version;
|
||||
if (discard == 1) {
|
||||
version = Integer.parseInt(versionElements[1]);
|
||||
} else {
|
||||
version = discard;
|
||||
}
|
||||
Assertions.assertThat(version).isEqualTo(expectedVersion);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled("Only valid for Java 8 and lower")
|
||||
public void givenJava_whenUsingSystemProp_thenGetVersion() {
|
||||
int expectedVersion = 8;
|
||||
String[] versionElements = System.getProperty("java.version").split("\\.");
|
||||
int discard = Integer.parseInt(versionElements[0]);
|
||||
int version;
|
||||
if (discard == 1) {
|
||||
version = Integer.parseInt(versionElements[1]);
|
||||
} else {
|
||||
version = discard;
|
||||
}
|
||||
Assertions.assertThat(version).isEqualTo(expectedVersion);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue