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.Iterator;
25  
26  import com.google.common.collect.Lists;
27  import org.apache.hadoop.hbase.testclassification.SmallTests;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  @Category(SmallTests.class)
32  public class TestSortedCopyOnWriteSet {
33  
34    @Test
35    public void testSorting() throws Exception {
36      SortedCopyOnWriteSet<String> set = new SortedCopyOnWriteSet<String>();
37      set.add("c");
38      set.add("d");
39      set.add("a");
40      set.add("b");
41  
42      String[] expected = new String[]{"a", "b", "c", "d"};
43      String[] stored = set.toArray(new String[4]);
44      assertArrayEquals(expected, stored);
45  
46      set.add("c");
47      assertEquals(4, set.size());
48      stored = set.toArray(new String[4]);
49      assertArrayEquals(expected, stored);
50    }
51  
52    @Test
53    public void testIteratorIsolation() throws Exception {
54      SortedCopyOnWriteSet<String> set = new SortedCopyOnWriteSet<String>(
55          Lists.newArrayList("a", "b", "c", "d", "e"));
56  
57      // isolation of remove()
58      Iterator<String> iter = set.iterator();
59      set.remove("c");
60      boolean found = false;
61      while (iter.hasNext() && !found) {
62        found = "c".equals(iter.next());
63      }
64      assertTrue(found);
65  
66      iter = set.iterator();
67      found = false;
68      while (iter.hasNext() && !found) {
69        found = "c".equals(iter.next());
70      }
71      assertFalse(found);
72  
73      // isolation of add()
74      iter = set.iterator();
75      set.add("f");
76      found = false;
77      while (iter.hasNext() && !found) {
78        String next = iter.next();
79        found = "f".equals(next);
80      }
81      assertFalse(found);
82  
83      // isolation of addAll()
84      iter = set.iterator();
85      set.addAll(Lists.newArrayList("g", "h", "i"));
86      found = false;
87      while (iter.hasNext() && !found) {
88        String next = iter.next();
89        found = "g".equals(next) || "h".equals(next) || "i".equals(next);
90      }
91      assertFalse(found);
92  
93      // isolation of clear()
94      iter = set.iterator();
95      set.clear();
96      assertEquals(0, set.size());
97      int size = 0;
98      while (iter.hasNext()) {
99        iter.next();
100       size++;
101     }
102     assertTrue(size > 0);
103   }
104 
105 }
106