001 /* 002 * Copyright 2005,2009 Ivan SZKIBA 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.ini4j; 017 018 import static org.junit.Assert.assertEquals; 019 import static org.junit.Assert.assertNull; 020 import static org.junit.Assert.assertTrue; 021 022 import org.junit.Test; 023 024 import java.util.HashMap; 025 import java.util.Map; 026 027 public class CommonMultiMapTest extends Ini4jCase 028 { 029 private static final String KEY = "key"; 030 private static final String VALUE = "value"; 031 private static final String COMMENT = "comment"; 032 033 @Test public void testClearAndRemove() throws Exception 034 { 035 CommonMultiMap<String, String> map = new CommonMultiMap<String, String>(); 036 037 assertNull(map.removeComment(KEY)); 038 039 // 040 map.put(KEY, VALUE); 041 map.clear(); 042 assertTrue(map.isEmpty()); 043 044 // 045 map.put(KEY, VALUE); 046 map.remove(KEY); 047 assertNull(map.getComment(KEY)); 048 049 // 050 map.put(KEY, VALUE); 051 map.remove(KEY, 0); 052 assertNull(map.getComment(KEY)); 053 054 // 055 map.add(KEY, VALUE); 056 map.add(KEY, VALUE); 057 map.putComment(KEY, COMMENT); 058 map.remove(KEY, 0); 059 assertEquals(COMMENT, map.getComment(KEY)); 060 061 // 062 map.put(KEY, VALUE); 063 map.putComment(KEY, COMMENT); 064 assertEquals(COMMENT, map.getComment(KEY)); 065 map.clear(); 066 assertNull(map.getComment(KEY)); 067 068 // 069 map.put(KEY, VALUE); 070 map.putComment(KEY, COMMENT); 071 map.remove(KEY); 072 assertNull(map.getComment(KEY)); 073 074 // 075 map.put(KEY, VALUE); 076 map.putComment(KEY, COMMENT); 077 assertEquals(COMMENT, map.removeComment(KEY)); 078 assertNull(map.getComment(KEY)); 079 080 // 081 map.put(KEY, VALUE); 082 map.putComment(KEY, COMMENT); 083 map.remove(KEY, 0); 084 assertNull(map.getComment(KEY)); 085 } 086 087 @Test public void testPutAll() throws Exception 088 { 089 CommonMultiMap<String, String> map = new CommonMultiMap<String, String>(); 090 CommonMultiMap<String, String> copy = new CommonMultiMap<String, String>(); 091 092 map.put(KEY, VALUE); 093 map.putComment(KEY, COMMENT); 094 copy.putAll(map); 095 assertEquals(COMMENT, copy.getComment(KEY)); 096 Map<String, String> simple = new HashMap<String, String>(); 097 098 simple.put(KEY, VALUE); 099 copy.clear(); 100 assertTrue(copy.isEmpty()); 101 copy.putAll(simple); 102 assertNull(copy.getComment(KEY)); 103 assertEquals(VALUE, copy.get(KEY)); 104 105 // 106 map = new CommonMultiMap<String, String>(); 107 map.put(KEY, VALUE); 108 copy.clear(); 109 copy.putAll(map); 110 assertEquals(VALUE, copy.get(KEY)); 111 assertNull(copy.getComment(KEY)); 112 } 113 }