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.27 by jsr166, Tue Apr 2 04:11:28 2013 UTC vs.
Revision 1.35 by jsr166, Sat Mar 18 20:42:20 2017 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 import junit.framework.*;
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 <    AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> updaterFor(String fieldName) {
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 >            assertNotSame(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      }
# Line 34 | Line 90 | public class AtomicReferenceFieldUpdater
90              updaterFor("y");
91              shouldThrow();
92          } catch (RuntimeException success) {
93 <            assertTrue(success.getCause() != null);
93 >            assertNotNull(success.getCause());
94          }
95      }
96  
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              updaterFor("z");
103              shouldThrow();
104 <        } catch (RuntimeException success) {}
104 >        } catch (ClassCastException success) {}
105      }
106  
107      /**
# Line 59 | Line 115 | public class AtomicReferenceFieldUpdater
115      }
116  
117      /**
118 +     * Constructor with non-reference field throws ClassCastException
119 +     */
120 +    public void testConstructor4() {
121 +        try {
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;
149 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
150          a = updaterFor("x");
151          x = one;
152          assertSame(one, a.get(this));
# Line 76 | Line 160 | public class AtomicReferenceFieldUpdater
160       * get returns the last value lazySet by same thread
161       */
162      public void testGetLazySet() {
163 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
163 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
164          a = updaterFor("x");
165          x = one;
166          assertSame(one, a.get(this));
# Line 90 | Line 174 | public class AtomicReferenceFieldUpdater
174       * compareAndSet succeeds in changing value if equal to expected else fails
175       */
176      public void testCompareAndSet() {
177 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
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));
184 <        assertFalse(seven == a.get(this));
184 >        assertNotSame(seven, a.get(this));
185          assertTrue(a.compareAndSet(this, m4, seven));
186          assertSame(seven, a.get(this));
187      }
# Line 108 | Line 192 | public class AtomicReferenceFieldUpdater
192       */
193      public void testCompareAndSetInMultipleThreads() throws Exception {
194          x = one;
195 <        final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
195 >        final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
196          a = updaterFor("x");
197  
198          Thread t = new Thread(new CheckedRunnable() {
# Line 129 | Line 213 | public class AtomicReferenceFieldUpdater
213       * to expected
214       */
215      public void testWeakCompareAndSet() {
216 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
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));
219 >        do {} while (!a.weakCompareAndSet(this, one, two));
220 >        do {} while (!a.weakCompareAndSet(this, two, m4));
221          assertSame(m4, a.get(this));
222 <        while (!a.weakCompareAndSet(this, m4, seven));
222 >        do {} while (!a.weakCompareAndSet(this, m4, seven));
223          assertSame(seven, a.get(this));
224      }
225  
# Line 143 | Line 227 | public class AtomicReferenceFieldUpdater
227       * getAndSet returns previous value and sets to given value
228       */
229      public void testGetAndSet() {
230 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
230 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
231          a = updaterFor("x");
232          x = one;
233          assertSame(one, a.getAndSet(this, zero));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines