Home | History | Annotate | Download | only in util
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 package libcore.java.util;
     19 
     20 import java.util.AbstractMap;
     21 import java.util.AbstractSet;
     22 import java.util.ArrayList;
     23 import java.util.Iterator;
     24 import java.util.List;
     25 import java.util.Set;
     26 import junit.framework.TestCase;
     27 
     28 public class OldAbstractMapTest extends TestCase {
     29 
     30     public void test_Constructor() {
     31         AMT amt = new AMT();
     32         assertNotNull(amt);
     33     }
     34 
     35     public void test_equalsLjava_lang_Object() {
     36         AbstractMap<String, String> amt1 = new AMT();
     37         AbstractMap<String, String> amt2 = new AMT();
     38         assertTrue("assert 0", amt1.equals(amt2));
     39         assertTrue("assert 1", amt1.equals(amt1));
     40         assertTrue("assert 2", amt2.equals(amt1));
     41         amt1.put("1", "one");
     42         assertFalse("assert 3", amt1.equals(amt2));
     43         amt1.put("2", "two");
     44         amt1.put("3", "three");
     45 
     46         amt2.put("1", "one");
     47         amt2.put("2", "two");
     48         amt2.put("3", "three");
     49         assertTrue("assert 4", amt1.equals(amt2));
     50         assertFalse("assert 5", amt1.equals(this));
     51     }
     52 
     53     public void test_hashCode() {
     54         AMT amt1 = new AMT();
     55         AMT amt2 = new AMT();
     56         amt1.put("1", "one");
     57 
     58         assertNotSame(amt1.hashCode(), amt2.hashCode());
     59     }
     60 
     61     public void test_isEmpty() {
     62         AMT amt = new AMT();
     63         assertTrue(amt.isEmpty());
     64         amt.put("1", "one");
     65         assertFalse(amt.isEmpty());
     66     }
     67 
     68     public void test_put() {
     69         AMT amt = new AMT();
     70         assertEquals(0, amt.size());
     71         amt.put("1", "one");
     72         assertEquals(1, amt.size());
     73         amt.put("2", "two");
     74         assertEquals(2, amt.size());
     75         amt.put("3", "three");
     76         assertEquals(3, amt.size());
     77     }
     78 
     79     public void test_size() {
     80         AMT amt = new AMT();
     81         assertEquals(0, amt.size());
     82         amt.put("1", "one");
     83         assertEquals(1, amt.size());
     84         amt.put("2", "two");
     85         assertEquals(2, amt.size());
     86         amt.put("3", "three");
     87         assertEquals(3, amt.size());
     88     }
     89 
     90     public void test_toString() {
     91         AMT amt = new AMT();
     92         assertEquals("{}", amt.toString());
     93         amt.put("1", "one");
     94         assertEquals("{1=one}", amt.toString());
     95         amt.put("2", "two");
     96         assertEquals("{1=one, 2=two}", amt.toString());
     97         amt.put("3", "three");
     98         assertEquals("{1=one, 2=two, 3=three}", amt.toString());
     99     }
    100 
    101     static class AMT extends AbstractMap<String, String> {
    102         private final List<Entry<String, String>> entries = new ArrayList<Entry<String, String>>();
    103 
    104         @Override public String put(String key, String value) {
    105             String result = remove(key);
    106             entries.add(new AbstractMap.SimpleEntry<String, String>(key, value));
    107             return result;
    108         }
    109 
    110         @Override public Set<Entry<String, String>> entrySet() {
    111             return new AbstractSet<Entry<String, String>>() {
    112                 @Override public Iterator<Entry<String, String>> iterator() {
    113                     return entries.iterator();
    114                 }
    115                 @Override public int size() {
    116                     return entries.size();
    117                 }
    118             };
    119         }
    120     }
    121 }
    122