Home | History | Annotate | Download | only in collect
      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 java.util.Arrays;
     23 import java.util.Map;
     24 import java.util.Map.Entry;
     25 
     26 /**
     27  * Unit tests for {@link Multimaps} filtering methods.
     28  *
     29  * @author Jared Levy
     30  */
     31 @GwtIncompatible("nottested")
     32 public class FilteredMultimapTest extends AbstractMultimapTest {
     33 
     34   private static final Predicate<Map.Entry<String, Integer>> ENTRY_PREDICATE
     35       = new Predicate<Map.Entry<String, Integer>>() {
     36         @Override public boolean apply(Entry<String, Integer> entry) {
     37           return !"badkey".equals(entry.getKey()) && !((Integer) 55556).equals(entry.getValue());
     38         }
     39   };
     40 
     41   @Override protected Multimap<String, Integer> create() {
     42     Multimap<String, Integer> unfiltered = HashMultimap.create();
     43     unfiltered.put("foo", 55556);
     44     unfiltered.put("badkey", 1);
     45     return Multimaps.filterEntries(unfiltered, ENTRY_PREDICATE);
     46   }
     47 
     48   // iterators don't support remove()
     49   // TODO(jlevy): Test logic that doesn't involve iterator.remove()
     50   @Override public void testKeysEntrySetIterator() {}
     51   @Override public void testGetIterator() {}
     52   @Override public void testEntriesUpdate() {}
     53   @Override public void testEntriesIterator() {}
     54   @Override public void testKeySetIterator() {}
     55   @Override public void testValuesIteratorRemove() {}
     56   @Override public void testAsMapEntriesUpdate() {}
     57 
     58   // not serializable
     59   @Override public void testSerializable() {}
     60 
     61   private static final Predicate<String> KEY_PREDICATE
     62       = new Predicate<String>() {
     63         @Override public boolean apply(String key) {
     64           return !"badkey".equals(key);
     65         }
     66   };
     67 
     68   public void testFilterKeys() {
     69     Multimap<String, Integer> unfiltered = HashMultimap.create();
     70     unfiltered.put("foo", 55556);
     71     unfiltered.put("badkey", 1);
     72     Multimap<String, Integer> filtered = Multimaps.filterKeys(unfiltered, KEY_PREDICATE);
     73     assertEquals(1, filtered.size());
     74     assertTrue(filtered.containsEntry("foo", 55556));
     75   }
     76 
     77   private static final Predicate<Integer> VALUE_PREDICATE
     78       = new Predicate<Integer>() {
     79         @Override public boolean apply(Integer value) {
     80           return !((Integer) 55556).equals(value);
     81         }
     82   };
     83 
     84   public void testFilterValues() {
     85     Multimap<String, Integer> unfiltered = HashMultimap.create();
     86     unfiltered.put("foo", 55556);
     87     unfiltered.put("badkey", 1);
     88     Multimap<String, Integer> filtered = Multimaps.filterValues(unfiltered, VALUE_PREDICATE);
     89     assertEquals(1, filtered.size());
     90     assertFalse(filtered.containsEntry("foo", 55556));
     91     assertTrue(filtered.containsEntry("badkey", 1));
     92   }
     93 
     94   public void testFilterFiltered() {
     95     Multimap<String, Integer> unfiltered = HashMultimap.create();
     96     unfiltered.put("foo", 55556);
     97     unfiltered.put("badkey", 1);
     98     unfiltered.put("foo", 1);
     99     Multimap<String, Integer> keyFiltered = Multimaps.filterKeys(unfiltered, KEY_PREDICATE);
    100     Multimap<String, Integer> filtered = Multimaps.filterValues(keyFiltered, VALUE_PREDICATE);
    101     assertEquals(1, filtered.size());
    102     assertTrue(filtered.containsEntry("foo", 1));
    103     assertTrue(filtered.keySet().retainAll(Arrays.asList("cat", "dog")));
    104     assertEquals(0, filtered.size());
    105   }
    106 
    107   // TODO(jlevy): Many more tests needed.
    108 }
    109