Skip to content

Commit

Permalink
Merge pull request #321 from newrelic/cleanup
Browse files Browse the repository at this point in the history
Cleanup JavaVersionUtils and test
  • Loading branch information
jasonjkeller authored Jun 10, 2021
2 parents ab943c7 + c85c90a commit de8b0d8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
* Simple set of utilities to help us validate that the agent is being run on a supported version of Java.
*/
public class JavaVersionUtils {
private static final Pattern SUPPORTED_JAVA_VERSION_PATTERN = Pattern.compile("^(1\\.7|1\\.8|9|1[0-5])$");
private static final Pattern EXCLUSIVE_MIN_JAVA_VERSION_PATTERN = Pattern.compile("^1\\.6$");
private static final Pattern SUPPORTED_JAVA_VERSION_PATTERN = Pattern.compile("^(1\\.8|9|1[0-5])$");
private static final Pattern EXCLUSIVE_MIN_JAVA_VERSION_PATTERN = Pattern.compile("^1\\.7$");
private static final Pattern EXCLUSIVE_MAX_JAVA_VERSION_PATTERN = Pattern.compile("^16$");

public static final String JAVA_6 = "1.6";
public static final String JAVA_7 = "1.7";
public static final String JAVA_8 = "1.8";
public static final String JAVA_9 = "9";
Expand Down Expand Up @@ -50,8 +49,8 @@ public static String getUnsupportedAgentJavaSpecVersionMessage(String javaSpecif
StringBuilder message = new StringBuilder();
if (EXCLUSIVE_MIN_JAVA_VERSION_PATTERN.matcher(javaSpecificationVersion).matches()) {
message.append("Java version is: ").append(javaSpecificationVersion).append(". ");
message.append("This version of the New Relic Agent does not support Java 1.6 or below. ")
.append("Please use a 4.3.x New Relic agent or a later version of Java.");
message.append("This version of the New Relic Agent does not support Java 1.7 or below. ")
.append("Please use a 6.5.0 New Relic agent or a later version of Java.");
} else if (EXCLUSIVE_MAX_JAVA_VERSION_PATTERN.matcher(javaSpecificationVersion).matches()) {
message.append("Java version is: ").append(javaSpecificationVersion).append(". ");
message.append("This version of the New Relic Agent does not support versions of Java greater than 15.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.junit.Test;

import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
Expand All @@ -18,7 +19,6 @@ public class JavaVersionUtilsTest {

@Test
public void supportAgentJavaSpecVersions() {
assertTrue(JavaVersionUtils.isAgentSupportedJavaSpecVersion(JavaVersionUtils.JAVA_7));
assertTrue(JavaVersionUtils.isAgentSupportedJavaSpecVersion(JavaVersionUtils.JAVA_8));
assertTrue(JavaVersionUtils.isAgentSupportedJavaSpecVersion(JavaVersionUtils.JAVA_9));
assertTrue(JavaVersionUtils.isAgentSupportedJavaSpecVersion(JavaVersionUtils.JAVA_10));
Expand All @@ -30,7 +30,7 @@ public void supportAgentJavaSpecVersions() {
}

@Test
public void unsupportedAgentVersionsLessThanJava7() {
public void unsupportedAgentVersionsLessThanJava8() {
assertFalse(JavaVersionUtils.isAgentSupportedJavaSpecVersion("1.5"));
assertFalse(JavaVersionUtils.isAgentSupportedJavaSpecVersion("1.5.0"));
assertFalse(JavaVersionUtils.isAgentSupportedJavaSpecVersion("1.5.0_11"));
Expand All @@ -41,10 +41,15 @@ public void unsupportedAgentVersionsLessThanJava7() {
assertFalse(JavaVersionUtils.isAgentSupportedJavaSpecVersion("1.6.0_11"));
assertFalse(JavaVersionUtils.isAgentSupportedJavaSpecVersion("1.6.0_11-b12"));
assertFalse(JavaVersionUtils.isAgentSupportedJavaSpecVersion("1.6.1_gibberish"));
assertFalse(JavaVersionUtils.isAgentSupportedJavaSpecVersion("1.7"));
assertFalse(JavaVersionUtils.isAgentSupportedJavaSpecVersion("1.7.0"));
assertFalse(JavaVersionUtils.isAgentSupportedJavaSpecVersion("1.7.0_11"));
assertFalse(JavaVersionUtils.isAgentSupportedJavaSpecVersion("1.7.0_11-b12"));
assertFalse(JavaVersionUtils.isAgentSupportedJavaSpecVersion("1.7.1_gibberish"));
}

@Test
public void unsupportedAgentVersionsExceedingJava14() {
public void unsupportedAgentVersionsExceedingJava15() {
assertFalse(JavaVersionUtils.isAgentSupportedJavaSpecVersion("16.0"));
assertFalse(JavaVersionUtils.isAgentSupportedJavaSpecVersion("16+181"));
assertFalse(JavaVersionUtils.isAgentSupportedJavaSpecVersion("16.0+181"));
Expand All @@ -71,10 +76,10 @@ public void otherUnsupportedVersionStrings() {
}

@Test
public void unsupportedJavaVersionMessageWhenLessThanJava7() {
String msg = JavaVersionUtils.getUnsupportedAgentJavaSpecVersionMessage(JavaVersionUtils.JAVA_6);
assertThat(msg, containsString(JavaVersionUtils.JAVA_6));
assertThat(msg, containsString("4.3.x New Relic agent"));
public void unsupportedJavaVersionMessageWhenLessThanJava8() {
String msg = JavaVersionUtils.getUnsupportedAgentJavaSpecVersionMessage(JavaVersionUtils.JAVA_7);
assertThat(msg, containsString(JavaVersionUtils.JAVA_7));
assertThat(msg, containsString("6.5.0 New Relic agent"));
}

@Test
Expand All @@ -87,10 +92,10 @@ public void unsupportedJavaVersionMessageWhenGreaterThanJava9() {
@Test
public void emptyMessageReturnedWhenJavaVersionSupported() {
String msg = JavaVersionUtils.getUnsupportedAgentJavaSpecVersionMessage(JavaVersionUtils.JAVA_8);
assertTrue(msg.length() == 0);
assertEquals(0, msg.length());

msg = JavaVersionUtils.getUnsupportedAgentJavaSpecVersionMessage(null);
assertTrue(msg.length() == 0);
assertEquals(0, msg.length());
}

}

0 comments on commit de8b0d8

Please sign in to comment.