public class ConfModelTest extends TestCase {
public void testConfig1() throws Exception {
String configPath = "config1.xml";
InputStream configStream = getClass().getResourceAsStream(configPath);
Conf config = XmlConfBuilder.build(configStream, configPath);
assertEquals("value1", config.getAttribute("att1"));
assertEquals("value2", config.getAttribute("att2"));
assertEquals(3, config.getChildren().size());
assertEquals(2, config.getChildren("element").size());
for (Conf childConf : config.getChildren("element")) {
assertEquals("element", childConf.getName());
assertEquals("abc", childConf.getValue());
}
assertEquals(2, config.getChild("parent").getChildren("child").size());
for (Conf childConf : config.getChild("parent").getChildren("child")) {
assertEquals("def", childConf.getValue());
}
assertEquals(0, config.getChild("parent").getChild("child").getChildren().size());
assertNotNull(config.getChild("nonexistingchild"));
assertNull(config.getChild("nonexistingchild", false));
// Getting the value of a node without a value should throw an error
try {
config.getChild("nonexistingchild").getValue();
fail("expected exception");
} catch (ConfException e) { /* ignore */ }
assertEquals("zit", config.getChild("nonexistingchild").getValue("zit"));
}