mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-12-01 06:33:35 +01:00
Remove old methods in Messages and add StringUtils tests
StringUtils - merge the two join methods to one common implementation with two interface; add tests Messages - remove the methods taking a String as code after the kind refactoring by @DNx5
This commit is contained in:
parent
210b691353
commit
8ed672e89d
@ -7,6 +7,7 @@ import org.bukkit.command.CommandSender;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Class for retrieving and sending translatable messages to players.
|
||||||
*/
|
*/
|
||||||
// TODO ljacqu 20151124: This class is a weird mix between singleton and POJO
|
// TODO ljacqu 20151124: This class is a weird mix between singleton and POJO
|
||||||
// TODO: change it into POJO
|
// TODO: change it into POJO
|
||||||
@ -15,7 +16,7 @@ public class Messages extends CustomConfiguration {
|
|||||||
/** The section symbol, used in Minecraft for formatting codes. */
|
/** The section symbol, used in Minecraft for formatting codes. */
|
||||||
private static final String SECTION_SIGN = "\u00a7";
|
private static final String SECTION_SIGN = "\u00a7";
|
||||||
private static Messages singleton;
|
private static Messages singleton;
|
||||||
private String language = "en";
|
private String language;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -51,42 +52,27 @@ public class Messages extends CustomConfiguration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send the given message code to the player.
|
* Retrieve the message from the text file and return it split by new line as an array.
|
||||||
*
|
*
|
||||||
* @param sender The entity to send the message to
|
* @param key The message key to retrieve
|
||||||
* @param msg The message code to send
|
|
||||||
*
|
*
|
||||||
* @deprecated Use {@link Messages#send(CommandSender, MessageKey)} instead
|
* @return The message split by new lines
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
|
||||||
public void send(CommandSender sender, String msg) {
|
|
||||||
String[] lines = retrieve(msg);
|
|
||||||
for (String line : lines) {
|
|
||||||
sender.sendMessage(line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public String[] retrieve(MessageKey key) {
|
public String[] retrieve(MessageKey key) {
|
||||||
return retrieve(key.getKey());
|
return retrieve(key.getKey());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the message from the text file.
|
||||||
|
*
|
||||||
|
* @param key The message key to retrieve
|
||||||
|
*
|
||||||
|
* @return The message from the file
|
||||||
|
*/
|
||||||
public String retrieveSingle(MessageKey key) {
|
public String retrieveSingle(MessageKey key) {
|
||||||
return StringUtils.join("\n", retrieve(key.getKey()));
|
return StringUtils.join("\n", retrieve(key.getKey()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve the message from the text file and returns it split by new line as an array.
|
|
||||||
*
|
|
||||||
* @param msg The message code to retrieve
|
|
||||||
*
|
|
||||||
* @return The message split by new lines
|
|
||||||
* @deprecated Use {@link Messages#retrieve(MessageKey)} instead.
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public String[] send(String msg) {
|
|
||||||
return retrieve(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve the message from the configuration file.
|
* Retrieve the message from the configuration file.
|
||||||
*
|
*
|
||||||
|
@ -6,6 +6,7 @@ import net.ricecode.similarity.StringSimilarityServiceImpl;
|
|||||||
|
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility class for String operations.
|
* Utility class for String operations.
|
||||||
@ -79,6 +80,9 @@ public final class StringUtils {
|
|||||||
* @return a new String that is composed of the elements separated by the delimiter
|
* @return a new String that is composed of the elements separated by the delimiter
|
||||||
*/
|
*/
|
||||||
public static String join(String delimiter, Iterable<String> elements) {
|
public static String join(String delimiter, Iterable<String> elements) {
|
||||||
|
if (delimiter == null) {
|
||||||
|
delimiter = "";
|
||||||
|
}
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
for (String element : elements) {
|
for (String element : elements) {
|
||||||
if (!isEmpty(element)) {
|
if (!isEmpty(element)) {
|
||||||
@ -101,21 +105,8 @@ public final class StringUtils {
|
|||||||
*
|
*
|
||||||
* @return a new String that is composed of the elements separated by the delimiter
|
* @return a new String that is composed of the elements separated by the delimiter
|
||||||
*/
|
*/
|
||||||
public static String join(CharSequence delimiter, CharSequence... elements) {
|
public static String join(String delimiter, String... elements) {
|
||||||
if (elements.length == 0) {
|
return join(delimiter, Arrays.asList(elements));
|
||||||
return "";
|
|
||||||
}
|
|
||||||
if (delimiter == null) {
|
|
||||||
delimiter = "";
|
|
||||||
}
|
|
||||||
StringBuilder sb = new StringBuilder(elements[0]);
|
|
||||||
if (elements.length > 1) {
|
|
||||||
for (int i = 1; i < elements.length; i++) {
|
|
||||||
sb.append(delimiter);
|
|
||||||
sb.append(elements[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return sb.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,8 +1,5 @@
|
|||||||
package fr.xephi.authme.settings;
|
package fr.xephi.authme.settings;
|
||||||
|
|
||||||
import fr.xephi.authme.AuthMe;
|
|
||||||
import fr.xephi.authme.AuthMeMockUtil;
|
|
||||||
import fr.xephi.authme.ConsoleLogger;
|
|
||||||
import fr.xephi.authme.util.WrapperMock;
|
import fr.xephi.authme.util.WrapperMock;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@ -51,11 +48,23 @@ public class MessagesTest {
|
|||||||
MessageKey key = MessageKey.UNKNOWN_USER;
|
MessageKey key = MessageKey.UNKNOWN_USER;
|
||||||
|
|
||||||
// when
|
// when
|
||||||
String[] send = messages.retrieve(key);
|
String[] message = messages.retrieve(key);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
String[] lines = new String[]{"This test message", "includes", "some new lines"};
|
String[] lines = new String[]{"This test message", "includes", "some new lines"};
|
||||||
assertThat(send, equalTo(lines));
|
assertThat(message, equalTo(lines));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldLoadMessageAsStringWithNewLines() {
|
||||||
|
// given
|
||||||
|
MessageKey key = MessageKey.UNKNOWN_USER;
|
||||||
|
|
||||||
|
// when
|
||||||
|
String message = messages.retrieveSingle(key);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(message, equalTo("This test message\nincludes\nsome new lines"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -65,7 +65,7 @@ public class StringUtilsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldJoinString() {
|
public void shouldJoinStrings() {
|
||||||
// given
|
// given
|
||||||
List<String> elements = Arrays.asList("test", "for", null, "join", "StringUtils");
|
List<String> elements = Arrays.asList("test", "for", null, "join", "StringUtils");
|
||||||
|
|
||||||
@ -76,6 +76,18 @@ public class StringUtilsTest {
|
|||||||
assertThat(result, equalTo("test, for, join, StringUtils"));
|
assertThat(result, equalTo("test, for, join, StringUtils"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldJoinStringArray() {
|
||||||
|
// given
|
||||||
|
String[] elements = {"A", "test", "sentence", "for", "the join", null, "method"};
|
||||||
|
|
||||||
|
// when
|
||||||
|
String result = StringUtils.join("_", elements);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(result, equalTo("A_test_sentence_for_the join_method"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldNotHaveDelimiter() {
|
public void shouldNotHaveDelimiter() {
|
||||||
// given
|
// given
|
||||||
@ -88,6 +100,15 @@ public class StringUtilsTest {
|
|||||||
assertThat(result, equalTo("hello"));
|
assertThat(result, equalTo("hello"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldJoinWithNullDelimiter() {
|
||||||
|
// given/when
|
||||||
|
String result = StringUtils.join(null, "A", "Few", "Words", "\n", "To", "Join");
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(result, equalTo("AFewWordsToJoin"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldFormatException() {
|
public void shouldFormatException() {
|
||||||
// given
|
// given
|
||||||
|
Loading…
Reference in New Issue
Block a user