1 /* 2 * Copyright (C) 2010 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.util.concurrent; 18 19 import com.google.common.testing.NullPointerTester; 20 import java.util.concurrent.atomic.AtomicReferenceArray; 21 import junit.framework.TestCase; 22 23 /** 24 * Unit test for {@link Atomics}. 25 * 26 * @author Kurt Alfred Kluever 27 */ 28 public class AtomicsTest extends TestCase { 29 30 private static final Object OBJECT = new Object(); 31 testNewReference()32 public void testNewReference() throws Exception { 33 assertEquals(null, Atomics.newReference().get()); 34 } 35 testNewReference_withInitialValue()36 public void testNewReference_withInitialValue() throws Exception { 37 assertEquals(null, Atomics.newReference(null).get()); 38 assertEquals(OBJECT, Atomics.newReference(OBJECT).get()); 39 } 40 testNewReferenceArray_withLength()41 public void testNewReferenceArray_withLength() throws Exception { 42 int length = 42; 43 AtomicReferenceArray<String> refArray = Atomics.newReferenceArray(length); 44 for (int i = 0; i < length; ++i) { 45 assertEquals(null, refArray.get(i)); 46 } 47 try { 48 refArray.get(length); 49 fail(); 50 } catch (IndexOutOfBoundsException expected) { 51 } 52 } 53 testNewReferenceArray_withNegativeLength()54 public void testNewReferenceArray_withNegativeLength() throws Exception { 55 try { 56 Atomics.newReferenceArray(-1); 57 fail(); 58 } catch (NegativeArraySizeException expected) { 59 } 60 } 61 testNewReferenceArray_withStringArray()62 public void testNewReferenceArray_withStringArray() throws Exception { 63 String[] array = {"foo", "bar", "baz"}; 64 AtomicReferenceArray<String> refArray = Atomics.newReferenceArray(array); 65 for (int i = 0; i < array.length; ++i) { 66 assertEquals(array[i], refArray.get(i)); 67 } 68 try { 69 refArray.get(array.length); 70 fail(); 71 } catch (IndexOutOfBoundsException expected) { 72 } 73 } 74 testNewReferenceArray_withNullArray()75 public void testNewReferenceArray_withNullArray() throws Exception { 76 try { 77 Atomics.newReferenceArray(null); 78 fail(); 79 } catch (NullPointerException expected) { 80 } 81 } 82 testNullPointers()83 public void testNullPointers() { 84 NullPointerTester tester = new NullPointerTester(); 85 tester.testAllPublicConstructors(Atomics.class); // there aren't any 86 tester.testAllPublicStaticMethods(Atomics.class); 87 } 88 } 89