Skip to content

Commit 1375eee

Browse files
authored
Fix tlschannel.util.Util.getJavaMajorVersion (#969)
This is a combination of a manual cherry-pick and squash of the following commits: - marianobarrios/tls-channel@3b5f8a0 - marianobarrios/tls-channel@f4b780e and a small fix on top of that. JAVA-4656
1 parent 5fc45e1 commit 1375eee

File tree

2 files changed

+61
-9
lines changed
  • driver-core/src
    • main/com/mongodb/internal/connection/tlschannel/util
    • test/unit/com/mongodb/internal/connection/tlschannel/util

2 files changed

+61
-9
lines changed

driver-core/src/main/com/mongodb/internal/connection/tlschannel/util/Util.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@
1919

2020
package com.mongodb.internal.connection.tlschannel.util;
2121

22+
import com.mongodb.internal.VisibleForTesting;
23+
2224
import javax.net.ssl.SSLEngineResult;
2325

26+
import static com.mongodb.internal.VisibleForTesting.AccessModifier.PRIVATE;
27+
2428
public class Util {
2529

2630
public static void assertTrue(boolean condition) {
@@ -44,15 +48,24 @@ public static String resultToString(SSLEngineResult result) {
4448
}
4549

4650
public static int getJavaMajorVersion() {
47-
String version = System.getProperty("java.version");
48-
if (version.startsWith("1.")) {
49-
version = version.substring(2, 3);
50-
} else {
51-
int dot = version.indexOf(".");
52-
if (dot != -1) {
53-
version = version.substring(0, dot);
51+
return getJavaMajorVersion(System.getProperty("java.version"));
52+
}
53+
54+
@VisibleForTesting(otherwise = PRIVATE)
55+
static int getJavaMajorVersion(final String javaVersion) {
56+
String version = javaVersion;
57+
if (version.startsWith("1.")) {
58+
version = version.substring(2);
5459
}
55-
}
56-
return Integer.parseInt(version);
60+
// Allow these formats:
61+
// 1.8.0_72-ea
62+
// 9-ea
63+
// 9
64+
// 9.0.1
65+
// 17
66+
int dotPos = version.indexOf('.');
67+
int dashPos = version.indexOf('-');
68+
return Integer.parseInt(
69+
version.substring(0, dotPos > -1 ? dotPos : dashPos > -1 ? dashPos : version.length()));
5770
}
5871
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.mongodb.internal.connection.tlschannel.util;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import static org.junit.jupiter.api.Assertions.assertAll;
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
23+
final class UtilTest {
24+
@Test
25+
void getJavaMajorVersion() {
26+
assertAll(
27+
() -> assertEquals(8, Util.getJavaMajorVersion("1.8.0_72-ea")),
28+
() -> assertEquals(9, Util.getJavaMajorVersion("9-ea")),
29+
() -> assertEquals(9, Util.getJavaMajorVersion("9")),
30+
() -> assertEquals(9, Util.getJavaMajorVersion("9.0.1")),
31+
() -> assertEquals(17, Util.getJavaMajorVersion("17")),
32+
() -> assertEquals(19, Util.getJavaMajorVersion("19-ea")),
33+
() -> assertEquals(42, Util.getJavaMajorVersion("42.1.0-ea"))
34+
);
35+
}
36+
37+
private UtilTest() {
38+
}
39+
}

0 commit comments

Comments
 (0)