1 package com.atlassian.util.concurrent.atomic;
2
3 import static com.google.common.base.Suppliers.ofInstance;
4 import static org.junit.Assert.assertEquals;
5
6 import org.junit.Test;
7
8 import com.google.common.base.Supplier;
9
10 public class AtomicReferenceArrayTest {
11
12 @Test
13 public void getAndSetArrayIfNormal() {
14 final String from = "from";
15 final String to = "to";
16 final AtomicReferenceArray<String> ref = new AtomicReferenceArray<String>(new String[] { from });
17 assertEquals(to, ref.getOrSetAndGetIf(0, from, ofInstance(to)));
18 }
19
20 @Test
21 public void getAndSetArrayIfNormalValue() {
22 final String from = "from";
23 final String to = "to";
24 final AtomicReferenceArray<String> ref = new AtomicReferenceArray<String>(new String[] { from });
25 assertEquals(to, ref.getOrSetAndGetIf(0, from, to));
26 }
27
28 @Test
29 public void getAndSetArrayIfNormalSupplierValue() {
30 final String from = "from";
31 final String to = "to";
32 final AtomicReferenceArray<String> ref = new AtomicReferenceArray<String>(new String[] { from });
33 assertEquals(to, ref.getOrSetAndGetIf(0, from, ofInstance(to)));
34 }
35
36 @Test(expected = IllegalArgumentException.class)
37 public void getAndSetIfArrayValueNPE() {
38 Atomics.getAndSetIf((AtomicReferenceArray<String>) null, 0, "", "");
39 }
40
41 @Test
42 public void getAndSetArrayIfNull() {
43 final String to = "to";
44 final AtomicReferenceArray<String> ref = new AtomicReferenceArray<String>(new String[] { null });
45 assertEquals(to, Atomics.getAndSetIfNull(ref, 0, ofInstance(to)));
46 }
47
48 @Test
49 public void getAndSetArrayContended() {
50 final String from = "from";
51 final String to = "to";
52 final String different = "different";
53 final AtomicReferenceArray<String> ref = new AtomicReferenceArray<String>(new String[] { from });
54 assertEquals(different, ref.getOrSetAndGetIf(0, from, new Supplier<String>() {
55 public String get() {
56
57 ref.set(0, different);
58 return to;
59 }
60 }));
61 }
62
63 @Test
64 public void getAndSetArrayReturnsOldValueIfChanged() {
65 final String old = "old";
66 final String from = "from";
67 final String to = "to";
68 final AtomicReferenceArray<String> ref = new AtomicReferenceArray<String>(new String[] { old });
69 assertEquals(old, ref.getOrSetAndGetIf(0, from, ofInstance(to)));
70 }
71
72 @Test
73 public void getAndSetArraySameValue() {
74 final String from = "from";
75 final String to = from;
76 final AtomicReferenceArray<String> ref = new AtomicReferenceArray<String>(new String[] { from });
77 assertEquals(to, ref.getOrSetAndGetIf(0, from, ofInstance(to)));
78 }
79
80 @Test
81 public void getAndSetArraySameValueDifferent() {
82 final String from = "from";
83 final String to = from;
84 final String different = "blah";
85 final AtomicReferenceArray<String> ref = new AtomicReferenceArray<String>(new String[] { different });
86 assertEquals(different, ref.getOrSetAndGetIf(0, from, ofInstance(to)));
87 }
88
89 @Test(expected = IndexOutOfBoundsException.class)
90 public void getAndSetArrayThrowsIndexOutOfBounds() {
91 Atomics.getAndSetIf(new AtomicReferenceArray<String>(new String[0]), 0, "test", ofInstance("blah"));
92 }
93 }