Added Process & Get Queue tests

Fixed TestInit (ColorScheme was missing)
This commit is contained in:
Rsl1122 2017-08-16 13:38:11 +03:00
parent 10ea5b1be3
commit 4d7ac8216e
6 changed files with 115 additions and 26 deletions

View File

@ -70,9 +70,10 @@ class ClearConsumer extends Consumer<UUID> implements Runnable {
@Override
void consume(UUID uuid) {
if (handler == null) {
if (!Verify.notNull(handler, uuid)) {
return;
}
try {
if (handler.isDataAccessed(uuid)) {
queue.add(uuid);

View File

@ -68,7 +68,7 @@ class GetConsumer extends Consumer<Map<UUID, List<DBCallableProcessor>>> {
@Override
void consume(Map<UUID, List<DBCallableProcessor>> processors) {
if (db == null) {
if (!Verify.notNull(processors, db)) {
return;
}

View File

@ -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<HandlingInfo> {
}
}
/**
* Used to add multiple HandlingInfo objects to be processed.
*
* @param info Collection of objects that extends HandlingInfo.
*/
public void addToPool(Collection<HandlingInfo> 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<HandlingInfo> {
@Override
void consume(HandlingInfo info) {
if (handler == null) {
if (!Verify.notNull(handler, info)) {
return;
}

View File

@ -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<UserData> {
@Override
void consume(UserData data) {
if (db == null) {
if (!Verify.notNull(handler, db, data)) {
return;
}

View File

@ -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<DBCallableProcessor> 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<Integer> calls = new ArrayList<>();
List<Integer> 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<Integer> getCalls = new ArrayList<>();
List<Integer> 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<Integer> processCalls = new ArrayList<>();
List<Integer> 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
}

View File

@ -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<Plan> 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);
}