13 |
|
|
14 |
|
public class AtomicLongFieldUpdaterTest extends JSR166TestCase { |
15 |
|
volatile long x = 0; |
16 |
< |
int z; |
16 |
> |
protected volatile long protectedField; |
17 |
> |
private volatile long privateField; |
18 |
|
long w; |
19 |
< |
|
19 |
> |
float z; |
20 |
|
public static void main(String[] args) { |
21 |
< |
junit.textui.TestRunner.run(suite()); |
21 |
> |
main(suite(), args); |
22 |
|
} |
23 |
|
public static Test suite() { |
24 |
|
return new TestSuite(AtomicLongFieldUpdaterTest.class); |
25 |
|
} |
26 |
|
|
27 |
+ |
// for testing subclass access |
28 |
+ |
static class AtomicLongFieldUpdaterTestSubclass extends AtomicLongFieldUpdaterTest { |
29 |
+ |
public void checkPrivateAccess() { |
30 |
+ |
try { |
31 |
+ |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = |
32 |
+ |
AtomicLongFieldUpdater.newUpdater |
33 |
+ |
(AtomicLongFieldUpdaterTest.class, "privateField"); |
34 |
+ |
shouldThrow(); |
35 |
+ |
} catch (RuntimeException success) { |
36 |
+ |
assertNotNull(success.getCause()); |
37 |
+ |
} |
38 |
+ |
} |
39 |
+ |
|
40 |
+ |
public void checkCompareAndSetProtectedSub() { |
41 |
+ |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = |
42 |
+ |
AtomicLongFieldUpdater.newUpdater |
43 |
+ |
(AtomicLongFieldUpdaterTest.class, "protectedField"); |
44 |
+ |
this.protectedField = 1; |
45 |
+ |
assertTrue(a.compareAndSet(this, 1, 2)); |
46 |
+ |
assertTrue(a.compareAndSet(this, 2, -4)); |
47 |
+ |
assertEquals(-4, a.get(this)); |
48 |
+ |
assertFalse(a.compareAndSet(this, -5, 7)); |
49 |
+ |
assertEquals(-4, a.get(this)); |
50 |
+ |
assertTrue(a.compareAndSet(this, -4, 7)); |
51 |
+ |
assertEquals(7, a.get(this)); |
52 |
+ |
} |
53 |
+ |
} |
54 |
+ |
|
55 |
+ |
static class UnrelatedClass { |
56 |
+ |
public void checkPackageAccess(AtomicLongFieldUpdaterTest obj) { |
57 |
+ |
obj.x = 72L; |
58 |
+ |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = |
59 |
+ |
AtomicLongFieldUpdater.newUpdater |
60 |
+ |
(AtomicLongFieldUpdaterTest.class, "x"); |
61 |
+ |
assertEquals(72L, a.get(obj)); |
62 |
+ |
assertTrue(a.compareAndSet(obj, 72L, 73L)); |
63 |
+ |
assertEquals(73L, a.get(obj)); |
64 |
+ |
} |
65 |
+ |
|
66 |
+ |
public void checkPrivateAccess(AtomicLongFieldUpdaterTest obj) { |
67 |
+ |
try { |
68 |
+ |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = |
69 |
+ |
AtomicLongFieldUpdater.newUpdater |
70 |
+ |
(AtomicLongFieldUpdaterTest.class, "privateField"); |
71 |
+ |
throw new AssertionError("should throw"); |
72 |
+ |
} catch (RuntimeException success) { |
73 |
+ |
assertNotNull(success.getCause()); |
74 |
+ |
} |
75 |
+ |
} |
76 |
+ |
} |
77 |
+ |
|
78 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> updaterFor(String fieldName) { |
79 |
|
return AtomicLongFieldUpdater.newUpdater |
80 |
|
(AtomicLongFieldUpdaterTest.class, fieldName); |
113 |
|
} |
114 |
|
|
115 |
|
/** |
116 |
+ |
* construction using private field from subclass throws RuntimeException |
117 |
+ |
*/ |
118 |
+ |
public void testPrivateFieldInSubclass() { |
119 |
+ |
AtomicLongFieldUpdaterTestSubclass s = |
120 |
+ |
new AtomicLongFieldUpdaterTestSubclass(); |
121 |
+ |
s.checkPrivateAccess(); |
122 |
+ |
} |
123 |
+ |
|
124 |
+ |
/** |
125 |
+ |
* construction from unrelated class; package access is allowed, |
126 |
+ |
* private access is not |
127 |
+ |
*/ |
128 |
+ |
public void testUnrelatedClassAccess() { |
129 |
+ |
new UnrelatedClass().checkPackageAccess(this); |
130 |
+ |
new UnrelatedClass().checkPrivateAccess(this); |
131 |
+ |
} |
132 |
+ |
|
133 |
+ |
/** |
134 |
|
* get returns the last value set or assigned |
135 |
|
*/ |
136 |
|
public void testGetSet() { |
175 |
|
} |
176 |
|
|
177 |
|
/** |
178 |
+ |
* compareAndSet succeeds in changing protected field value if |
179 |
+ |
* equal to expected else fails |
180 |
+ |
*/ |
181 |
+ |
public void testCompareAndSetProtected() { |
182 |
+ |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
183 |
+ |
a = updaterFor("protectedField"); |
184 |
+ |
protectedField = 1; |
185 |
+ |
assertTrue(a.compareAndSet(this, 1, 2)); |
186 |
+ |
assertTrue(a.compareAndSet(this, 2, -4)); |
187 |
+ |
assertEquals(-4, a.get(this)); |
188 |
+ |
assertFalse(a.compareAndSet(this, -5, 7)); |
189 |
+ |
assertEquals(-4, a.get(this)); |
190 |
+ |
assertTrue(a.compareAndSet(this, -4, 7)); |
191 |
+ |
assertEquals(7, a.get(this)); |
192 |
+ |
} |
193 |
+ |
|
194 |
+ |
/** |
195 |
+ |
* compareAndSet succeeds in changing protected field value if |
196 |
+ |
* equal to expected else fails |
197 |
+ |
*/ |
198 |
+ |
public void testCompareAndSetProtectedInSubclass() { |
199 |
+ |
AtomicLongFieldUpdaterTestSubclass s = |
200 |
+ |
new AtomicLongFieldUpdaterTestSubclass(); |
201 |
+ |
s.checkCompareAndSetProtectedSub(); |
202 |
+ |
} |
203 |
+ |
|
204 |
+ |
/** |
205 |
|
* compareAndSet in one thread enables another waiting for value |
206 |
|
* to succeed |
207 |
|
*/ |
231 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
232 |
|
a = updaterFor("x"); |
233 |
|
x = 1; |
234 |
< |
while (!a.weakCompareAndSet(this, 1, 2)); |
235 |
< |
while (!a.weakCompareAndSet(this, 2, -4)); |
234 |
> |
do {} while (!a.weakCompareAndSet(this, 1, 2)); |
235 |
> |
do {} while (!a.weakCompareAndSet(this, 2, -4)); |
236 |
|
assertEquals(-4, a.get(this)); |
237 |
< |
while (!a.weakCompareAndSet(this, -4, 7)); |
237 |
> |
do {} while (!a.weakCompareAndSet(this, -4, 7)); |
238 |
|
assertEquals(7, a.get(this)); |
239 |
|
} |
240 |
|
|