+ * Parsed by hand because it is called before RegEx can be initialized safely.
+ *
+ * @param str a version string
+ * @throws IllegalArgumentException if the string does not start with digits
+ * or digits do not follow '.'
+ */
+ public static Version parse(String str) throws IllegalArgumentException {
+ int len = str.length();
+ int majorStart = 0;
+ int majorEnd = skipDigits(str, majorStart);
+ int major = Integer.parseInt(str.substring(majorStart, majorEnd));
+
+ int minor = 0, micro = 0;
+ if (majorEnd < len && str.charAt(majorEnd) == '.') {
+ int minorStart = majorEnd + 1;
+ int minorEnd = skipDigits(str, minorStart);
+ minor = Integer.parseInt(str.substring(minorStart, minorEnd));
+
+ if (minorEnd < len && str.charAt(minorEnd) == '.') {
+ int microStart = minorEnd + 1;
+ int microEnd = skipDigits(str, microStart);
+ micro = Integer.parseInt(str.substring(microStart, microEnd));
+ }
+ }
+ return new Version(major, minor, micro);
+ }
+
+ /**
+ * {@return The index of the first non-digit from start}
+ * @throws IllegalArgumentException if there are no digits
+ */
+
+ private static int skipDigits(String s, int start) {
+ int index = start;
+ while (index < s.length() && Character.isDigit(s.charAt(index))) {
+ index++;
+ }
+ if (index == start)
+ throw new IllegalArgumentException("malformed version, missing digits: " + s);
+ return index;
+ }
+}
diff --git a/test/jdk/jdk/internal/util/OSTest.java b/test/jdk/jdk/internal/util/OSTest.java
index 92a4781bb20..9bf28ce7c26 100644
--- a/test/jdk/jdk/internal/util/OSTest.java
+++ b/test/jdk/jdk/internal/util/OSTest.java
@@ -32,11 +32,14 @@ import static jdk.internal.util.OperatingSystem.LINUX;
import static jdk.internal.util.OperatingSystem.MACOS;
import static jdk.internal.util.OperatingSystem.WINDOWS;
+import jdk.internal.util.StaticProperty;
+import jdk.internal.util.Version;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
/**
@@ -83,4 +86,12 @@ public class OSTest {
assertEquals(os == current, isXXX,
"Mismatch " + os + " == " + current + " vs is" + os);
}
+
+ @Test
+ public void checkOsVersion() {
+ Version ver = OperatingSystem.version();
+ String osVersion = StaticProperty.osVersion();
+ System.err.printf("os.version: %s, version().toString(): %s%n", osVersion, ver);
+ assertTrue(osVersion.startsWith(ver.toString()), "version().toString() is not prefix of vs os.version property");
+ }
}
diff --git a/test/jdk/jdk/internal/util/VersionTest.java b/test/jdk/jdk/internal/util/VersionTest.java
new file mode 100644
index 00000000000..0b916a0463f
--- /dev/null
+++ b/test/jdk/jdk/internal/util/VersionTest.java
@@ -0,0 +1,103 @@
+/*
+ * Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+
+import java.util.stream.Stream;
+
+import jdk.internal.util.Version;
+
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.fail;
+
+/**
+ * @test
+ * @summary test jdk.internal.util.Version
+ * @modules java.base/jdk.internal.util
+ * @run junit VersionTest
+ */
+
+public class VersionTest {
+
+ private static Stream