Skip to content

Commit e7df8e4

Browse files
authored
set-props: support UTF-8 encoded properties file (#328)
1 parent 9031519 commit e7df8e4

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ java {
123123
sourceCompatibility = JavaVersion.VERSION_1_8
124124
}
125125

126+
compileJava {
127+
options.encoding = 'utf-8'
128+
}
129+
130+
compileTestJava {
131+
options.encoding = 'utf-8'
132+
}
133+
126134
githubReleaser {
127135
project {
128136
homepage = 'https://github.com/itzg/mc-image-helper'

src/main/java/me/itzg/helpers/properties/SetPropertiesCommand.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package me.itzg.helpers.properties;
22

33
import com.fasterxml.jackson.core.type.TypeReference;
4-
import java.io.InputStream;
54
import java.io.OutputStream;
5+
import java.io.Reader;
6+
import java.nio.charset.StandardCharsets;
67
import java.nio.file.Files;
78
import java.nio.file.Path;
89
import java.nio.file.StandardOpenOption;
@@ -70,8 +71,8 @@ public Integer call() throws Exception {
7071

7172
final Properties properties = new Properties();
7273
if (Files.exists(propertiesFile)) {
73-
try (InputStream propsIn = Files.newInputStream(propertiesFile)) {
74-
properties.load(propsIn);
74+
try (Reader propsReader = Files.newBufferedReader(propertiesFile, StandardCharsets.UTF_8)) {
75+
properties.load(propsReader);
7576
}
7677
}
7778

src/test/java/me/itzg/helpers/properties/SetPropertiesCommandTest.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.io.InputStream;
88
import java.net.URISyntaxException;
99
import java.net.URL;
10+
import java.nio.charset.StandardCharsets;
1011
import java.nio.file.Files;
1112
import java.nio.file.Path;
1213
import java.nio.file.Paths;
@@ -34,7 +35,7 @@ class SetPropertiesCommandTest {
3435

3536
@BeforeEach
3637
void setUp() throws IOException, URISyntaxException {
37-
propertiesFile = preparePropertiesFile();
38+
propertiesFile = preparePropertiesFile("server.properties");
3839

3940
final URL definitionsResource = getClass().getResource("/properties/property-definitions.json");
4041
assertThat(definitionsResource).isNotNull();
@@ -173,6 +174,24 @@ void handlesModifiedCustomProperties() throws IOException {
173174
.containsEntry("key2", "value2");
174175
}
175176

177+
@Test
178+
void handlesExistingUnicodePropertyValue() throws IOException {
179+
final Path outputProperties = preparePropertiesFile("with-unicode.txt");
180+
181+
final int exitCode = new CommandLine(new SetPropertiesCommand())
182+
.execute(
183+
"--custom-property", "key1=value1",
184+
outputProperties.toString()
185+
);
186+
187+
assertThat(exitCode).isEqualTo(ExitCode.OK);
188+
189+
assertThat(outputProperties)
190+
.content(StandardCharsets.UTF_8)
191+
.containsIgnoringNewLines("motd=\\u00A7c\\u00A7lT\\u00A76\\u00A7le\\u00A7e\\u00A7ls\\u00A7a\\u00A7lt\\u00A73\\u00A7li\\u00A79\\u00A7ln\\u00A75\\u00A7lg \\u00A76\\u00A7l1\\u00A7e\\u00A7l2\\u00A7a\\u00A7l3")
192+
.containsIgnoringNewLines("key1=value1");
193+
}
194+
176195
private void assertPropertiesEqualExcept(Properties properties, String... propertiesToIgnore) {
177196
final HashSet<Object> actualKeys = new HashSet<>(properties.keySet());
178197
Arrays.asList(propertiesToIgnore).forEach(actualKeys::remove);
@@ -196,12 +215,13 @@ private Properties loadProperties() throws IOException {
196215
return properties;
197216
}
198217

199-
private Path preparePropertiesFile() throws IOException {
200-
try (InputStream in = getClass().getClassLoader().getResourceAsStream("properties/server.properties")) {
201-
assertThat(in).isNotNull();
202-
final Path outFile = tempDir.resolve("server.properties");
203-
Files.copy(in, outFile);
204-
return outFile;
218+
private Path preparePropertiesFile(String filename) throws IOException {
219+
final URL resource = getClass().getClassLoader().getResource("properties/" + filename);
220+
assertThat(resource).isNotNull();
221+
try {
222+
return Files.copy(Paths.get(resource.toURI()), tempDir.resolve(filename));
223+
} catch (URISyntaxException e) {
224+
throw new RuntimeException(e);
205225
}
206226
}
207227
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
motd=§c§lT§6§le§e§ls§a§lt§3§li§9§ln§5§lg §6§l1§e§l2§a§l3

0 commit comments

Comments
 (0)