SortedMapNavigationTester.java revision 3c77433663281544363151bf284b0240dfd22a42
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 17package com.google.common.collect.testing.testers; 18 19import static com.google.common.collect.testing.features.CollectionSize.ONE; 20import static com.google.common.collect.testing.features.CollectionSize.SEVERAL; 21import static com.google.common.collect.testing.features.CollectionSize.ZERO; 22 23import com.google.common.annotations.GwtCompatible; 24import com.google.common.collect.testing.AbstractMapTester; 25import com.google.common.collect.testing.Helpers; 26import com.google.common.collect.testing.features.CollectionSize; 27 28import java.util.Collections; 29import java.util.List; 30import java.util.Map.Entry; 31import java.util.NoSuchElementException; 32import java.util.SortedMap; 33 34/** 35 * A generic JUnit test which tests operations on a SortedMap. Can't be 36 * invoked directly; please see {@code SortedMapTestSuiteBuilder}. 37 * 38 * @author Jesse Wilson 39 * @author Louis Wasserman 40 */ 41@GwtCompatible 42public class SortedMapNavigationTester<K, V> extends AbstractMapTester<K, V> { 43 44 private SortedMap<K, V> navigableMap; 45 private Entry<K, V> a; 46 private Entry<K, V> c; 47 48 @Override public void setUp() throws Exception { 49 super.setUp(); 50 navigableMap = (SortedMap<K, V>) getMap(); 51 List<Entry<K, V>> entries = Helpers.copyToList(getSubjectGenerator().getSampleElements( 52 getSubjectGenerator().getCollectionSize().getNumElements())); 53 Collections.sort(entries, Helpers.<K, V>entryComparator(navigableMap.comparator())); 54 55 // some tests assume SEVERAL == 3 56 if (entries.size() >= 1) { 57 a = entries.get(0); 58 if (entries.size() >= 3) { 59 c = entries.get(2); 60 } 61 } 62 } 63 64 @CollectionSize.Require(ZERO) 65 public void testEmptyMapFirst() { 66 try { 67 navigableMap.firstKey(); 68 fail(); 69 } catch (NoSuchElementException e) { 70 } 71 } 72 73 @CollectionSize.Require(ZERO) 74 public void testEmptyMapLast() { 75 try { 76 assertNull(navigableMap.lastKey()); 77 fail(); 78 } catch (NoSuchElementException e) { 79 } 80 } 81 82 @CollectionSize.Require(ONE) 83 public void testSingletonMapFirst() { 84 assertEquals(a.getKey(), navigableMap.firstKey()); 85 } 86 87 @CollectionSize.Require(ONE) 88 public void testSingletonMapLast() { 89 assertEquals(a.getKey(), navigableMap.lastKey()); 90 } 91 92 @CollectionSize.Require(SEVERAL) 93 public void testFirst() { 94 assertEquals(a.getKey(), navigableMap.firstKey()); 95 } 96 97 @CollectionSize.Require(SEVERAL) 98 public void testLast() { 99 assertEquals(c.getKey(), navigableMap.lastKey()); 100 } 101} 102