Issue #273: Changed DB to internal Java data structure to avoid compilation errors + decrease in code coverage
This commit is contained in:
parent
9891c2e17b
commit
d0f5009996
Binary file not shown.
Before Width: | Height: | Size: 50 KiB After Width: | Height: | Size: 55 KiB |
@ -57,7 +57,7 @@
|
|||||||
</class>
|
</class>
|
||||||
<class id="7" language="java" name="main.java.com.wssia.caching.UserAccount" project="CachingPatterns"
|
<class id="7" language="java" name="main.java.com.wssia.caching.UserAccount" project="CachingPatterns"
|
||||||
file="/CachingPatterns/src/main/java/com/wssia/caching/UserAccount.java" binary="false" corner="BOTTOM_RIGHT">
|
file="/CachingPatterns/src/main/java/com/wssia/caching/UserAccount.java" binary="false" corner="BOTTOM_RIGHT">
|
||||||
<position height="-1" width="-1" x="1137" y="382"/>
|
<position height="-1" width="-1" x="1140" y="405"/>
|
||||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||||
sort-features="false" accessors="true" visibility="true">
|
sort-features="false" accessors="true" visibility="true">
|
||||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||||
|
@ -33,8 +33,8 @@
|
|||||||
<!--
|
<!--
|
||||||
Due to the use of MongoDB in the test of this pattern, TRAVIS and/or MAVEN might fail if the DB connection is
|
Due to the use of MongoDB in the test of this pattern, TRAVIS and/or MAVEN might fail if the DB connection is
|
||||||
not open for the JUnit test. To avoid disrupting the compilation process, the surefire plug-in was used
|
not open for the JUnit test. To avoid disrupting the compilation process, the surefire plug-in was used
|
||||||
to SKIP the running of the JUnit tests for this pattern. To RE-ACTIVATE the running of the tests, change the
|
to SKIP the running of the JUnit tests for this pattern. To ACTIVATE the running of the tests, change the
|
||||||
skipTests (below) flag to 'false'.
|
skipTests (below) flag to 'false' and vice-versa.
|
||||||
-->
|
-->
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
@ -43,7 +43,7 @@
|
|||||||
<artifactId>maven-surefire-plugin</artifactId>
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
<version>2.19</version>
|
<version>2.19</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<skipTests>true</skipTests>
|
<skipTests>false</skipTests>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
|
@ -15,11 +15,21 @@ public class AppManager {
|
|||||||
|
|
||||||
private static CachingPolicy cachingPolicy;
|
private static CachingPolicy cachingPolicy;
|
||||||
|
|
||||||
public static void init() {
|
/**
|
||||||
try {
|
*
|
||||||
DBManager.connect();
|
* Developer/Tester is able to choose whether the application should use MongoDB as its underlying
|
||||||
} catch (ParseException e) {
|
* data storage or a simple Java data structure to (temporarily) store the data/objects during
|
||||||
e.printStackTrace();
|
* runtime.
|
||||||
|
*/
|
||||||
|
public static void initDB(boolean useMongoDB) {
|
||||||
|
if (useMongoDB) {
|
||||||
|
try {
|
||||||
|
DBManager.connect();
|
||||||
|
} catch (ParseException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
DBManager.createVirtualDB();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ public class CacheStore {
|
|||||||
if (cache.contains(userAccount.getUserID())) {
|
if (cache.contains(userAccount.getUserID())) {
|
||||||
DBManager.updateDB(userAccount);
|
DBManager.updateDB(userAccount);
|
||||||
cache.invalidate(userAccount.getUserID()); // Cache data has been updated -- remove older
|
cache.invalidate(userAccount.getUserID()); // Cache data has been updated -- remove older
|
||||||
// version from cache.
|
// version from cache.
|
||||||
} else {
|
} else {
|
||||||
DBManager.writeToDB(userAccount);
|
DBManager.writeToDB(userAccount);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package main.java.com.wssia.caching;
|
package main.java.com.wssia.caching;
|
||||||
|
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
import org.bson.Document;
|
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
|
* implemented methods for querying, inserting, and updating data. MongoDB was used as the database
|
||||||
* for the application.
|
* 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 {
|
public class DBManager {
|
||||||
|
|
||||||
private static MongoClient mongoClient;
|
private static MongoClient mongoClient;
|
||||||
private static MongoDatabase db;
|
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 {
|
public static void connect() throws ParseException {
|
||||||
|
useMongoDB = true;
|
||||||
mongoClient = new MongoClient();
|
mongoClient = new MongoClient();
|
||||||
db = mongoClient.getDatabase("test");
|
db = mongoClient.getDatabase("test");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UserAccount readFromDB(String userID) {
|
public static UserAccount readFromDB(String userID) {
|
||||||
|
if (!useMongoDB) {
|
||||||
|
if (virtualDB.containsKey(userID))
|
||||||
|
return virtualDB.get(userID);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
if (null == db) {
|
if (null == db) {
|
||||||
try {
|
try {
|
||||||
connect();
|
connect();
|
||||||
@ -45,6 +63,10 @@ public class DBManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void writeToDB(UserAccount userAccount) {
|
public static void writeToDB(UserAccount userAccount) {
|
||||||
|
if (!useMongoDB) {
|
||||||
|
virtualDB.put(userAccount.getUserID(), userAccount);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (null == db) {
|
if (null == db) {
|
||||||
try {
|
try {
|
||||||
connect();
|
connect();
|
||||||
@ -58,6 +80,10 @@ public class DBManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void updateDB(UserAccount userAccount) {
|
public static void updateDB(UserAccount userAccount) {
|
||||||
|
if (!useMongoDB) {
|
||||||
|
virtualDB.put(userAccount.getUserID(), userAccount);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (null == db) {
|
if (null == db) {
|
||||||
try {
|
try {
|
||||||
connect();
|
connect();
|
||||||
@ -76,6 +102,10 @@ public class DBManager {
|
|||||||
* Insert data into DB if it does not exist. Else, update it.
|
* Insert data into DB if it does not exist. Else, update it.
|
||||||
*/
|
*/
|
||||||
public static void upsertDB(UserAccount userAccount) {
|
public static void upsertDB(UserAccount userAccount) {
|
||||||
|
if (!useMongoDB) {
|
||||||
|
virtualDB.put(userAccount.getUserID(), userAccount);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (null == db) {
|
if (null == db) {
|
||||||
try {
|
try {
|
||||||
connect();
|
connect();
|
||||||
|
@ -138,7 +138,7 @@ public class LRUCache {
|
|||||||
public void setCapacity(int newCapacity) {
|
public void setCapacity(int newCapacity) {
|
||||||
if (capacity > newCapacity) {
|
if (capacity > newCapacity) {
|
||||||
clear(); // Behavior can be modified to accommodate for decrease in cache size. For now, we'll
|
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 {
|
} else {
|
||||||
this.capacity = newCapacity;
|
this.capacity = newCapacity;
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,10 @@ public class AppTest {
|
|||||||
*/
|
*/
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
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);
|
AppManager.initCacheCapacity(3);
|
||||||
app = new App();
|
app = new App();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user