/** * Creates a subsection wit the specified name in the section. * <p> * If a subsection with the specified name already exists, it will be returned. * </p> * @param name name of the new section. * @return the subsection with the specified name. */ public ConfigurationSection addSection(String name) { ConfigurationSection section; // The section already exists, returns it. if((section = getSection(name)) != null) return section; // Creates the new section. sections.put(name, section = new ConfigurationSection()); return section; }
/** * Tests the {@link ConfigurationSection#removeVariable(String)} method. * @param value value to which the variable should be set before being removed. */ @Test(dataProvider = "setVariable") public void testRemoveVariable(String value) { ConfigurationSection section; section = new ConfigurationSection(); assert section.setVariable("var", value); assertVariable(section, "var", value); assert value == null ? section.removeVariable("var") == null : section.removeVariable("var").equals(value); }
/** * Tests the {@link ConfigurationSection#setVariable(String, String)} method. * @param first first value to which the test variable should be set. * @param second second value to which the test variable should be set. * @param expected expected return value of the second call to * {@link ConfigurationSection#setVariable(String, String)}. */ @Test(dataProvider = "setVariable") public void testSetVariable(String first, String second, boolean expected) { ConfigurationSection section; section = new ConfigurationSection(); assert section.setVariable("var", first); assertVariable(section, "var", first); assert expected == section.setVariable("var", second); assertVariable(section, "var", second); }
@Test public void testSectionNames() { ConfigurationSection section; ConfigurationSection buffer; section = new ConfigurationSection(); // Create 10 variables. buffer = null; for(int i = 0; i < 10; i++) buffer = section.addSection("sect" + i); assertSectionNames(section, 10); // Removes the last section by value, makes sure we're in a coherent state. assert section.removeSection(buffer); assertSectionNames(section, 9); // Removes the last section by name, makes sure we're in a coherent state. assert section.removeSection("sect8") != null; assertSectionNames(section, 8); for(int i = 0; i < 8; i++) assert section.removeSection("sect" + i) != null; assertSectionNames(section, 0); } }
/** * Tests the {@link ConfigurationSection#variableNames()} method. */ @Test public void testVariableNames() { ConfigurationSection section; section = new ConfigurationSection(); // Create 10 variables. for(int i = 0; i < 10; i++) section.setVariable("var" + i, "value"); assertVariableNames(section, 10); section.setVariable("var9", ""); assertVariableNames(section, 9); section.setVariable("var8", null); assertVariableNames(section, 8); section.removeVariable("var7"); assertVariableNames(section, 7); for(int i = 0; i < 7; i++) section.removeVariable("var" + i); assertVariableNames(section, 0); }
/** * Tests various section related method. */ @Test public void testSections() { ConfigurationSection section; ConfigurationSection buffer; section = new ConfigurationSection(); // Makes sure we can add a section. buffer = section.addSection("sect"); assert buffer != null; assert section.hasSections(); // Makes sure that adding the same section twice yields the same instance. assert section.addSection("sect") == buffer; assert section.hasSections(); // Makes sure we can remove a section by name. assert section.removeSection("sect") == buffer; assert section.getSection("sect") == null; assert !section.hasSections(); // Makes sure we can remove a section by value. buffer = section.addSection("sect"); assert section.removeSection(buffer); assert buffer.getSection("sect") == null; assert !section.hasSections(); assert !section.removeSection(buffer); }