Skip to content

Commit cd75885

Browse files
committed
Update CcmBridge#getCurrentJvmMajorVersion to use regular expressions
1 parent 1991537 commit cd75885

File tree

2 files changed

+97
-10
lines changed

2 files changed

+97
-10
lines changed

test-infra/src/main/java/com/datastax/oss/driver/api/testinfra/ccm/CcmBridge.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import java.util.Optional;
3939
import java.util.concurrent.TimeUnit;
4040
import java.util.concurrent.atomic.AtomicBoolean;
41+
import java.util.regex.Matcher;
42+
import java.util.regex.Pattern;
4143
import java.util.stream.Collectors;
4244
import org.apache.commons.exec.CommandLine;
4345
import org.apache.commons.exec.DefaultExecutor;
@@ -47,6 +49,7 @@
4749
import org.apache.commons.exec.LogOutputStream;
4850
import org.apache.commons.exec.PumpStreamHandler;
4951
import org.assertj.core.util.Lists;
52+
import org.assertj.core.util.VisibleForTesting;
5053
import org.slf4j.Logger;
5154
import org.slf4j.LoggerFactory;
5255

@@ -111,6 +114,9 @@ public class CcmBridge implements AutoCloseable {
111114
private static final Version V3_0_15 = Version.parse("3.0.15");
112115
private static final Version V2_1_19 = Version.parse("2.1.19");
113116

117+
private static final Pattern JVM_VERSION_PATTERN =
118+
Pattern.compile("^(1\\.)?(?<major>[0-9]+)\\..*$");
119+
114120
static {
115121
if (DSE_ENABLEMENT) {
116122
LOG.info("CCM Bridge configured with DSE version {}", VERSION);
@@ -430,17 +436,18 @@ private static File createTempStore(String storePath) {
430436
*
431437
* @return major version of current JVM
432438
*/
433-
private static int getCurrentJvmMajorVersion() {
434-
String version = System.getProperty("java.version");
435-
if (version.startsWith("1.")) {
436-
version = version.substring(2, 3);
437-
} else {
438-
int dot = version.indexOf(".");
439-
if (dot != -1) {
440-
version = version.substring(0, dot);
441-
}
439+
@VisibleForTesting
440+
static int getCurrentJvmMajorVersion() {
441+
return getJvmMajorVersion(System.getProperty("java.version"));
442+
}
443+
444+
@VisibleForTesting
445+
static int getJvmMajorVersion(String version) {
446+
Matcher matcher = version != null ? JVM_VERSION_PATTERN.matcher(version) : null;
447+
if (matcher == null || !matcher.matches() || matcher.group("major") == null) {
448+
throw new IllegalStateException("Unable to parse JVM version: " + version);
442449
}
443-
return Integer.parseInt(version);
450+
return Integer.parseInt(matcher.group("major"));
444451
}
445452

446453
private Optional<Integer> overrideJvmVersionForDseWorkloads() {
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package com.datastax.oss.driver.api.testinfra.ccm;
19+
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
import static org.assertj.core.api.Assertions.assertThatCode;
22+
23+
import org.junit.Test;
24+
25+
public class CcmBridgeTest {
26+
27+
@Test
28+
public void should_parse_system_jvm() {
29+
assertThat(CcmBridge.getCurrentJvmMajorVersion()).isGreaterThan(0);
30+
}
31+
32+
@Test
33+
public void should_parse_jvm_8_like() {
34+
assertThat(CcmBridge.getJvmMajorVersion("1.8.0_181")).isEqualTo(8);
35+
}
36+
37+
@Test
38+
public void should_parse_jvm_8_like_with_trailing_alphabetic() {
39+
assertThat(CcmBridge.getJvmMajorVersion("1.8.0_181_b1")).isEqualTo(8);
40+
}
41+
42+
@Test
43+
public void should_parse_jvm_11_like() {
44+
assertThat(CcmBridge.getJvmMajorVersion("11.0.20.1")).isEqualTo(11);
45+
}
46+
47+
@Test
48+
public void should_parse_jvm_17_like() {
49+
assertThat(CcmBridge.getJvmMajorVersion("17.0.8.1")).isEqualTo(17);
50+
}
51+
52+
@Test
53+
public void should_parse_jvm_21_like() {
54+
assertThat(CcmBridge.getJvmMajorVersion("21.0.1")).isEqualTo(21);
55+
}
56+
57+
@Test
58+
public void should_fail_null_version() {
59+
assertThatCode(() -> CcmBridge.getJvmMajorVersion(null))
60+
.isInstanceOf(IllegalStateException.class);
61+
}
62+
63+
@Test
64+
public void should_fail_empty_version() {
65+
assertThatCode(() -> CcmBridge.getJvmMajorVersion(""))
66+
.isInstanceOf(IllegalStateException.class);
67+
}
68+
69+
@Test
70+
public void should_fail_non_number() {
71+
assertThatCode(() -> CcmBridge.getJvmMajorVersion("abc"))
72+
.isInstanceOf(IllegalStateException.class);
73+
}
74+
75+
@Test
76+
public void should_fail_not_versioned() {
77+
assertThatCode(() -> CcmBridge.getJvmMajorVersion("8"))
78+
.isInstanceOf(IllegalStateException.class);
79+
}
80+
}

0 commit comments

Comments
 (0)