• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static org.junit.Assert.assertThrows;
20 
21 import com.google.common.testing.NullPointerTester;
22 import java.util.concurrent.atomic.AtomicReferenceArray;
23 import junit.framework.TestCase;
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     assertThrows(IndexOutOfBoundsException.class, () -> refArray.get(length));
50   }
51 
testNewReferenceArray_withNegativeLength()52   public void testNewReferenceArray_withNegativeLength() throws Exception {
53     assertThrows(NegativeArraySizeException.class, () -> Atomics.newReferenceArray(-1));
54   }
55 
testNewReferenceArray_withStringArray()56   public void testNewReferenceArray_withStringArray() throws Exception {
57     String[] array = {"foo", "bar", "baz"};
58     AtomicReferenceArray<String> refArray = Atomics.newReferenceArray(array);
59     for (int i = 0; i < array.length; ++i) {
60       assertEquals(array[i], refArray.get(i));
61     }
62     assertThrows(IndexOutOfBoundsException.class, () -> refArray.get(array.length));
63   }
64 
testNewReferenceArray_withNullArray()65   public void testNewReferenceArray_withNullArray() throws Exception {
66     assertThrows(NullPointerException.class, () -> Atomics.newReferenceArray(null));
67   }
68 
testNullPointers()69   public void testNullPointers() {
70     NullPointerTester tester = new NullPointerTester();
71     tester.testAllPublicConstructors(Atomics.class); // there aren't any
72     tester.testAllPublicStaticMethods(Atomics.class);
73   }
74 }
75