Merge pull request #252 from Fuzzlemann/master

PR for 3.6.3 (Fuzzlemann) (4)
This commit is contained in:
Rsl1122 2017-08-16 10:04:20 +03:00 committed by GitHub
commit 99da328544
16 changed files with 212 additions and 186 deletions

View File

@ -174,7 +174,7 @@
</dependencies>
<properties>
<sonar.language>java</sonar.language>
<sonar.jacoco.reportPath>${project.basedir}/target/jacoco.exec</sonar.jacoco.reportPath>
<sonar.jacoco.reportPaths>${project.basedir}/target/jacoco.exec</sonar.jacoco.reportPaths>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<maven.compiler.source>1.8</maven.compiler.source>

View File

@ -119,28 +119,4 @@ public class Log {
public static void toLog(String source, Collection<Throwable> e) {
Plan.getInstance().getPluginLogger().toLog(source, e);
}
/**
* Logs a message to the a given file with a timestamp.
*
* @param message Message to log to Errors.txt [timestamp] Message
* @param filename Name of the file to write to.
* @deprecated Should no longer be used, may break other log handling mechanisms.
* If exception requires additional information it should be placed in the source string.
*/
@Deprecated
public static void toLog(String message, String filename) {
Plan.getInstance().getPluginLogger().toLog(message, filename);
}
/**
* Used to get the name for the error log file.
*
* @return Name of the error log file.
* @deprecated Should no longer be used, Errors.txt is handled by a separate class.
*/
@Deprecated
public static String getErrorsFilename() {
return Plan.getInstance().getPluginLogger().getErrorsFilename();
}
}

View File

@ -147,7 +147,7 @@ public class Plan extends BukkitPlugin<Plan> {
Benchmark.start("Init Database");
Log.info(Locale.get(Msg.ENABLE_DB_INIT).toString());
if (Check.ErrorIfFalse(initDatabase(), Locale.get(Msg.ENABLE_DB_FAIL_DISABLE_INFO).toString())) {
if (Check.errorIfFalse(initDatabase(), Locale.get(Msg.ENABLE_DB_FAIL_DISABLE_INFO).toString())) {
Log.info(Locale.get(Msg.ENABLE_DB_INFO).parse(db.getConfigName()));
} else {
disablePlugin();
@ -316,7 +316,7 @@ public class Plan extends BukkitPlugin<Plan> {
return false;
}
return Check.ErrorIfFalse(db.init(), Locale.get(Msg.ENABLE_DB_FAIL_DISABLE_INFO).toString());
return Check.errorIfFalse(db.init(), Locale.get(Msg.ENABLE_DB_FAIL_DISABLE_INFO).toString());
}
private void startAnalysisRefreshTask(int everyXMinutes) throws IllegalStateException {

View File

@ -33,22 +33,10 @@ public abstract class Importer {
/**
* Constructor.
*/
public Importer() {
Importer() {
info = "No info specified";
}
/**
* Import data from users.
*
* @param uuids UUIDs of players to import
* @return Success of import
* @deprecated Use importData(Collection, String...) instead (new system)
*/
@Deprecated
public boolean importData(Collection<UUID> uuids) {
return importData(uuids, new String[0]);
}
/**
* Method used for the import.
* <p>
@ -157,19 +145,6 @@ public abstract class Importer {
this.info = info;
}
/**
* Import data of a single player.
*
* @param uuid UUID of the player
* @return HandlingInfo used to modify saved userdata.
* @deprecated Deprecated (new system), use importData(UUID, String...)
* instead
*/
@Deprecated
public HandlingInfo importData(UUID uuid) {
return importData(uuid, new String[0]);
}
/**
* Method used for getting the HandlingInfo object for the import data.
*

View File

@ -14,7 +14,6 @@ import main.java.com.djrapitops.plan.utilities.comparators.StringLengthComparato
import main.java.com.djrapitops.plan.utilities.file.FileUtil;
import org.bukkit.ChatColor;
import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
@ -33,7 +32,7 @@ import java.util.stream.Collectors;
* @author Rsl1122
* @since 3.6.2
*/
public class Locale implements Closeable {
public class Locale {
private final Plan plugin;
private final Map<Msg, Message> messages;
@ -47,7 +46,8 @@ public class Locale implements Closeable {
public static void unload() {
Locale locale = LocaleHolder.getLocale();
if (locale != null) {
locale.close();
locale.messages.clear();
LocaleHolder.locale = null;
}
}
@ -334,12 +334,6 @@ public class Locale implements Closeable {
return messages.getOrDefault(msg, new Message(""));
}
@Override
public void close() {
messages.clear();
LocaleHolder.locale = null;
}
private static class LocaleHolder {
private static Locale locale;

View File

@ -54,7 +54,7 @@ public class Check {
* @param message Message to send if Condition is false
* @return Condition
*/
public static boolean ErrorIfFalse(boolean condition, String message) {
public static boolean errorIfFalse(boolean condition, String message) {
if (!condition) {
Log.error(message);
}

View File

@ -17,18 +17,18 @@ import java.security.spec.InvalidKeySpecException;
*/
public class PassEncryptUtil {
public static final String PBKDF2_ALGORITHM = "PBKDF2WithHmacSHA1";
private static final String PBKDF2_ALGORITHM = "PBKDF2WithHmacSHA1";
// These constants may be changed without breaking existing hashes.
public static final int SALT_BYTE_SIZE = 24;
public static final int HASH_BYTE_SIZE = 18;
public static final int PBKDF2_ITERATIONS = 64000;
private static final int SALT_BYTE_SIZE = 24;
private static final int HASH_BYTE_SIZE = 18;
private static final int PBKDF2_ITERATIONS = 64000;
// These constants define the encoding and may not be changed.
public static final int HASH_SECTIONS = 5;
public static final int HASH_ALGORITHM_INDEX = 0;
public static final int ITERATION_INDEX = 1;
public static final int HASH_SIZE_INDEX = 2;
public static final int SALT_INDEX = 3;
public static final int PBKDF2_INDEX = 4;
private static final int HASH_SECTIONS = 5;
private static final int HASH_ALGORITHM_INDEX = 0;
private static final int ITERATION_INDEX = 1;
private static final int HASH_SIZE_INDEX = 2;
private static final int SALT_INDEX = 3;
private static final int PBKDF2_INDEX = 4;
/**
* Constructor used to hide the public constructor
@ -41,7 +41,7 @@ public class PassEncryptUtil {
return createHash(password.toCharArray());
}
public static String createHash(char[] password) throws CannotPerformOperationException {
private static String createHash(char[] password) throws CannotPerformOperationException {
// Generate a random salt
SecureRandom random = new SecureRandom();
byte[] salt = new byte[SALT_BYTE_SIZE];
@ -63,7 +63,7 @@ public class PassEncryptUtil {
return verifyPassword(password.toCharArray(), correctHash);
}
public static boolean verifyPassword(char[] password, String correctHash) throws CannotPerformOperationException, InvalidHashException {
private static boolean verifyPassword(char[] password, String correctHash) throws CannotPerformOperationException, InvalidHashException {
// Decode the hash into its parameters
String[] params = correctHash.split(":");
if (params.length != HASH_SECTIONS) {

View File

@ -0,0 +1,31 @@
package test.java.main.java.com.djrapitops.plan;
import main.java.com.djrapitops.plan.Plan;
import org.bukkit.plugin.java.JavaPlugin;
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 static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
/**
* @author Fuzzlemann
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(JavaPlugin.class)
public class ServerVariableHolderTest {
@Test
public void testServerVariable() throws Exception {
TestInit.init();
boolean usingPaper = Plan.getInstance().getVariable().isUsingPaper();
assertFalse(usingPaper);
String ip = Plan.getInstance().getVariable().getIp();
assertEquals(ip, "0.0.0.0");
}
}

View File

@ -18,6 +18,7 @@ import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
@ -27,47 +28,40 @@ import static org.junit.Assert.assertTrue;
@PrepareForTest(JavaPlugin.class)
public class SettingsTest {
/**
*
*/
public SettingsTest() {
}
/**
*
*/
@Before
public void setUp() throws Exception {
TestInit.init();
}
/**
*
*/
@Test
public void testIsTrue() {
assertTrue("Webserver supposed to be enabled by default", Settings.WEBSERVER_ENABLED.isTrue());
}
/**
*
*/
@Test
public void testIsTrue2() {
Settings gatherCommands = Settings.GATHERCOMMANDS;
gatherCommands.setValue(false);
assertFalse(gatherCommands.isTrue());
gatherCommands.setValue(true);
assertTrue(gatherCommands.isTrue());
}
@Test
public void testToString() {
assertEquals("sqlite", Settings.DB_TYPE.toString());
}
/**
*
*/
@Test
public void testGetNumber() {
assertEquals(8804, Settings.WEBSERVER_PORT.getNumber());
}
/**
*
*/
@Test
public void testGetStringList() {
List<String> exp = new ArrayList<>();
@ -76,9 +70,6 @@ public class SettingsTest {
assertEquals(exp, result);
}
/**
*
*/
@Test
public void testGetPath() {
assertEquals("Settings.WebServer.Enabled", Settings.WEBSERVER_ENABLED.getPath());

View File

@ -111,7 +111,7 @@ public class DataCacheGetQueueTest {
UserData exp = MockUtils.mockUser2();
DataCacheGetQueue instance = new DataCacheGetQueue(plan);
instance.scheduleForGet(exp.getUuid(), (DBCallableProcessor) data -> assertTrue(data.equals(exp)));
instance.scheduleForGet(exp.getUuid(), data -> assertTrue(data.equals(exp)));
}
/**
@ -122,6 +122,6 @@ public class DataCacheGetQueueTest {
public void testStop() {
DataCacheGetQueue instance = new DataCacheGetQueue(plan);
instance.stop();
instance.scheduleForGet(MockUtils.getPlayerUUID(), (DBCallableProcessor) data -> fail("Called get process after stop."));
instance.scheduleForGet(MockUtils.getPlayerUUID(), data -> fail("Called get process after stop."));
}
}

View File

@ -0,0 +1,53 @@
package test.java.main.java.com.djrapitops.plan.utilities;
import com.djrapitops.plugin.command.ISender;
import main.java.com.djrapitops.plan.utilities.Check;
import org.bukkit.plugin.java.JavaPlugin;
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;
import test.java.utils.MockUtils;
import test.java.utils.RandomData;
import test.java.utils.TestInit;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertTrue;
/**
* @author Fuzzlemann
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(JavaPlugin.class)
public class CheckTest {
@Before
public void setUp() throws Exception {
TestInit.init();
}
@Test
public void testTrueCheck() {
String message = RandomData.randomString(10);
assertTrue(Check.isTrue(true, message));
}
@Test
public void testTrueAtISenderCheck() {
String message = RandomData.randomString(10);
ISender sender = MockUtils.mockIPlayer();
assertTrue(Check.isTrue(true, message, sender));
assertFalse(Check.isTrue(false, message, sender));
}
@Test
public void testErrorCheck() {
String message = RandomData.randomString(10);
assertTrue(Check.errorIfFalse(true, message));
assertFalse(Check.errorIfFalse(false, message));
}
}

View File

@ -9,11 +9,12 @@ 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 test.java.utils.MockUtils;
import test.java.utils.TestInit;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import static org.junit.Assert.*;
@ -24,40 +25,24 @@ import static org.junit.Assert.*;
@PrepareForTest(JavaPlugin.class)
public class FormatUtilsTest {
/**
*
*/
public FormatUtilsTest() {
}
/**
*
*/
@Before
public void setUp() throws Exception {
TestInit.init();
}
/**
*
*/
@Test
public void testFormatTimeAmount() throws Exception {
TestInit.init();
long second = 1000L;
public void testFormatTimeAmount() {
long ms = 1000L;
String expResult = "1s";
String result = FormatUtils.formatTimeAmount(second);
String result = FormatUtils.formatTimeAmount(ms);
assertEquals(expResult, result);
}
/**
*
*/
@Test
public void testFormatTimeAmountSinceDate() throws Exception {
TestInit.init();
public void testFormatTimeAmountSinceDate() {
Date before = new Date(300000L);
Date now = new Date(310000L);
String expResult = "10s";
String result = FormatUtils.formatTimeAmountDifference(before.getTime(), now.getTime());
assertEquals(expResult, result);
@ -65,7 +50,7 @@ public class FormatUtilsTest {
@Test
public void testFormatTimeStamp() {
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd',' HH':'mm");
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd, HH:mm", Locale.ENGLISH);
Date date = new Date();
date.setTime(0);
@ -79,7 +64,7 @@ public class FormatUtilsTest {
@Test
public void testFormatTimeStampYear() {
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd YYYY',' HH':'mm");
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd YYYY, HH:mm", Locale.ENGLISH);
Date date = new Date();
date.setTime(0);
@ -91,9 +76,20 @@ public class FormatUtilsTest {
assertEquals(expResult, result);
}
/**
*
*/
@Test
public void testFormatTimeStampSecond() {
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd, HH:mm:ss", Locale.ENGLISH);
Date date = new Date();
date.setTime(0);
String expResult = dateFormat.format(date);
long epochZero = 0L;
String result = FormatUtils.formatTimeStampSecond(epochZero);
assertEquals(expResult, result);
}
@Test
public void testRemoveLetters() {
String dataPoint = "435729847jirggu.eiwb¤#¤%¤#";
@ -102,9 +98,6 @@ public class FormatUtilsTest {
assertEquals(expResult, result);
}
/**
*
*/
@Test
public void testRemoveNumbers() {
String dataPoint = "34532453.5 $";
@ -113,9 +106,6 @@ public class FormatUtilsTest {
assertEquals(expResult, result);
}
/**
*
*/
@Test
public void testRemoveNumbers2() {
String dataPoint = "l43r4545tl43 4.5";
@ -124,9 +114,6 @@ public class FormatUtilsTest {
assertEquals(expResult, result);
}
/**
*
*/
@Test
public void testParseVersionNumber() {
String versionString = "2.10.2";
@ -135,9 +122,6 @@ public class FormatUtilsTest {
assertEquals(expResult, result);
}
/**
*
*/
@Test
public void testVersionNumber() {
String versionString = "2.10.2";
@ -147,9 +131,6 @@ public class FormatUtilsTest {
assertTrue("Higher version not higher", result > result2);
}
/**
*
*/
@Test
public void testMergeArrays() {
String[][] arrays = new String[][]{new String[]{"Test", "One"}, new String[]{"Test", "Two"}};
@ -158,9 +139,6 @@ public class FormatUtilsTest {
assertArrayEquals(expResult, result);
}
/**
*
*/
@Test
public void testFormatLocation() {
World mockWorld = MockUtils.mockWorld();
@ -170,24 +148,16 @@ public class FormatUtilsTest {
assertEquals(expResult, result);
}
/**
*
*/
@Test
public void testCutDecimals() throws Exception {
TestInit.init();
double d = 0.05234;
String expResult = "0,05";
String result = FormatUtils.cutDecimals(d);
assertEquals(expResult, result);
}
/**
*
*/
@Test
public void testCutDecimals2() throws Exception {
TestInit.init();
double d = 0.05634;
String expResult = "0,06";
String result = FormatUtils.cutDecimals(d);

View File

@ -10,7 +10,6 @@ import com.djrapitops.plugin.command.bukkit.BukkitCMDSender;
import main.java.com.djrapitops.plan.utilities.MiscUtils;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -30,19 +29,6 @@ import static org.junit.Assert.assertEquals;
@PrepareForTest({JavaPlugin.class, Bukkit.class})
public class MiscUtilsTest {
/**
*
*/
public MiscUtilsTest() {
}
/**
*
*/
@Before
public void setUp() throws Exception {
}
@Test
public void testGetPlayerDisplaynameArgsPerm() {
String[] args = new String[]{"Rsl1122", "Test"};

View File

@ -0,0 +1,44 @@
package test.java.main.java.com.djrapitops.plan.utilities;
import main.java.com.djrapitops.plan.utilities.PassEncryptUtil;
import org.junit.Before;
import org.junit.Test;
import test.java.utils.RandomData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* @author Fuzzlemann
*/
public class PassEncryptTest {
private final List<String> PASSWORDS = new ArrayList<>();
private final Map<String, String> PASSWORD_MAP = new HashMap<>();
@Before
public void setUp() throws Exception {
IntStream.range(0, 50).forEach(i -> PASSWORDS.add(RandomData.randomString(RandomData.randomInt(1, 100))));
for (String password : PASSWORDS) {
PASSWORD_MAP.put(password, PassEncryptUtil.createHash(password));
}
}
@Test
public void testVerification() throws Exception {
for (Map.Entry<String, String> entry : PASSWORD_MAP.entrySet()) {
String password = entry.getKey();
String hash = entry.getValue();
assertTrue(PassEncryptUtil.verifyPassword(password, hash));
assertFalse(PassEncryptUtil.verifyPassword(RandomData.randomString(RandomData.randomInt(1, 100)), hash));
}
}
}

View File

@ -15,11 +15,16 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
public class RandomData {
private static final Random r = new Random();
public static int randomInt(int rangeStart, int rangeEnd) {
return ThreadLocalRandom.current().nextInt(rangeStart, rangeEnd);
}
public static List<UserData> randomUserData() {
List<UserData> test = new ArrayList<>();
for (int i = 0; i < 20; i++) {

View File

@ -47,26 +47,7 @@ public class TestInit {
return t;
}
private static File getTestFolder() {
File 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());
}
}
}
@Deprecated // Use Test.init instead.
public void setUp(boolean clearOnStart) throws Exception {
private void setUp(boolean clearOnStart) throws Exception {
planMock = PowerMockito.mock(Plan.class);
StaticHolder.setInstance(Plan.class, planMock);
StaticHolder.setInstance(planMock.getClass(), planMock);
@ -108,12 +89,32 @@ public class TestInit {
when(planMock.fetch()).thenReturn(fetch);
}
private static File getTestFolder() {
File 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());
}
}
}
private Server mockServer() {
Server mockServer = PowerMockito.mock(Server.class);
OfflinePlayer[] ops = new OfflinePlayer[]{MockUtils.mockPlayer(), MockUtils.mockPlayer2()};
when(mockServer.getIp()).thenReturn("0.0.0.0");
when(mockServer.getMaxPlayers()).thenReturn(20);
when(mockServer.getName()).thenReturn("Bukkit");
OfflinePlayer[] ops = new OfflinePlayer[]{MockUtils.mockPlayer(), MockUtils.mockPlayer2()};
when(mockServer.getOfflinePlayers()).thenReturn(ops);
return mockServer;
}