Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correctly detects java versions above java 8 #94

Merged
merged 2 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/main/java/org/audit4j/core/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@

package org.audit4j.core;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.audit4j.core.command.CommandProcessor;
import org.audit4j.core.command.impl.BatchCommand;
import org.audit4j.core.command.impl.MetadataCommand;
Expand Down Expand Up @@ -316,7 +312,7 @@ private final static void checkEnvironment() {
// Check java support.!
boolean javaSupport = EnvUtil.isJDK7OrHigher();
if (!javaSupport) {
Log.error("Your Java version (", EnvUtil.getJavaersion(), ") is not supported for Audit4j. ",
Log.error("Your Java version (", EnvUtil.getJavaVersion(), ") is not supported for Audit4j. ",
ErrorGuide.getGuide(ErrorGuide.JAVA_VERSION_ERROR));
throw new InitializationException("Java version is not supported.");
}
Expand Down
78 changes: 28 additions & 50 deletions src/main/java/org/audit4j/core/util/EnvUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,99 +18,77 @@

package org.audit4j.core.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.audit4j.core.Configurations;

import javax.servlet.ServletContext;

import org.audit4j.core.Configurations;
import java.io.File;

/**
* The Class EnvUtil.
*
*
* @author <a href="mailto:[email protected]">Janith Bandara</a>
*/
public class EnvUtil {


/**
* private constructor to avoid instantiation of this class
*/
private EnvUtil(){

}

/**
* Checks if is jD k_ n_ or higher.
*
* @param n
* the n
* @return true, if is jD k_ n_ or higher
*/
private static boolean isJDK_N_OrHigher(int n) {
List<String> versionList = new ArrayList<String>();
// this code should work at least until JDK 10 (assuming n parameter is
// always 6 or more)
for (int i = 0; i < 5; i++) {
//Till JDK 1.8 versioning is 1.x after 10 its will JDK N.x
if(n + i<10)
versionList.add("1." + (n + i));
else
versionList.add((n + i)+".");
}

String javaVersion = System.getProperty("java.version");
if (javaVersion == null) {
return false;
}
for (String v : versionList) {
if (javaVersion.startsWith(v))
return true;
}
return false;
}

/**
* Checks if is jD k5.
*
*
* @return true, if is jD k5
*/
static public boolean isJDK5() {
return isJDK_N_OrHigher(5);
return JavaVersion.isJDK_N_OrHigher(5, getJavaVersion());
}

/**
* Checks if is jD k6 or higher.
*
*
* @return true, if is jD k6 or higher
*/
static public boolean isJDK6OrHigher() {
return isJDK_N_OrHigher(6);
return JavaVersion.isJDK_N_OrHigher(6, getJavaVersion());
}

/**
* Checks if is jD k7 or higher.
*
*
* @return true, if is jD k7 or higher
*/
static public boolean isJDK7OrHigher() {
return isJDK_N_OrHigher(7);
return JavaVersion.isJDK_N_OrHigher(7, getJavaVersion());
}


/**
* Gets the javaersion.
*
* Gets the javaersion
*
* @deprecated renamed this method. use {@link EnvUtil#getJavaVersion()} instead.
*
* @return the javaersion
*/
@Deprecated
public static String getJavaersion() {
return System.getProperty("java.version");
return getJavaVersion();
}

/**
* Gets the java version.
*
* @return the java version
*/
public static String getJavaVersion() {
return JavaVersion.current();
}

/**
* Checks if is janino available.
*
*
* @return true, if is janino available
*/
static public boolean isJaninoAvailable() {
Expand All @@ -125,7 +103,7 @@ static public boolean isJaninoAvailable() {

/**
* Checks if is servlet spec 3 or higher.
*
*
* @param context
* the context
* @return true, if is servlet spec3 or higher
Expand All @@ -139,7 +117,7 @@ public static boolean isServletSpec3OrHigher(ServletContext context) {

/**
* Checks if is windows.
*
*
* @return true, if is windows
*/
public static boolean isWindows() {
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/org/audit4j/core/util/JavaVersion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.audit4j.core.util;

/*

java.version is a system property that exists in every JVM. There are two possible formats for it:

Java 8 or lower: 1.6.0_23, 1.7.0, 1.7.0_80, 1.8.0_211
Java 9 or higher: 9.0.1, 11.0.4, 12, 12.0.1

*/
public class JavaVersion {
/**
* Checks if is jD k_ n_ or higher.
*
* @param n
* the n
* @return true, if is jD k_ n_ or higher
*/
static boolean isJDK_N_OrHigher(int n, String javaVersion) {
try {
final String[] versionParts = javaVersion.split("\\.");

int version;
if ("1".equals(versionParts[0])) {
version = Integer.parseInt(versionParts[1]);
} else {
version = Integer.parseInt(versionParts[0]);
}
return version >= n;
} catch (Exception e) {
// swallow any error and return false to maintain previous behaviour of defaulting to false
return false;
}
}

public static String current() {
return System.getProperty("java.version");
}
}
49 changes: 49 additions & 0 deletions src/test/java/org/audit4j/core/util/JavaVersionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.audit4j.core.util;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

@RunWith(Parameterized.class)
public class JavaVersionTest {

public JavaVersionTest(final int javaVersion, final String javaSystemProperty) {
this.javaVersion = javaVersion;
this.javaSystemProperty = javaSystemProperty;
}

@Parameterized.Parameters(name = "{index}: checking java({0}) with version string {1}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{ 6, "1.6.0_23"},
{ 7, "1.7.0_80"},
{ 8, "1.8.0_211"},
{ 9, "9.0.1"},
{ 10, "10.0.1"},
{ 11, "11.0.4"},
{ 12, "12"}
});
}

private final int javaVersion;
private final String javaSystemProperty;

@Test
public void canDetectWhetherJavaVersionIsHigherThanAGivenValue() {
final List<Integer> javaVersions = Arrays.asList(5, 6, 7, 8, 9, 10, 11, 12, 13);

for (final Integer version : javaVersions) {
assertThat(
"java version " + javaVersion + " with description " + javaSystemProperty + " was not correctly detected as higher than java " + version,
JavaVersion.isJDK_N_OrHigher(version, javaSystemProperty),
is(version <= javaVersion));
}
}
}