2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.util;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
15 import com.google.common.collect.Iterables;
16 import com.google.common.collect.Maps;
17 import com.romix.scala.collection.concurrent.TrieMap;
18 import java.util.Collection;
20 import java.util.Map.Entry;
22 import org.junit.Test;
24 public class ReadWriteTrieMapTest {
27 public void testMethodsOfReadWriteTrieMap() {
28 final TrieMap<String, String> trieMap = new TrieMap<>();
29 trieMap.put("0", "zero");
30 trieMap.put("1", "one");
32 final ReadWriteTrieMap<String, String> readWriteTrieMap = new ReadWriteTrieMap<String, String>(trieMap, 5);
34 assertNotNull("Object readOnlyTrieMap shouldn't be 'null'.", readWriteTrieMap);
36 assertEquals("Size of readOnlyTrieMap should be '5'.", 5, readWriteTrieMap.size());
37 assertFalse("Object readOnlyTrieMap shouldn't be empty.", readWriteTrieMap.isEmpty());
39 assertTrue("Object readOnlyTrieMap should have key '0'.", readWriteTrieMap.containsKey("0"));
40 assertTrue("Object readOnlyTrieMap should have value 'zero'.", readWriteTrieMap.containsValue("zero"));
41 assertEquals("Object readOnlyTrieMap should have value 'zero'.", "zero", readWriteTrieMap.get("0"));
43 final Map<String, String> rwMap = readWriteTrieMap;
44 rwMap.put("2", "two");
45 rwMap.put("3", "three");
47 assertEquals("Removed value from readOnlyTrieMap should be 'one'.", "one", rwMap.remove("1"));
49 final Set<String> trieMapKeySet = readWriteTrieMap.keySet();
50 assertEquals("Size of keySet should be '3'.", 3, Iterables.size(trieMapKeySet));
52 final Collection<String> trieMapValues = readWriteTrieMap.values();
53 assertEquals("Size of values should be '3'.", 3, Iterables.size(trieMapValues));
55 assertTrue("Entry set of readWriteTrieMap and trieMap should by equals.", convertSetEntryToMap(readWriteTrieMap.entrySet()).equals(trieMap));
57 trieMap.put("2", "two");
58 final ReadWriteTrieMap<String, String> readWriteTrieMap2 = new ReadWriteTrieMap<String, String>(trieMap, 4);
60 assertFalse("Objects readWriteTrieMap and readOnlyTrieMap2 should be different.", readWriteTrieMap.equals(readWriteTrieMap2));
61 assertFalse("Hash codes of object readWriteTrieMap and readOnelyTrieMap2 should be different.", readWriteTrieMap.hashCode() == readWriteTrieMap2.hashCode());
63 final Map<String, String> readOnlyTrieMap = readWriteTrieMap.toReadOnly();
65 readWriteTrieMap.clear();
66 assertEquals("Size of readOnlyTrieMap should be '0'.", 0, readWriteTrieMap.size());
69 public Map<String, String> convertSetEntryToMap(Set<Entry<String, String>> input) {
70 Map<String, String> resultMap = Maps.newHashMap();
71 for (Entry<String, String> entry : input) {
72 resultMap.put(entry.getKey(), entry.getValue());