ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicReferenceFieldUpdaterTest.java
(Generate patch)

Comparing jsr166/src/test/tck/AtomicReferenceFieldUpdaterTest.java (file contents):
Revision 1.18 by jsr166, Tue Dec 1 09:56:28 2009 UTC vs.
Revision 1.34 by jsr166, Mon Nov 9 18:42:41 2015 UTC

# Line 1 | Line 1
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 java.util.concurrent.atomic.*;
10 < import junit.framework.*;
11 < import java.util.*;
9 > import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
10 >
11 > import junit.framework.Test;
12 > import junit.framework.TestSuite;
13  
14   public class AtomicReferenceFieldUpdaterTest extends JSR166TestCase {
15      volatile Integer x = null;
16 +    protected volatile Integer protectedField;
17 +    private volatile Integer privateField;
18      Object z;
19      Integer w;
20 +    volatile int i;
21  
22      public static void main(String[] args) {
23 <        junit.textui.TestRunner.run(suite());
23 >        main(suite(), args);
24      }
25      public static Test suite() {
26          return new TestSuite(AtomicReferenceFieldUpdaterTest.class);
27      }
28  
29 +    // for testing subclass access
30 +    static class AtomicReferenceFieldUpdaterTestSubclass extends AtomicReferenceFieldUpdaterTest {
31 +        public void checkPrivateAccess() {
32 +            try {
33 +                AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a =
34 +                    AtomicReferenceFieldUpdater.newUpdater
35 +                    (AtomicReferenceFieldUpdaterTest.class, Integer.class, "privateField");
36 +                shouldThrow();
37 +            } catch (RuntimeException success) {
38 +                assertNotNull(success.getCause());
39 +            }
40 +        }
41 +
42 +        public void checkCompareAndSetProtectedSub() {
43 +            AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a =
44 +                AtomicReferenceFieldUpdater.newUpdater
45 +                (AtomicReferenceFieldUpdaterTest.class, Integer.class, "protectedField");
46 +            this.protectedField = one;
47 +            assertTrue(a.compareAndSet(this, one, two));
48 +            assertTrue(a.compareAndSet(this, two, m4));
49 +            assertSame(m4, a.get(this));
50 +            assertFalse(a.compareAndSet(this, m5, seven));
51 +            assertFalse(seven == a.get(this));
52 +            assertTrue(a.compareAndSet(this, m4, seven));
53 +            assertSame(seven, a.get(this));
54 +        }
55 +    }
56 +
57 +    static class UnrelatedClass {
58 +        public void checkPackageAccess(AtomicReferenceFieldUpdaterTest obj) {
59 +            obj.x = one;
60 +            AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a =
61 +                AtomicReferenceFieldUpdater.newUpdater
62 +                (AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
63 +            assertSame(one, a.get(obj));
64 +            assertTrue(a.compareAndSet(obj, one, two));
65 +            assertSame(two, a.get(obj));
66 +        }
67 +
68 +        public void checkPrivateAccess(AtomicReferenceFieldUpdaterTest obj) {
69 +            try {
70 +                AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a =
71 +                    AtomicReferenceFieldUpdater.newUpdater
72 +                    (AtomicReferenceFieldUpdaterTest.class, Integer.class, "privateField");
73 +                throw new AssertionError("should throw");
74 +            } catch (RuntimeException success) {
75 +                assertNotNull(success.getCause());
76 +            }
77 +        }
78 +    }
79 +
80 +    static AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> updaterFor(String fieldName) {
81 +        return AtomicReferenceFieldUpdater.newUpdater
82 +            (AtomicReferenceFieldUpdaterTest.class, Integer.class, fieldName);
83 +    }
84 +
85      /**
86       * Construction with non-existent field throws RuntimeException
87       */
88      public void testConstructor() {
89          try {
90 <            AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
31 <                a = AtomicReferenceFieldUpdater.newUpdater
32 <                (AtomicReferenceFieldUpdaterTest.class, Integer.class, "y");
90 >            updaterFor("y");
91              shouldThrow();
92 <        } catch (RuntimeException success) {}
92 >        } catch (RuntimeException success) {
93 >            assertNotNull(success.getCause());
94 >        }
95      }
96  
37
97      /**
98 <     * construction with field not of given type throws RuntimeException
98 >     * construction with field not of given type throws ClassCastException
99       */
100      public void testConstructor2() {
101          try {
102 <            AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
44 <                a = AtomicReferenceFieldUpdater.newUpdater
45 <                (AtomicReferenceFieldUpdaterTest.class, Integer.class, "z");
102 >            updaterFor("z");
103              shouldThrow();
104 <        } catch (RuntimeException success) {}
104 >        } catch (ClassCastException success) {}
105      }
106  
107      /**
108 <     * Constructor with non-volatile field throws exception
108 >     * Constructor with non-volatile field throws IllegalArgumentException
109       */
110      public void testConstructor3() {
111          try {
112 <            AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
56 <                a = AtomicReferenceFieldUpdater.newUpdater
57 <                (AtomicReferenceFieldUpdaterTest.class, Integer.class, "w");
112 >            updaterFor("w");
113              shouldThrow();
114 <        } catch (RuntimeException success) {}
114 >        } catch (IllegalArgumentException success) {}
115      }
116  
117      /**
118 <     *  get returns the last value set or assigned
118 >     * Constructor with non-reference field throws ClassCastException
119       */
120 <    public void testGetSet() {
66 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
120 >    public void testConstructor4() {
121          try {
122 <            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
123 <        } catch (RuntimeException ok) {
124 <            return;
125 <        }
122 >            updaterFor("i");
123 >            shouldThrow();
124 >        } catch (ClassCastException success) {}
125 >    }
126 >
127 >    /**
128 >     * construction using private field from subclass throws RuntimeException
129 >     */
130 >    public void testPrivateFieldInSubclass() {
131 >        AtomicReferenceFieldUpdaterTestSubclass s =
132 >            new AtomicReferenceFieldUpdaterTestSubclass();
133 >        s.checkPrivateAccess();
134 >    }
135 >
136 >    /**
137 >     * construction from unrelated class; package access is allowed,
138 >     * private access is not
139 >     */
140 >    public void testUnrelatedClassAccess() {
141 >        new UnrelatedClass().checkPackageAccess(this);
142 >        new UnrelatedClass().checkPrivateAccess(this);
143 >    }
144 >
145 >    /**
146 >     * get returns the last value set or assigned
147 >     */
148 >    public void testGetSet() {
149 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
150 >        a = updaterFor("x");
151          x = one;
152 <        assertSame(one,a.get(this));
153 <        a.set(this,two);
154 <        assertSame(two,a.get(this));
155 <        a.set(this,m3);
156 <        assertSame(m3,a.get(this));
152 >        assertSame(one, a.get(this));
153 >        a.set(this, two);
154 >        assertSame(two, a.get(this));
155 >        a.set(this, m3);
156 >        assertSame(m3, a.get(this));
157      }
158  
159      /**
160 <     *  get returns the last value lazySet by same thread
160 >     * get returns the last value lazySet by same thread
161       */
162      public void testGetLazySet() {
163 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
164 <        try {
86 <            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
87 <        } catch (RuntimeException ok) {
88 <            return;
89 <        }
163 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
164 >        a = updaterFor("x");
165          x = one;
166 <        assertSame(one,a.get(this));
167 <        a.lazySet(this,two);
168 <        assertSame(two,a.get(this));
169 <        a.lazySet(this,m3);
170 <        assertSame(m3,a.get(this));
166 >        assertSame(one, a.get(this));
167 >        a.lazySet(this, two);
168 >        assertSame(two, a.get(this));
169 >        a.lazySet(this, m3);
170 >        assertSame(m3, a.get(this));
171      }
172  
173      /**
174       * compareAndSet succeeds in changing value if equal to expected else fails
175       */
176      public void testCompareAndSet() {
177 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
178 <        try {
104 <            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
105 <        } catch (RuntimeException ok) {
106 <            return;
107 <        }
177 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
178 >        a = updaterFor("x");
179          x = one;
180 <        assertTrue(a.compareAndSet(this,one,two));
181 <        assertTrue(a.compareAndSet(this,two,m4));
182 <        assertSame(m4,a.get(this));
183 <        assertFalse(a.compareAndSet(this,m5,seven));
180 >        assertTrue(a.compareAndSet(this, one, two));
181 >        assertTrue(a.compareAndSet(this, two, m4));
182 >        assertSame(m4, a.get(this));
183 >        assertFalse(a.compareAndSet(this, m5, seven));
184          assertFalse(seven == a.get(this));
185 <        assertTrue(a.compareAndSet(this,m4,seven));
186 <        assertSame(seven,a.get(this));
185 >        assertTrue(a.compareAndSet(this, m4, seven));
186 >        assertSame(seven, a.get(this));
187      }
188  
189      /**
# Line 121 | Line 192 | public class AtomicReferenceFieldUpdater
192       */
193      public void testCompareAndSetInMultipleThreads() throws Exception {
194          x = one;
195 <        final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
196 <        try {
126 <            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
127 <        } catch (RuntimeException ok) {
128 <            return;
129 <        }
195 >        final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
196 >        a = updaterFor("x");
197  
198          Thread t = new Thread(new CheckedRunnable() {
199              public void realRun() {
# Line 138 | Line 205 | public class AtomicReferenceFieldUpdater
205          assertTrue(a.compareAndSet(this, one, two));
206          t.join(LONG_DELAY_MS);
207          assertFalse(t.isAlive());
208 <        assertSame(a.get(this), three);
208 >        assertSame(three, a.get(this));
209      }
210  
211      /**
# Line 146 | Line 213 | public class AtomicReferenceFieldUpdater
213       * to expected
214       */
215      public void testWeakCompareAndSet() {
216 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
217 <        try {
151 <            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
152 <        } catch (RuntimeException ok) {
153 <            return;
154 <        }
216 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
217 >        a = updaterFor("x");
218          x = one;
219 <        while (!a.weakCompareAndSet(this,one,two));
220 <        while (!a.weakCompareAndSet(this,two,m4));
221 <        assertSame(m4,a.get(this));
222 <        while (!a.weakCompareAndSet(this,m4,seven));
223 <        assertSame(seven,a.get(this));
219 >        do {} while (!a.weakCompareAndSet(this, one, two));
220 >        do {} while (!a.weakCompareAndSet(this, two, m4));
221 >        assertSame(m4, a.get(this));
222 >        do {} while (!a.weakCompareAndSet(this, m4, seven));
223 >        assertSame(seven, a.get(this));
224      }
225  
226      /**
227       * getAndSet returns previous value and sets to given value
228       */
229      public void testGetAndSet() {
230 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
231 <        try {
169 <            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
170 <        } catch (RuntimeException ok) {
171 <            return;
172 <        }
230 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
231 >        a = updaterFor("x");
232          x = one;
233 <        assertSame(one,a.getAndSet(this, zero));
234 <        assertSame(zero,a.getAndSet(this,m10));
235 <        assertSame(m10,a.getAndSet(this,1));
233 >        assertSame(one, a.getAndSet(this, zero));
234 >        assertSame(zero, a.getAndSet(this, m10));
235 >        assertSame(m10, a.getAndSet(this, 1));
236      }
237  
238   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines