ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/NonNestmates.java
Revision: 1.4
Committed: Tue Jan 26 13:33:06 2021 UTC (3 years, 3 months ago) by dl
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +10 -10 lines
Log Message:
Replace Integer with Item class

File Contents

# Content
1 /*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/publicdomain/zero/1.0/
5 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 * Pat Fisher, Mike Judd.
7 */
8
9 import static org.junit.Assert.assertEquals;
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertSame;
12 import static org.junit.Assert.assertTrue;
13
14 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
15 import java.util.concurrent.atomic.AtomicLongFieldUpdater;
16 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
17
18 /**
19 * This source file contains test code deliberately not contained in
20 * the same source file as the tests that use them, to avoid making
21 * them nestmates, which affects accessibility rules (see JEP 181).
22 */
23 class NonNestmates {
24
25 public void checkPackageAccess(AtomicIntegerFieldUpdaterTest obj) {
26 obj.x = 72;
27 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a =
28 AtomicIntegerFieldUpdater.newUpdater(
29 AtomicIntegerFieldUpdaterTest.class, "x");
30 assertEquals(72, a.get(obj));
31 assertTrue(a.compareAndSet(obj, 72, 73));
32 assertEquals(73, a.get(obj));
33 }
34
35 public void checkPackageAccess(AtomicLongFieldUpdaterTest obj) {
36 obj.x = 72L;
37 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a =
38 AtomicLongFieldUpdater.newUpdater(
39 AtomicLongFieldUpdaterTest.class, "x");
40 assertEquals(72L, a.get(obj));
41 assertTrue(a.compareAndSet(obj, 72L, 73L));
42 assertEquals(73L, a.get(obj));
43 }
44
45 public void checkPackageAccess(AtomicReferenceFieldUpdaterTest obj) {
46 Item one = new Item(1);
47 Item two = new Item(2);
48 obj.x = one;
49 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Item> a =
50 AtomicReferenceFieldUpdater.newUpdater(
51 AtomicReferenceFieldUpdaterTest.class, Item.class, "x");
52 assertSame(one, a.get(obj));
53 assertTrue(a.compareAndSet(obj, one, two));
54 assertSame(two, a.get(obj));
55 }
56
57 public void checkPrivateAccess(AtomicIntegerFieldUpdaterTest obj) {
58 try {
59 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a =
60 AtomicIntegerFieldUpdater.newUpdater(
61 AtomicIntegerFieldUpdaterTest.class, "privateField");
62 throw new AssertionError("should throw");
63 } catch (RuntimeException success) {
64 assertNotNull(success.getCause());
65 }
66 }
67
68 public void checkPrivateAccess(AtomicLongFieldUpdaterTest obj) {
69 try {
70 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a =
71 AtomicLongFieldUpdater.newUpdater(
72 AtomicLongFieldUpdaterTest.class, "privateField");
73 throw new AssertionError("should throw");
74 } catch (RuntimeException success) {
75 assertNotNull(success.getCause());
76 }
77 }
78
79 public void checkPrivateAccess(AtomicReferenceFieldUpdaterTest obj) {
80 try {
81 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a =
82 AtomicReferenceFieldUpdater.newUpdater(
83 AtomicReferenceFieldUpdaterTest.class,
84 Integer.class, "privateField");
85 throw new AssertionError("should throw");
86 } catch (RuntimeException success) {
87 assertNotNull(success.getCause());
88 }
89 }
90
91 static class AtomicIntegerFieldUpdaterTestSubclass
92 extends AtomicIntegerFieldUpdaterTest {
93
94 public void checkPrivateAccess() {
95 try {
96 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a =
97 AtomicIntegerFieldUpdater.newUpdater(
98 AtomicIntegerFieldUpdaterTest.class, "privateField");
99 shouldThrow();
100 } catch (RuntimeException success) {
101 assertNotNull(success.getCause());
102 }
103 }
104
105 public void checkCompareAndSetProtectedSub() {
106 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a =
107 AtomicIntegerFieldUpdater.newUpdater(
108 AtomicIntegerFieldUpdaterTest.class, "protectedField");
109 this.protectedField = 1;
110 assertTrue(a.compareAndSet(this, 1, 2));
111 assertTrue(a.compareAndSet(this, 2, -4));
112 assertEquals(-4, a.get(this));
113 assertFalse(a.compareAndSet(this, -5, 7));
114 assertEquals(-4, a.get(this));
115 assertTrue(a.compareAndSet(this, -4, 7));
116 assertEquals(7, a.get(this));
117 }
118 }
119
120 static class AtomicLongFieldUpdaterTestSubclass
121 extends AtomicLongFieldUpdaterTest {
122
123 public void checkPrivateAccess() {
124 try {
125 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a =
126 AtomicLongFieldUpdater.newUpdater(
127 AtomicLongFieldUpdaterTest.class, "privateField");
128 shouldThrow();
129 } catch (RuntimeException success) {
130 assertNotNull(success.getCause());
131 }
132 }
133
134 public void checkCompareAndSetProtectedSub() {
135 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a =
136 AtomicLongFieldUpdater.newUpdater(
137 AtomicLongFieldUpdaterTest.class, "protectedField");
138 this.protectedField = 1;
139 assertTrue(a.compareAndSet(this, 1, 2));
140 assertTrue(a.compareAndSet(this, 2, -4));
141 assertEquals(-4, a.get(this));
142 assertFalse(a.compareAndSet(this, -5, 7));
143 assertEquals(-4, a.get(this));
144 assertTrue(a.compareAndSet(this, -4, 7));
145 assertEquals(7, a.get(this));
146 }
147 }
148
149 static class AtomicReferenceFieldUpdaterTestSubclass
150 extends AtomicReferenceFieldUpdaterTest {
151
152 public void checkPrivateAccess() {
153 try {
154 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a =
155 AtomicReferenceFieldUpdater.newUpdater(
156 AtomicReferenceFieldUpdaterTest.class,
157 Integer.class, "privateField");
158 shouldThrow();
159 } catch (RuntimeException success) {
160 assertNotNull(success.getCause());
161 }
162 }
163
164 public void checkCompareAndSetProtectedSub() {
165 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Item> a =
166 AtomicReferenceFieldUpdater.newUpdater(
167 AtomicReferenceFieldUpdaterTest.class,
168 Item.class, "protectedField");
169 this.protectedField = one;
170 assertTrue(a.compareAndSet(this, one, two));
171 assertTrue(a.compareAndSet(this, two, minusFour));
172 assertSame(minusFour, a.get(this));
173 assertFalse(a.compareAndSet(this, minusFive, seven));
174 assertNotSame(seven, a.get(this));
175 assertTrue(a.compareAndSet(this, minusFour, seven));
176 assertSame(seven, a.get(this));
177 }
178 }
179 }