Added new Configuration classes

By: Dinnerbone <dinnerbone@dinnerbone.com>
This commit is contained in:
Bukkit/Spigot
2011-09-19 20:36:44 +01:00
parent e99e39ee62
commit 6c7412d365
31 changed files with 3454 additions and 9 deletions

View File

@@ -0,0 +1,504 @@
package org.bukkit.configuration;
import org.bukkit.Material;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;
import org.junit.Test;
import static org.junit.Assert.*;
public abstract class ConfigurationSectionTest {
public abstract ConfigurationSection getConfigurationSection();
@Test
public void testGetKeys() {
ConfigurationSection section = getConfigurationSection();
section.set("key", true);
section.set("subsection.subkey", true);
section.set("subsection.subkey2", true);
section.set("subsection.subsubsection.key", true);
section.set("key2", true);
assertArrayEquals(new String[] {"key", "subsection", "key2"}, section.getKeys(false).toArray());
assertArrayEquals(new String[] {"key", "subsection", "subsection.subkey", "subsection.subkey2", "subsection.subsubsection", "subsection.subsubsection.key", "key2"}, section.getKeys(true).toArray());
assertArrayEquals(new String[] {"subkey", "subkey2", "subsubsection", "subsubsection.key"}, section.getConfigurationSection("subsection").getKeys(true).toArray());
}
@Test
public void testGetKeysWithDefaults() {
ConfigurationSection section = getConfigurationSection();
section.getRoot().options().copyDefaults(true);
section.set("key", true);
section.addDefault("subsection.subkey", true);
section.addDefault("subsection.subkey2", true);
section.addDefault("subsection.subsubsection.key", true);
section.addDefault("key2", true);
assertArrayEquals(new String[] {"subsection", "key2", "key"}, section.getKeys(false).toArray());
assertArrayEquals(new String[] {"subsection", "subsection.subkey", "subsection.subkey2", "subsection.subsubsection", "subsection.subsubsection.key", "key2", "key"}, section.getKeys(true).toArray());
assertArrayEquals(new String[] {"subkey", "subkey2", "subsubsection", "subsubsection.key"}, section.getConfigurationSection("subsection").getKeys(true).toArray());
}
@Test
public void testGetValues() {
ConfigurationSection section = getConfigurationSection();
section.set("bool", true);
section.set("subsection.string", "test");
section.set("subsection.long", Long.MAX_VALUE);
section.set("int", 42);
Map<String, Object> shallowValues = section.getValues(false);
assertArrayEquals(new String[] {"bool", "subsection", "int"}, shallowValues.keySet().toArray());
assertArrayEquals(new Object[] {true, section.getConfigurationSection("subsection"), 42}, shallowValues.values().toArray());
Map<String, Object> deepValues = section.getValues(true);
assertArrayEquals(new String[] {"bool", "subsection", "subsection.string", "subsection.long", "int"}, deepValues.keySet().toArray());
assertArrayEquals(new Object[] {true, section.getConfigurationSection("subsection"), "test", Long.MAX_VALUE, 42}, deepValues.values().toArray());
}
@Test
public void testGetValuesWithDefaults() {
ConfigurationSection section = getConfigurationSection();
section.getRoot().options().copyDefaults(true);
section.set("bool", true);
section.set("subsection.string", "test");
section.addDefault("subsection.long", Long.MAX_VALUE);
section.addDefault("int", 42);
Map<String, Object> shallowValues = section.getValues(false);
assertArrayEquals(new String[] {"subsection", "int", "bool"}, shallowValues.keySet().toArray());
assertArrayEquals(new Object[] {section.getConfigurationSection("subsection"), 42, true}, shallowValues.values().toArray());
Map<String, Object> deepValues = section.getValues(true);
assertArrayEquals(new String[] {"subsection", "subsection.long", "int", "bool", "subsection.string"}, deepValues.keySet().toArray());
assertArrayEquals(new Object[] {section.getConfigurationSection("subsection"), Long.MAX_VALUE, 42, true, "test"}, deepValues.values().toArray());
}
@Test
public void testContains() {
ConfigurationSection section = getConfigurationSection();
section.set("exists", true);
assertTrue(section.contains("exists"));
assertFalse(section.contains("doesnt-exist"));
}
@Test
public void testIsSet() {
ConfigurationSection section = getConfigurationSection();
section.set("notDefault", true);
section.getRoot().addDefault("default", true);
section.getRoot().addDefault("defaultAndSet", true);
section.set("defaultAndSet", true);
assertTrue(section.isSet("notDefault"));
assertFalse(section.isSet("default"));
assertTrue(section.isSet("defaultAndSet"));
}
@Test
public void testGetCurrentPath() {
ConfigurationSection section = getConfigurationSection();
assertEquals(section.getName(), section.getCurrentPath());
}
@Test
public void testGetName() {
ConfigurationSection section = getConfigurationSection().createSection("subsection");
assertEquals("subsection", section.getName());
assertEquals("", section.getRoot().getName());
}
@Test
public void testGetRoot() {
ConfigurationSection section = getConfigurationSection();
assertNotNull(section.getRoot());
assertTrue(section.getRoot().contains(section.getCurrentPath()));
}
@Test
public void testGetParent() {
ConfigurationSection section = getConfigurationSection();
ConfigurationSection subsection = section.createSection("subsection");
assertEquals(section.getRoot(), section.getParent());
assertEquals(section, subsection.getParent());
}
@Test
public void testGet_String() {
ConfigurationSection section = getConfigurationSection();
section.set("exists", "hello world");
assertEquals("hello world", section.getString("exists"));
assertNull(section.getString("doesntExist"));
}
@Test
public void testGet_String_Object() {
ConfigurationSection section = getConfigurationSection();
section.set("exists", "Set Value");
assertEquals("Set Value", section.get("exists", "Default Value"));
assertEquals("Default Value", section.get("doesntExist", "Default Value"));
}
@Test
public void testSet() {
ConfigurationSection section = getConfigurationSection();
section.set("exists", "hello world");
assertEquals("hello world", section.get("exists"));
}
@Test
public void testCreateSection() {
ConfigurationSection section = getConfigurationSection();
ConfigurationSection subsection = section.createSection("subsection");
assertEquals("subsection", subsection.getName());
}
@Test
public void testGetString_String() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
String value = "Hello World";
section.set(key, value);
assertEquals(value, section.getString(key));
assertNull(section.getString("doesntExist"));
}
@Test
public void testGetString_String_String() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
String value = "Hello World";
String def = "Default Value";
section.set(key, value);
assertEquals(value, section.getString(key, def));
assertEquals(def, section.getString("doesntExist", def));
}
@Test
public void testIsString() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
String value = "Hello World";
section.set(key, value);
assertTrue(section.isString(key));
assertFalse(section.isString("doesntExist"));
}
@Test
public void testGetInt_String() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
int value = Integer.MAX_VALUE;
section.set(key, value);
assertEquals(value, section.getInt(key));
assertNull(section.getString("doesntExist"));
}
@Test
public void testGetInt_String_Int() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
int value = Integer.MAX_VALUE;
int def = Integer.MIN_VALUE;
section.set(key, value);
assertEquals(value, section.getInt(key, def));
assertEquals(def, section.getInt("doesntExist", def));
}
@Test
public void testIsInt() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
int value = Integer.MAX_VALUE;
section.set(key, value);
assertTrue(section.isInt(key));
assertFalse(section.isInt("doesntExist"));
}
@Test
public void testGetBoolean_String() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
boolean value = true;
section.set(key, value);
assertEquals(value, section.getBoolean(key));
assertNull(section.getString("doesntExist"));
}
@Test
public void testGetBoolean_String_Boolean() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
boolean value = true;
boolean def = false;
section.set(key, value);
assertEquals(value, section.getBoolean(key, def));
assertEquals(def, section.getBoolean("doesntExist", def));
}
@Test
public void testIsBoolean() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
boolean value = true;
section.set(key, value);
assertTrue(section.isBoolean(key));
assertFalse(section.isBoolean("doesntExist"));
}
@Test
public void testGetDouble_String() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
double value = Double.MAX_VALUE;
section.set(key, value);
assertEquals(value, section.getDouble(key), 1);
assertNull(section.getString("doesntExist"));
}
@Test
public void testGetDouble_String_Double() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
double value = Double.MAX_VALUE;
double def = Double.MIN_VALUE;
section.set(key, value);
assertEquals(value, section.getDouble(key, def), 1);
assertEquals(def, section.getDouble("doesntExist", def), 1);
}
@Test
public void testIsDouble() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
double value = Double.MAX_VALUE;
section.set(key, value);
assertTrue(section.isDouble(key));
assertFalse(section.isDouble("doesntExist"));
}
@Test
public void testGetLong_String() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
long value = Long.MAX_VALUE;
section.set(key, value);
assertEquals(value, section.getLong(key));
assertNull(section.getString("doesntExist"));
}
@Test
public void testGetLong_String_Long() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
long value = Long.MAX_VALUE;
long def = Long.MIN_VALUE;
section.set(key, value);
assertEquals(value, section.getLong(key, def));
assertEquals(def, section.getLong("doesntExist", def));
}
@Test
public void testIsLong() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
long value = Long.MAX_VALUE;
section.set(key, value);
assertTrue(section.isLong(key));
assertFalse(section.isLong("doesntExist"));
}
@Test
public void testGetList_String() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
List value = Arrays.asList("One", "Two", "Three");
section.set(key, value);
assertEquals(value, section.getList(key));
assertNull(section.getString("doesntExist"));
}
@Test
public void testGetList_String_List() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
List value = Arrays.asList("One", "Two", "Three");
List def = Arrays.asList("A", "B", "C");
section.set(key, value);
assertEquals(value, section.getList(key, def));
assertEquals(def, section.getList("doesntExist", def));
}
@Test
public void testIsList() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
List value = Arrays.asList("One", "Two", "Three");
section.set(key, value);
assertTrue(section.isList(key));
assertFalse(section.isList("doesntExist"));
}
@Test
public void testGetVector_String() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
Vector value = new Vector(Double.MIN_VALUE, Double.MAX_VALUE, 5);
section.set(key, value);
assertEquals(value, section.getVector(key));
assertNull(section.getString("doesntExist"));
}
@Test
public void testGetVector_String_Vector() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
Vector value = new Vector(Double.MIN_VALUE, Double.MAX_VALUE, 5);
Vector def = new Vector(100, Double.MIN_VALUE, Double.MAX_VALUE);
section.set(key, value);
assertEquals(value, section.getVector(key, def));
assertEquals(def, section.getVector("doesntExist", def));
}
@Test
public void testIsVector() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
Vector value = new Vector(Double.MIN_VALUE, Double.MAX_VALUE, 5);
section.set(key, value);
assertTrue(section.isVector(key));
assertFalse(section.isVector("doesntExist"));
}
@Test
public void testGetItemStack_String() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
ItemStack value = new ItemStack(Material.WOOD, 50, (short)2);
section.set(key, value);
assertEquals(value, section.getItemStack(key));
assertNull(section.getString("doesntExist"));
}
@Test
public void testGetItemStack_String_ItemStack() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
ItemStack value = new ItemStack(Material.WOOD, 50, (short)2);
ItemStack def = new ItemStack(Material.STONE, 1);
section.set(key, value);
assertEquals(value, section.getItemStack(key, def));
assertEquals(def, section.getItemStack("doesntExist", def));
}
@Test
public void testIsItemStack() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
ItemStack value = new ItemStack(Material.WOOD, 50, (short)2);
section.set(key, value);
assertTrue(section.isItemStack(key));
assertFalse(section.isItemStack("doesntExist"));
}
@Test
public void testGetConfigurationSection() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
ConfigurationSection subsection = section.createSection(key);
assertEquals(subsection, section.getConfigurationSection(key));
}
@Test
public void testIsConfigurationSection() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
ConfigurationSection subsection = section.createSection(key);
assertTrue(section.isConfigurationSection(key));
assertFalse(section.isConfigurationSection("doesntExist"));
}
public enum TestEnum {
HELLO,
WORLD,
BANANAS
}
}

View File

@@ -0,0 +1,135 @@
package org.bukkit.configuration;
import java.util.LinkedHashMap;
import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.util.Vector;
import org.junit.Test;
import static org.junit.Assert.*;
public abstract class ConfigurationTest {
public abstract Configuration getConfig();
public Map<String, Object> getTestValues() {
HashMap<String, Object> result = new LinkedHashMap<String, Object>();
result.put("integer", Integer.MIN_VALUE);
result.put("string", "String Value");
result.put("long", Long.MAX_VALUE);
result.put("true-boolean", true);
result.put("false-boolean", false);
result.put("vector", new Vector(12345.67, 64, -12345.6789));
result.put("list", Arrays.asList(1, 2, 3, 4, 5));
return result;
}
/**
* Test of addDefault method, of class Configuration.
*/
@Test
public void testAddDefault() {
Configuration config = getConfig();
Map<String, Object> values = getTestValues();
for (Map.Entry<String, Object> entry : values.entrySet()) {
String path = entry.getKey();
Object object = entry.getValue();
config.addDefault(path, object);
assertEquals(object, config.get(path));
assertTrue(config.contains(path));
assertFalse(config.isSet(path));
assertTrue(config.getDefaults().isSet(path));
}
}
/**
* Test of addDefaults method, of class Configuration.
*/
@Test
public void testAddDefaults_Map() {
Configuration config = getConfig();
Map<String, Object> values = getTestValues();
config.addDefaults(values);
for (Map.Entry<String, Object> entry : values.entrySet()) {
String path = entry.getKey();
Object object = entry.getValue();
assertEquals(object, config.get(path));
assertTrue(config.contains(path));
assertFalse(config.isSet(path));
assertTrue(config.getDefaults().isSet(path));
}
}
/**
* Test of addDefaults method, of class Configuration.
*/
@Test
public void testAddDefaults_Configuration() {
Configuration config = getConfig();
Map<String, Object> values = getTestValues();
Configuration defaults = getConfig();
for (Map.Entry<String, Object> entry : values.entrySet()) {
defaults.set(entry.getKey(), entry.getValue());
}
config.addDefaults(defaults);
for (Map.Entry<String, Object> entry : values.entrySet()) {
String path = entry.getKey();
Object object = entry.getValue();
assertEquals(object, config.get(path));
assertTrue(config.contains(path));
assertFalse(config.isSet(path));
assertTrue(config.getDefaults().isSet(path));
}
}
/**
* Test of setDefaults method, of class Configuration.
*/
@Test
public void testSetDefaults() {
Configuration config = getConfig();
Map<String, Object> values = getTestValues();
Configuration defaults = getConfig();
for (Map.Entry<String, Object> entry : values.entrySet()) {
defaults.set(entry.getKey(), entry.getValue());
}
config.setDefaults(defaults);
for (Map.Entry<String, Object> entry : values.entrySet()) {
String path = entry.getKey();
Object object = entry.getValue();
assertEquals(object, config.get(path));
assertTrue(config.contains(path));
assertFalse(config.isSet(path));
assertTrue(config.getDefaults().isSet(path));
}
}
/**
* Test of getDefaults method, of class Configuration.
*/
@Test
public void testGetDefaults() {
Configuration config = getConfig();
Configuration defaults = getConfig();
config.setDefaults(defaults);
assertEquals(defaults, config.getDefaults());
}
}

View File

@@ -0,0 +1,8 @@
package org.bukkit.configuration;
public class MemoryConfigurationTest extends ConfigurationTest {
@Override
public Configuration getConfig() {
return new MemoryConfiguration();
}
}

View File

@@ -0,0 +1,8 @@
package org.bukkit.configuration;
public class MemorySectionTest extends ConfigurationSectionTest {
@Override
public ConfigurationSection getConfigurationSection() {
return new MemoryConfiguration().createSection("section");
}
}

View File

@@ -0,0 +1 @@
package org.bukkit.configuration;

View File

@@ -0,0 +1,127 @@
package org.bukkit.configuration.file;
import java.io.BufferedReader;
import java.io.BufferedWriter;
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;
import org.junit.rules.TemporaryFolder;
import static org.junit.Assert.*;
public abstract class FileConfigurationTest extends MemoryConfigurationTest {
@Rule
public TemporaryFolder testFolder = new TemporaryFolder();
@Override
public abstract FileConfiguration getConfig();
public abstract String getTestValuesString();
@Test
public void testSave_File() throws Exception {
FileConfiguration config = getConfig();
File file = testFolder.newFile("test.config");
for (Map.Entry<String, Object> entry : getTestValues().entrySet()) {
config.set(entry.getKey(), entry.getValue());
}
config.save(file);
assertTrue(file.isFile());
}
@Test
public void testSave_String() throws Exception {
FileConfiguration config = getConfig();
File file = testFolder.newFile("test.config");
for (Map.Entry<String, Object> entry : getTestValues().entrySet()) {
config.set(entry.getKey(), entry.getValue());
}
config.save(file.getAbsolutePath());
assertTrue(file.isFile());
}
@Test
public void testSaveToString() {
FileConfiguration config = getConfig();
for (Map.Entry<String, Object> entry : getTestValues().entrySet()) {
config.set(entry.getKey(), entry.getValue());
}
String result = config.saveToString();
String expected = getTestValuesString();
assertEquals(expected, result);
}
@Test
public void testLoad_File() throws Exception {
FileConfiguration config = getConfig();
File file = testFolder.newFile("test.config");
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
String saved = getTestValuesString();
Map<String, Object> values = getTestValues();
try {
writer.write(saved);
} finally {
writer.close();
}
config.load(file);
for (Map.Entry<String, Object> entry : values.entrySet()) {
assertEquals(entry.getValue(), config.get(entry.getKey()));
}
assertEquals(values.keySet(), config.getKeys(true));
}
@Test
public void testLoad_String() throws Exception {
FileConfiguration config = getConfig();
File file = testFolder.newFile("test.config");
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
String saved = getTestValuesString();
Map<String, Object> values = getTestValues();
try {
writer.write(saved);
} finally {
writer.close();
}
config.load(file.getAbsolutePath());
for (Map.Entry<String, Object> entry : values.entrySet()) {
assertEquals(entry.getValue(), config.get(entry.getKey()));
}
assertEquals(values.keySet(), config.getKeys(true));
}
@Test
public void testLoadFromString() throws Exception {
FileConfiguration config = getConfig();
Map<String, Object> values = getTestValues();
String saved = getTestValuesString();
config.loadFromString(saved);
for (Map.Entry<String, Object> entry : values.entrySet()) {
assertEquals(entry.getValue(), config.get(entry.getKey()));
}
assertEquals(values.keySet(), config.getKeys(true));
}
}

View File

@@ -0,0 +1,62 @@
package org.bukkit.configuration.file;
import java.util.Map;
import org.junit.Test;
import static org.junit.Assert.*;
public class YamlConfigurationTest extends FileConfigurationTest {
@Override
public YamlConfiguration getConfig() {
return new YamlConfiguration();
}
@Override
public String getTestValuesString() {
return "integer: -2147483648\n" +
"string: String Value\n" +
"long: 9223372036854775807\n" +
"true-boolean: true\n" +
"false-boolean: false\n" +
"vector:\n" +
" ==: Vector\n" +
" x: 12345.67\n" +
" y: 64.0\n" +
" z: -12345.6789\n" +
"list:\n" +
"- 1\n" +
"- 2\n" +
"- 3\n" +
"- 4\n" +
"- 5\n";
}
@Test
public void testSaveToStringWithheader() {
YamlConfiguration config = getConfig();
config.options().header("This is a sample\nheader.");
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" + getTestValuesString();
assertEquals(expected, result);
}
@Test
public void testSaveToStringWithIndent() {
YamlConfiguration config = getConfig();
config.options().indent(9);
config.set("section.key", 1);
String result = config.saveToString();
String expected = "section:\n key: 1\n";
System.out.println(result);
assertEquals(expected, result);
}
}