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; |
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 |
> |
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 |
|
} |
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() { |