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 
21 import junit.framework.TestCase;
22 
23 import java.util.concurrent.atomic.AtomicReferenceArray;
24 
25 /**
26  * Unit test for {@link Atomics}.
27  *
28  * @author Kurt Alfred Kluever
29  */
30 public class AtomicsTest extends TestCase {
31 
32   private static final Object OBJECT = new Object();
33 
testNewReference()34   public void testNewReference() throws Exception {
35     assertEquals(null, Atomics.newReference().get());
36   }
37 
testNewReference_withInitialValue()38   public void testNewReference_withInitialValue() throws Exception {
39     assertEquals(null, Atomics.newReference(null).get());
40     assertEquals(OBJECT, Atomics.newReference(OBJECT).get());
41   }
42 
testNewReferenceArray_withLength()43   public void testNewReferenceArray_withLength() throws Exception {
44     int length = 42;
45     AtomicReferenceArray<String> refArray = Atomics.newReferenceArray(length);
46     for (int i = 0; i < length; ++i) {
47       assertEquals(null, refArray.get(i));
48     }
49     try {
50       refArray.get(length);
51       fail();
52     } catch (IndexOutOfBoundsException expected) {
53     }
54   }
55 
testNewReferenceArray_withNegativeLength()56   public void testNewReferenceArray_withNegativeLength() throws Exception {
57     try {
58       Atomics.newReferenceArray(-1);
59       fail();
60     } catch (NegativeArraySizeException expected) {
61     }
62   }
63 
testNewReferenceArray_withStringArray()64   public void testNewReferenceArray_withStringArray() throws Exception {
65     String[] array = {"foo", "bar", "baz"};
66     AtomicReferenceArray<String> refArray = Atomics.newReferenceArray(array);
67     for (int i = 0; i < array.length; ++i) {
68       assertEquals(array[i], refArray.get(i));
69     }
70     try {
71       refArray.get(array.length);
72       fail();
73     } catch (IndexOutOfBoundsException expected) {
74     }
75   }
76 
testNewReferenceArray_withNullArray()77   public void testNewReferenceArray_withNullArray() throws Exception {
78     try {
79       Atomics.newReferenceArray(null);
80       fail();
81     } catch (NullPointerException expected) {
82     }
83   }
84 
testNullPointers()85   public void testNullPointers() {
86     NullPointerTester tester = new NullPointerTester();
87     tester.testAllPublicConstructors(Atomics.class); // there aren't any
88     tester.testAllPublicStaticMethods(Atomics.class);
89   }
90 }
91