Skip to content

Commit d0f5009

Browse files
committed
Issue iluwatar#273: Changed DB to internal Java data structure to avoid compilation errors + decrease in code coverage
1 parent 9891c2e commit d0f5009

File tree

8 files changed

+55
-12
lines changed

8 files changed

+55
-12
lines changed

caching/etc/caching.png

4.67 KB
Loading

caching/etc/caching.ucls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
</class>
5858
<class id="7" language="java" name="main.java.com.wssia.caching.UserAccount" project="CachingPatterns"
5959
file="/CachingPatterns/src/main/java/com/wssia/caching/UserAccount.java" binary="false" corner="BOTTOM_RIGHT">
60-
<position height="-1" width="-1" x="1137" y="382"/>
60+
<position height="-1" width="-1" x="1140" y="405"/>
6161
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
6262
sort-features="false" accessors="true" visibility="true">
6363
<attributes public="true" package="true" protected="true" private="true" static="true"/>

caching/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
<!--
3434
Due to the use of MongoDB in the test of this pattern, TRAVIS and/or MAVEN might fail if the DB connection is
3535
not open for the JUnit test. To avoid disrupting the compilation process, the surefire plug-in was used
36-
to SKIP the running of the JUnit tests for this pattern. To RE-ACTIVATE the running of the tests, change the
37-
skipTests (below) flag to 'false'.
36+
to SKIP the running of the JUnit tests for this pattern. To ACTIVATE the running of the tests, change the
37+
skipTests (below) flag to 'false' and vice-versa.
3838
-->
3939
<build>
4040
<plugins>
@@ -43,7 +43,7 @@
4343
<artifactId>maven-surefire-plugin</artifactId>
4444
<version>2.19</version>
4545
<configuration>
46-
<skipTests>true</skipTests>
46+
<skipTests>false</skipTests>
4747
</configuration>
4848
</plugin>
4949
</plugins>

caching/src/main/java/com/wssia/caching/AppManager.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,21 @@ public class AppManager {
1515

1616
private static CachingPolicy cachingPolicy;
1717

18-
public static void init() {
19-
try {
20-
DBManager.connect();
21-
} catch (ParseException e) {
22-
e.printStackTrace();
18+
/**
19+
*
20+
* Developer/Tester is able to choose whether the application should use MongoDB as its underlying
21+
* data storage or a simple Java data structure to (temporarily) store the data/objects during
22+
* runtime.
23+
*/
24+
public static void initDB(boolean useMongoDB) {
25+
if (useMongoDB) {
26+
try {
27+
DBManager.connect();
28+
} catch (ParseException e) {
29+
e.printStackTrace();
30+
}
31+
} else {
32+
DBManager.createVirtualDB();
2333
}
2434
}
2535

caching/src/main/java/com/wssia/caching/CacheStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static void writeAround(UserAccount userAccount) {
4242
if (cache.contains(userAccount.getUserID())) {
4343
DBManager.updateDB(userAccount);
4444
cache.invalidate(userAccount.getUserID()); // Cache data has been updated -- remove older
45-
// version from cache.
45+
// version from cache.
4646
} else {
4747
DBManager.writeToDB(userAccount);
4848
}

caching/src/main/java/com/wssia/caching/DBManager.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main.java.com.wssia.caching;
22

33
import java.text.ParseException;
4+
import java.util.HashMap;
45

56
import org.bson.Document;
67

@@ -15,18 +16,35 @@
1516
* implemented methods for querying, inserting, and updating data. MongoDB was used as the database
1617
* for the application.
1718
*
19+
* Developer/Tester is able to choose whether the application should use MongoDB as its underlying
20+
* data storage (connect()) or a simple Java data structure to (temporarily) store the data/objects
21+
* during runtime (createVirtualDB()).
1822
*/
1923
public class DBManager {
2024

2125
private static MongoClient mongoClient;
2226
private static MongoDatabase db;
27+
private static boolean useMongoDB;
28+
29+
private static HashMap<String, UserAccount> virtualDB;
30+
31+
public static void createVirtualDB() {
32+
useMongoDB = false;
33+
virtualDB = new HashMap<String, UserAccount>();
34+
}
2335

2436
public static void connect() throws ParseException {
37+
useMongoDB = true;
2538
mongoClient = new MongoClient();
2639
db = mongoClient.getDatabase("test");
2740
}
2841

2942
public static UserAccount readFromDB(String userID) {
43+
if (!useMongoDB) {
44+
if (virtualDB.containsKey(userID))
45+
return virtualDB.get(userID);
46+
return null;
47+
}
3048
if (null == db) {
3149
try {
3250
connect();
@@ -45,6 +63,10 @@ public static UserAccount readFromDB(String userID) {
4563
}
4664

4765
public static void writeToDB(UserAccount userAccount) {
66+
if (!useMongoDB) {
67+
virtualDB.put(userAccount.getUserID(), userAccount);
68+
return;
69+
}
4870
if (null == db) {
4971
try {
5072
connect();
@@ -58,6 +80,10 @@ public static void writeToDB(UserAccount userAccount) {
5880
}
5981

6082
public static void updateDB(UserAccount userAccount) {
83+
if (!useMongoDB) {
84+
virtualDB.put(userAccount.getUserID(), userAccount);
85+
return;
86+
}
6187
if (null == db) {
6288
try {
6389
connect();
@@ -76,6 +102,10 @@ public static void updateDB(UserAccount userAccount) {
76102
* Insert data into DB if it does not exist. Else, update it.
77103
*/
78104
public static void upsertDB(UserAccount userAccount) {
105+
if (!useMongoDB) {
106+
virtualDB.put(userAccount.getUserID(), userAccount);
107+
return;
108+
}
79109
if (null == db) {
80110
try {
81111
connect();

caching/src/main/java/com/wssia/caching/LRUCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public ArrayList<UserAccount> getCacheDataInListForm() {
138138
public void setCapacity(int newCapacity) {
139139
if (capacity > newCapacity) {
140140
clear(); // Behavior can be modified to accommodate for decrease in cache size. For now, we'll
141-
// just clear the cache.
141+
// just clear the cache.
142142
} else {
143143
this.capacity = newCapacity;
144144
}

caching/src/test/java/com/wssia/caching/AppTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ public class AppTest {
1919
*/
2020
@Before
2121
public void setUp() {
22-
AppManager.init();
22+
AppManager.initDB(false); // VirtualDB (instead of MongoDB) was used in running the JUnit tests
23+
// to avoid Maven compilation errors. Set flag to true to run the
24+
// tests with MongoDB (provided that MongoDB is installed and socket
25+
// connection is open).
2326
AppManager.initCacheCapacity(3);
2427
app = new App();
2528
}

0 commit comments

Comments
 (0)