View Javadoc
1   /*
2    * Copyright (C) 2010 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing.testers;
18  
19  import static com.google.common.collect.testing.features.CollectionSize.ONE;
20  import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
21  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
22  
23  import com.google.common.annotations.GwtCompatible;
24  import com.google.common.collect.testing.Helpers;
25  import com.google.common.collect.testing.features.CollectionSize;
26  import java.util.Collections;
27  import java.util.List;
28  import java.util.NoSuchElementException;
29  import java.util.SortedSet;
30  
31  /**
32   * A generic JUnit test which tests operations on a SortedSet. Can't be
33   * invoked directly; please see {@code SortedSetTestSuiteBuilder}.
34   *
35   * @author Jesse Wilson
36   * @author Louis Wasserman
37   */
38  @GwtCompatible
39  public class SortedSetNavigationTester<E> extends AbstractSetTester<E> {
40  
41    private SortedSet<E> sortedSet;
42    private List<E> values;
43    private E a;
44    private E b;
45    private E c;
46  
47    @Override
48    public void setUp() throws Exception {
49      super.setUp();
50      sortedSet = (SortedSet<E>) getSet();
51      values =
52          Helpers.copyToList(
53              getSubjectGenerator()
54                  .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements()));
55      Collections.sort(values, sortedSet.comparator());
56  
57      // some tests assume SEVERAL == 3
58      if (values.size() >= 1) {
59        a = values.get(0);
60        if (values.size() >= 3) {
61          b = values.get(1);
62          c = values.get(2);
63        }
64      }
65    }
66  
67    @CollectionSize.Require(ZERO)
68    public void testEmptySetFirst() {
69      try {
70        sortedSet.first();
71        fail();
72      } catch (NoSuchElementException e) {
73      }
74    }
75  
76    @CollectionSize.Require(ZERO)
77    public void testEmptySetLast() {
78      try {
79        sortedSet.last();
80        fail();
81      } catch (NoSuchElementException e) {
82      }
83    }
84  
85    @CollectionSize.Require(ONE)
86    public void testSingletonSetFirst() {
87      assertEquals(a, sortedSet.first());
88    }
89  
90    @CollectionSize.Require(ONE)
91    public void testSingletonSetLast() {
92      assertEquals(a, sortedSet.last());
93    }
94  
95    @CollectionSize.Require(SEVERAL)
96    public void testFirst() {
97      assertEquals(a, sortedSet.first());
98    }
99  
100   @CollectionSize.Require(SEVERAL)
101   public void testLast() {
102     assertEquals(c, sortedSet.last());
103   }
104 }