This commit is contained in:
Rsl1122 2017-08-16 13:44:27 +03:00
parent 4d7ac8216e
commit ffa27e7806
2 changed files with 15 additions and 273 deletions

View File

@ -5,46 +5,25 @@
*/
package test.java.main.java.com.djrapitops.plan.data.cache;
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.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.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.powermock.api.mockito.PowerMockito.when;
/**
* @author Rsl1122
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(JavaPlugin.class)
public class DataCacheHandlerTest {
private Database db;
private DataCacheHandler handler;
private boolean calledSaveCommandUse;
private boolean calledSaveUserData;
private boolean calledSaveMultiple;
private int callsToSaveCommandUse;
private int callsToSaveUserData;
private int callsToSaveMultiple;
/**
*
@ -56,249 +35,12 @@ public class DataCacheHandlerTest {
*
*/
@Before
public void setUp() throws Exception {
TestInit t = TestInit.init();
Plan plan = t.getPlanMock();
calledSaveCommandUse = false;
calledSaveUserData = false;
db = new SQLiteDB(plan, "debug" + MiscUtils.getTime()) {
@Override
public void startConnectionPingTask() {
public void setUp() {
}
@Override
public void convertBukkitDataToDB() {
}
@Override
public Map<String, Integer> getCommandUse() {
return new HashMap<>();
}
@Override
public void giveUserDataToProcessors(UUID uuid, Collection<DBCallableProcessor> processors) {
if (uuid.equals(MockUtils.getPlayerUUID())) {
UserData d = MockUtils.mockUser();
for (DBCallableProcessor p : processors) {
p.process(d);
}
} else if (uuid.equals(MockUtils.getPlayer2UUID())) {
UserData d = MockUtils.mockUser2();
for (DBCallableProcessor p : processors) {
p.process(d);
}
}
}
@Override
public void saveCommandUse(Map<String, Integer> c) {
calledSaveCommandUse = true;
}
@Override
public void saveUserData(UserData data) throws SQLException {
calledSaveUserData = true;
}
@Override
public void saveMultipleUserData(Collection<UserData> data) throws SQLException {
calledSaveMultiple = true;
}
};
when(plan.getDB()).thenReturn(db);
handler = new DataCacheHandler(plan) {
@Override
public void startAsyncPeriodicSaveTask() {
}
};
}
/**
* @throws SQLException
*/
@After
public void tearDown() throws SQLException {
// db.close();
}
public void tearDown() {
/**
* @throws SQLException
* @throws InterruptedException
*/
@Ignore("Scheduler")
@Test
public void testGetUserDataForProcessingCache() throws SQLException, InterruptedException {
// db.init();
UserData data = MockUtils.mockUser();
db.saveUserData(data);
handler.getUserDataForProcessing(d -> assertTrue(d.equals(data)), data.getUuid());
Thread.sleep(1000);
assertTrue(handler.getDataCache().containsKey(data.getUuid()));
assertTrue(handler.getDataCache().get(data.getUuid()).equals(data));
}
/**
* @throws SQLException
* @throws InterruptedException
*/
@Ignore
@Test
public void testGetUserDataForProcessingDontCache() throws SQLException, InterruptedException {
UserData data = MockUtils.mockUser();
db.saveUserData(data);
handler.getUserDataForProcessing(d -> assertTrue(d.equals(data)), data.getUuid(), false);
Thread.sleep(1000);
assertTrue(!handler.getDataCache().containsKey(data.getUuid()));
assertTrue(handler.getDataCache().get(data.getUuid()) == null);
}
/**
*
*/
@Ignore("Scheduler")
@Test
public void testSaveCachedUserData() {
UserData data = MockUtils.mockUser();
handler.getDataCache().put(data.getUuid(), data);
handler.saveCachedUserData();
assertTrue("Didn't call saveMultiple", calledSaveMultiple);
}
/**
*
*/
@Ignore
@Test
public void testAddToPool() {
}
/**
*
*/
@Ignore
@Test
public void testSaveCacheOnDisable() {
UserData data = MockUtils.mockUser();
handler.getDataCache().put(data.getUuid(), data);
handler.startSession(data.getUuid());
handler.saveCacheOnDisable();
assertTrue(calledSaveMultiple);
}
/**
*
*/
@Ignore
@Test
public void testSaveCachedData() {
}
/**
*
*/
@Ignore("Scheduler")
@Test
public void testSaveCommandUse() {
handler.saveCommandUse();
assertTrue("Didn't call saveCMDUse for db", calledSaveCommandUse);
}
/**
*
*/
@Ignore
@Test
public void testSaveHandlerDataToCache() {
}
/**
*
*/
@Ignore
@Test
public void testClearCache() {
}
/**
*
*/
@Ignore("mock log")
@Test
public void testClearFromCache() {
UUID uuid = MockUtils.getPlayerUUID();
handler.getDataCache().put(uuid, null);
handler.clearFromCache(uuid);
assertTrue("Found uuid", !handler.getDataCache().containsKey(uuid));
assertTrue("Not empty", handler.getDataCache().isEmpty());
}
/**
*
*/
@Ignore
@Test
public void testScheludeForClear() {
}
/**
*
*/
@Ignore
@Test
public void testIsDataAccessed() {
}
/**
*
*/
@Ignore
@Test
public void testNewPlayer_Player() {
}
/**
*
*/
@Ignore
@Test
public void testNewPlayer_OfflinePlayer() {
}
/**
*
*/
@Ignore
@Test
public void testNewPlayer_UserData() {
}
/**
*
*/
@Ignore("Scheduler")
@Test
public void testGetDataCache() {
assertTrue("Cache was null", handler.getDataCache() != null);
}
/**
*
*/
@Ignore
@Test
public void testHandleReload() {
}
/**
*
*/
@Ignore("Scheduler")
@Test
public void testHandleCommand() {
handler.handleCommand("/plan");
assertEquals((Integer) 1, handler.getCommandUse().get("/plan"));
}
}

View File

@ -41,8 +41,8 @@ public class DataCacheQueueTest {
private final UserData data1 = MockUtils.mockUserWithMoreData();
private final UserData data2 = new UserData(MockUtils.mockIPlayer2());
private int calledSaveUserData;
private int calledGetUserData;
private int callsToSaveUserData;
private int callsToGetUserData;
private DataCacheHandler handler;
private Database db;
@ -52,8 +52,8 @@ public class DataCacheQueueTest {
@Before
public void setUp() throws Exception {
calledSaveUserData = 0;
calledGetUserData = 0;
callsToSaveUserData = 0;
callsToGetUserData = 0;
TestInit t = TestInit.init();
Plan plan = t.getPlanMock();
db = new SQLiteDB(plan, "debug" + MiscUtils.getTime()) {
@ -77,14 +77,14 @@ public class DataCacheQueueTest {
@Override
public void giveUserDataToProcessors(UUID uuid, Collection<DBCallableProcessor> processors) throws SQLException {
calledGetUserData++;
callsToGetUserData++;
UserData data = getData(uuid);
processors.forEach(processor -> processor.process(data));
}
@Override
public void saveUserData(UserData data) throws SQLException {
calledSaveUserData++;
callsToSaveUserData++;
}
};
db.init();
@ -123,7 +123,7 @@ public class DataCacheQueueTest {
}
assertEquals(1, calls.size());
assertEquals(0, errors.size());
assertEquals(1, calledGetUserData);
assertEquals(1, callsToGetUserData);
assertTrue(handler.getDataCache().containsKey(uuid1));
}
@ -148,7 +148,7 @@ public class DataCacheQueueTest {
}
assertEquals(1, getCalls.size());
assertEquals(0, errors.size());
assertEquals(1, calledGetUserData);
assertEquals(1, callsToGetUserData);
assertTrue(!handler.getDataCache().containsKey(uuid1));
}
@ -175,7 +175,7 @@ public class DataCacheQueueTest {
}
assertEquals(1, processCalls.size());
assertEquals(0, errors.size());
assertEquals(1, calledGetUserData);
assertEquals(1, callsToGetUserData);
assertTrue(handler.getDataCache().containsKey(uuid1));
assertEquals("TestSuccessful", handler.getDataCache().get(uuid1).getName());
}