Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
| 17 | package org.onosproject.persistence.impl.test; |
| 18 | |
| 19 | import com.google.common.collect.Maps; |
| 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.mapdb.DB; |
| 24 | import org.mapdb.DBMaker; |
| 25 | import org.onosproject.persistence.impl.PersistentMap; |
| 26 | import org.onosproject.store.service.Serializer; |
| 27 | |
| 28 | import java.nio.file.Paths; |
| 29 | import java.util.Map; |
| 30 | import java.util.Set; |
| 31 | |
| 32 | import static org.junit.Assert.assertEquals; |
| 33 | import static org.junit.Assert.assertFalse; |
| 34 | import static org.junit.Assert.assertNull; |
| 35 | import static org.junit.Assert.assertTrue; |
| 36 | |
| 37 | /** |
| 38 | * Test suite for Persistent Map. |
| 39 | */ |
| 40 | public class PersistentMapTest { |
| 41 | |
| 42 | private Map<Integer, Integer> map = null; |
| 43 | private DB fakeDB = null; |
| 44 | |
| 45 | |
| 46 | /** |
| 47 | * Set up the database, create a map and a direct executor to handle it. |
| 48 | * |
| 49 | * @throws Exception if instantiation fails |
| 50 | */ |
| 51 | @Before |
| 52 | public void setUp() throws Exception { |
| 53 | //Creates a db, a map within it and a basic integer serializer (async writing is off) |
| 54 | fakeDB = DBMaker |
| 55 | .newFileDB(Paths.get("../testDb").toFile()) |
| 56 | .asyncWriteEnable() |
| 57 | .closeOnJvmShutdown() |
| 58 | .make(); |
| 59 | map = new PersistentMap<Integer, Integer>(new Serializer() { |
| 60 | @Override |
| 61 | public <T> byte[] encode(T object) { |
| 62 | if (object == null) { |
| 63 | return null; |
| 64 | } |
| 65 | int num = (Integer) object; |
| 66 | byte[] result = new byte[4]; |
| 67 | |
| 68 | result[0] = (byte) (num >> 24); |
| 69 | result[1] = (byte) (num >> 16); |
| 70 | result[2] = (byte) (num >> 8); |
| 71 | result[3] = (byte) num; |
| 72 | return result; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public <T> T decode(byte[] bytes) { |
| 77 | if (bytes == null) { |
| 78 | return null; |
| 79 | } |
| 80 | int num = 0x00000000; |
| 81 | |
| 82 | num = num | bytes[0] << 24; |
| 83 | num = num | bytes[1] << 16; |
| 84 | num = num | bytes[2] << 8; |
| 85 | num = num | bytes[3]; |
| 86 | |
| 87 | return (T) new java.lang.Integer(num); |
| 88 | } |
| 89 | }, fakeDB, "map"); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Clears and deletes the map, closes the datbase and deletes the file. |
| 94 | * |
| 95 | * @throws Exception if shutdown fails |
| 96 | */ |
| 97 | @After |
| 98 | public void tearDown() throws Exception { |
| 99 | map.clear(); |
| 100 | fakeDB.delete("map:map"); |
| 101 | fakeDB.commit(); |
| 102 | fakeDB.close(); |
| 103 | //This is key to prevent artifacts persisting between tests. |
| 104 | Paths.get("../testDB").toFile().delete(); |
| 105 | |
| 106 | |
| 107 | } |
| 108 | |
| 109 | @Test |
| 110 | public void testRemove() throws Exception { |
| 111 | //Checks removal and return values |
| 112 | fillMap(10); |
| 113 | assertEquals(10, map.size()); |
| 114 | for (int i = 0; i < 10; i++) { |
| 115 | assertEquals("The previous value was wrong.", new Integer(i), map.remove(i)); |
| 116 | assertNull("The previous value was wrong.", map.remove(i)); |
| 117 | //(i+1) compensates for base zero. |
| 118 | assertEquals("The size was wrong.", 10 - (i + 1), map.size()); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | @Test |
| 123 | public void testSize() throws Exception { |
| 124 | //Checks size values throughout addition and removal |
| 125 | for (int i = 0; i < 10; i++) { |
| 126 | map.put(i, i); |
| 127 | assertEquals("The map size is wrong.", i + 1, map.size()); |
| 128 | } |
| 129 | for (int i = 0; i < 10; i++) { |
| 130 | map.remove(i); |
| 131 | assertEquals("The map size is wrong.", 9 - i, map.size()); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | @Test |
| 136 | public void testIsEmpty() throws Exception { |
| 137 | //Checks empty condition |
| 138 | //asserts that the map starts out empty |
| 139 | assertTrue("Map should be empty", map.isEmpty()); |
| 140 | map.put(1, 1); |
| 141 | assertFalse("Map shouldn't be empty.", map.isEmpty()); |
| 142 | map.remove(1); |
| 143 | assertTrue("Map should be empty", map.isEmpty()); |
| 144 | } |
| 145 | |
| 146 | @Test |
| 147 | public void testContains() throws Exception { |
| 148 | //Checks both containsKey and containsValue be aware the implementations vary widely (value does not use mapDB |
| 149 | //due to object '=='being an insufficient check) |
| 150 | for (int i = 0; i < 10; i++) { |
| 151 | assertFalse("Map should not contain the key", map.containsKey(i)); |
| 152 | assertFalse("Map should not contain the value", map.containsValue(i)); |
| 153 | map.put(i, i); |
| 154 | assertTrue("Map should contain the key", map.containsKey(i)); |
| 155 | assertTrue("Map should contain the value", map.containsValue(i)); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | @Test |
| 160 | public void testGet() throws Exception { |
| 161 | //Tests value retrieval and nonexistent key return values |
| 162 | for (int i = 0; i < 10; i++) { |
| 163 | map.put(i, i); |
| 164 | for (int j = 0; j <= i; j++) { |
| 165 | assertEquals("The value was wrong.", new Integer(j), map.get(j)); |
| 166 | } |
| 167 | } |
| 168 | assertNull("Null return value for nonexistent keys.", map.get(10)); |
| 169 | } |
| 170 | |
| 171 | @Test |
| 172 | public void testPutAll() throws Exception { |
| 173 | //Tests adding of an outside map |
| 174 | Map<Integer, Integer> testMap = Maps.newHashMap(); |
| 175 | fillMap(10); |
| 176 | map.putAll(testMap); |
| 177 | for (int i = 0; i < 10; i++) { |
| 178 | assertTrue("The map should contain the current 'i' value.", map.containsKey(i)); |
| 179 | assertTrue("The map should contain the current 'i' value.", map.containsValue(i)); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | @Test |
| 184 | public void testClear() throws Exception { |
| 185 | //Tests clearing the map |
| 186 | assertTrue("Map was initialized incorrectly, should be empty.", map.isEmpty()); |
| 187 | fillMap(10); |
| 188 | assertFalse("Map should contain entries now.", map.isEmpty()); |
| 189 | map.clear(); |
| 190 | assertTrue("Map should have been cleared of entries.", map.isEmpty()); |
| 191 | |
| 192 | } |
| 193 | |
| 194 | @Test |
| 195 | public void testKeySet() throws Exception { |
| 196 | //Tests key set generation |
| 197 | fillMap(10); |
| 198 | Set<Integer> keys = map.keySet(); |
| 199 | for (int i = 0; i < 10; i++) { |
| 200 | assertTrue("The key set doesn't contain all keys 0-9", keys.contains(i)); |
| 201 | } |
| 202 | assertEquals("The key set has an incorrect number of entries", 10, keys.size()); |
| 203 | } |
| 204 | |
| 205 | @Test |
| 206 | public void testValues() throws Exception { |
| 207 | //Tests value set generation |
| 208 | fillMap(10); |
| 209 | Set<Integer> values = (Set<Integer>) map.values(); |
| 210 | for (int i = 0; i < 10; i++) { |
| 211 | assertTrue("The key set doesn't contain all keys 0-9", values.contains(i)); |
| 212 | } |
| 213 | assertEquals("The key set has an incorrect number of entries", 10, values.size()); |
| 214 | } |
| 215 | |
| 216 | @Test |
| 217 | public void testEntrySet() throws Exception { |
| 218 | //Test entry set generation (violates abstraction by knowing the type of the returned entries) |
| 219 | fillMap(10); |
| 220 | Set<Map.Entry<Integer, Integer>> entries = map.entrySet(); |
| 221 | for (int i = 0; i < 10; i++) { |
| 222 | assertTrue("The key set doesn't contain all keys 0-9", entries.contains(Maps.immutableEntry(i, i))); |
| 223 | } |
| 224 | assertEquals("The key set has an incorrect number of entries", 10, entries.size()); |
| 225 | } |
| 226 | |
| 227 | @Test public void testPut() throws Exception { |
| 228 | //Tests insertion behavior (particularly the returning of previous value) |
| 229 | fillMap(10); |
| 230 | for (int i = 0; i < 10; i++) { |
| 231 | assertEquals("Put should return the previous value", new Integer(i), map.put(i, i + 1)); |
| 232 | } |
| 233 | assertNull(map.put(11, 11)); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Populated the map with pairs of integers from (0, 0) up to (numEntries - 1, numEntries -1). |
| 238 | * @param numEntries number of entries to add |
| 239 | */ |
| 240 | private void fillMap(int numEntries) { |
| 241 | for (int i = 0; i < numEntries; i++) { |
| 242 | map.put(i, i); |
| 243 | } |
| 244 | } |
| 245 | } |