mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-03-31 18:00:30 +08:00
Cleaned up tests a bit
This commit is contained in:
parent
c88e7dae2f
commit
8efe9c4ca4
@ -20,6 +20,7 @@ public class InfoUuidCorrectionTest {
|
||||
HandlingInfo[] h = new HandlingInfo[]{
|
||||
new ChatInfo(test, ""),
|
||||
new DeathInfo(test),
|
||||
new KickInfo(test),
|
||||
new KillInfo(test, now, null, ""),
|
||||
new LoginInfo(test, now, null, false, "", "", 0, ""),
|
||||
new LogoutInfo(test, now, false, "", null, ""),
|
||||
|
@ -58,11 +58,10 @@ public class ReloadInfoTest {
|
||||
assertTrue(i.process(data));
|
||||
assertTrue("LastPlayed wrong: " + data.getLastPlayed(), data.getLastPlayed() == time);
|
||||
assertTrue("Ip not added", data.getIps().contains(ip));
|
||||
assertTrue("Logintimes +1", data.getLoginTimes() == loginTimes);
|
||||
assertTrue("Login times is not the same", data.getLoginTimes() == loginTimes);
|
||||
assertTrue("Nick not added", data.getNicknames().contains(nick));
|
||||
assertTrue("Nick not last nick", data.getLastNick().equals(nick));
|
||||
String geo = data.getGeolocation();
|
||||
assertTrue("Wrong location " + geo, geo.equals("United States"));
|
||||
assertEquals(nick, data.getLastNick());
|
||||
assertEquals("United States", data.getGeolocation());
|
||||
assertEquals("CREATIVE", data.getGmTimes().getState());
|
||||
assertEquals("World", data.getWorldTimes().getState());
|
||||
}
|
||||
|
@ -20,10 +20,7 @@ import main.java.com.djrapitops.plan.database.tables.TPSTable;
|
||||
import main.java.com.djrapitops.plan.utilities.ManageUtils;
|
||||
import main.java.com.djrapitops.plan.utilities.MiscUtils;
|
||||
import main.java.com.djrapitops.plan.utilities.analysis.MathUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import org.bukkit.scheduler.BukkitScheduler;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
@ -53,7 +50,7 @@ import static org.junit.Assert.assertTrue;
|
||||
* @author Rsl1122
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({JavaPlugin.class, Bukkit.class, BukkitScheduler.class, BukkitRunnable.class})
|
||||
@PrepareForTest(JavaPlugin.class)
|
||||
public class DatabaseTest {
|
||||
|
||||
private Plan plan;
|
||||
|
@ -5,11 +5,15 @@ import main.java.com.djrapitops.plan.data.TPS;
|
||||
import main.java.com.djrapitops.plan.data.UserData;
|
||||
import main.java.com.djrapitops.plan.data.WebUser;
|
||||
import main.java.com.djrapitops.plan.data.handling.info.HandlingInfo;
|
||||
import main.java.com.djrapitops.plan.utilities.PassEncryptUtil;
|
||||
import main.java.com.djrapitops.plan.utilities.analysis.Point;
|
||||
import main.java.com.djrapitops.plan.utilities.comparators.*;
|
||||
import org.junit.Test;
|
||||
import test.java.utils.RandomData;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@ -20,15 +24,7 @@ public class ComparatorTest {
|
||||
|
||||
@Test
|
||||
public void testHandlingInfoComparator() {
|
||||
List<HandlingInfo> test = new ArrayList<>();
|
||||
for (int i = 0; i < 20; i++) {
|
||||
test.add(new HandlingInfo(null, null, r.nextLong()) {
|
||||
@Override
|
||||
public boolean process(UserData uData) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
List<HandlingInfo> test = RandomData.randomHandlingInfo();
|
||||
List<Long> longValues = test.stream().map(HandlingInfo::getTime).collect(Collectors.toList());
|
||||
longValues.sort(Long::compare);
|
||||
test.sort(new HandlingInfoTimeComparator());
|
||||
@ -38,10 +34,7 @@ public class ComparatorTest {
|
||||
|
||||
@Test
|
||||
public void testPointComparator() {
|
||||
List<Point> test = new ArrayList<>();
|
||||
for (int i = 0; i < 20; i++) {
|
||||
test.add(new Point(r.nextLong(), r.nextLong()));
|
||||
}
|
||||
List<Point> test = RandomData.randomPoints();
|
||||
|
||||
List<Long> longValues = test.stream().map(Point::getX).map(i -> (long) (double) i).collect(Collectors.toList());
|
||||
longValues.sort(Long::compare);
|
||||
@ -52,10 +45,7 @@ public class ComparatorTest {
|
||||
|
||||
@Test
|
||||
public void testSessionDataComparator() {
|
||||
List<SessionData> test = new ArrayList<>();
|
||||
for (int i = 0; i < 20; i++) {
|
||||
test.add(new SessionData(r.nextLong(), r.nextLong()));
|
||||
}
|
||||
List<SessionData> test = RandomData.randomSessions();
|
||||
List<Long> longValues = test.stream().map(SessionData::getSessionStart).collect(Collectors.toList());
|
||||
longValues.sort(Long::compare);
|
||||
test.sort(new SessionDataComparator());
|
||||
@ -65,10 +55,7 @@ public class ComparatorTest {
|
||||
|
||||
@Test
|
||||
public void testTPSComparator() {
|
||||
List<TPS> test = new ArrayList<>();
|
||||
for (int i = 0; i < 20; i++) {
|
||||
test.add(new TPS(r.nextLong(), 0, 0, 0, 0, 0, 0));
|
||||
}
|
||||
List<TPS> test = RandomData.randomTPS();
|
||||
List<Long> longValues = test.stream().map(TPS::getDate).collect(Collectors.toList());
|
||||
longValues.sort(Long::compare);
|
||||
test.sort(new TPSComparator());
|
||||
@ -78,12 +65,7 @@ public class ComparatorTest {
|
||||
|
||||
@Test
|
||||
public void testUserDataLastPlayedComparator() {
|
||||
List<UserData> test = new ArrayList<>();
|
||||
for (int i = 0; i < 20; i++) {
|
||||
UserData uD = new UserData(null, 0, false, "", "", false);
|
||||
uD.setLastPlayed(r.nextLong());
|
||||
test.add(uD);
|
||||
}
|
||||
List<UserData> test = RandomData.randomUserData();
|
||||
List<Long> longValues = test.stream().map(UserData::getLastPlayed).collect(Collectors.toList());
|
||||
longValues.sort(Long::compare);
|
||||
Collections.reverse(longValues);
|
||||
@ -92,13 +74,10 @@ public class ComparatorTest {
|
||||
assertEquals(longValues, afterSort);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testUserDataNameComparator() {
|
||||
List<UserData> test = new ArrayList<>();
|
||||
for (int i = 0; i < 20; i++) {
|
||||
String randomName = UUID.randomUUID().toString().split("-")[0];
|
||||
test.add(new UserData(null, 0, false, "", randomName, false));
|
||||
}
|
||||
List<UserData> test = RandomData.randomUserData();
|
||||
List<String> stringValues = test.stream().map(UserData::getName).collect(Collectors.toList());
|
||||
Collections.sort(stringValues);
|
||||
test.sort(new UserDataNameComparator());
|
||||
@ -107,11 +86,8 @@ public class ComparatorTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWebUserComparator() {
|
||||
List<WebUser> test = new ArrayList<>();
|
||||
for (int i = 0; i < 20; i++) {
|
||||
test.add(new WebUser("", "", r.nextInt()));
|
||||
}
|
||||
public void testWebUserComparator() throws PassEncryptUtil.CannotPerformOperationException {
|
||||
List<WebUser> test = RandomData.randomWebUsers();
|
||||
List<Integer> intValues = test.stream().map(WebUser::getPermLevel).collect(Collectors.toList());
|
||||
intValues.sort(Integer::compare);
|
||||
Collections.reverse(intValues);
|
||||
|
89
Plan/src/test/java/utils/RandomData.java
Normal file
89
Plan/src/test/java/utils/RandomData.java
Normal file
@ -0,0 +1,89 @@
|
||||
package test.java.utils;
|
||||
|
||||
import main.java.com.djrapitops.plan.data.SessionData;
|
||||
import main.java.com.djrapitops.plan.data.TPS;
|
||||
import main.java.com.djrapitops.plan.data.UserData;
|
||||
import main.java.com.djrapitops.plan.data.WebUser;
|
||||
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.tables.GMTimesTable;
|
||||
import main.java.com.djrapitops.plan.utilities.PassEncryptUtil;
|
||||
import main.java.com.djrapitops.plan.utilities.analysis.Point;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.UUID;
|
||||
|
||||
public class RandomData {
|
||||
|
||||
private static final Random r = new Random();
|
||||
|
||||
public static List<UserData> randomUserData() {
|
||||
List<UserData> test = new ArrayList<>();
|
||||
for (int i = 0; i < 20; i++) {
|
||||
String randomName = randomString(10);
|
||||
UserData uD = new UserData(UUID.randomUUID(), r.nextLong(), r.nextBoolean(), GMTimesTable.getGMKeyArray()[r.nextInt(3)], randomName, r.nextBoolean());
|
||||
uD.setLastPlayed(r.nextLong());
|
||||
test.add(uD);
|
||||
}
|
||||
return test;
|
||||
}
|
||||
|
||||
/**
|
||||
* Random enough.
|
||||
*/
|
||||
public static String randomString(int size) {
|
||||
String randomString = UUID.randomUUID().toString().replaceAll("-", "").substring(0, size);
|
||||
return randomString;
|
||||
}
|
||||
|
||||
public static List<WebUser> randomWebUsers() throws PassEncryptUtil.CannotPerformOperationException {
|
||||
List<WebUser> test = new ArrayList<>();
|
||||
for (int i = 0; i < 20; i++) {
|
||||
test.add(new WebUser(randomString(5), PassEncryptUtil.createHash(randomString(7)), r.nextInt()));
|
||||
}
|
||||
return test;
|
||||
}
|
||||
|
||||
public static List<TPS> randomTPS() {
|
||||
List<TPS> test = new ArrayList<>();
|
||||
for (int i = 0; i < 20; i++) {
|
||||
int randInt = r.nextInt();
|
||||
long randLong = r.nextLong();
|
||||
test.add(new TPS(randLong, randLong, randInt, randLong, randLong, randInt, randInt));
|
||||
}
|
||||
return test;
|
||||
}
|
||||
|
||||
public static List<SessionData> randomSessions() {
|
||||
List<SessionData> test = new ArrayList<>();
|
||||
for (int i = 0; i < 20; i++) {
|
||||
test.add(new SessionData(r.nextLong(), r.nextLong()));
|
||||
}
|
||||
return test;
|
||||
}
|
||||
|
||||
public static List<Point> randomPoints() {
|
||||
List<Point> test = new ArrayList<>();
|
||||
for (int i = 0; i < 20; i++) {
|
||||
test.add(new Point(r.nextLong(), r.nextLong()));
|
||||
}
|
||||
return test;
|
||||
}
|
||||
|
||||
public static List<HandlingInfo> randomHandlingInfo() {
|
||||
List<HandlingInfo> test = new ArrayList<>();
|
||||
for (int i = 0; i < 20; i++) {
|
||||
test.add(new HandlingInfo(UUID.randomUUID(), InfoType.OTHER, r.nextLong()) {
|
||||
@Override
|
||||
public boolean process(UserData uData) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
return test;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -37,12 +37,18 @@ public class TestInit {
|
||||
|
||||
public static TestInit init() throws Exception {
|
||||
TestInit t = new TestInit();
|
||||
t.setUp();
|
||||
t.setUp(true);
|
||||
return t;
|
||||
}
|
||||
|
||||
public static TestInit init(boolean clearOnStart) throws Exception {
|
||||
TestInit t = new TestInit();
|
||||
t.setUp(clearOnStart);
|
||||
return t;
|
||||
}
|
||||
|
||||
@Deprecated // Use Test.init instead.
|
||||
public void setUp() throws Exception {
|
||||
public void setUp(boolean clearOnStart) throws Exception {
|
||||
planMock = PowerMockito.mock(Plan.class);
|
||||
StaticHolder.setInstance(Plan.class, planMock);
|
||||
StaticHolder.setInstance(planMock.getClass(), planMock);
|
||||
@ -50,7 +56,10 @@ public class TestInit {
|
||||
YamlConfiguration config = mockConfig();
|
||||
when(planMock.getConfig()).thenReturn(config);
|
||||
|
||||
File testFolder = getEmptyTestFolder();
|
||||
File testFolder = getTestFolder();
|
||||
if (clearOnStart) {
|
||||
clean(testFolder);
|
||||
}
|
||||
when(planMock.getDataFolder()).thenReturn(testFolder);
|
||||
|
||||
// Html Files
|
||||
@ -91,17 +100,23 @@ public class TestInit {
|
||||
return mockServer;
|
||||
}
|
||||
|
||||
private File getEmptyTestFolder() throws IOException {
|
||||
private static File getTestFolder() throws IOException {
|
||||
File testFolder = new File("temporaryTestFolder");
|
||||
if (testFolder.exists()) {
|
||||
testFolder = new File("temporaryTestFolder");
|
||||
testFolder.mkdir();
|
||||
return testFolder;
|
||||
}
|
||||
|
||||
public static void clean() throws IOException {
|
||||
clean(getTestFolder());
|
||||
}
|
||||
|
||||
public static void clean(File testFolder) throws IOException {
|
||||
if (testFolder.exists() && testFolder.isDirectory()) {
|
||||
for (File f : testFolder.listFiles()) {
|
||||
Files.deleteIfExists(f.toPath());
|
||||
}
|
||||
}
|
||||
Files.deleteIfExists(new File("temporaryTestFolder").toPath());
|
||||
testFolder = new File("temporaryTestFolder");
|
||||
testFolder.mkdir();
|
||||
return testFolder;
|
||||
}
|
||||
|
||||
private YamlConfiguration mockConfig() throws IOException, InvalidConfigurationException {
|
||||
|
Loading…
x
Reference in New Issue
Block a user