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

This commit is contained in:
Nathan Adams 2011-10-12 04:39:22 +01:00
parent 984c8ec90d
commit 5703334ac8
3 changed files with 26 additions and 7 deletions

View File

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

View File

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

View File

@ -31,7 +31,7 @@ public class YamlConfigurationTest extends FileConfigurationTest {
} }
@Test @Test
public void testSaveToStringWithheader() { public void testSaveToStringWithHeader() {
YamlConfiguration config = getConfig(); YamlConfiguration config = getConfig();
config.options().header("This is a sample\nheader."); config.options().header("This is a sample\nheader.");
@ -45,6 +45,21 @@ public class YamlConfigurationTest extends FileConfigurationTest {
assertEquals(expected, result); 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 @Test
public void testSaveToStringWithIndent() { public void testSaveToStringWithIndent() {
YamlConfiguration config = getConfig(); YamlConfiguration config = getConfig();