Issue #273: Changed DB to internal Java data structure to avoid compilation errors + decrease in code coverage

This commit is contained in:
waisuan
2015-10-29 00:57:41 +08:00
parent 9891c2e17b
commit d0f5009996
8 changed files with 55 additions and 12 deletions

View File

@ -15,11 +15,21 @@ public class AppManager {
private static CachingPolicy cachingPolicy;
public static void init() {
try {
DBManager.connect();
} catch (ParseException e) {
e.printStackTrace();
/**
*
* Developer/Tester is able to choose whether the application should use MongoDB as its underlying
* data storage or a simple Java data structure to (temporarily) store the data/objects during
* runtime.
*/
public static void initDB(boolean useMongoDB) {
if (useMongoDB) {
try {
DBManager.connect();
} catch (ParseException e) {
e.printStackTrace();
}
} else {
DBManager.createVirtualDB();
}
}

View File

@ -42,7 +42,7 @@ public class CacheStore {
if (cache.contains(userAccount.getUserID())) {
DBManager.updateDB(userAccount);
cache.invalidate(userAccount.getUserID()); // Cache data has been updated -- remove older
// version from cache.
// version from cache.
} else {
DBManager.writeToDB(userAccount);
}

View File

@ -1,6 +1,7 @@
package main.java.com.wssia.caching;
import java.text.ParseException;
import java.util.HashMap;
import org.bson.Document;
@ -15,18 +16,35 @@ import com.mongodb.client.model.UpdateOptions;
* implemented methods for querying, inserting, and updating data. MongoDB was used as the database
* for the application.
*
* Developer/Tester is able to choose whether the application should use MongoDB as its underlying
* data storage (connect()) or a simple Java data structure to (temporarily) store the data/objects
* during runtime (createVirtualDB()).
*/
public class DBManager {
private static MongoClient mongoClient;
private static MongoDatabase db;
private static boolean useMongoDB;
private static HashMap<String, UserAccount> virtualDB;
public static void createVirtualDB() {
useMongoDB = false;
virtualDB = new HashMap<String, UserAccount>();
}
public static void connect() throws ParseException {
useMongoDB = true;
mongoClient = new MongoClient();
db = mongoClient.getDatabase("test");
}
public static UserAccount readFromDB(String userID) {
if (!useMongoDB) {
if (virtualDB.containsKey(userID))
return virtualDB.get(userID);
return null;
}
if (null == db) {
try {
connect();
@ -45,6 +63,10 @@ public class DBManager {
}
public static void writeToDB(UserAccount userAccount) {
if (!useMongoDB) {
virtualDB.put(userAccount.getUserID(), userAccount);
return;
}
if (null == db) {
try {
connect();
@ -58,6 +80,10 @@ public class DBManager {
}
public static void updateDB(UserAccount userAccount) {
if (!useMongoDB) {
virtualDB.put(userAccount.getUserID(), userAccount);
return;
}
if (null == db) {
try {
connect();
@ -76,6 +102,10 @@ public class DBManager {
* Insert data into DB if it does not exist. Else, update it.
*/
public static void upsertDB(UserAccount userAccount) {
if (!useMongoDB) {
virtualDB.put(userAccount.getUserID(), userAccount);
return;
}
if (null == db) {
try {
connect();

View File

@ -138,7 +138,7 @@ public class LRUCache {
public void setCapacity(int newCapacity) {
if (capacity > newCapacity) {
clear(); // Behavior can be modified to accommodate for decrease in cache size. For now, we'll
// just clear the cache.
// just clear the cache.
} else {
this.capacity = newCapacity;
}

View File

@ -19,7 +19,10 @@ public class AppTest {
*/
@Before
public void setUp() {
AppManager.init();
AppManager.initDB(false); // VirtualDB (instead of MongoDB) was used in running the JUnit tests
// to avoid Maven compilation errors. Set flag to true to run the
// tests with MongoDB (provided that MongoDB is installed and socket
// connection is open).
AppManager.initCacheCapacity(3);
app = new App();
}