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.assertArrayEquals;
019    import static org.junit.Assert.assertEquals;
020    import static org.junit.Assert.assertFalse;
021    import static org.junit.Assert.assertNotNull;
022    import static org.junit.Assert.assertNull;
023    import static org.junit.Assert.assertTrue;
024    
025    import org.junit.Before;
026    import org.junit.Test;
027    
028    import java.util.Arrays;
029    import java.util.HashMap;
030    import java.util.Map;
031    import java.util.Map.Entry;
032    import java.util.Set;
033    
034    public class BasicMultiMapTest extends Ini4jCase
035    {
036        private static final String KEY1 = "key1";
037        private static final String KEY2 = "key2";
038        private static final String KEY3 = "key3";
039        private static final String VALUE1 = "value1";
040        private static final String VALUE2 = "value2";
041        private static final String VALUE3 = "value3";
042        private static final String[] VALUES = { VALUE1, VALUE2, VALUE3 };
043        private MultiMap<String, String> _map;
044    
045        @Before @Override public void setUp() throws Exception
046        {
047            super.setUp();
048            _map = new BasicMultiMap<String, String>();
049        }
050    
051        @Test public void testAdd()
052        {
053            _map.add(KEY1, VALUE1);
054            _map.add(KEY1, VALUE2);
055            _map.add(KEY1, VALUE3);
056            assertEquals(3, _map.length(KEY1));
057            _map.add(KEY1, VALUE3, 0);
058            assertEquals(4, _map.length(KEY1));
059            assertEquals(VALUE3, _map.get(KEY1, 0));
060            assertEquals(VALUE3, _map.get(KEY1, 3));
061            _map.clear();
062            assertTrue(_map.isEmpty());
063        }
064    
065        @Test public void testAll()
066        {
067            _map.putAll(KEY1, Arrays.asList(VALUES));
068            assertEquals(VALUES.length, _map.length(KEY1));
069            String[] values = _map.getAll(KEY1).toArray(new String[] {});
070    
071            assertArrayEquals(VALUES, values);
072        }
073    
074        @Test public void testContainsValue()
075        {
076            _map.putAll(KEY1, Arrays.asList(VALUES));
077            assertTrue(_map.containsValue(VALUE1));
078            assertTrue(_map.containsValue(VALUE2));
079            assertTrue(_map.containsValue(VALUE3));
080            _map.clear();
081            _map.put(KEY2, VALUE1);
082            assertFalse(_map.containsValue(VALUE3));
083        }
084    
085        @Test public void testEntrySet()
086        {
087            _map.putAll(KEY1, Arrays.asList(VALUES));
088            _map.put(KEY2, VALUE2);
089            _map.put(KEY3, VALUE3);
090            Set<Entry<String, String>> set = _map.entrySet();
091    
092            assertNotNull(set);
093            assertEquals(3, set.size());
094            for (Entry<String, String> e : set)
095            {
096                if (e.getKey().equals(KEY1))
097                {
098                    assertEquals(VALUES[2], e.getValue());
099                    e.setValue(VALUES[1]);
100                }
101                else if (e.getKey().equals(KEY2))
102                {
103                    assertEquals(VALUE2, e.getValue());
104                    e.setValue(VALUE3);
105                }
106                else if (e.getKey().equals(KEY3))
107                {
108                    assertEquals(VALUE3, e.getValue());
109                    e.setValue(VALUE2);
110                }
111            }
112    
113            assertEquals(VALUES[1], _map.get(KEY1));
114            assertEquals(VALUES.length, _map.length(KEY1));
115            assertEquals(VALUE3, _map.get(KEY2));
116            assertEquals(VALUE2, _map.get(KEY3));
117        }
118    
119        @Test public void testGetEmpty()
120        {
121            assertNull(_map.get(KEY1));
122            assertNull(_map.get(KEY1, 1));
123        }
124    
125        @Test public void testPut()
126        {
127            _map.put(KEY1, VALUE1);
128            _map.add(KEY1, VALUE2);
129            assertEquals(VALUE2, _map.get(KEY1, 1));
130            _map.put(KEY1, VALUE3, 1);
131            assertEquals(VALUE3, _map.get(KEY1, 1));
132            assertEquals(VALUE3, _map.get(KEY1));
133        }
134    
135        @Test public void testPutAll()
136        {
137            _map.put(KEY1, VALUE1);
138            _map.put(KEY2, VALUE1);
139            _map.add(KEY2, VALUE2);
140            MultiMap<String, String> other = new BasicMultiMap<String, String>();
141    
142            other.putAll(_map);
143            assertEquals(2, other.size());
144            assertEquals(2, other.length(KEY2));
145            assertEquals(1, other.length(KEY1));
146            assertEquals(VALUE1, _map.get(KEY1));
147            assertEquals(VALUE1, _map.get(KEY2, 0));
148            assertEquals(VALUE2, _map.get(KEY2, 1));
149            Map<String, String> regular = new HashMap<String, String>(_map);
150    
151            _map.clear();
152            _map.putAll(regular);
153            assertEquals(regular.keySet(), _map.keySet());
154        }
155    
156        @Test public void testRemove()
157        {
158            _map.add(KEY1, VALUE1);
159            _map.add(KEY2, VALUE1);
160            _map.add(KEY2, VALUE2);
161            _map.add(KEY3, VALUE1);
162            _map.add(KEY3, VALUE2);
163            _map.add(KEY3, VALUE3);
164            assertEquals(VALUE2, _map.get(KEY3, 1));
165            _map.remove(KEY3, 1);
166            assertEquals(VALUE3, _map.get(KEY3, 1));
167            _map.remove(KEY3, 1);
168            assertEquals(VALUE1, _map.get(KEY3));
169            _map.remove(KEY3, 0);
170            assertEquals(0, _map.length(KEY3));
171            assertFalse(_map.containsKey(KEY3));
172            _map.remove(KEY2);
173            assertFalse(_map.containsKey(KEY2));
174            _map.remove(KEY1);
175            assertFalse(_map.containsKey(KEY1));
176            assertEquals(0, _map.size());
177            assertTrue(_map.isEmpty());
178            assertNull(_map.remove(KEY1));
179            assertNull(_map.remove(KEY1, 1));
180        }
181    
182        @Test public void testValues()
183        {
184            _map.put(KEY1, VALUE1);
185            _map.put(KEY2, VALUE2);
186            _map.add(KEY2, VALUE3);
187            String[] values = _map.values().toArray(new String[] {});
188    
189            Arrays.sort(values);
190            assertArrayEquals(values, VALUES);
191        }
192    }