1 /*
2 * Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 import static org.junit.jupiter.api.Assertions.*;
25
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.params.provider.MethodSource;
28 import org.junit.jupiter.params.ParameterizedTest;
29
30 import jdk.internal.misc.PreviewFeatures;
31
32 import java.util.Arrays;
33 import java.util.stream.Stream;
34
35 import java.time.Duration;
36 import java.time.Instant;
37 import java.time.LocalDate;
38 import java.time.LocalDateTime;
39 import java.time.LocalTime;
40 import java.time.MonthDay;
41 import java.time.OffsetDateTime;
42 import java.time.OffsetTime;
43 import java.util.Optional;
44 import java.util.OptionalDouble;
45 import java.util.OptionalInt;
46 import java.util.OptionalLong;
47 import java.time.Period;
48 import java.time.Year;
49 import java.time.YearMonth;
50 import java.time.ZonedDateTime;
51
52 /*
53 * @test
54 * @summary Test that classes are value classes or not depending on --enable-preview
55 * @modules java.base/jdk.internal.misc
56 * @run junit/othervm -Xlog --enable-preview UseValueClassTest
57 * @run junit/othervm -Xlog UseValueClassTest
58 */
59
60 public class UseValueClassTest {
61
62 // Classes to be checked
63 private static Stream<Class<?>> classProvider() {
64 Class<?>[] classes = {
65 Byte.class,
66 Short.class,
67 Integer.class,
68 Long.class,
69 Float.class,
70 Double.class,
71 Boolean.class,
72 Character.class,
73 Number.class,
74 Record.class,
75 Duration.class,
76 Instant.class,
77 LocalDate.class,
78 LocalDateTime.class,
79 LocalTime.class,
80 MonthDay.class,
81 OffsetDateTime.class,
82 OffsetTime.class,
83 Optional.class,
84 OptionalDouble.class,
85 OptionalInt.class,
86 OptionalLong.class,
87 Period.class,
88 Year.class,
89 YearMonth.class,
90 ZonedDateTime.class,
91 };
92 return Arrays.stream(classes, 0, classes.length);
93 }
94
95 /**
96 * Verify that the class is a value class if --enable-preview is true
97 * @param clazz a class
98 */
99 @ParameterizedTest
100 @MethodSource("classProvider")
101 public void testValue(Class<?> clazz) {
102 System.out.printf("isPreview: %s%n", PreviewFeatures.isEnabled());
103 assertEquals(PreviewFeatures.isEnabled(), clazz.isValue(), clazz.getName());
104 }
105 }