1 /*
2 * Copyright (c) 2026, 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 Basic test of Object methods on value objects
27 * @enablePreview
28 * @library /test/lib
29 * @run junit ${test.main.class}
30 */
31
32 import java.util.concurrent.CountDownLatch;
33 import java.util.concurrent.TimeUnit;
34
35 import org.junit.jupiter.api.Test;
36 import org.junit.jupiter.params.provider.MethodSource;
37 import static org.junit.jupiter.api.Assertions.*;
38
39 import jdk.test.lib.Utils;
40
41 class ValueObjects {
42
43 /**
44 * Test the Object.clone method on a value object. It should return an object that
45 * is indistinguishable from the original.
46 */
47 @Test
48 void testClone() throws Exception {
49 value class V implements Cloneable {
50 @Override
51 protected V clone() throws CloneNotSupportedException {
52 return (V) super.clone();
53 }
54 }
55
56 var obj = new V();
57 assertSame(obj, obj.clone());
58 }
59
60 /**
61 * Test the Object.clone method on a value object when the value class does
62 * not implement Cloneable.
63 */
64 @Test
65 void testCloneNotSupportedException() throws Exception {
66 value class V {
67 @Override
68 protected V clone() throws CloneNotSupportedException {
69 return (V) super.clone();
70 }
71 }
72
73 var obj = new V();
74 assertThrows(CloneNotSupportedException.class, obj::clone);
75 }
76
77 /**
78 * Test that the finalize method on a value class is not invoked by the GC.
79 */
80 @Test
81 void testValueClassFinalize() throws Exception {
82 value class V {
83 CountDownLatch latch;
84 V(CountDownLatch latch) {
85 this.latch = latch;
86 }
87 @Override
88 protected void finalize() {
89 latch.countDown();
90 }
91 }
92
93 var latch = new TimeoutAdjustingLatch();
94 var obj = new V(latch);
95 obj = null;
96 for (int i = 0; i < 3; i++) {
97 System.gc();
98 // latch should not count down
99 assertFalse(latch.await(100, TimeUnit.MILLISECONDS));
100 }
101 }
102
103 /**
104 * Test that the finalize method on an abstract value value is not invoked by the GC.
105 */
106 @Test
107 void testAbstractValueClassFinalize() throws Exception {
108 abstract value class AV {
109 CountDownLatch latch;
110 AV(CountDownLatch latch) {
111 this.latch = latch;
112 }
113 @Override
114 protected void finalize() {
115 latch.countDown();
116 }
117 }
118 /*identity*/ class C extends AV {
119 C(CountDownLatch latch) {
120 super(latch);
121 }
122 }
123
124 var latch = new TimeoutAdjustingLatch();
125 var obj = new C(latch);
126 obj = null;
127 for (int i = 0; i < 3; i++) {
128 System.gc();
129 // latch should not count down
130 assertFalse(latch.await(100, TimeUnit.MILLISECONDS));
131 }
132 }
133
134 /**
135 * Test that the wait/notify methods on a value object throw IMSE.
136 */
137 @Test
138 void testWaitNotify() {
139 value class V {}
140 Object obj = new V();
141 assertThrows(IllegalMonitorStateException.class, () -> obj.wait());
142 assertThrows(IllegalMonitorStateException.class, () -> obj.wait(1000));
143 assertThrows(IllegalMonitorStateException.class, () -> obj.wait(1000, 10));
144 assertThrows(IllegalMonitorStateException.class, () -> obj.notify());
145 assertThrows(IllegalMonitorStateException.class, () -> obj.notifyAll());
146 }
147
148 /**
149 * Test default toString method.
150 */
151 @Test
152 void testToString() {
153 value class V { }
154 var obj = new V();
155 String expected = V.class.getName() + "@" + Integer.toHexString(obj.hashCode());
156 assertEquals(expected, obj.toString());
157 }
158
159 /**
160 * A CountDownLatch that is created with a count of 1 and with a timed-await method
161 * that adjusts the timeout based on the jtreg timeout factory configuration.
162 */
163 private static class TimeoutAdjustingLatch extends CountDownLatch {
164 TimeoutAdjustingLatch() {
165 super(1);
166 }
167 @Override
168 public boolean await(long timeout, TimeUnit unit) throws InterruptedException {
169 return super.await(Utils.adjustTimeout(timeout), unit);
170 }
171 }
172 }