YamlConfiguration now correctly writes extra blank lines at the end if requested

By: Nathan Adams <dinnerbone@dinnerbone.com>
This commit is contained in:
Bukkit/Spigot 2011-10-12 04:39:22 +01:00
parent b94e100930
commit ea55a50756
3 changed files with 26 additions and 7 deletions

View file

@ -137,12 +137,17 @@ public class YamlConfiguration extends FileConfiguration {
}
StringBuilder builder = new StringBuilder();
String[] lines = header.split("\r?\n");
String[] lines = header.split("\r?\n", -1);
boolean startedHeader = false;
for (int i = 0; i < lines.length; i++) {
builder.append(COMMENT_PREFIX);
builder.append(lines[i]);
builder.append("\n");
for (int i = lines.length - 1; i >= 0; i--) {
builder.insert(0, "\n");
if ((startedHeader) || (lines[i].length() != 0)) {
builder.insert(0, lines[i]);
builder.insert(0, COMMENT_PREFIX);
startedHeader = true;
}
}
return builder.toString();

View file

@ -6,7 +6,6 @@ import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Map;
import java.util.Scanner;
import org.bukkit.configuration.MemoryConfigurationTest;
import org.junit.Rule;
import org.junit.Test;

View file

@ -31,7 +31,7 @@ public class YamlConfigurationTest extends FileConfigurationTest {
}
@Test
public void testSaveToStringWithheader() {
public void testSaveToStringWithHeader() {
YamlConfiguration config = getConfig();
config.options().header("This is a sample\nheader.");
@ -45,6 +45,21 @@ public class YamlConfigurationTest extends FileConfigurationTest {
assertEquals(expected, result);
}
@Test
public void testSaveToStringWithLongHeader() {
YamlConfiguration config = getConfig();
config.options().header("This is a sample\nheader.\n\nNewline above should be commented.\n\n");
for (Map.Entry<String, Object> entry : getTestValues().entrySet()) {
config.set(entry.getKey(), entry.getValue());
}
String result = config.saveToString();
String expected = "# This is a sample\n# header.\n# \n# Newline above should be commented.\n\n\n" + getTestValuesString();
assertEquals(expected, result);
}
@Test
public void testSaveToStringWithIndent() {
YamlConfiguration config = getConfig();