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