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 |
+ |
a = updaterFor("protectedField"); |
43 |
+ |
protectedField = 1; |
44 |
+ |
assertTrue(a.compareAndSet(this, 1, 2)); |
45 |
+ |
assertTrue(a.compareAndSet(this, 2, -4)); |
46 |
+ |
assertEquals(-4, a.get(this)); |
47 |
+ |
assertFalse(a.compareAndSet(this, -5, 7)); |
48 |
+ |
assertEquals(-4, a.get(this)); |
49 |
+ |
assertTrue(a.compareAndSet(this, -4, 7)); |
50 |
+ |
assertEquals(7, a.get(this)); |
51 |
+ |
} |
52 |
+ |
} |
53 |
+ |
|
54 |
+ |
static class UnrelatedClass { |
55 |
+ |
public void checkPrivateAccess() { |
56 |
+ |
Exception ex = null; |
57 |
+ |
try { |
58 |
+ |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = |
59 |
+ |
AtomicLongFieldUpdater.newUpdater |
60 |
+ |
(AtomicLongFieldUpdaterTest.class, "x"); |
61 |
+ |
} catch (RuntimeException rex) { |
62 |
+ |
ex = rex; |
63 |
+ |
} |
64 |
+ |
if (ex != null) throw new Error(); |
65 |
+ |
} |
66 |
+ |
} |
67 |
+ |
|
68 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> updaterFor(String fieldName) { |
69 |
|
return AtomicLongFieldUpdater.newUpdater |
70 |
|
(AtomicLongFieldUpdaterTest.class, fieldName); |
103 |
|
} |
104 |
|
|
105 |
|
/** |
106 |
+ |
* construction using private field from subclass throws RuntimeException |
107 |
+ |
*/ |
108 |
+ |
public void testPrivateFieldInSubclass() { |
109 |
+ |
AtomicLongFieldUpdaterTestSubclass s = |
110 |
+ |
new AtomicLongFieldUpdaterTestSubclass(); |
111 |
+ |
s.checkPrivateAccess(); |
112 |
+ |
} |
113 |
+ |
|
114 |
+ |
/** |
115 |
+ |
* construction from unrelated class throws RuntimeException |
116 |
+ |
*/ |
117 |
+ |
public void testUnrelatedClassAccess() { |
118 |
+ |
UnrelatedClass s = new UnrelatedClass(); |
119 |
+ |
s.checkPrivateAccess(); |
120 |
+ |
} |
121 |
+ |
|
122 |
+ |
/** |
123 |
|
* get returns the last value set or assigned |
124 |
|
*/ |
125 |
|
public void testGetSet() { |
164 |
|
} |
165 |
|
|
166 |
|
/** |
167 |
+ |
* compareAndSet succeeds in changing protected field value if |
168 |
+ |
* equal to expected else fails |
169 |
+ |
*/ |
170 |
+ |
public void testCompareAndSetProtected() { |
171 |
+ |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
172 |
+ |
a = updaterFor("protectedField"); |
173 |
+ |
protectedField = 1; |
174 |
+ |
assertTrue(a.compareAndSet(this, 1, 2)); |
175 |
+ |
assertTrue(a.compareAndSet(this, 2, -4)); |
176 |
+ |
assertEquals(-4, a.get(this)); |
177 |
+ |
assertFalse(a.compareAndSet(this, -5, 7)); |
178 |
+ |
assertEquals(-4, a.get(this)); |
179 |
+ |
assertTrue(a.compareAndSet(this, -4, 7)); |
180 |
+ |
assertEquals(7, a.get(this)); |
181 |
+ |
} |
182 |
+ |
|
183 |
+ |
/** |
184 |
+ |
* compareAndSet succeeds in changing protected field value if |
185 |
+ |
* equal to expected else fails |
186 |
+ |
*/ |
187 |
+ |
public void testCompareAndSetProtectedInSubclass() { |
188 |
+ |
AtomicLongFieldUpdaterTestSubclass s = |
189 |
+ |
new AtomicLongFieldUpdaterTestSubclass(); |
190 |
+ |
s.checkCompareAndSetProtectedSub(); |
191 |
+ |
} |
192 |
+ |
|
193 |
+ |
/** |
194 |
|
* compareAndSet in one thread enables another waiting for value |
195 |
|
* to succeed |
196 |
|
*/ |