/** * Creates a new sub-section to the current section. * @param name name of the new section. */ public void startSection(String name) throws ConfigurationException { ConfigurationSection buffer; buffer = currentSection.addSection(name); sections.push(currentSection); if(sectionNames.empty()) sectionNames.push(name + '.'); else sectionNames.push(sectionNames.peek() + name + '.'); currentSection = buffer; }
/** * Move to the specified section. * @param name name of the current section's subsection in which to move. * @param create if <code>true</code> and <code>name</code> doesn't exist, it will be created. * @return <code>true</code> if we could move to <code>name</code>, <code>false</code> otherwise. */ public boolean moveTo(String name, boolean create) { ConfigurationSection buffer; // Buffer for the subsection. // Checks whether the requested subsection exists. if((buffer = section.getSection(name)) == null) { // If it doesn't exist, either return false or create it depending on // parameters. if(create) { section = section.addSection(name); return true; } return false; } section = buffer; return true; } }
/** * 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); }
@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); } }