/* * Copyright (C) 2010 The Guava Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.common.collect; import static org.junit.contrib.truth.Truth.ASSERT; import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; import com.google.common.base.Function; import com.google.common.base.Predicate; import com.google.common.collect.Maps.EntryTransformer; import com.google.common.collect.testing.SortedMapInterfaceTest; import com.google.common.testing.NullPointerTester; import junit.framework.TestCase; import java.util.Comparator; import java.util.Map; import java.util.Map.Entry; import java.util.SortedMap; /** * Tests for SortedMaps. * * @author Louis Wasserman */ @GwtCompatible(emulated = true) @SuppressWarnings("deprecation") public class SortedMapsTest extends TestCase { private static final EntryTransformer ALWAYS_NULL = new EntryTransformer() { @Override public Object transformEntry(Object k, Object v1) { return null; } }; @GwtIncompatible("NullPointerTester") public void testNullPointer() throws Exception { NullPointerTester nullPointerTester = new NullPointerTester(); nullPointerTester.setDefault(EntryTransformer.class, ALWAYS_NULL); nullPointerTester.setDefault( SortedMap.class, Maps.newTreeMap()); nullPointerTester.testAllPublicStaticMethods(SortedMaps.class); } public void testTransformSortedValues() { SortedMap map = ImmutableSortedMap.of("a", 4, "b", 9); Function sqrt = new Function() { @Override public Double apply(Integer in) { return Math.sqrt(in); } }; SortedMap transformed = SortedMaps.transformValues(map, sqrt); assertEquals(ImmutableSortedMap.of("a", 2.0, "b", 3.0), transformed); } public void testTransformSortedEntries() { SortedMap map = ImmutableSortedMap.of("a", "4", "b", "9"); EntryTransformer concat = new EntryTransformer() { @Override public String transformEntry(String key, String value) { return key + value; } }; SortedMap transformed = SortedMaps.transformEntries(map, concat); assertEquals(ImmutableSortedMap.of("a", "a4", "b", "b9"), transformed); } // Not testing Map methods of SortedMaps.filter*, since the implementation // doesn't override Maps.FilteredEntryMap, which is already tested. private static final Predicate EVEN = new Predicate() { @Override public boolean apply(Integer input) { return input % 2 == 0; } }; public void testFilterKeys() { Comparator comparator = Ordering.natural(); SortedMap unfiltered = Maps.newTreeMap(comparator); unfiltered.put(1, "one"); unfiltered.put(2, "two"); unfiltered.put(3, "three"); unfiltered.put(4, "four"); unfiltered.put(5, "five"); unfiltered.put(6, "six"); unfiltered.put(7, "seven"); SortedMap filtered = SortedMaps.filterKeys(unfiltered, EVEN); ASSERT.that(filtered.keySet()).hasContentsInOrder(2, 4, 6); assertSame(comparator, filtered.comparator()); assertEquals((Integer) 2, filtered.firstKey()); assertEquals((Integer) 6, filtered.lastKey()); ASSERT.that(filtered.headMap(5).keySet()).hasContentsInOrder(2, 4); ASSERT.that(filtered.tailMap(3).keySet()).hasContentsInOrder(4, 6); ASSERT.that(filtered.subMap(3, 5).keySet()).hasContentsInOrder(4); } private static final Predicate NOT_LENGTH_3 = new Predicate() { @Override public boolean apply(String input) { return input == null || input.length() != 3; } }; public void testFilterValues() { Comparator comparator = Ordering.natural(); SortedMap unfiltered = Maps.newTreeMap(comparator); unfiltered.put(1, "one"); unfiltered.put(2, "two"); unfiltered.put(3, "three"); unfiltered.put(4, "four"); unfiltered.put(5, "five"); unfiltered.put(6, "six"); unfiltered.put(7, "seven"); SortedMap filtered = SortedMaps.filterValues(unfiltered, NOT_LENGTH_3); ASSERT.that(filtered.keySet()).hasContentsInOrder(3, 4, 5, 7); assertSame(comparator, filtered.comparator()); assertEquals((Integer) 3, filtered.firstKey()); assertEquals((Integer) 7, filtered.lastKey()); ASSERT.that(filtered.headMap(5).keySet()).hasContentsInOrder(3, 4); ASSERT.that(filtered.tailMap(4).keySet()).hasContentsInOrder(4, 5, 7); ASSERT.that(filtered.subMap(4, 6).keySet()).hasContentsInOrder(4, 5); } private static final Predicate> EVEN_AND_LENGTH_3 = new Predicate>() { @Override public boolean apply(Entry entry) { return (entry.getKey() == null || entry.getKey() % 2 == 0) && (entry.getValue() == null || entry.getValue().length() == 3); } }; private static class ContainsKeySafeSortedMap extends ForwardingSortedMap { SortedMap delegate = Maps.newTreeMap(Ordering.natural().nullsFirst()); @Override protected SortedMap delegate() { return delegate; } // Needed by MapInterfaceTest.testContainsKey() @Override public boolean containsKey(Object key) { try { return super.containsKey(key); } catch (ClassCastException e) { return false; } } } public static class FilteredEntriesSortedMapInterfaceTest extends SortedMapInterfaceTest { public FilteredEntriesSortedMapInterfaceTest() { super(true, true, true, true, true); } @Override protected SortedMap makeEmptyMap() { SortedMap unfiltered = new ContainsKeySafeSortedMap(); unfiltered.put(1, "one"); unfiltered.put(3, "three"); unfiltered.put(4, "four"); unfiltered.put(5, "five"); return SortedMaps.filterEntries(unfiltered, EVEN_AND_LENGTH_3); } @Override protected SortedMap makePopulatedMap() { SortedMap unfiltered = new ContainsKeySafeSortedMap(); unfiltered.put(1, "one"); unfiltered.put(2, "two"); unfiltered.put(3, "three"); unfiltered.put(4, "four"); unfiltered.put(5, "five"); unfiltered.put(6, "six"); return SortedMaps.filterEntries(unfiltered, EVEN_AND_LENGTH_3); } @Override protected Integer getKeyNotInPopulatedMap() { return 10; } @Override protected String getValueNotInPopulatedMap() { return "ten"; } // Iterators don't support remove. @Override public void testEntrySetIteratorRemove() {} @Override public void testValuesIteratorRemove() {} // These tests fail on GWT. // TODO: Investigate why. @Override public void testEntrySetRemoveAll() {} @Override public void testEntrySetRetainAll() {} } }