20 |
|
return new TestSuite(AtomicIntegerFieldUpdaterTest.class); |
21 |
|
} |
22 |
|
|
23 |
+ |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> updaterFor(String fieldName) { |
24 |
+ |
return AtomicIntegerFieldUpdater.newUpdater |
25 |
+ |
(AtomicIntegerFieldUpdaterTest.class, fieldName); |
26 |
+ |
} |
27 |
+ |
|
28 |
|
/** |
29 |
|
* Construction with non-existent field throws RuntimeException |
30 |
|
*/ |
31 |
|
public void testConstructor() { |
32 |
|
try { |
33 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
29 |
< |
a = AtomicIntegerFieldUpdater.newUpdater |
30 |
< |
(AtomicIntegerFieldUpdaterTest.class, "y"); |
33 |
> |
updaterFor("y"); |
34 |
|
shouldThrow(); |
35 |
< |
} catch (RuntimeException success) {} |
35 |
> |
} catch (RuntimeException success) { |
36 |
> |
assertNotNull(success.getCause()); |
37 |
> |
} |
38 |
|
} |
39 |
|
|
40 |
|
/** |
41 |
< |
* construction with field not of given type throws RuntimeException |
41 |
> |
* construction with field not of given type throws IllegalArgumentException |
42 |
|
*/ |
43 |
|
public void testConstructor2() { |
44 |
|
try { |
45 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
41 |
< |
a = AtomicIntegerFieldUpdater.newUpdater |
42 |
< |
(AtomicIntegerFieldUpdaterTest.class, "z"); |
45 |
> |
updaterFor("z"); |
46 |
|
shouldThrow(); |
47 |
< |
} catch (RuntimeException success) {} |
47 |
> |
} catch (IllegalArgumentException success) {} |
48 |
|
} |
49 |
|
|
50 |
|
/** |
51 |
< |
* construction with non-volatile field throws RuntimeException |
51 |
> |
* construction with non-volatile field throws IllegalArgumentException |
52 |
|
*/ |
53 |
|
public void testConstructor3() { |
54 |
|
try { |
55 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
53 |
< |
a = AtomicIntegerFieldUpdater.newUpdater |
54 |
< |
(AtomicIntegerFieldUpdaterTest.class, "w"); |
55 |
> |
updaterFor("w"); |
56 |
|
shouldThrow(); |
57 |
< |
} catch (RuntimeException success) {} |
57 |
> |
} catch (IllegalArgumentException success) {} |
58 |
|
} |
59 |
|
|
60 |
|
/** |
62 |
|
*/ |
63 |
|
public void testGetSet() { |
64 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
65 |
< |
try { |
65 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
66 |
< |
} catch (RuntimeException ok) { |
67 |
< |
return; |
68 |
< |
} |
65 |
> |
a = updaterFor("x"); |
66 |
|
x = 1; |
67 |
|
assertEquals(1, a.get(this)); |
68 |
|
a.set(this, 2); |
76 |
|
*/ |
77 |
|
public void testGetLazySet() { |
78 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
79 |
< |
try { |
83 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
84 |
< |
} catch (RuntimeException ok) { |
85 |
< |
return; |
86 |
< |
} |
79 |
> |
a = updaterFor("x"); |
80 |
|
x = 1; |
81 |
|
assertEquals(1, a.get(this)); |
82 |
|
a.lazySet(this, 2); |
90 |
|
*/ |
91 |
|
public void testCompareAndSet() { |
92 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
93 |
< |
try { |
101 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
102 |
< |
} catch (RuntimeException ok) { |
103 |
< |
return; |
104 |
< |
} |
93 |
> |
a = updaterFor("x"); |
94 |
|
x = 1; |
95 |
|
assertTrue(a.compareAndSet(this, 1, 2)); |
96 |
|
assertTrue(a.compareAndSet(this, 2, -4)); |
107 |
|
*/ |
108 |
|
public void testCompareAndSetInMultipleThreads() throws Exception { |
109 |
|
x = 1; |
110 |
< |
final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>a; |
111 |
< |
try { |
123 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
124 |
< |
} catch (RuntimeException ok) { |
125 |
< |
return; |
126 |
< |
} |
110 |
> |
final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
111 |
> |
a = updaterFor("x"); |
112 |
|
|
113 |
|
Thread t = new Thread(new CheckedRunnable() { |
114 |
|
public void realRun() { |
120 |
|
assertTrue(a.compareAndSet(this, 1, 2)); |
121 |
|
t.join(LONG_DELAY_MS); |
122 |
|
assertFalse(t.isAlive()); |
123 |
< |
assertEquals(a.get(this), 3); |
123 |
> |
assertEquals(3, a.get(this)); |
124 |
|
} |
125 |
|
|
126 |
|
/** |
129 |
|
*/ |
130 |
|
public void testWeakCompareAndSet() { |
131 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
132 |
< |
try { |
148 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
149 |
< |
} catch (RuntimeException ok) { |
150 |
< |
return; |
151 |
< |
} |
132 |
> |
a = updaterFor("x"); |
133 |
|
x = 1; |
134 |
|
while (!a.weakCompareAndSet(this, 1, 2)); |
135 |
|
while (!a.weakCompareAndSet(this, 2, -4)); |
143 |
|
*/ |
144 |
|
public void testGetAndSet() { |
145 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
146 |
< |
try { |
166 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
167 |
< |
} catch (RuntimeException ok) { |
168 |
< |
return; |
169 |
< |
} |
146 |
> |
a = updaterFor("x"); |
147 |
|
x = 1; |
148 |
|
assertEquals(1, a.getAndSet(this, 0)); |
149 |
|
assertEquals(0, a.getAndSet(this, -10)); |
155 |
|
*/ |
156 |
|
public void testGetAndAdd() { |
157 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
158 |
< |
try { |
182 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
183 |
< |
} catch (RuntimeException ok) { |
184 |
< |
return; |
185 |
< |
} |
158 |
> |
a = updaterFor("x"); |
159 |
|
x = 1; |
160 |
|
assertEquals(1, a.getAndAdd(this, 2)); |
161 |
|
assertEquals(3, a.get(this)); |
168 |
|
*/ |
169 |
|
public void testGetAndDecrement() { |
170 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
171 |
< |
try { |
199 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
200 |
< |
} catch (RuntimeException ok) { |
201 |
< |
return; |
202 |
< |
} |
171 |
> |
a = updaterFor("x"); |
172 |
|
x = 1; |
173 |
|
assertEquals(1, a.getAndDecrement(this)); |
174 |
|
assertEquals(0, a.getAndDecrement(this)); |
180 |
|
*/ |
181 |
|
public void testGetAndIncrement() { |
182 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
183 |
< |
try { |
215 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
216 |
< |
} catch (RuntimeException ok) { |
217 |
< |
return; |
218 |
< |
} |
183 |
> |
a = updaterFor("x"); |
184 |
|
x = 1; |
185 |
|
assertEquals(1, a.getAndIncrement(this)); |
186 |
|
assertEquals(2, a.get(this)); |
196 |
|
*/ |
197 |
|
public void testAddAndGet() { |
198 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
199 |
< |
try { |
235 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
236 |
< |
} catch (RuntimeException ok) { |
237 |
< |
return; |
238 |
< |
} |
199 |
> |
a = updaterFor("x"); |
200 |
|
x = 1; |
201 |
|
assertEquals(3, a.addAndGet(this, 2)); |
202 |
|
assertEquals(3, a.get(this)); |
209 |
|
*/ |
210 |
|
public void testDecrementAndGet() { |
211 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
212 |
< |
try { |
252 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
253 |
< |
} catch (RuntimeException ok) { |
254 |
< |
return; |
255 |
< |
} |
212 |
> |
a = updaterFor("x"); |
213 |
|
x = 1; |
214 |
|
assertEquals(0, a.decrementAndGet(this)); |
215 |
|
assertEquals(-1, a.decrementAndGet(this)); |
222 |
|
*/ |
223 |
|
public void testIncrementAndGet() { |
224 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
225 |
< |
try { |
269 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
270 |
< |
} catch (RuntimeException ok) { |
271 |
< |
return; |
272 |
< |
} |
225 |
> |
a = updaterFor("x"); |
226 |
|
x = 1; |
227 |
|
assertEquals(2, a.incrementAndGet(this)); |
228 |
|
assertEquals(2, a.get(this)); |