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.25 by jsr166, Fri Jun 10 20:17:11 2011 UTC vs.
Revision 1.36 by jsr166, Wed Sep 20 00:41:13 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 +    static AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> updaterFor(String fieldName) {
30 +        return AtomicReferenceFieldUpdater.newUpdater
31 +            (AtomicReferenceFieldUpdaterTest.class, Integer.class, fieldName);
32 +    }
33 +
34      /**
35       * Construction with non-existent field throws RuntimeException
36       */
37      public void testConstructor() {
38          try {
39 <            AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
30 <                a = AtomicReferenceFieldUpdater.newUpdater
31 <                (AtomicReferenceFieldUpdaterTest.class, Integer.class, "y");
39 >            updaterFor("y");
40              shouldThrow();
41 <        } catch (RuntimeException success) {}
41 >        } catch (RuntimeException success) {
42 >            assertNotNull(success.getCause());
43 >        }
44      }
45  
46      /**
47 <     * construction with field not of given type throws RuntimeException
47 >     * construction with field not of given type throws ClassCastException
48       */
49      public void testConstructor2() {
50          try {
51 <            AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
42 <                a = AtomicReferenceFieldUpdater.newUpdater
43 <                (AtomicReferenceFieldUpdaterTest.class, Integer.class, "z");
51 >            updaterFor("z");
52              shouldThrow();
53 <        } catch (RuntimeException success) {}
53 >        } catch (ClassCastException success) {}
54      }
55  
56      /**
57 <     * Constructor with non-volatile field throws exception
57 >     * Constructor with non-volatile field throws IllegalArgumentException
58       */
59      public void testConstructor3() {
60          try {
61 <            AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>
54 <                a = AtomicReferenceFieldUpdater.newUpdater
55 <                (AtomicReferenceFieldUpdaterTest.class, Integer.class, "w");
61 >            updaterFor("w");
62              shouldThrow();
63 <        } catch (RuntimeException success) {}
63 >        } catch (IllegalArgumentException success) {}
64 >    }
65 >
66 >    /**
67 >     * Constructor with non-reference field throws ClassCastException
68 >     */
69 >    public void testConstructor4() {
70 >        try {
71 >            updaterFor("i");
72 >            shouldThrow();
73 >        } catch (ClassCastException success) {}
74 >    }
75 >
76 >    /**
77 >     * construction using private field from subclass throws RuntimeException
78 >     */
79 >    public void testPrivateFieldInSubclass() {
80 >        new NonNestmates.AtomicReferenceFieldUpdaterTestSubclass()
81 >            .checkPrivateAccess();
82 >    }
83 >
84 >    /**
85 >     * construction from unrelated class; package access is allowed,
86 >     * private access is not
87 >     */
88 >    public void testUnrelatedClassAccess() {
89 >        new NonNestmates().checkPackageAccess(this);
90 >        new NonNestmates().checkPrivateAccess(this);
91      }
92  
93      /**
94       * get returns the last value set or assigned
95       */
96      public void testGetSet() {
97 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
98 <        try {
66 <            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
67 <        } catch (RuntimeException ok) {
68 <            return;
69 <        }
97 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
98 >        a = updaterFor("x");
99          x = one;
100          assertSame(one, a.get(this));
101          a.set(this, two);
# Line 79 | Line 108 | public class AtomicReferenceFieldUpdater
108       * get returns the last value lazySet by same thread
109       */
110      public void testGetLazySet() {
111 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
112 <        try {
84 <            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
85 <        } catch (RuntimeException ok) {
86 <            return;
87 <        }
111 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
112 >        a = updaterFor("x");
113          x = one;
114          assertSame(one, a.get(this));
115          a.lazySet(this, two);
# Line 97 | Line 122 | public class AtomicReferenceFieldUpdater
122       * compareAndSet succeeds in changing value if equal to expected else fails
123       */
124      public void testCompareAndSet() {
125 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
126 <        try {
102 <            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
103 <        } catch (RuntimeException ok) {
104 <            return;
105 <        }
125 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
126 >        a = updaterFor("x");
127          x = one;
128          assertTrue(a.compareAndSet(this, one, two));
129          assertTrue(a.compareAndSet(this, two, m4));
130          assertSame(m4, a.get(this));
131          assertFalse(a.compareAndSet(this, m5, seven));
132 <        assertFalse(seven == a.get(this));
132 >        assertNotSame(seven, a.get(this));
133          assertTrue(a.compareAndSet(this, m4, seven));
134          assertSame(seven, a.get(this));
135      }
# Line 119 | Line 140 | public class AtomicReferenceFieldUpdater
140       */
141      public void testCompareAndSetInMultipleThreads() throws Exception {
142          x = one;
143 <        final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
144 <        try {
124 <            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
125 <        } catch (RuntimeException ok) {
126 <            return;
127 <        }
143 >        final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
144 >        a = updaterFor("x");
145  
146          Thread t = new Thread(new CheckedRunnable() {
147              public void realRun() {
# Line 144 | Line 161 | public class AtomicReferenceFieldUpdater
161       * to expected
162       */
163      public void testWeakCompareAndSet() {
164 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
165 <        try {
149 <            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
150 <        } catch (RuntimeException ok) {
151 <            return;
152 <        }
164 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
165 >        a = updaterFor("x");
166          x = one;
167 <        while (!a.weakCompareAndSet(this, one, two));
168 <        while (!a.weakCompareAndSet(this, two, m4));
167 >        do {} while (!a.weakCompareAndSet(this, one, two));
168 >        do {} while (!a.weakCompareAndSet(this, two, m4));
169          assertSame(m4, a.get(this));
170 <        while (!a.weakCompareAndSet(this, m4, seven));
170 >        do {} while (!a.weakCompareAndSet(this, m4, seven));
171          assertSame(seven, a.get(this));
172      }
173  
# Line 162 | Line 175 | public class AtomicReferenceFieldUpdater
175       * getAndSet returns previous value and sets to given value
176       */
177      public void testGetAndSet() {
178 <        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer>a;
179 <        try {
167 <            a = AtomicReferenceFieldUpdater.newUpdater(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
168 <        } catch (RuntimeException ok) {
169 <            return;
170 <        }
178 >        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
179 >        a = updaterFor("x");
180          x = one;
181          assertSame(one, a.getAndSet(this, zero));
182          assertSame(zero, a.getAndSet(this, m10));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines