1 /* 2 * Copyright (C) 2011 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; 18 19 import com.google.common.annotations.GwtIncompatible; 20 import com.google.common.base.Predicate; 21 22 import junit.framework.TestCase; 23 24 import java.util.Arrays; 25 import java.util.Map; 26 import java.util.Map.Entry; 27 28 /** 29 * Unit tests for {@link Multimaps} filtering methods. 30 * 31 * @author Jared Levy 32 */ 33 @GwtIncompatible("nottested") 34 public class FilteredMultimapTest extends TestCase { 35 36 private static final Predicate<Map.Entry<String, Integer>> ENTRY_PREDICATE 37 = new Predicate<Map.Entry<String, Integer>>() { 38 @Override public boolean apply(Entry<String, Integer> entry) { 39 return !"badkey".equals(entry.getKey()) && !((Integer) 55556).equals(entry.getValue()); 40 } 41 }; 42 43 protected Multimap<String, Integer> create() { 44 Multimap<String, Integer> unfiltered = HashMultimap.create(); 45 unfiltered.put("foo", 55556); 46 unfiltered.put("badkey", 1); 47 return Multimaps.filterEntries(unfiltered, ENTRY_PREDICATE); 48 } 49 50 private static final Predicate<String> KEY_PREDICATE 51 = new Predicate<String>() { 52 @Override public boolean apply(String key) { 53 return !"badkey".equals(key); 54 } 55 }; 56 57 public void testFilterKeys() { 58 Multimap<String, Integer> unfiltered = HashMultimap.create(); 59 unfiltered.put("foo", 55556); 60 unfiltered.put("badkey", 1); 61 Multimap<String, Integer> filtered = Multimaps.filterKeys(unfiltered, KEY_PREDICATE); 62 assertEquals(1, filtered.size()); 63 assertTrue(filtered.containsEntry("foo", 55556)); 64 } 65 66 private static final Predicate<Integer> VALUE_PREDICATE 67 = new Predicate<Integer>() { 68 @Override public boolean apply(Integer value) { 69 return !((Integer) 55556).equals(value); 70 } 71 }; 72 73 public void testFilterValues() { 74 Multimap<String, Integer> unfiltered = HashMultimap.create(); 75 unfiltered.put("foo", 55556); 76 unfiltered.put("badkey", 1); 77 Multimap<String, Integer> filtered = Multimaps.filterValues(unfiltered, VALUE_PREDICATE); 78 assertEquals(1, filtered.size()); 79 assertFalse(filtered.containsEntry("foo", 55556)); 80 assertTrue(filtered.containsEntry("badkey", 1)); 81 } 82 83 public void testFilterFiltered() { 84 Multimap<String, Integer> unfiltered = HashMultimap.create(); 85 unfiltered.put("foo", 55556); 86 unfiltered.put("badkey", 1); 87 unfiltered.put("foo", 1); 88 Multimap<String, Integer> keyFiltered = Multimaps.filterKeys(unfiltered, KEY_PREDICATE); 89 Multimap<String, Integer> filtered = Multimaps.filterValues(keyFiltered, VALUE_PREDICATE); 90 assertEquals(1, filtered.size()); 91 assertTrue(filtered.containsEntry("foo", 1)); 92 assertTrue(filtered.keySet().retainAll(Arrays.asList("cat", "dog"))); 93 assertEquals(0, filtered.size()); 94 } 95 96 // TODO(jlevy): Many more tests needed. 97 } 98