diff --git a/caching/etc/caching.png b/caching/etc/caching.png index e69df17ea..6b3b2d055 100644 Binary files a/caching/etc/caching.png and b/caching/etc/caching.png differ diff --git a/caching/etc/caching.ucls b/caching/etc/caching.ucls index 84aad0d18..815a62ec9 100644 --- a/caching/etc/caching.ucls +++ b/caching/etc/caching.ucls @@ -57,7 +57,7 @@ </class> <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"> - <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" sort-features="false" accessors="true" visibility="true"> <attributes public="true" package="true" protected="true" private="true" static="true"/> diff --git a/caching/pom.xml b/caching/pom.xml index 180320eea..d2284a5f1 100644 --- a/caching/pom.xml +++ b/caching/pom.xml @@ -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 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 - skipTests (below) flag to 'false'. + 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' and vice-versa. --> <build> <plugins> @@ -43,7 +43,7 @@ <artifactId>maven-surefire-plugin</artifactId> <version>2.19</version> <configuration> - <skipTests>true</skipTests> + <skipTests>false</skipTests> </configuration> </plugin> </plugins> diff --git a/caching/src/main/java/com/wssia/caching/AppManager.java b/caching/src/main/java/com/wssia/caching/AppManager.java index 96b5fbdc8..cc3c665c8 100644 --- a/caching/src/main/java/com/wssia/caching/AppManager.java +++ b/caching/src/main/java/com/wssia/caching/AppManager.java @@ -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(); } } diff --git a/caching/src/main/java/com/wssia/caching/CacheStore.java b/caching/src/main/java/com/wssia/caching/CacheStore.java index edb644bdb..5eb231cc5 100644 --- a/caching/src/main/java/com/wssia/caching/CacheStore.java +++ b/caching/src/main/java/com/wssia/caching/CacheStore.java @@ -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); } diff --git a/caching/src/main/java/com/wssia/caching/DBManager.java b/caching/src/main/java/com/wssia/caching/DBManager.java index 20918b22f..0222b5956 100644 --- a/caching/src/main/java/com/wssia/caching/DBManager.java +++ b/caching/src/main/java/com/wssia/caching/DBManager.java @@ -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(); diff --git a/caching/src/main/java/com/wssia/caching/LRUCache.java b/caching/src/main/java/com/wssia/caching/LRUCache.java index c69e7e3fd..1389a9bca 100644 --- a/caching/src/main/java/com/wssia/caching/LRUCache.java +++ b/caching/src/main/java/com/wssia/caching/LRUCache.java @@ -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; } diff --git a/caching/src/test/java/com/wssia/caching/AppTest.java b/caching/src/test/java/com/wssia/caching/AppTest.java index 3a93afecc..c2d13ce03 100644 --- a/caching/src/test/java/com/wssia/caching/AppTest.java +++ b/caching/src/test/java/com/wssia/caching/AppTest.java @@ -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(); }