6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
9 |
|
import java.util.concurrent.atomic.AtomicLongFieldUpdater; |
10 |
|
|
11 |
+ |
import junit.framework.Test; |
12 |
+ |
import junit.framework.TestSuite; |
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); |
36 |
|
try { |
37 |
|
updaterFor("y"); |
38 |
|
shouldThrow(); |
39 |
< |
} catch (RuntimeException success) {} |
39 |
> |
} catch (RuntimeException success) { |
40 |
> |
assertNotNull(success.getCause()); |
41 |
> |
} |
42 |
|
} |
43 |
|
|
44 |
|
/** |
45 |
< |
* construction with field not of given type throws RuntimeException |
45 |
> |
* construction with field not of given type throws IllegalArgumentException |
46 |
|
*/ |
47 |
|
public void testConstructor2() { |
48 |
|
try { |
49 |
|
updaterFor("z"); |
50 |
|
shouldThrow(); |
51 |
< |
} catch (RuntimeException success) {} |
51 |
> |
} catch (IllegalArgumentException success) {} |
52 |
|
} |
53 |
|
|
54 |
|
/** |
55 |
< |
* construction with non-volatile field throws RuntimeException |
55 |
> |
* construction with non-volatile field throws IllegalArgumentException |
56 |
|
*/ |
57 |
|
public void testConstructor3() { |
58 |
|
try { |
59 |
|
updaterFor("w"); |
60 |
|
shouldThrow(); |
61 |
< |
} catch (RuntimeException success) {} |
61 |
> |
} catch (IllegalArgumentException success) {} |
62 |
> |
} |
63 |
> |
|
64 |
> |
/** |
65 |
> |
* construction using private field from subclass throws RuntimeException |
66 |
> |
*/ |
67 |
> |
public void testPrivateFieldInSubclass() { |
68 |
> |
new NonNestmates.AtomicLongFieldUpdaterTestSubclass() |
69 |
> |
.checkPrivateAccess(); |
70 |
> |
} |
71 |
> |
|
72 |
> |
/** |
73 |
> |
* construction from unrelated class; package access is allowed, |
74 |
> |
* private access is not |
75 |
> |
*/ |
76 |
> |
public void testUnrelatedClassAccess() { |
77 |
> |
new NonNestmates().checkPackageAccess(this); |
78 |
> |
new NonNestmates().checkPrivateAccess(this); |
79 |
|
} |
80 |
|
|
81 |
|
/** |
123 |
|
} |
124 |
|
|
125 |
|
/** |
126 |
+ |
* compareAndSet succeeds in changing protected field value if |
127 |
+ |
* equal to expected else fails |
128 |
+ |
*/ |
129 |
+ |
public void testCompareAndSetProtected() { |
130 |
+ |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
131 |
+ |
a = updaterFor("protectedField"); |
132 |
+ |
protectedField = 1; |
133 |
+ |
assertTrue(a.compareAndSet(this, 1, 2)); |
134 |
+ |
assertTrue(a.compareAndSet(this, 2, -4)); |
135 |
+ |
assertEquals(-4, a.get(this)); |
136 |
+ |
assertFalse(a.compareAndSet(this, -5, 7)); |
137 |
+ |
assertEquals(-4, a.get(this)); |
138 |
+ |
assertTrue(a.compareAndSet(this, -4, 7)); |
139 |
+ |
assertEquals(7, a.get(this)); |
140 |
+ |
} |
141 |
+ |
|
142 |
+ |
/** |
143 |
+ |
* compareAndSet succeeds in changing protected field value if |
144 |
+ |
* equal to expected else fails |
145 |
+ |
*/ |
146 |
+ |
public void testCompareAndSetProtectedInSubclass() { |
147 |
+ |
new NonNestmates.AtomicLongFieldUpdaterTestSubclass() |
148 |
+ |
.checkCompareAndSetProtectedSub(); |
149 |
+ |
} |
150 |
+ |
|
151 |
+ |
/** |
152 |
|
* compareAndSet in one thread enables another waiting for value |
153 |
|
* to succeed |
154 |
|
*/ |
178 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
179 |
|
a = updaterFor("x"); |
180 |
|
x = 1; |
181 |
< |
while (!a.weakCompareAndSet(this, 1, 2)); |
182 |
< |
while (!a.weakCompareAndSet(this, 2, -4)); |
181 |
> |
do {} while (!a.weakCompareAndSet(this, 1, 2)); |
182 |
> |
do {} while (!a.weakCompareAndSet(this, 2, -4)); |
183 |
|
assertEquals(-4, a.get(this)); |
184 |
< |
while (!a.weakCompareAndSet(this, -4, 7)); |
184 |
> |
do {} while (!a.weakCompareAndSet(this, -4, 7)); |
185 |
|
assertEquals(7, a.get(this)); |
186 |
|
} |
187 |
|
|