View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.util;
21  
22  import static org.junit.Assert.*;
23  
24  import java.util.Arrays;
25  import java.util.Iterator;
26  
27  import com.google.common.collect.Lists;
28  import org.apache.hadoop.hbase.SmallTests;
29  import org.junit.Test;
30  import org.junit.experimental.categories.Category;
31  
32  @Category(SmallTests.class)
33  public class TestSortedCopyOnWriteSet {
34  
35    @Test
36    public void testSorting() throws Exception {
37      SortedCopyOnWriteSet<String> set = new SortedCopyOnWriteSet<String>();
38      set.add("c");
39      set.add("d");
40      set.add("a");
41      set.add("b");
42  
43      String[] expected = new String[]{"a", "b", "c", "d"};
44      String[] stored = set.toArray(new String[4]);
45      assertArrayEquals(expected, stored);
46  
47      set.add("c");
48      assertEquals(4, set.size());
49      stored = set.toArray(new String[4]);
50      assertArrayEquals(expected, stored);
51    }
52  
53    @Test
54    public void testIteratorIsolation() throws Exception {
55      SortedCopyOnWriteSet<String> set = new SortedCopyOnWriteSet<String>(
56          Lists.newArrayList("a", "b", "c", "d", "e"));
57  
58      // isolation of remove()
59      Iterator<String> iter = set.iterator();
60      set.remove("c");
61      boolean found = false;
62      while (iter.hasNext() && !found) {
63        found = "c".equals(iter.next());
64      }
65      assertTrue(found);
66  
67      iter = set.iterator();
68      found = false;
69      while (iter.hasNext() && !found) {
70        found = "c".equals(iter.next());
71      }
72      assertFalse(found);
73  
74      // isolation of add()
75      iter = set.iterator();
76      set.add("f");
77      found = false;
78      while (iter.hasNext() && !found) {
79        String next = iter.next();
80        found = "f".equals(next);
81      }
82      assertFalse(found);
83  
84      // isolation of addAll()
85      iter = set.iterator();
86      set.addAll(Lists.newArrayList("g", "h", "i"));
87      found = false;
88      while (iter.hasNext() && !found) {
89        String next = iter.next();
90        found = "g".equals(next) || "h".equals(next) || "i".equals(next);
91      }
92      assertFalse(found);
93  
94      // isolation of clear()
95      iter = set.iterator();
96      set.clear();
97      assertEquals(0, set.size());
98      int size = 0;
99      while (iter.hasNext()) {
100       iter.next();
101       size++;
102     }
103     assertTrue(size > 0);
104   }
105 
106 }
107