13 |
|
volatile Integer x = null; |
14 |
|
Object z; |
15 |
|
Integer w; |
16 |
+ |
volatile int i; |
17 |
|
|
18 |
|
public static void main(String[] args) { |
19 |
|
junit.textui.TestRunner.run(suite()); |
22 |
|
return new TestSuite(AtomicReferenceFieldUpdaterTest.class); |
23 |
|
} |
24 |
|
|
25 |
+ |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> updaterFor(String fieldName) { |
26 |
+ |
return AtomicReferenceFieldUpdater.newUpdater |
27 |
+ |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, fieldName); |
28 |
+ |
} |
29 |
+ |
|
30 |
|
/** |
31 |
|
* Construction with non-existent field throws RuntimeException |
32 |
|
*/ |
33 |
|
public void testConstructor() { |
34 |
|
try { |
35 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
30 |
< |
a = AtomicReferenceFieldUpdater.newUpdater |
31 |
< |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "y"); |
35 |
> |
updaterFor("y"); |
36 |
|
shouldThrow(); |
37 |
< |
} catch (RuntimeException success) {} |
37 |
> |
} catch (RuntimeException success) { |
38 |
> |
assertNotNull(success.getCause()); |
39 |
> |
} |
40 |
|
} |
41 |
|
|
42 |
|
/** |
43 |
< |
* construction with field not of given type throws RuntimeException |
43 |
> |
* construction with field not of given type throws ClassCastException |
44 |
|
*/ |
45 |
|
public void testConstructor2() { |
46 |
|
try { |
47 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
42 |
< |
a = AtomicReferenceFieldUpdater.newUpdater |
43 |
< |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "z"); |
47 |
> |
updaterFor("z"); |
48 |
|
shouldThrow(); |
49 |
< |
} catch (RuntimeException success) {} |
49 |
> |
} catch (ClassCastException success) {} |
50 |
|
} |
51 |
|
|
52 |
|
/** |
53 |
< |
* Constructor with non-volatile field throws exception |
53 |
> |
* Constructor with non-volatile field throws IllegalArgumentException |
54 |
|
*/ |
55 |
|
public void testConstructor3() { |
56 |
|
try { |
57 |
< |
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> |
54 |
< |
a = AtomicReferenceFieldUpdater.newUpdater |
55 |
< |
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "w"); |
57 |
> |
updaterFor("w"); |
58 |
|
shouldThrow(); |
59 |
< |
} catch (RuntimeException success) {} |
59 |
> |
} catch (IllegalArgumentException success) {} |
60 |
> |
} |
61 |
> |
|
62 |
> |
/** |
63 |
> |
* Constructor with non-reference field throws ClassCastException |
64 |
> |
*/ |
65 |
> |
public void testConstructor4() { |
66 |
> |
try { |
67 |
> |
updaterFor("i"); |
68 |
> |
shouldThrow(); |
69 |
> |
} catch (ClassCastException success) {} |
70 |
|
} |
71 |
|
|
72 |
|
/** |
74 |
|
*/ |
75 |
|
public void testGetSet() { |
76 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
77 |
< |
try { |
66 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
67 |
< |
} catch (RuntimeException ok) { |
68 |
< |
return; |
69 |
< |
} |
77 |
> |
a = updaterFor("x"); |
78 |
|
x = one; |
79 |
|
assertSame(one, a.get(this)); |
80 |
|
a.set(this, two); |
88 |
|
*/ |
89 |
|
public void testGetLazySet() { |
90 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
91 |
< |
try { |
84 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
85 |
< |
} catch (RuntimeException ok) { |
86 |
< |
return; |
87 |
< |
} |
91 |
> |
a = updaterFor("x"); |
92 |
|
x = one; |
93 |
|
assertSame(one, a.get(this)); |
94 |
|
a.lazySet(this, two); |
102 |
|
*/ |
103 |
|
public void testCompareAndSet() { |
104 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
105 |
< |
try { |
102 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
103 |
< |
} catch (RuntimeException ok) { |
104 |
< |
return; |
105 |
< |
} |
105 |
> |
a = updaterFor("x"); |
106 |
|
x = one; |
107 |
|
assertTrue(a.compareAndSet(this, one, two)); |
108 |
|
assertTrue(a.compareAndSet(this, two, m4)); |
120 |
|
public void testCompareAndSetInMultipleThreads() throws Exception { |
121 |
|
x = one; |
122 |
|
final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
123 |
< |
try { |
124 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
125 |
< |
} catch (RuntimeException ok) { |
126 |
< |
return; |
127 |
< |
} |
123 |
> |
a = updaterFor("x"); |
124 |
|
|
125 |
|
Thread t = new Thread(new CheckedRunnable() { |
126 |
|
public void realRun() { |
141 |
|
*/ |
142 |
|
public void testWeakCompareAndSet() { |
143 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
144 |
< |
try { |
149 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
150 |
< |
} catch (RuntimeException ok) { |
151 |
< |
return; |
152 |
< |
} |
144 |
> |
a = updaterFor("x"); |
145 |
|
x = one; |
146 |
|
while (!a.weakCompareAndSet(this, one, two)); |
147 |
|
while (!a.weakCompareAndSet(this, two, m4)); |
155 |
|
*/ |
156 |
|
public void testGetAndSet() { |
157 |
|
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a; |
158 |
< |
try { |
167 |
< |
a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); |
168 |
< |
} catch (RuntimeException ok) { |
169 |
< |
return; |
170 |
< |
} |
158 |
> |
a = updaterFor("x"); |
159 |
|
x = one; |
160 |
|
assertSame(one, a.getAndSet(this, zero)); |
161 |
|
assertSame(zero, a.getAndSet(this, m10)); |