ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/NonNestmates.java
Revision: 1.2
Committed: Wed Sep 20 15:47:41 2017 UTC (6 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.1: +3 -3 lines
Log Message:
8187607: [Testbug] Atomic*FieldUpdaterTest.checkPrivateAccess uses nested classes

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