1 /*
 2  * Copyright (c) 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 /*
25  * @test
26  * @summary Test jdk.internal.value.ValueClass
27  * @modules java.base/jdk.internal.misc
28  *          java.base/jdk.internal.value
29  * @run junit ValueClassTest
30  * @run junit/othervm --enable-preview ValueClassTest
31  */
32 
33 import java.util.ArrayList;
34 
35 import jdk.internal.misc.PreviewFeatures;
36 import jdk.internal.value.ValueClass;
37 import org.junit.jupiter.api.Test;
38 
39 import static org.junit.jupiter.api.Assertions.*;
40 
41 class ValueClassTest {
42     @Test
43     void testIsValueObjectCompatible() {
44         isValueObjectCompatibleCase(false, int.class, "primitive");
45         isValueObjectCompatibleCase(true, Object.class, "Object");
46         isValueObjectCompatibleCase(true, Number.class, "abstract value class");
47         isValueObjectCompatibleCase(true, Integer.class, "final value class");
48         isValueObjectCompatibleCase(false, ClassValue.class, "abstract identity class");
49         isValueObjectCompatibleCase(false, ArrayList.class, "identity class");
50         isValueObjectCompatibleCase(false, String.class, "final identity class");
51         isValueObjectCompatibleCase(true, Comparable.class, "interface");
52         isValueObjectCompatibleCase(false, int[].class, "array class");
53         isValueObjectCompatibleCase(false, Object[].class, "array class");
54         isValueObjectCompatibleCase(false, Number[].class, "array class");
55         isValueObjectCompatibleCase(false, Integer[].class, "array class");
56         isValueObjectCompatibleCase(false, ClassValue[].class, "array class");
57         isValueObjectCompatibleCase(false, ArrayList[].class, "array class");
58         isValueObjectCompatibleCase(false, String[].class, "array class");
59         isValueObjectCompatibleCase(false, Comparable[].class, "array class");
60     }
61 
62     private static void isValueObjectCompatibleCase(boolean expected, Class<?> arg, String classification) {
63         assertEquals(PreviewFeatures.isEnabled() && expected,
64                      ValueClass.isValueObjectCompatible(arg),
65                      () -> classification + ": " + arg.getTypeName());
66     }
67 
68     @Test
69     void testIsConcreteValueClass() {
70         isConcreteValueClassCase(false, int.class, "primitive");
71         isConcreteValueClassCase(false, Object.class, "Object");
72         isConcreteValueClassCase(false, Number.class, "abstract value class");
73         isConcreteValueClassCase(true, Integer.class, "final value class");
74         isConcreteValueClassCase(false, ClassValue.class, "abstract identity class");
75         isConcreteValueClassCase(false, ArrayList.class, "identity class");
76         isConcreteValueClassCase(false, String.class, "final identity class");
77         isConcreteValueClassCase(false, Comparable.class, "interface");
78         isConcreteValueClassCase(false, int[].class, "array class");
79         isConcreteValueClassCase(false, Object[].class, "array class");
80         isConcreteValueClassCase(false, Number[].class, "array class");
81         isConcreteValueClassCase(false, Integer[].class, "array class");
82         isConcreteValueClassCase(false, ClassValue[].class, "array class");
83         isConcreteValueClassCase(false, ArrayList[].class, "array class");
84         isConcreteValueClassCase(false, String[].class, "array class");
85         isConcreteValueClassCase(false, Comparable[].class, "array class");
86     }
87 
88     private static void isConcreteValueClassCase(boolean expected, Class<?> arg, String classification) {
89         assertEquals(PreviewFeatures.isEnabled() && expected,
90                      ValueClass.isConcreteValueClass(arg),
91                      () -> classification + ": " + arg.getTypeName());
92     }
93 }