#534 Get default messages from the JAR's messages_en.yml

- Using new File(class.getResource(path)) apparently is the wrong approach for in-JAR files
This commit is contained in:
ljacqu 2016-02-20 12:12:24 +01:00
parent 8511a257ed
commit 511f961d29
5 changed files with 19 additions and 21 deletions

View File

@ -8,6 +8,8 @@ import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* Class for retrieving and sending translatable messages to players.
@ -16,7 +18,7 @@ public class Messages {
private FileConfiguration configuration;
private String fileName;
private final File defaultFile;
private final String defaultFile;
private FileConfiguration defaultConfiguration;
/**
@ -25,7 +27,7 @@ public class Messages {
* @param messageFile The messages file to use
* @param defaultFile The file with messages to use as default if missing
*/
public Messages(File messageFile, File defaultFile) {
public Messages(File messageFile, String defaultFile) {
initializeFile(messageFile);
this.defaultFile = defaultFile;
}
@ -117,7 +119,8 @@ public class Messages {
}
if (defaultConfiguration == null) {
defaultConfiguration = YamlConfiguration.loadConfiguration(defaultFile);
InputStream stream = Messages.class.getResourceAsStream(defaultFile);
defaultConfiguration = YamlConfiguration.loadConfiguration(new InputStreamReader(stream));
}
String message = defaultConfiguration.getString(code);
return message == null ? getDefaultErrorMessage(code) : message;

View File

@ -18,7 +18,6 @@ import org.yaml.snakeyaml.Yaml;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
@ -110,18 +109,12 @@ public class NewSetting {
}
/**
* Return the default messages file within the JAR that should contain all messages.
* Return the path to the default messages file within the JAR.
*
* @return The default messages file, or {@code null} if it could not be retrieved
* @return The default messages file path
*/
public File getDefaultMessagesFile() {
String defaultFilePath = "/messages/messages_en.yml";
URL url = NewSetting.class.getResource(defaultFilePath);
if (url == null) {
return null;
}
File file = new File(url.getFile());
return file.exists() ? file : null;
public String getDefaultMessagesFile() {
return "/messages/messages_en.yml";
}
public String getEmailMessage() {

View File

@ -50,8 +50,7 @@ public class MessagesIntegrationTest {
@Before
public void setUpMessages() {
File testFile = TestHelper.getJarFile(YML_TEST_FILE);
File defaultFile = TestHelper.getJarFile(YML_DEFAULT_TEST_FILE);
messages = new Messages(testFile, defaultFile);
messages = new Messages(testFile, YML_DEFAULT_TEST_FILE);
}
@Test

View File

@ -7,7 +7,8 @@ import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.Test;
import org.mockito.internal.stubbing.answers.ReturnsArgumentAt;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
@ -55,17 +56,19 @@ public class NewSettingTest {
}
@Test
public void shouldReturnDefaultFile() {
public void shouldReturnDefaultFile() throws IOException {
// given
YamlConfiguration configuration = mock(YamlConfiguration.class);
NewSetting settings = new NewSetting(configuration, null, null);
// when
File defaultFile = settings.getDefaultMessagesFile();
String defaultFile = settings.getDefaultMessagesFile();
// then
assertThat(defaultFile, not(nullValue()));
assertThat(defaultFile.exists(), equalTo(true));
InputStream stream = this.getClass().getResourceAsStream(defaultFile);
assertThat(stream, not(nullValue()));
assertThat(stream.read(), not(equalTo(0)));
}
private static <T> void setReturnValue(YamlConfiguration config, Property<T> property, T value) {

View File

@ -35,7 +35,7 @@ public final class TestConfiguration implements SettingsClass {
newProperty(PropertyType.STRING_LIST, "features.boring.colors");
public static final Property<Integer> DUST_LEVEL =
newProperty(PropertyType.INTEGER, "features.boring.dustLevel", -1);
newProperty("features.boring.dustLevel", -1);
public static final Property<Boolean> USE_COOL_FEATURES =
newProperty("features.cool.enabled", false);