Search in sources :

Example 6 with ConfigurationNode

use of com.bergerkiller.bukkit.common.config.ConfigurationNode in project BKCommonLib by bergerhealer.

the class ConfigTest method testYAMLLoadSave.

@Test
public void testYAMLLoadSave() {
    // Don't run <= 1.8.8
    if (Common.evaluateMCVersion("<=", "1.8.8")) {
        return;
    }
    FileConfiguration config = new FileConfiguration("test.yml");
    ItemStack testItem = new ItemStack(Material.DIAMOND_SWORD, 1, (short) 100);
    config.load();
    config.set("hello", "world");
    config.set("number", 12);
    config.set("anEnum", GameMode.CREATIVE);
    config.set("item", testItem.clone());
    assertEquals("world", config.get("hello", String.class));
    assertEquals(Integer.valueOf(12), config.get("number", Integer.class));
    assertEquals(GameMode.CREATIVE, config.get("anEnum", GameMode.class));
    assertEquals(testItem, config.get("item", ItemStack.class));
    // Add node by path directly
    ConfigurationNode node = config.getNode("node");
    node.set("key", "value");
    assertEquals("value", node.get("key", String.class));
    assertEquals("value", config.get("node.key", String.class));
    // Create a new node and then set it
    ConfigurationNode lazyNode = new ConfigurationNode();
    lazyNode.set("key", "value");
    assertEquals("value", lazyNode.get("key", String.class));
    config.set("lazyNode", lazyNode);
    assertEquals("value", config.get("lazyNode.key", String.class));
    assertEquals("value", config.getNode("lazyNode").get("key", String.class));
    // Create a list of nodes
    List<ConfigurationNode> nodeList = new ArrayList<ConfigurationNode>();
    nodeList.add(new ConfigurationNode());
    nodeList.add(new ConfigurationNode());
    nodeList.get(0).set("node1Key", "value1");
    nodeList.get(1).set("node2Key", "value2");
    nodeList.get(1).getNode("subNode").set("subNodeKey", "value3");
    config.setNodeList("nodeList", nodeList);
    // Verify (2 nodes)
    checkNodeListA(config);
    // Getting the list again and altering it by inserting an entry in the middle
    nodeList = config.getNodeList("nodeList");
    nodeList.add(1, new ConfigurationNode());
    nodeList.get(1).set("node3Key", "value4");
    config.setNodeList("nodeList", nodeList);
    // Verify again (3 nodes)
    checkNodeListB(config);
    config.save();
    config = new FileConfiguration("test.yml");
    config.load();
    assertEquals("world", config.get("hello", String.class));
    assertEquals(Integer.valueOf(12), config.get("number", Integer.class));
    assertEquals(GameMode.CREATIVE, config.get("anEnum", GameMode.class));
    assertEquals("value", config.get("node.key", String.class));
    assertEquals("value", config.getNode("node").get("key", String.class));
    assertEquals("value", config.get("lazyNode.key", String.class));
    assertEquals("value", config.getNode("lazyNode").get("key", String.class));
    assertEquals(testItem, config.get("item", ItemStack.class));
    // Verify
    checkNodeListB(config);
    // Cleanup
    new File("test.yml").delete();
}
Also used : FileConfiguration(com.bergerkiller.bukkit.common.config.FileConfiguration) GameMode(org.bukkit.GameMode) ConfigurationNode(com.bergerkiller.bukkit.common.config.ConfigurationNode) ArrayList(java.util.ArrayList) ItemStack(org.bukkit.inventory.ItemStack) File(java.io.File) Test(org.junit.Test)

Aggregations

ConfigurationNode (com.bergerkiller.bukkit.common.config.ConfigurationNode)6 FileConfiguration (com.bergerkiller.bukkit.common.config.FileConfiguration)3 ItemStack (org.bukkit.inventory.ItemStack)2 Permission (org.bukkit.permissions.Permission)2 Test (org.junit.Test)2 CommonPlugin (com.bergerkiller.bukkit.common.internal.CommonPlugin)1 Metrics (com.bergerkiller.bukkit.common.metrics.Metrics)1 IPermissionDefault (com.bergerkiller.bukkit.common.permissions.IPermissionDefault)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 GameMode (org.bukkit.GameMode)1 Material (org.bukkit.Material)1 PermissionDefault (org.bukkit.permissions.PermissionDefault)1 Plugin (org.bukkit.plugin.Plugin)1 JavaPlugin (org.bukkit.plugin.java.JavaPlugin)1 Ignore (org.junit.Ignore)1