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