Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      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 package com.android.tradefed.util;
     17 
     18 import junit.framework.TestCase;
     19 
     20 import java.util.Map;
     21 
     22 /**
     23  * Unit tests for {@link MultiMap}.
     24  */
     25 public class MultiMapTest extends TestCase {
     26 
     27     /**
     28      * Test for {@link MultiMap#getUniqueMap()}.
     29      */
     30     public void testGetUniqueMap() {
     31         MultiMap<String, String> multiMap = new MultiMap<String, String>();
     32         multiMap.put("key", "value1");
     33         multiMap.put("key", "value2");
     34         multiMap.put("uniquekey", "value");
     35         multiMap.put("key2", "collisionKeyvalue");
     36         Map<String, String> uniqueMap = multiMap.getUniqueMap();
     37         assertEquals(4, uniqueMap.size());
     38         // key's for value1, value2 and collisionKeyvalue might be one of three possible values,
     39         // depending on order of collision resolvement
     40         assertTrue(checkKeyForValue(uniqueMap, "value1"));
     41         assertTrue(checkKeyForValue(uniqueMap, "value2"));
     42         assertTrue(checkKeyForValue(uniqueMap, "collisionKeyvalue"));
     43         // uniquekey should be unmodified
     44         assertEquals("value", uniqueMap.get("uniquekey"));
     45     }
     46 
     47     /**
     48      * Helper method testGetUniqueMap() for that will check that the given value's key matches
     49      * one of the expected values
     50      *
     51      * @param uniqueMap
     52      * @param value
     53      * @return <code>true</code> if key matched one of the expected values
     54      */
     55     private boolean checkKeyForValue(Map<String, String> uniqueMap, String value) {
     56         for (Map.Entry<String, String> entry : uniqueMap.entrySet()) {
     57             if (entry.getValue().equals(value)) {
     58                 String key = entry.getKey();
     59                 return key.equals("key") || key.equals("key2") || key.equals("key2X");
     60             }
     61         }
     62         return false;
     63     }
     64 }
     65