From 4d7ac8216e1ab87f11eed29cb0069a921abee103 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Wed, 16 Aug 2017 13:38:11 +0300 Subject: [PATCH] Added Process & Get Queue tests Fixed TestInit (ColorScheme was missing) --- .../data/cache/queue/DataCacheClearQueue.java | 3 +- .../data/cache/queue/DataCacheGetQueue.java | 2 +- .../cache/queue/DataCacheProcessQueue.java | 17 +-- .../data/cache/queue/DataCacheSaveQueue.java | 3 +- .../data/cache/queue/DataCacheQueueTest.java | 112 ++++++++++++++++-- Plan/test/test/java/utils/TestInit.java | 4 + 6 files changed, 115 insertions(+), 26 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheClearQueue.java b/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheClearQueue.java index c53be3b7f..4da0d3016 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheClearQueue.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheClearQueue.java @@ -70,9 +70,10 @@ class ClearConsumer extends Consumer implements Runnable { @Override void consume(UUID uuid) { - if (handler == null) { + if (!Verify.notNull(handler, uuid)) { return; } + try { if (handler.isDataAccessed(uuid)) { queue.add(uuid); diff --git a/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheGetQueue.java b/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheGetQueue.java index d1fb061c9..944db41bc 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheGetQueue.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheGetQueue.java @@ -68,7 +68,7 @@ class GetConsumer extends Consumer>> { @Override void consume(Map> processors) { - if (db == null) { + if (!Verify.notNull(processors, db)) { return; } diff --git a/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheProcessQueue.java b/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheProcessQueue.java index 1a6116c54..c3d38fb04 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheProcessQueue.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheProcessQueue.java @@ -1,11 +1,11 @@ package main.java.com.djrapitops.plan.data.cache.queue; +import com.djrapitops.plugin.utilities.Verify; import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.data.cache.DBCallableProcessor; import main.java.com.djrapitops.plan.data.cache.DataCacheHandler; import main.java.com.djrapitops.plan.data.handling.info.HandlingInfo; -import java.util.Collection; import java.util.UUID; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; @@ -43,19 +43,6 @@ public class DataCacheProcessQueue extends Queue { } } - /** - * Used to add multiple HandlingInfo objects to be processed. - * - * @param info Collection of objects that extends HandlingInfo. - */ - public void addToPool(Collection info) { - try { - queue.addAll(info); - } catch (IllegalStateException e) { - Log.toLog(this.getClass().getName(), e); - } - } - /** * Check whether or not the queue contains a HandlingInfo object with the * uuid. @@ -79,7 +66,7 @@ class ProcessConsumer extends Consumer { @Override void consume(HandlingInfo info) { - if (handler == null) { + if (!Verify.notNull(handler, info)) { return; } diff --git a/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheSaveQueue.java b/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheSaveQueue.java index fcc664650..01f9da755 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheSaveQueue.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheSaveQueue.java @@ -1,5 +1,6 @@ package main.java.com.djrapitops.plan.data.cache.queue; +import com.djrapitops.plugin.utilities.Verify; import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.Settings; @@ -82,7 +83,7 @@ class SaveConsumer extends Consumer { @Override void consume(UserData data) { - if (db == null) { + if (!Verify.notNull(handler, db, data)) { return; } diff --git a/Plan/test/test/java/main/java/com/djrapitops/plan/data/cache/queue/DataCacheQueueTest.java b/Plan/test/test/java/main/java/com/djrapitops/plan/data/cache/queue/DataCacheQueueTest.java index f9da783fb..83f75fe69 100644 --- a/Plan/test/test/java/main/java/com/djrapitops/plan/data/cache/queue/DataCacheQueueTest.java +++ b/Plan/test/test/java/main/java/com/djrapitops/plan/data/cache/queue/DataCacheQueueTest.java @@ -9,12 +9,15 @@ import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.data.UserData; import main.java.com.djrapitops.plan.data.cache.DBCallableProcessor; import main.java.com.djrapitops.plan.data.cache.DataCacheHandler; +import main.java.com.djrapitops.plan.data.handling.info.HandlingInfo; +import main.java.com.djrapitops.plan.data.handling.info.InfoType; import main.java.com.djrapitops.plan.database.Database; import main.java.com.djrapitops.plan.database.databases.SQLiteDB; import main.java.com.djrapitops.plan.utilities.MiscUtils; import org.bukkit.plugin.java.JavaPlugin; import org.junit.After; import org.junit.Before; +import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @@ -22,25 +25,38 @@ import test.java.utils.MockUtils; import test.java.utils.TestInit; import java.sql.SQLException; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; import java.util.UUID; +import static org.junit.Assert.*; import static org.powermock.api.mockito.PowerMockito.when; @RunWith(PowerMockRunner.class) @PrepareForTest({JavaPlugin.class}) public class DataCacheQueueTest { - private int calledSaveUserData = 0; + private final UUID uuid1 = MockUtils.getPlayerUUID(); + private final UserData data1 = MockUtils.mockUserWithMoreData(); + private final UserData data2 = new UserData(MockUtils.mockIPlayer2()); + + private int calledSaveUserData; + private int calledGetUserData; + + private DataCacheHandler handler; + private Database db; public DataCacheQueueTest() { } @Before public void setUp() throws Exception { + calledSaveUserData = 0; + calledGetUserData = 0; TestInit t = TestInit.init(); Plan plan = t.getPlanMock(); - Database db = new SQLiteDB(plan, "debug" + MiscUtils.getTime()) { + db = new SQLiteDB(plan, "debug" + MiscUtils.getTime()) { @Override public void startConnectionPingTask() { } @@ -50,18 +66,18 @@ public class DataCacheQueueTest { } private UserData getData(UUID uuid) { - UUID one = MockUtils.getPlayerUUID(); UserData data; - if (uuid.equals(one)) { - data = MockUtils.mockUserWithMoreData(); + if (uuid.equals(uuid1)) { + data = data1; } else { - data = new UserData(MockUtils.mockIPlayer2()); + data = data2; } return data; } @Override public void giveUserDataToProcessors(UUID uuid, Collection processors) throws SQLException { + calledGetUserData++; UserData data = getData(uuid); processors.forEach(processor -> processor.process(data)); } @@ -71,8 +87,9 @@ public class DataCacheQueueTest { calledSaveUserData++; } }; + db.init(); when(plan.getDB()).thenReturn(db); - DataCacheHandler handler = new DataCacheHandler(plan) { + handler = new DataCacheHandler(plan) { @Override public void startAsyncPeriodicSaveTask() { } @@ -81,8 +98,87 @@ public class DataCacheQueueTest { } @After - public void tearDown() { + public void tearDown() throws SQLException { + db.close(); } + @Test + public void testGetQueue_cache() { + List calls = new ArrayList<>(); + List errors = new ArrayList<>(); + handler.getUserDataForProcessing(new DBCallableProcessor() { + @Override + public void process(UserData data) { + if (data.equals(data1)) { + calls.add(1); + } else { + errors.add(1); + } + } + }, uuid1); + while (calls.size() < 1) { + if (errors.size() > 0) { + fail(); + } + } + assertEquals(1, calls.size()); + assertEquals(0, errors.size()); + assertEquals(1, calledGetUserData); + assertTrue(handler.getDataCache().containsKey(uuid1)); + } + @Test + public void testGetQueue_dontCache() { + List getCalls = new ArrayList<>(); + List errors = new ArrayList<>(); + handler.getUserDataForProcessing(new DBCallableProcessor() { + @Override + public void process(UserData data) { + if (data.equals(data1)) { + getCalls.add(1); + } else { + errors.add(1); + } + } + }, uuid1, false); + while (getCalls.size() < 1) { + if (errors.size() > 0) { + fail(); + } + } + assertEquals(1, getCalls.size()); + assertEquals(0, errors.size()); + assertEquals(1, calledGetUserData); + assertTrue(!handler.getDataCache().containsKey(uuid1)); + } + + @Test + public void testProcessQueue() { + List processCalls = new ArrayList<>(); + List errors = new ArrayList<>(); + handler.addToPool(new HandlingInfo(uuid1, InfoType.OTHER, 0) { + @Override + public boolean process(UserData uData) { + if (uData.equals(data1)) { + uData.setName("TestSuccessful"); + processCalls.add(1); + } else { + errors.add(1); + } + return true; + } + }); + while (processCalls.size() < 1) { + if (errors.size() > 0) { + fail(); + } + } + assertEquals(1, processCalls.size()); + assertEquals(0, errors.size()); + assertEquals(1, calledGetUserData); + assertTrue(handler.getDataCache().containsKey(uuid1)); + assertEquals("TestSuccessful", handler.getDataCache().get(uuid1).getName()); + } + + // TODO Save & Clear Queue tests } diff --git a/Plan/test/test/java/utils/TestInit.java b/Plan/test/test/java/utils/TestInit.java index 255193e60..bbe7220ff 100644 --- a/Plan/test/test/java/utils/TestInit.java +++ b/Plan/test/test/java/utils/TestInit.java @@ -1,6 +1,7 @@ package test.java.utils; import com.djrapitops.plugin.StaticHolder; +import com.djrapitops.plugin.settings.ColorScheme; import com.djrapitops.plugin.task.AbsRunnable; import com.djrapitops.plugin.task.IRunnable; import com.djrapitops.plugin.task.ITask; @@ -13,6 +14,7 @@ import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.ServerVariableHolder; import main.java.com.djrapitops.plan.Settings; import main.java.com.djrapitops.plan.locale.Locale; +import org.bukkit.ChatColor; import org.bukkit.OfflinePlayer; import org.bukkit.Server; import org.bukkit.configuration.InvalidConfigurationException; @@ -114,6 +116,8 @@ public class TestInit { when(planMock.fetch()).thenReturn(fetch); RunnableFactory runnableFactory = mockRunnableFactory(); when(planMock.getRunnableFactory()).thenReturn(runnableFactory); + ColorScheme cs = new ColorScheme(ChatColor.BLACK, ChatColor.BLACK, ChatColor.BLACK, ChatColor.BLACK); + when(planMock.getColorScheme()).thenReturn(cs); initLocale(planMock); }