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

Comparing jsr166/src/test/tck/AtomicIntegerFieldUpdaterTest.java (file contents):
Revision 1.29 by jsr166, Sat Apr 25 04:55:30 2015 UTC vs.
Revision 1.30 by dl, Sun Nov 8 15:34:00 2015 UTC

# Line 13 | Line 13 | import junit.framework.TestSuite;
13  
14   public class AtomicIntegerFieldUpdaterTest extends JSR166TestCase {
15      volatile int x = 0;
16 +    protected volatile int protectedField;
17 +    private volatile int privateField;
18      int w;
19 <    long z;
19 >    float z;
20      public static void main(String[] args) {
21          main(suite(), args);
22      }
# Line 22 | Line 24 | public class AtomicIntegerFieldUpdaterTe
24          return new TestSuite(AtomicIntegerFieldUpdaterTest.class);
25      }
26  
27 +    // for testing subclass access
28 +    static class AtomicIntegerFieldUpdaterTestSubclass extends AtomicIntegerFieldUpdaterTest {
29 +        public void checkPrivateAccess() {
30 +            try {
31 +                AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a =
32 +                    AtomicIntegerFieldUpdater.newUpdater
33 +                    (AtomicIntegerFieldUpdaterTest.class, "privateField");
34 +                shouldThrow();
35 +            } catch (RuntimeException success) {
36 +                assertNotNull(success.getCause());
37 +            }
38 +        }
39 +
40 +        public void checkCompareAndSetProtectedSub() {
41 +            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
42 +            a = updaterFor("protectedField");
43 +            protectedField = 1;
44 +            assertTrue(a.compareAndSet(this, 1, 2));
45 +            assertTrue(a.compareAndSet(this, 2, -4));
46 +            assertEquals(-4, a.get(this));
47 +            assertFalse(a.compareAndSet(this, -5, 7));
48 +            assertEquals(-4, a.get(this));
49 +            assertTrue(a.compareAndSet(this, -4, 7));
50 +            assertEquals(7, a.get(this));
51 +        }
52 +    }
53 +
54 +    static class UnrelatedClass {
55 +        public void checkPrivateAccess() {
56 +            Exception ex = null;
57 +            try {
58 +                AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a =
59 +                    AtomicIntegerFieldUpdater.newUpdater
60 +                    (AtomicIntegerFieldUpdaterTest.class, "x");
61 +            } catch (RuntimeException rex) {
62 +                ex = rex;
63 +            }
64 +            if (ex != null) throw new Error();
65 +        }
66 +    }
67 +
68      AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> updaterFor(String fieldName) {
69          return AtomicIntegerFieldUpdater.newUpdater
70              (AtomicIntegerFieldUpdaterTest.class, fieldName);
# Line 60 | Line 103 | public class AtomicIntegerFieldUpdaterTe
103      }
104  
105      /**
106 +     * construction using private field from subclass throws RuntimeException
107 +     */
108 +    public void testPrivateFieldInSubclass() {
109 +        AtomicIntegerFieldUpdaterTestSubclass s =
110 +            new AtomicIntegerFieldUpdaterTestSubclass();
111 +        s.checkPrivateAccess();
112 +    }
113 +
114 +    /**
115 +     * construction from unrelated class throws RuntimeException
116 +     */
117 +    public void testUnrelatedClassAccess() {
118 +        UnrelatedClass s = new UnrelatedClass();
119 +        s.checkPrivateAccess();
120 +    }
121 +
122 +    /**
123       * get returns the last value set or assigned
124       */
125      public void testGetSet() {
# Line 104 | Line 164 | public class AtomicIntegerFieldUpdaterTe
164      }
165  
166      /**
167 +     * compareAndSet succeeds in changing protected field value if
168 +     * equal to expected else fails
169 +     */
170 +    public void testCompareAndSetProtected() {
171 +        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
172 +        a = updaterFor("protectedField");
173 +        protectedField = 1;
174 +        assertTrue(a.compareAndSet(this, 1, 2));
175 +        assertTrue(a.compareAndSet(this, 2, -4));
176 +        assertEquals(-4, a.get(this));
177 +        assertFalse(a.compareAndSet(this, -5, 7));
178 +        assertEquals(-4, a.get(this));
179 +        assertTrue(a.compareAndSet(this, -4, 7));
180 +        assertEquals(7, a.get(this));
181 +    }
182 +
183 +    /**
184 +     * compareAndSet succeeds in changing protected field value if
185 +     * equal to expected else fails
186 +     */
187 +    public void testCompareAndSetProtectedInSubclass() {
188 +        AtomicIntegerFieldUpdaterTestSubclass s =
189 +            new AtomicIntegerFieldUpdaterTestSubclass();
190 +        s.checkCompareAndSetProtectedSub();
191 +    }
192 +
193 +    /**
194       * compareAndSet in one thread enables another waiting for value
195       * to succeed
196       */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines