Skip to content

Commit 8ed672e

Browse files
committed
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
1 parent 210b691 commit 8ed672e

File tree

4 files changed

+52
-45
lines changed

4 files changed

+52
-45
lines changed

src/main/java/fr/xephi/authme/settings/Messages.java

+10-24
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.io.File;
88

99
/**
10+
* Class for retrieving and sending translatable messages to players.
1011
*/
1112
// TODO ljacqu 20151124: This class is a weird mix between singleton and POJO
1213
// TODO: change it into POJO
@@ -15,7 +16,7 @@ public class Messages extends CustomConfiguration {
1516
/** The section symbol, used in Minecraft for formatting codes. */
1617
private static final String SECTION_SIGN = "\u00a7";
1718
private static Messages singleton;
18-
private String language = "en";
19+
private String language;
1920

2021

2122
/**
@@ -51,40 +52,25 @@ public void send(CommandSender sender, MessageKey key) {
5152
}
5253

5354
/**
54-
* Send the given message code to the player.
55+
* Retrieve the message from the text file and return it split by new line as an array.
5556
*
56-
* @param sender The entity to send the message to
57-
* @param msg The message code to send
57+
* @param key The message key to retrieve
5858
*
59-
* @deprecated Use {@link Messages#send(CommandSender, MessageKey)} instead
59+
* @return The message split by new lines
6060
*/
61-
@Deprecated
62-
public void send(CommandSender sender, String msg) {
63-
String[] lines = retrieve(msg);
64-
for (String line : lines) {
65-
sender.sendMessage(line);
66-
}
67-
}
68-
6961
public String[] retrieve(MessageKey key) {
7062
return retrieve(key.getKey());
7163
}
7264

73-
public String retrieveSingle(MessageKey key) {
74-
return StringUtils.join("\n", retrieve(key.getKey()));
75-
}
76-
7765
/**
78-
* Retrieve the message from the text file and returns it split by new line as an array.
66+
* Retrieve the message from the text file.
7967
*
80-
* @param msg The message code to retrieve
68+
* @param key The message key to retrieve
8169
*
82-
* @return The message split by new lines
83-
* @deprecated Use {@link Messages#retrieve(MessageKey)} instead.
70+
* @return The message from the file
8471
*/
85-
@Deprecated
86-
public String[] send(String msg) {
87-
return retrieve(msg);
72+
public String retrieveSingle(MessageKey key) {
73+
return StringUtils.join("\n", retrieve(key.getKey()));
8874
}
8975

9076
/**

src/main/java/fr/xephi/authme/util/StringUtils.java

+6-15
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.io.PrintWriter;
88
import java.io.StringWriter;
9+
import java.util.Arrays;
910

1011
/**
1112
* Utility class for String operations.
@@ -79,6 +80,9 @@ public static boolean isEmpty(String str) {
7980
* @return a new String that is composed of the elements separated by the delimiter
8081
*/
8182
public static String join(String delimiter, Iterable<String> elements) {
83+
if (delimiter == null) {
84+
delimiter = "";
85+
}
8286
StringBuilder sb = new StringBuilder();
8387
for (String element : elements) {
8488
if (!isEmpty(element)) {
@@ -101,21 +105,8 @@ public static String join(String delimiter, Iterable<String> elements) {
101105
*
102106
* @return a new String that is composed of the elements separated by the delimiter
103107
*/
104-
public static String join(CharSequence delimiter, CharSequence... elements) {
105-
if (elements.length == 0) {
106-
return "";
107-
}
108-
if (delimiter == null) {
109-
delimiter = "";
110-
}
111-
StringBuilder sb = new StringBuilder(elements[0]);
112-
if (elements.length > 1) {
113-
for (int i = 1; i < elements.length; i++) {
114-
sb.append(delimiter);
115-
sb.append(elements[i]);
116-
}
117-
}
118-
return sb.toString();
108+
public static String join(String delimiter, String... elements) {
109+
return join(delimiter, Arrays.asList(elements));
119110
}
120111

121112
/**

src/test/java/fr/xephi/authme/settings/MessagesTest.java

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package fr.xephi.authme.settings;
22

3-
import fr.xephi.authme.AuthMe;
4-
import fr.xephi.authme.AuthMeMockUtil;
5-
import fr.xephi.authme.ConsoleLogger;
63
import fr.xephi.authme.util.WrapperMock;
74
import org.bukkit.entity.Player;
85
import org.junit.Before;
@@ -51,11 +48,23 @@ public void shouldLoadMessageAndSplitAtNewLines() {
5148
MessageKey key = MessageKey.UNKNOWN_USER;
5249

5350
// when
54-
String[] send = messages.retrieve(key);
51+
String[] message = messages.retrieve(key);
5552

5653
// then
5754
String[] lines = new String[]{"This test message", "includes", "some new lines"};
58-
assertThat(send, equalTo(lines));
55+
assertThat(message, equalTo(lines));
56+
}
57+
58+
@Test
59+
public void shouldLoadMessageAsStringWithNewLines() {
60+
// given
61+
MessageKey key = MessageKey.UNKNOWN_USER;
62+
63+
// when
64+
String message = messages.retrieveSingle(key);
65+
66+
// then
67+
assertThat(message, equalTo("This test message\nincludes\nsome new lines"));
5968
}
6069

6170
@Test

src/test/java/fr/xephi/authme/util/StringUtilsTest.java

+22-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void shouldCheckIsEmptyUtil() {
6565
}
6666

6767
@Test
68-
public void shouldJoinString() {
68+
public void shouldJoinStrings() {
6969
// given
7070
List<String> elements = Arrays.asList("test", "for", null, "join", "StringUtils");
7171

@@ -76,6 +76,18 @@ public void shouldJoinString() {
7676
assertThat(result, equalTo("test, for, join, StringUtils"));
7777
}
7878

79+
@Test
80+
public void shouldJoinStringArray() {
81+
// given
82+
String[] elements = {"A", "test", "sentence", "for", "the join", null, "method"};
83+
84+
// when
85+
String result = StringUtils.join("_", elements);
86+
87+
// then
88+
assertThat(result, equalTo("A_test_sentence_for_the join_method"));
89+
}
90+
7991
@Test
8092
public void shouldNotHaveDelimiter() {
8193
// given
@@ -88,6 +100,15 @@ public void shouldNotHaveDelimiter() {
88100
assertThat(result, equalTo("hello"));
89101
}
90102

103+
@Test
104+
public void shouldJoinWithNullDelimiter() {
105+
// given/when
106+
String result = StringUtils.join(null, "A", "Few", "Words", "\n", "To", "Join");
107+
108+
// then
109+
assertThat(result, equalTo("AFewWordsToJoin"));
110+
}
111+
91112
@Test
92113
public void shouldFormatException() {
93114
// given

0 commit comments

Comments
 (0)