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