HIGUCHI Yuta
Committed by Ray Milkey

Consolidate code clones in Persistent* tests.

Change-Id: Ib46e300a3b3af36eab923e43f1b1faee7b1e2d38
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.persistence.impl;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import org.mapdb.DB;
import org.mapdb.DBMaker;
/**
* Utils for Tests using MapDB.
*/
public abstract class MapDBTest {
@Rule
public TemporaryFolder tmpFolder = new TemporaryFolder();
protected DB fakeDB = null;
/**
* Set up the database.
*
* @throws Exception if instantiation fails
*/
@Before
public void setUpDB() throws Exception {
// Creates a db
fakeDB = DBMaker
.newFileDB(tmpFolder.newFile())
.asyncWriteEnable()
.commitFileSyncDisable()
.mmapFileEnableIfSupported()
.closeOnJvmShutdown()
.deleteFilesAfterClose()
.make();
}
/**
* Closes the database.
*
* @throws Exception if shutdown fails
*/
@After
public void tearDownDB() throws Exception {
fakeDB.close();
}
}
......@@ -17,13 +17,8 @@
package org.onosproject.persistence.impl;
import com.google.common.collect.Maps;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mapdb.DB;
import org.mapdb.DBMaker;
import org.onosproject.store.service.Serializer;
import java.util.Map;
......@@ -37,13 +32,9 @@ import static org.junit.Assert.assertTrue;
/**
* Test suite for Persistent Map.
*/
public class PersistentMapTest {
public class PersistentMapTest extends MapDBTest {
private Map<Integer, Integer> map = null;
private DB fakeDB = null;
@Rule
public TemporaryFolder tmpFolder = new TemporaryFolder();
private PersistentMap<Integer, Integer> map = null;
/**
* Set up the database, create a map and a direct executor to handle it.
......@@ -52,13 +43,8 @@ public class PersistentMapTest {
*/
@Before
public void setUp() throws Exception {
//Creates a db, a map within it and a basic integer serializer (async writing is off)
fakeDB = DBMaker
.newFileDB(tmpFolder.newFile("testDb"))
.asyncWriteEnable()
.closeOnJvmShutdown()
.make();
map = new PersistentMap<Integer, Integer>(new Serializer() {
//Creates, a map within it and a basic integer serializer
map = new PersistentMap<>(new Serializer() {
@Override
public <T> byte[] encode(T object) {
if (object == null) {
......@@ -86,31 +72,18 @@ public class PersistentMapTest {
num = num | bytes[2] << 8;
num = num | bytes[3];
return (T) new java.lang.Integer(num);
return (T) Integer.valueOf(num);
}
}, fakeDB, "map");
}
/**
* Clears and deletes the map, closes the datbase and deletes the file.
*
* @throws Exception if shutdown fails
*/
@After
public void tearDown() throws Exception {
map.clear();
fakeDB.delete("map:map");
fakeDB.commit();
fakeDB.close();
}
@Test
public void testRemove() throws Exception {
//Checks removal and return values
fillMap(10);
assertEquals(10, map.size());
for (int i = 0; i < 10; i++) {
assertEquals("The previous value was wrong.", new Integer(i), map.remove(i));
assertEquals("The previous value was wrong.", Integer.valueOf(i), map.remove(i));
assertNull("The previous value was wrong.", map.remove(i));
//(i+1) compensates for base zero.
assertEquals("The size was wrong.", 10 - (i + 1), map.size());
......@@ -160,7 +133,7 @@ public class PersistentMapTest {
for (int i = 0; i < 10; i++) {
map.put(i, i);
for (int j = 0; j <= i; j++) {
assertEquals("The value was wrong.", new Integer(j), map.get(j));
assertEquals("The value was wrong.", Integer.valueOf(j), map.get(j));
}
}
assertNull("Null return value for nonexistent keys.", map.get(10));
......@@ -226,7 +199,7 @@ public class PersistentMapTest {
//Tests insertion behavior (particularly the returning of previous value)
fillMap(10);
for (int i = 0; i < 10; i++) {
assertEquals("Put should return the previous value", new Integer(i), map.put(i, i + 1));
assertEquals("Put should return the previous value", Integer.valueOf(i), map.put(i, i + 1));
}
assertNull(map.put(11, 11));
}
......
......@@ -17,13 +17,8 @@
package org.onosproject.persistence.impl;
import com.google.common.collect.Sets;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mapdb.DB;
import org.mapdb.DBMaker;
import org.onosproject.store.service.Serializer;
import java.util.HashSet;
......@@ -40,23 +35,14 @@ import static org.junit.Assert.assertTrue;
/**
* Test suite for Persistent Set.
*/
public class PersistentSetTest {
public class PersistentSetTest extends MapDBTest {
private Set<Integer> set = null;
private DB fakeDB = null;
@Rule
public TemporaryFolder tmpFolder = new TemporaryFolder();
private PersistentSet<Integer> set = null;
@Before
public void setUp() throws Exception {
//Creates a db, a set within it and a basic integer serializer (async writing is off)
fakeDB = DBMaker
.newFileDB(tmpFolder.newFile("testDb"))
.asyncWriteEnable()
.closeOnJvmShutdown()
.make();
set = new PersistentSet<Integer>(new Serializer() {
//Creates a set within it and a basic integer serializer
set = new PersistentSet<>(new Serializer() {
@Override
public <T> byte[] encode(T object) {
if (object == null) {
......@@ -84,20 +70,12 @@ public class PersistentSetTest {
num = num | bytes[2] << 8;
num = num | bytes[3];
return (T) new java.lang.Integer(num);
return (T) Integer.valueOf(num);
}
}, fakeDB, "set");
}
@After
public void tearDown() throws Exception {
set.clear();
fakeDB.delete("map:map");
fakeDB.commit();
fakeDB.close();
}
@Test
public void testSize() throws Exception {
//Check correct sizing throughout population
......