Bypassed Bukkit Scheduler mock by overriding APF RunnableFactory methods in TestInit

This commit is contained in:
Rsl1122 2017-08-16 13:13:28 +03:00
parent baec9dd220
commit 10ea5b1be3
7 changed files with 155 additions and 514 deletions

View File

@ -1,87 +0,0 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test.java.main.java.com.djrapitops.plan.data.cache.queue;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.cache.DataCacheHandler;
import org.bukkit.plugin.java.JavaPlugin;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import test.java.utils.TestInit;
/**
* @author Rsl1122
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({JavaPlugin.class})
public class DataCacheClearQueueTest {
/**
*
*/
public DataCacheClearQueueTest() {
}
/**
*
*/
@Before
public void setUp() throws Exception {
TestInit t = TestInit.init();
Plan plan = t.getPlanMock();
new DataCacheHandler(plan) {
@Override
public boolean getCommandUseFromDb() {
return true;
}
@Override
public void startQueues() {
}
@Override
public void startAsyncPeriodicSaveTask() {
}
};
}
/**
*
*/
@After
public void tearDown() {
}
/**
*
*/
@Ignore
@Test
public void testScheduleForClear_UUID() {
}
/**
*
*/
@Ignore
@Test
public void testScheduleForClear_Collection() {
}
/**
*
*/
@Ignore
@Test
public void testStop() {
}
}

View File

@ -1,127 +0,0 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test.java.main.java.com.djrapitops.plan.data.cache.queue;
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.queue.DataCacheGetQueue;
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 main.java.com.djrapitops.plan.utilities.file.FileUtil;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import test.java.utils.MockUtils;
import test.java.utils.TestInit;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author Rsl1122
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({JavaPlugin.class, Bukkit.class})
public class DataCacheGetQueueTest {
private Plan plan;
private Database db;
private int rows;
/**
*
*/
public DataCacheGetQueueTest() {
}
/**
* @throws IOException
* @throws Exception
*/
@Before
public void setUp() throws Exception {
TestInit t = TestInit.init();
plan = t.getPlanMock();
db = new SQLiteDB(plan, "debug" + MiscUtils.getTime()) {
@Override
public void startConnectionPingTask() {
}
@Override
public void giveUserDataToProcessors(UUID uuid, DBCallableProcessor... processors) {
if (uuid.equals(MockUtils.getPlayerUUID())) {
UserData d = MockUtils.mockUser();
for (DBCallableProcessor processor : processors) {
processor.process(d);
}
} else if (uuid.equals(MockUtils.getPlayer2UUID())) {
UserData d = MockUtils.mockUser2();
for (DBCallableProcessor processor : processors) {
processor.process(d);
}
}
}
};
File f = new File(plan.getDataFolder(), "Errors.txt");
rows = FileUtil.lines(f).size();
}
/**
* @throws IOException
* @throws SQLException
*/
@After
public void tearDown() throws IOException, SQLException {
db.close();
File f = new File(plan.getDataFolder(), "Errors.txt");
List<String> lines = FileUtil.lines(f);
int rowsAgain = lines.size();
if (rowsAgain > 0) {
for (String line : lines) {
System.out.println(line);
}
}
assertTrue("Errors were caught.", rows == rowsAgain);
}
/**
*
*/
@Ignore("Scheduler")
@Test
public void testScheduleForGet() {
UserData exp = MockUtils.mockUser2();
DataCacheGetQueue instance = new DataCacheGetQueue(plan);
instance.scheduleForGet(exp.getUuid(), data -> assertTrue(data.equals(exp)));
}
/**
*
*/
@Ignore("Scheduler")
@Test
public void testStop() {
DataCacheGetQueue instance = new DataCacheGetQueue(plan);
instance.stop();
instance.scheduleForGet(MockUtils.getPlayerUUID(), data -> fail("Called get process after stop."));
}
}

View File

@ -1,151 +0,0 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test.java.main.java.com.djrapitops.plan.data.cache.queue;
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.cache.queue.DataCacheProcessQueue;
import main.java.com.djrapitops.plan.data.handling.info.HandlingInfo;
import main.java.com.djrapitops.plan.data.handling.info.InfoType;
import org.bukkit.plugin.java.JavaPlugin;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import test.java.utils.MockUtils;
import test.java.utils.TestInit;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* @author Rsl1122
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({JavaPlugin.class})
public class DataCacheProcessQueueTest {
private DataCacheHandler handler;
/**
*
*/
public DataCacheProcessQueueTest() {
}
/**
*
*/
@Before
public void setUp() throws Exception {
TestInit.init();
handler = new DataCacheHandler(Plan.getInstance()) {
@Override
public boolean getCommandUseFromDb() {
return true;
}
@Override
public void startQueues() {
}
@Override
public void startAsyncPeriodicSaveTask() {
}
@Override
public void getUserDataForProcessing(DBCallableProcessor p, UUID uuid) {
if (uuid.equals(MockUtils.getPlayerUUID())) {
UserData d = MockUtils.mockUser();
p.process(d);
} else if (uuid.equals(MockUtils.getPlayer2UUID())) {
UserData d = MockUtils.mockUser2();
p.process(d);
}
}
};
}
/**
*
*/
@After
public void tearDown() {
}
/**
* @throws InterruptedException
*/
@Ignore("Scheduler")
@Test
public void testAddToPool_HandlingInfo() throws InterruptedException {
DataCacheProcessQueue q = new DataCacheProcessQueue(handler);
UUID uuid = MockUtils.getPlayerUUID();
q.addToPool(new HandlingInfo(uuid, InfoType.CHAT, 0) {
@Override
public boolean process(UserData uData) {
assertEquals(uuid, uData.getUuid());
return true;
}
});
Thread.sleep(1000);
assertTrue(q.stopAndReturnLeftovers().isEmpty());
}
/**
* @throws InterruptedException
*/
@Ignore("Scheduler")
@Test
public void testAddToPool_Collection() throws InterruptedException {
DataCacheProcessQueue q = new DataCacheProcessQueue(handler);
UUID uuid = MockUtils.getPlayerUUID();
HandlingInfo h = new HandlingInfo(uuid, InfoType.CHAT, 0) {
@Override
public boolean process(UserData uData) {
assertEquals(uuid, uData.getUuid());
return true;
}
};
List<HandlingInfo> l = new ArrayList<>();
l.add(h);
l.add(h);
l.add(h);
q.addToPool(l);
Thread.sleep(1000);
assertTrue(q.stopAndReturnLeftovers().isEmpty());
}
/**
* @throws InterruptedException
*/
@Ignore("Inconsistant")
@Test
public void testContainsUUID() throws InterruptedException {
DataCacheProcessQueue q = new DataCacheProcessQueue(handler);
UUID uuid = MockUtils.getPlayerUUID();
HandlingInfo h = new HandlingInfo(uuid, InfoType.CHAT, 0) {
@Override
public boolean process(UserData uData) {
assertEquals(uuid, uData.getUuid());
return true;
}
};
q.stop();
q.addToPool(h);
assertTrue(q.containsUUID(uuid));
}
}

View File

@ -0,0 +1,88 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test.java.main.java.com.djrapitops.plan.data.cache.queue;
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.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.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import test.java.utils.MockUtils;
import test.java.utils.TestInit;
import java.sql.SQLException;
import java.util.Collection;
import java.util.UUID;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest({JavaPlugin.class})
public class DataCacheQueueTest {
private int calledSaveUserData = 0;
public DataCacheQueueTest() {
}
@Before
public void setUp() throws Exception {
TestInit t = TestInit.init();
Plan plan = t.getPlanMock();
Database db = new SQLiteDB(plan, "debug" + MiscUtils.getTime()) {
@Override
public void startConnectionPingTask() {
}
@Override
public void convertBukkitDataToDB() {
}
private UserData getData(UUID uuid) {
UUID one = MockUtils.getPlayerUUID();
UserData data;
if (uuid.equals(one)) {
data = MockUtils.mockUserWithMoreData();
} else {
data = new UserData(MockUtils.mockIPlayer2());
}
return data;
}
@Override
public void giveUserDataToProcessors(UUID uuid, Collection<DBCallableProcessor> processors) throws SQLException {
UserData data = getData(uuid);
processors.forEach(processor -> processor.process(data));
}
@Override
public void saveUserData(UserData data) throws SQLException {
calledSaveUserData++;
}
};
when(plan.getDB()).thenReturn(db);
DataCacheHandler handler = new DataCacheHandler(plan) {
@Override
public void startAsyncPeriodicSaveTask() {
}
};
when(plan.getHandler()).thenReturn(handler);
}
@After
public void tearDown() {
}
}

View File

@ -1,147 +0,0 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test.java.main.java.com.djrapitops.plan.data.cache.queue;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.UserData;
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.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import test.java.utils.TestInit;
import java.sql.SQLException;
import static org.powermock.api.mockito.PowerMockito.when;
/**
* @author Rsl1122
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({JavaPlugin.class})
public class DataCacheSaveQueueTest {
private boolean calledSaveUserData;
private boolean calledSaveUserData2;
/**
*
*/
public DataCacheSaveQueueTest() {
}
/**
*
*/
@Before
public void setUp() throws Exception {
TestInit t = TestInit.init();
Plan plan = t.getPlanMock();
calledSaveUserData = false;
calledSaveUserData2 = false;
Database db = new SQLiteDB(plan, "debug" + MiscUtils.getTime()) {
@Override
public void startConnectionPingTask() {
}
@Override
public void saveUserData(UserData data) throws SQLException {
if (calledSaveUserData) {
calledSaveUserData2 = true;
}
calledSaveUserData = true;
}
};
when(plan.getDB()).thenReturn(db);
}
/**
*
*/
@After
public void tearDown() {
}
/**
* @throws InterruptedException
*/
@Ignore
@Test
public void testScheduleForSave_UserData() throws InterruptedException {
// DataCacheSaveQueue q = new DataCacheSaveQueue(plan);
// UserData data = new UserData(MockUtils.mockPlayer(), new DemographicsData());
// q.scheduleForSave(data);
// Thread.sleep(500);
// assertTrue(calledSaveUserData);
}
/**
* @throws InterruptedException
*/
@Ignore("Inconsistant")
@Test
public void testScheduleForSave_Collection() throws InterruptedException {
// DataCacheSaveQueue q = new DataCacheSaveQueue(plan);
// UserData data = new UserData(MockUtils.mockPlayer(), new DemographicsData());
// UserData data2 = new UserData(MockUtils.mockPlayer2(), new DemographicsData());
// List<UserData> l = new ArrayList<>();
// l.add(data);
// l.add(data2);
// q.scheduleForSave(l);
// Thread.sleep(1000);
// assertTrue(calledSaveUserData);
// assertTrue(calledSaveUserData2);
}
/**
* @throws InterruptedException
*/
@Ignore
@Test
public void testScheduleNewPlayer() throws InterruptedException {
// DataCacheSaveQueue q = new DataCacheSaveQueue(plan);
// UserData data = new UserData(MockUtils.mockPlayer(), new DemographicsData());
// q.scheduleNewPlayer(data);
// Thread.sleep(500);
// assertTrue(calledSaveUserData);
}
/**
*
*/
@Ignore("Inconsistant")
@Test
public void testContainsUUID() {
// DataCacheSaveQueue q = new DataCacheSaveQueue(plan);
// UserData data = new UserData(MockUtils.mockPlayer(), new DemographicsData());
// q.stop();
// q.scheduleNewPlayer(data);
// assertTrue(q.containsUUID(data.getUuid()));
}
/**
* @throws InterruptedException
*/
@Ignore
@Test
public void testStop() throws InterruptedException {
// DataCacheSaveQueue q = new DataCacheSaveQueue(plan);
// UserData data = new UserData(MockUtils.mockPlayer(), new DemographicsData());
// q.stop();
// Thread.sleep(2000);
// q.scheduleNewPlayer(data);
// assertTrue(!calledSaveUserData);
}
}

View File

@ -18,6 +18,7 @@ import org.powermock.api.mockito.PowerMockito;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
@ -42,9 +43,12 @@ public class MockUtils {
return NewPlayerCreator.createNewPlayer(mockIPlayer());
}
public static UserData mockUserWithMoreData() throws Exception {
public static UserData mockUserWithMoreData() {
UserData mock = mockUser();
try {
mock.addIpAddress(InetAddress.getByName("247.183.163.155"));
} catch (UnknownHostException ignored) {
}
mock.addNickname("MoreNicks");
mock.addPlayerKill(new KillData(getPlayer2UUID(), 1, "WEP", 126873643232L));
mock.addSession(new SessionData(12345L, 23456L));

View File

@ -1,6 +1,10 @@
package test.java.utils;
import com.djrapitops.plugin.StaticHolder;
import com.djrapitops.plugin.task.AbsRunnable;
import com.djrapitops.plugin.task.IRunnable;
import com.djrapitops.plugin.task.ITask;
import com.djrapitops.plugin.task.RunnableFactory;
import com.djrapitops.plugin.utilities.BenchUtil;
import com.djrapitops.plugin.utilities.log.BukkitLog;
import com.djrapitops.plugin.utilities.player.Fetch;
@ -108,9 +112,66 @@ public class TestInit {
when(planMock.getVariable()).thenReturn(serverVariableHolder);
when(planMock.processStatus()).thenReturn(process);
when(planMock.fetch()).thenReturn(fetch);
RunnableFactory<Plan> runnableFactory = mockRunnableFactory();
when(planMock.getRunnableFactory()).thenReturn(runnableFactory);
initLocale(planMock);
}
private RunnableFactory<Plan> mockRunnableFactory() {
RunnableFactory<Plan> runnableFactory = new RunnableFactory<Plan>(planMock) {
@Override
public IRunnable createNew(String name, final AbsRunnable runnable) {
return new IRunnable() {
@Override
public String getTaskName() {
return "Test";
}
@Override
public void cancel() {
}
@Override
public int getTaskId() {
return 0;
}
@Override
public ITask runTask() {
new Thread(() -> runnable.run()).start();
return null;
}
@Override
public ITask runTaskAsynchronously() {
return runTask();
}
@Override
public ITask runTaskLater(long l) {
return runTask();
}
@Override
public ITask runTaskLaterAsynchronously(long l) {
return runTask();
}
@Override
public ITask runTaskTimer(long l, long l1) {
return runTask();
}
@Override
public ITask runTaskTimerAsynchronously(long l, long l1) {
return runTask();
}
};
}
};
return runnableFactory;
}
private static File getTestFolder() {
File testFolder = new File("temporaryTestFolder");
testFolder.mkdir();