11/*
2- * Copyright (c) 2011, 2020 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2011, 2021 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2323
2424/*
2525 * @test
26- * @bug 7025809 8028543 6415644 8028544 8029942 8187951 8193291 8196551 8233096
26+ * @bug 7025809 8028543 6415644 8028544 8029942 8187951 8193291 8196551 8233096 8275308
2727 * @summary Test latest, latestSupported, underscore as keyword, etc.
2828 * @author Joseph D. Darcy
2929 * @modules java.compiler
@@ -45,6 +45,8 @@ public static void main(String... args) {
4545 testRestrictedKeywords ();
4646 testVar ();
4747 testYield ();
48+ testValueOfRV ();
49+ testRuntimeVersion ();
4850 }
4951
5052 private static void testLatestSupported () {
@@ -147,4 +149,61 @@ private static void check(boolean expected,
147149 " on " + version );
148150 }
149151 }
152+
153+ /**
154+ * Test that SourceVersion.valueOf() maps a Runtime.Version to a
155+ * SourceVersion properly. The SourceVersion result is only a
156+ * function of the feature() component of a Runtime.Version.
157+ */
158+ private static void testValueOfRV () {
159+ for (SourceVersion sv : SourceVersion .values ()) {
160+ if (sv == RELEASE_0 ) {
161+ continue ;
162+ } else {
163+ // Plain mapping; e.g. "17" -> RELEASE_17
164+ String featureBase = Integer .toString (sv .ordinal ());
165+ checkValueOfResult (sv , featureBase );
166+
167+ // More populated runtime version, N.N
168+ checkValueOfResult (sv , featureBase + "." + featureBase );
169+ }
170+ }
171+
172+ // Out of range test
173+ try {
174+ int latestFeature = SourceVersion .latest ().runtimeVersion ().feature ();
175+ SourceVersion .valueOf (Runtime .Version .parse (Integer .toString (latestFeature +1 )));
176+ throw new RuntimeException ("Should not reach" );
177+ } catch (IllegalArgumentException iae ) {
178+ ; // Expected
179+ }
180+ }
181+
182+ private static void checkValueOfResult (SourceVersion expected , String versionString ) {
183+ Runtime .Version rv = Runtime .Version .parse (versionString );
184+ SourceVersion result = SourceVersion .valueOf (rv );
185+ if (result != expected ) {
186+ throw new RuntimeException ("Unexpected result " + result +
187+ " of mapping Runtime.Version " + versionString +
188+ " intead of " + expected );
189+ }
190+ }
191+
192+ private static void testRuntimeVersion () {
193+ for (SourceVersion sv : SourceVersion .values ()) {
194+ Runtime .Version result = sv .runtimeVersion ();
195+ if (sv .compareTo (RELEASE_6 ) < 0 ) {
196+ if (result != null ) {
197+ throw new RuntimeException ("Unexpected result non-null " + result +
198+ " as runtime version of " + sv );
199+ }
200+ } else {
201+ Runtime .Version expected = Runtime .Version .parse (Integer .toString (sv .ordinal ()));
202+ if (!result .equals (expected )) {
203+ throw new RuntimeException ("Unexpected result " + result +
204+ " as runtime version of " + sv );
205+ }
206+ }
207+ }
208+ }
150209}
0 commit comments