Committed by
Ray Milkey
Consolidate code clones in Persistent* tests.
Change-Id: Ib46e300a3b3af36eab923e43f1b1faee7b1e2d38
Showing
3 changed files
with
75 additions
and
62 deletions
| 1 | +/* | ||
| 2 | + * Copyright 2016-present 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 | +package org.onosproject.persistence.impl; | ||
| 17 | + | ||
| 18 | +import org.junit.After; | ||
| 19 | +import org.junit.Before; | ||
| 20 | +import org.junit.Rule; | ||
| 21 | +import org.junit.rules.TemporaryFolder; | ||
| 22 | +import org.mapdb.DB; | ||
| 23 | +import org.mapdb.DBMaker; | ||
| 24 | + | ||
| 25 | +/** | ||
| 26 | + * Utils for Tests using MapDB. | ||
| 27 | + */ | ||
| 28 | +public abstract class MapDBTest { | ||
| 29 | + | ||
| 30 | + @Rule | ||
| 31 | + public TemporaryFolder tmpFolder = new TemporaryFolder(); | ||
| 32 | + | ||
| 33 | + protected DB fakeDB = null; | ||
| 34 | + | ||
| 35 | + /** | ||
| 36 | + * Set up the database. | ||
| 37 | + * | ||
| 38 | + * @throws Exception if instantiation fails | ||
| 39 | + */ | ||
| 40 | + @Before | ||
| 41 | + public void setUpDB() throws Exception { | ||
| 42 | + // Creates a db | ||
| 43 | + fakeDB = DBMaker | ||
| 44 | + .newFileDB(tmpFolder.newFile()) | ||
| 45 | + .asyncWriteEnable() | ||
| 46 | + .commitFileSyncDisable() | ||
| 47 | + .mmapFileEnableIfSupported() | ||
| 48 | + .closeOnJvmShutdown() | ||
| 49 | + .deleteFilesAfterClose() | ||
| 50 | + .make(); | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + /** | ||
| 54 | + * Closes the database. | ||
| 55 | + * | ||
| 56 | + * @throws Exception if shutdown fails | ||
| 57 | + */ | ||
| 58 | + @After | ||
| 59 | + public void tearDownDB() throws Exception { | ||
| 60 | + fakeDB.close(); | ||
| 61 | + } | ||
| 62 | +} |
| ... | @@ -17,13 +17,8 @@ | ... | @@ -17,13 +17,8 @@ |
| 17 | package org.onosproject.persistence.impl; | 17 | package org.onosproject.persistence.impl; |
| 18 | 18 | ||
| 19 | import com.google.common.collect.Maps; | 19 | import com.google.common.collect.Maps; |
| 20 | -import org.junit.After; | ||
| 21 | import org.junit.Before; | 20 | import org.junit.Before; |
| 22 | -import org.junit.Rule; | ||
| 23 | import org.junit.Test; | 21 | import org.junit.Test; |
| 24 | -import org.junit.rules.TemporaryFolder; | ||
| 25 | -import org.mapdb.DB; | ||
| 26 | -import org.mapdb.DBMaker; | ||
| 27 | import org.onosproject.store.service.Serializer; | 22 | import org.onosproject.store.service.Serializer; |
| 28 | 23 | ||
| 29 | import java.util.Map; | 24 | import java.util.Map; |
| ... | @@ -37,13 +32,9 @@ import static org.junit.Assert.assertTrue; | ... | @@ -37,13 +32,9 @@ import static org.junit.Assert.assertTrue; |
| 37 | /** | 32 | /** |
| 38 | * Test suite for Persistent Map. | 33 | * Test suite for Persistent Map. |
| 39 | */ | 34 | */ |
| 40 | -public class PersistentMapTest { | 35 | +public class PersistentMapTest extends MapDBTest { |
| 41 | 36 | ||
| 42 | - private Map<Integer, Integer> map = null; | 37 | + private PersistentMap<Integer, Integer> map = null; |
| 43 | - private DB fakeDB = null; | ||
| 44 | - | ||
| 45 | - @Rule | ||
| 46 | - public TemporaryFolder tmpFolder = new TemporaryFolder(); | ||
| 47 | 38 | ||
| 48 | /** | 39 | /** |
| 49 | * Set up the database, create a map and a direct executor to handle it. | 40 | * Set up the database, create a map and a direct executor to handle it. |
| ... | @@ -52,13 +43,8 @@ public class PersistentMapTest { | ... | @@ -52,13 +43,8 @@ public class PersistentMapTest { |
| 52 | */ | 43 | */ |
| 53 | @Before | 44 | @Before |
| 54 | public void setUp() throws Exception { | 45 | public void setUp() throws Exception { |
| 55 | - //Creates a db, a map within it and a basic integer serializer (async writing is off) | 46 | + //Creates, a map within it and a basic integer serializer |
| 56 | - fakeDB = DBMaker | 47 | + map = new PersistentMap<>(new Serializer() { |
| 57 | - .newFileDB(tmpFolder.newFile("testDb")) | ||
| 58 | - .asyncWriteEnable() | ||
| 59 | - .closeOnJvmShutdown() | ||
| 60 | - .make(); | ||
| 61 | - map = new PersistentMap<Integer, Integer>(new Serializer() { | ||
| 62 | @Override | 48 | @Override |
| 63 | public <T> byte[] encode(T object) { | 49 | public <T> byte[] encode(T object) { |
| 64 | if (object == null) { | 50 | if (object == null) { |
| ... | @@ -86,31 +72,18 @@ public class PersistentMapTest { | ... | @@ -86,31 +72,18 @@ public class PersistentMapTest { |
| 86 | num = num | bytes[2] << 8; | 72 | num = num | bytes[2] << 8; |
| 87 | num = num | bytes[3]; | 73 | num = num | bytes[3]; |
| 88 | 74 | ||
| 89 | - return (T) new java.lang.Integer(num); | 75 | + return (T) Integer.valueOf(num); |
| 90 | } | 76 | } |
| 91 | }, fakeDB, "map"); | 77 | }, fakeDB, "map"); |
| 92 | } | 78 | } |
| 93 | 79 | ||
| 94 | - /** | ||
| 95 | - * Clears and deletes the map, closes the datbase and deletes the file. | ||
| 96 | - * | ||
| 97 | - * @throws Exception if shutdown fails | ||
| 98 | - */ | ||
| 99 | - @After | ||
| 100 | - public void tearDown() throws Exception { | ||
| 101 | - map.clear(); | ||
| 102 | - fakeDB.delete("map:map"); | ||
| 103 | - fakeDB.commit(); | ||
| 104 | - fakeDB.close(); | ||
| 105 | - } | ||
| 106 | - | ||
| 107 | @Test | 80 | @Test |
| 108 | public void testRemove() throws Exception { | 81 | public void testRemove() throws Exception { |
| 109 | //Checks removal and return values | 82 | //Checks removal and return values |
| 110 | fillMap(10); | 83 | fillMap(10); |
| 111 | assertEquals(10, map.size()); | 84 | assertEquals(10, map.size()); |
| 112 | for (int i = 0; i < 10; i++) { | 85 | for (int i = 0; i < 10; i++) { |
| 113 | - assertEquals("The previous value was wrong.", new Integer(i), map.remove(i)); | 86 | + assertEquals("The previous value was wrong.", Integer.valueOf(i), map.remove(i)); |
| 114 | assertNull("The previous value was wrong.", map.remove(i)); | 87 | assertNull("The previous value was wrong.", map.remove(i)); |
| 115 | //(i+1) compensates for base zero. | 88 | //(i+1) compensates for base zero. |
| 116 | assertEquals("The size was wrong.", 10 - (i + 1), map.size()); | 89 | assertEquals("The size was wrong.", 10 - (i + 1), map.size()); |
| ... | @@ -160,7 +133,7 @@ public class PersistentMapTest { | ... | @@ -160,7 +133,7 @@ public class PersistentMapTest { |
| 160 | for (int i = 0; i < 10; i++) { | 133 | for (int i = 0; i < 10; i++) { |
| 161 | map.put(i, i); | 134 | map.put(i, i); |
| 162 | for (int j = 0; j <= i; j++) { | 135 | for (int j = 0; j <= i; j++) { |
| 163 | - assertEquals("The value was wrong.", new Integer(j), map.get(j)); | 136 | + assertEquals("The value was wrong.", Integer.valueOf(j), map.get(j)); |
| 164 | } | 137 | } |
| 165 | } | 138 | } |
| 166 | assertNull("Null return value for nonexistent keys.", map.get(10)); | 139 | assertNull("Null return value for nonexistent keys.", map.get(10)); |
| ... | @@ -226,7 +199,7 @@ public class PersistentMapTest { | ... | @@ -226,7 +199,7 @@ public class PersistentMapTest { |
| 226 | //Tests insertion behavior (particularly the returning of previous value) | 199 | //Tests insertion behavior (particularly the returning of previous value) |
| 227 | fillMap(10); | 200 | fillMap(10); |
| 228 | for (int i = 0; i < 10; i++) { | 201 | for (int i = 0; i < 10; i++) { |
| 229 | - assertEquals("Put should return the previous value", new Integer(i), map.put(i, i + 1)); | 202 | + assertEquals("Put should return the previous value", Integer.valueOf(i), map.put(i, i + 1)); |
| 230 | } | 203 | } |
| 231 | assertNull(map.put(11, 11)); | 204 | assertNull(map.put(11, 11)); |
| 232 | } | 205 | } | ... | ... |
| ... | @@ -17,13 +17,8 @@ | ... | @@ -17,13 +17,8 @@ |
| 17 | package org.onosproject.persistence.impl; | 17 | package org.onosproject.persistence.impl; |
| 18 | 18 | ||
| 19 | import com.google.common.collect.Sets; | 19 | import com.google.common.collect.Sets; |
| 20 | -import org.junit.After; | ||
| 21 | import org.junit.Before; | 20 | import org.junit.Before; |
| 22 | -import org.junit.Rule; | ||
| 23 | import org.junit.Test; | 21 | import org.junit.Test; |
| 24 | -import org.junit.rules.TemporaryFolder; | ||
| 25 | -import org.mapdb.DB; | ||
| 26 | -import org.mapdb.DBMaker; | ||
| 27 | import org.onosproject.store.service.Serializer; | 22 | import org.onosproject.store.service.Serializer; |
| 28 | 23 | ||
| 29 | import java.util.HashSet; | 24 | import java.util.HashSet; |
| ... | @@ -40,23 +35,14 @@ import static org.junit.Assert.assertTrue; | ... | @@ -40,23 +35,14 @@ import static org.junit.Assert.assertTrue; |
| 40 | /** | 35 | /** |
| 41 | * Test suite for Persistent Set. | 36 | * Test suite for Persistent Set. |
| 42 | */ | 37 | */ |
| 43 | -public class PersistentSetTest { | 38 | +public class PersistentSetTest extends MapDBTest { |
| 44 | 39 | ||
| 45 | - private Set<Integer> set = null; | 40 | + private PersistentSet<Integer> set = null; |
| 46 | - private DB fakeDB = null; | ||
| 47 | - | ||
| 48 | - @Rule | ||
| 49 | - public TemporaryFolder tmpFolder = new TemporaryFolder(); | ||
| 50 | 41 | ||
| 51 | @Before | 42 | @Before |
| 52 | public void setUp() throws Exception { | 43 | public void setUp() throws Exception { |
| 53 | - //Creates a db, a set within it and a basic integer serializer (async writing is off) | 44 | + //Creates a set within it and a basic integer serializer |
| 54 | - fakeDB = DBMaker | 45 | + set = new PersistentSet<>(new Serializer() { |
| 55 | - .newFileDB(tmpFolder.newFile("testDb")) | ||
| 56 | - .asyncWriteEnable() | ||
| 57 | - .closeOnJvmShutdown() | ||
| 58 | - .make(); | ||
| 59 | - set = new PersistentSet<Integer>(new Serializer() { | ||
| 60 | @Override | 46 | @Override |
| 61 | public <T> byte[] encode(T object) { | 47 | public <T> byte[] encode(T object) { |
| 62 | if (object == null) { | 48 | if (object == null) { |
| ... | @@ -84,20 +70,12 @@ public class PersistentSetTest { | ... | @@ -84,20 +70,12 @@ public class PersistentSetTest { |
| 84 | num = num | bytes[2] << 8; | 70 | num = num | bytes[2] << 8; |
| 85 | num = num | bytes[3]; | 71 | num = num | bytes[3]; |
| 86 | 72 | ||
| 87 | - return (T) new java.lang.Integer(num); | 73 | + return (T) Integer.valueOf(num); |
| 88 | } | 74 | } |
| 89 | }, fakeDB, "set"); | 75 | }, fakeDB, "set"); |
| 90 | 76 | ||
| 91 | } | 77 | } |
| 92 | 78 | ||
| 93 | - @After | ||
| 94 | - public void tearDown() throws Exception { | ||
| 95 | - set.clear(); | ||
| 96 | - fakeDB.delete("map:map"); | ||
| 97 | - fakeDB.commit(); | ||
| 98 | - fakeDB.close(); | ||
| 99 | - } | ||
| 100 | - | ||
| 101 | @Test | 79 | @Test |
| 102 | public void testSize() throws Exception { | 80 | public void testSize() throws Exception { |
| 103 | //Check correct sizing throughout population | 81 | //Check correct sizing throughout population | ... | ... |
-
Please register or login to post a comment