1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 */ 22 23 /* 24 * This file is available under and governed by the GNU General Public 25 * License version 2 only, as published by the Free Software Foundation. 26 * However, the following notice accompanied the original version of this 27 * file: 28 * 29 * Written by Doug Lea with assistance from members of JCP JSR-166 30 * Expert Group and released to the public domain, as explained at 31 * http://creativecommons.org/publicdomain/zero/1.0/ 32 * Other contributors include Andrew Wright, Jeffrey Hayes, 33 * Pat Fisher, Mike Judd. 34 */ 35 36 import static org.junit.Assert.assertEquals; 37 import static org.junit.Assert.assertNotNull; 38 import static org.junit.Assert.assertSame; 39 import static org.junit.Assert.assertTrue; 40 41 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; 42 import java.util.concurrent.atomic.AtomicLongFieldUpdater; 43 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; 44 45 /** 46 * This source file contains test code deliberately not contained in 47 * the same source file as the tests that use them, to avoid making 48 * them nestmates, which affects accessibility rules (see JEP 181). 49 */ 50 class NonNestmates { 51 checkPackageAccess(AtomicIntegerFieldUpdaterTest obj)52 public void checkPackageAccess(AtomicIntegerFieldUpdaterTest obj) { 53 obj.x = 72; 54 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = 55 AtomicIntegerFieldUpdater.newUpdater( 56 AtomicIntegerFieldUpdaterTest.class, "x"); 57 assertEquals(72, a.get(obj)); 58 assertTrue(a.compareAndSet(obj, 72, 73)); 59 assertEquals(73, a.get(obj)); 60 } 61 checkPackageAccess(AtomicLongFieldUpdaterTest obj)62 public void checkPackageAccess(AtomicLongFieldUpdaterTest obj) { 63 obj.x = 72L; 64 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = 65 AtomicLongFieldUpdater.newUpdater( 66 AtomicLongFieldUpdaterTest.class, "x"); 67 assertEquals(72L, a.get(obj)); 68 assertTrue(a.compareAndSet(obj, 72L, 73L)); 69 assertEquals(73L, a.get(obj)); 70 } 71 checkPackageAccess(AtomicReferenceFieldUpdaterTest obj)72 public void checkPackageAccess(AtomicReferenceFieldUpdaterTest obj) { 73 Integer one = new Integer(1); 74 Integer two = new Integer(2); 75 obj.x = one; 76 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a = 77 AtomicReferenceFieldUpdater.newUpdater( 78 AtomicReferenceFieldUpdaterTest.class, Integer.class, "x"); 79 assertSame(one, a.get(obj)); 80 assertTrue(a.compareAndSet(obj, one, two)); 81 assertSame(two, a.get(obj)); 82 } 83 checkPrivateAccess(AtomicIntegerFieldUpdaterTest obj)84 public void checkPrivateAccess(AtomicIntegerFieldUpdaterTest obj) { 85 try { 86 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = 87 AtomicIntegerFieldUpdater.newUpdater( 88 AtomicIntegerFieldUpdaterTest.class, "privateField"); 89 throw new AssertionError("should throw"); 90 } catch (RuntimeException success) { 91 assertNotNull(success.getCause()); 92 } 93 } 94 checkPrivateAccess(AtomicLongFieldUpdaterTest obj)95 public void checkPrivateAccess(AtomicLongFieldUpdaterTest obj) { 96 try { 97 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = 98 AtomicLongFieldUpdater.newUpdater( 99 AtomicLongFieldUpdaterTest.class, "privateField"); 100 throw new AssertionError("should throw"); 101 } catch (RuntimeException success) { 102 assertNotNull(success.getCause()); 103 } 104 } 105 checkPrivateAccess(AtomicReferenceFieldUpdaterTest obj)106 public void checkPrivateAccess(AtomicReferenceFieldUpdaterTest obj) { 107 try { 108 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a = 109 AtomicReferenceFieldUpdater.newUpdater( 110 AtomicReferenceFieldUpdaterTest.class, 111 Integer.class, "privateField"); 112 throw new AssertionError("should throw"); 113 } catch (RuntimeException success) { 114 assertNotNull(success.getCause()); 115 } 116 } 117 118 static class AtomicIntegerFieldUpdaterTestSubclass 119 extends AtomicIntegerFieldUpdaterTest { 120 checkPrivateAccess()121 public void checkPrivateAccess() { 122 try { 123 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = 124 AtomicIntegerFieldUpdater.newUpdater( 125 AtomicIntegerFieldUpdaterTest.class, "privateField"); 126 shouldThrow(); 127 } catch (RuntimeException success) { 128 assertNotNull(success.getCause()); 129 } 130 } 131 checkCompareAndSetProtectedSub()132 public void checkCompareAndSetProtectedSub() { 133 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = 134 AtomicIntegerFieldUpdater.newUpdater( 135 AtomicIntegerFieldUpdaterTest.class, "protectedField"); 136 this.protectedField = 1; 137 assertTrue(a.compareAndSet(this, 1, 2)); 138 assertTrue(a.compareAndSet(this, 2, -4)); 139 assertEquals(-4, a.get(this)); 140 assertFalse(a.compareAndSet(this, -5, 7)); 141 assertEquals(-4, a.get(this)); 142 assertTrue(a.compareAndSet(this, -4, 7)); 143 assertEquals(7, a.get(this)); 144 } 145 } 146 147 static class AtomicLongFieldUpdaterTestSubclass 148 extends AtomicLongFieldUpdaterTest { 149 checkPrivateAccess()150 public void checkPrivateAccess() { 151 try { 152 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = 153 AtomicLongFieldUpdater.newUpdater( 154 AtomicLongFieldUpdaterTest.class, "privateField"); 155 shouldThrow(); 156 } catch (RuntimeException success) { 157 assertNotNull(success.getCause()); 158 } 159 } 160 checkCompareAndSetProtectedSub()161 public void checkCompareAndSetProtectedSub() { 162 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = 163 AtomicLongFieldUpdater.newUpdater( 164 AtomicLongFieldUpdaterTest.class, "protectedField"); 165 this.protectedField = 1; 166 assertTrue(a.compareAndSet(this, 1, 2)); 167 assertTrue(a.compareAndSet(this, 2, -4)); 168 assertEquals(-4, a.get(this)); 169 assertFalse(a.compareAndSet(this, -5, 7)); 170 assertEquals(-4, a.get(this)); 171 assertTrue(a.compareAndSet(this, -4, 7)); 172 assertEquals(7, a.get(this)); 173 } 174 } 175 176 static class AtomicReferenceFieldUpdaterTestSubclass 177 extends AtomicReferenceFieldUpdaterTest { 178 checkPrivateAccess()179 public void checkPrivateAccess() { 180 try { 181 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a = 182 AtomicReferenceFieldUpdater.newUpdater( 183 AtomicReferenceFieldUpdaterTest.class, 184 Integer.class, "privateField"); 185 shouldThrow(); 186 } catch (RuntimeException success) { 187 assertNotNull(success.getCause()); 188 } 189 } 190 checkCompareAndSetProtectedSub()191 public void checkCompareAndSetProtectedSub() { 192 AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a = 193 AtomicReferenceFieldUpdater.newUpdater( 194 AtomicReferenceFieldUpdaterTest.class, 195 Integer.class, "protectedField"); 196 this.protectedField = one; 197 assertTrue(a.compareAndSet(this, one, two)); 198 assertTrue(a.compareAndSet(this, two, m4)); 199 assertSame(m4, a.get(this)); 200 assertFalse(a.compareAndSet(this, m5, seven)); 201 assertNotSame(seven, a.get(this)); 202 assertTrue(a.compareAndSet(this, m4, seven)); 203 assertSame(seven, a.get(this)); 204 } 205 } 206 } 207