1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
10 |
< |
import java.util.concurrent.atomic.*; |
11 |
< |
import java.io.*; |
9 |
> |
import java.util.concurrent.atomic.AtomicReference; |
10 |
> |
|
11 |
> |
import junit.framework.Test; |
12 |
> |
import junit.framework.TestSuite; |
13 |
|
|
14 |
|
public class AtomicReferenceTest extends JSR166TestCase { |
15 |
< |
public static void main (String[] args) { |
16 |
< |
junit.textui.TestRunner.run (suite()); |
15 |
> |
public static void main(String[] args) { |
16 |
> |
junit.textui.TestRunner.run(suite()); |
17 |
|
} |
18 |
|
public static Test suite() { |
19 |
|
return new TestSuite(AtomicReferenceTest.class); |
24 |
|
*/ |
25 |
|
public void testConstructor() { |
26 |
|
AtomicReference ai = new AtomicReference(one); |
27 |
< |
assertSame(one,ai.get()); |
27 |
> |
assertSame(one, ai.get()); |
28 |
|
} |
29 |
|
|
30 |
|
/** |
40 |
|
*/ |
41 |
|
public void testGetSet() { |
42 |
|
AtomicReference ai = new AtomicReference(one); |
43 |
< |
assertSame(one,ai.get()); |
43 |
> |
assertSame(one, ai.get()); |
44 |
|
ai.set(two); |
45 |
< |
assertSame(two,ai.get()); |
45 |
> |
assertSame(two, ai.get()); |
46 |
|
ai.set(m3); |
47 |
< |
assertSame(m3,ai.get()); |
47 |
> |
assertSame(m3, ai.get()); |
48 |
|
} |
49 |
|
|
50 |
|
/** |
52 |
|
*/ |
53 |
|
public void testGetLazySet() { |
54 |
|
AtomicReference ai = new AtomicReference(one); |
55 |
< |
assertSame(one,ai.get()); |
55 |
> |
assertSame(one, ai.get()); |
56 |
|
ai.lazySet(two); |
57 |
< |
assertSame(two,ai.get()); |
57 |
> |
assertSame(two, ai.get()); |
58 |
|
ai.lazySet(m3); |
59 |
< |
assertSame(m3,ai.get()); |
59 |
> |
assertSame(m3, ai.get()); |
60 |
|
} |
61 |
|
|
62 |
|
/** |
64 |
|
*/ |
65 |
|
public void testCompareAndSet() { |
66 |
|
AtomicReference ai = new AtomicReference(one); |
67 |
< |
assertTrue(ai.compareAndSet(one,two)); |
68 |
< |
assertTrue(ai.compareAndSet(two,m4)); |
69 |
< |
assertSame(m4,ai.get()); |
70 |
< |
assertFalse(ai.compareAndSet(m5,seven)); |
71 |
< |
assertSame(m4,ai.get()); |
72 |
< |
assertTrue(ai.compareAndSet(m4,seven)); |
73 |
< |
assertSame(seven,ai.get()); |
67 |
> |
assertTrue(ai.compareAndSet(one, two)); |
68 |
> |
assertTrue(ai.compareAndSet(two, m4)); |
69 |
> |
assertSame(m4, ai.get()); |
70 |
> |
assertFalse(ai.compareAndSet(m5, seven)); |
71 |
> |
assertSame(m4, ai.get()); |
72 |
> |
assertTrue(ai.compareAndSet(m4, seven)); |
73 |
> |
assertSame(seven, ai.get()); |
74 |
|
} |
75 |
|
|
76 |
|
/** |
89 |
|
assertTrue(ai.compareAndSet(one, two)); |
90 |
|
t.join(LONG_DELAY_MS); |
91 |
|
assertFalse(t.isAlive()); |
92 |
< |
assertSame(ai.get(), three); |
92 |
> |
assertSame(three, ai.get()); |
93 |
|
} |
94 |
|
|
95 |
|
/** |
98 |
|
*/ |
99 |
|
public void testWeakCompareAndSet() { |
100 |
|
AtomicReference ai = new AtomicReference(one); |
101 |
< |
while (!ai.weakCompareAndSet(one,two)); |
102 |
< |
while (!ai.weakCompareAndSet(two,m4)); |
103 |
< |
assertSame(m4,ai.get()); |
104 |
< |
while (!ai.weakCompareAndSet(m4,seven)); |
105 |
< |
assertSame(seven,ai.get()); |
101 |
> |
while (!ai.weakCompareAndSet(one, two)); |
102 |
> |
while (!ai.weakCompareAndSet(two, m4)); |
103 |
> |
assertSame(m4, ai.get()); |
104 |
> |
while (!ai.weakCompareAndSet(m4, seven)); |
105 |
> |
assertSame(seven, ai.get()); |
106 |
|
} |
107 |
|
|
108 |
|
/** |
110 |
|
*/ |
111 |
|
public void testGetAndSet() { |
112 |
|
AtomicReference ai = new AtomicReference(one); |
113 |
< |
assertSame(one,ai.getAndSet(zero)); |
114 |
< |
assertSame(zero,ai.getAndSet(m10)); |
115 |
< |
assertSame(m10,ai.getAndSet(one)); |
113 |
> |
assertSame(one, ai.getAndSet(zero)); |
114 |
> |
assertSame(zero, ai.getAndSet(m10)); |
115 |
> |
assertSame(m10, ai.getAndSet(one)); |
116 |
|
} |
117 |
|
|
118 |
|
/** |
119 |
|
* a deserialized serialized atomic holds same value |
120 |
|
*/ |
121 |
|
public void testSerialization() throws Exception { |
122 |
< |
AtomicReference l = new AtomicReference(); |
123 |
< |
|
124 |
< |
l.set(one); |
125 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
126 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
127 |
< |
out.writeObject(l); |
128 |
< |
out.close(); |
129 |
< |
|
130 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
130 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
131 |
< |
AtomicReference r = (AtomicReference) in.readObject(); |
132 |
< |
assertEquals(l.get(), r.get()); |
122 |
> |
AtomicReference x = new AtomicReference(); |
123 |
> |
AtomicReference y = serialClone(x); |
124 |
> |
assertNotSame(x, y); |
125 |
> |
x.set(one); |
126 |
> |
AtomicReference z = serialClone(x); |
127 |
> |
assertNotSame(y, z); |
128 |
> |
assertEquals(one, x.get()); |
129 |
> |
assertEquals(null, y.get()); |
130 |
> |
assertEquals(one, z.get()); |
131 |
|
} |
132 |
|
|
133 |
|
/** |
135 |
|
*/ |
136 |
|
public void testToString() { |
137 |
|
AtomicReference<Integer> ai = new AtomicReference<Integer>(one); |
138 |
< |
assertEquals(ai.toString(), one.toString()); |
138 |
> |
assertEquals(one.toString(), ai.toString()); |
139 |
|
ai.set(two); |
140 |
< |
assertEquals(ai.toString(), two.toString()); |
140 |
> |
assertEquals(two.toString(), ai.toString()); |
141 |
|
} |
142 |
|
|
143 |
|
} |