Fixed YamlConfiguration creating empty lines when reading a file with no contents, and made it trim the initial newlines of any existing config header

By: Nathan Adams <dinnerbone@dinnerbone.com>
This commit is contained in:
Bukkit/Spigot 2012-01-08 23:29:33 +00:00
parent 5fc8f4f576
commit e73be72fae
2 changed files with 30 additions and 6 deletions

View file

@ -148,6 +148,7 @@ public class YamlConfiguration extends FileConfiguration {
String[] lines = input.split("\r?\n", -1);
StringBuilder result = new StringBuilder();
boolean readingHeader = true;
boolean foundHeader = false;
for (int i = 0; (i < lines.length) && (readingHeader); i++) {
String line = lines[i];
@ -160,9 +161,11 @@ public class YamlConfiguration extends FileConfiguration {
if (line.length() > COMMENT_PREFIX.length()) {
result.append(line.substring(COMMENT_PREFIX.length()));
}
} else if (line.length() == 0) {
foundHeader = true;
} else if ((foundHeader) && (line.length() == 0)) {
result.append("\n");
} else {
} else if (foundHeader) {
readingHeader = false;
}
}

View file

@ -170,19 +170,40 @@ public abstract class FileConfigurationTest extends MemoryConfigurationTest {
String saved = getTestValuesString();
String header = getTestHeaderResult();
String expected = getTestHeaderInput();
defaults.loadFromString(header);
config.loadFromString(saved);
config.setDefaults(defaults);
assertNull(config.options().header());
assertEquals(expected, defaults.options().header());
for (Map.Entry<String, Object> entry : values.entrySet()) {
assertEquals(entry.getValue(), config.get(entry.getKey()));
}
assertEquals(values.keySet(), config.getKeys(true));
assertEquals(header + "\n" + saved, config.saveToString());
config = getConfig();
config.loadFromString(getTestHeaderResult() + saved);
assertEquals(getTestHeaderResult() + saved, config.saveToString());
}
@Test
public void testReloadEmptyConfig() throws Exception {
FileConfiguration config = getConfig();
assertEquals("", config.saveToString());
config = getConfig();
config.loadFromString("");
assertEquals("", config.saveToString());
config = getConfig();
config.loadFromString("\n\n"); // Should trim the first newlines of a header
assertEquals("", config.saveToString());
}
}