1 16 17 package org.apache.commons.configuration; 18 19 import java.io.IOException ; 20 import java.util.Arrays ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 import org.dom4j.Document; 25 import org.dom4j.Element; 26 import org.dom4j.io.SAXReader; 27 import org.xml.sax.SAXException ; 28 import org.xml.sax.helpers.DefaultHandler ; 29 30 import junit.framework.TestCase; 31 32 37 public class TestBaseConfigurationXMLReader extends TestCase 38 { 39 private static final String [] CONTINENTS = 40 { 41 "Africa", "America", "Asia", "Australia", "Europe" 42 }; 43 44 private BaseConfiguration config; 45 private BaseConfigurationXMLReader configReader; 46 47 protected void setUp() throws Exception 48 { 49 config = new BaseConfiguration(); 50 config.addProperty("world.continents.continent", Arrays.asList(CONTINENTS)); 51 config.addProperty("world.greeting", "Hello"); 52 config.addProperty("world.greeting", "Salute"); 53 config.addProperty("world.wish", "Peace"); 54 config.addProperty("application.mail.smtp", "smtp.mymail.org"); 55 config.addProperty("application.mail.pop", "pop3.mymail.org"); 56 config.addProperty("application.mail.account.type", "pop3"); 57 config.addProperty("application.mail.account.user", "postmaster"); 58 config.addProperty("application.mail.account.pwd", "?.-gulp*#"); 59 config.addProperty("application.mail.timeout", new Integer (42)); 60 config.addProperty("test", Boolean.TRUE); 61 62 configReader = new BaseConfigurationXMLReader(config); 63 } 64 65 public void testParse() throws Exception 66 { 67 checkDocument(configReader, "config"); 68 } 69 70 public void testParseSAXException() throws IOException 71 { 72 configReader.setContentHandler(new TestContentHandler()); 73 try 74 { 75 configReader.parse("systemID"); 76 fail("Expected exception was not thrown!"); 77 } 78 catch(SAXException ex) 79 { 80 } 81 } 82 83 public void testParseIOException() throws SAXException 84 { 85 BaseConfigurationXMLReader reader = new BaseConfigurationXMLReader(); 86 try 87 { 88 reader.parse("document"); 89 fail("Expected exception was not thrown!"); 90 } 91 catch(IOException ex) 92 { 93 } 94 } 95 96 public void testSetRootName() throws Exception 97 { 98 BaseConfigurationXMLReader reader = new BaseConfigurationXMLReader(config); 99 reader.setRootName("apache"); 100 checkDocument(reader, "apache"); 101 } 102 103 private void checkDocument(BaseConfigurationXMLReader creader, 104 String rootName) throws Exception 105 { 106 SAXReader reader = new SAXReader(creader); 107 Document document = reader.read("config"); 108 109 Element root = document.getRootElement(); 110 assertEquals(rootName, root.getName()); 111 assertEquals(3, root.elements().size()); 112 113 check(root, "world.continents.continent", CONTINENTS); 114 check(root, "world.greeting", new String [] { "Hello", "Salute" }); 115 check(root, "world.wish", "Peace"); 116 check(root, "application.mail.smtp", "smtp.mymail.org"); 117 check(root, "application.mail.timeout", "42"); 118 check(root, "application.mail.account.type", "pop3"); 119 check(root, "application.mail.account.user", "postmaster"); 120 check(root, "test", "true"); 121 } 122 123 129 private void check(Element root, String path, String [] values) 130 { 131 ConfigurationKey.KeyIterator keyIt = 132 new ConfigurationKey(path).iterator(); 133 Element e = root; 134 135 for(keyIt.nextKey(); keyIt.hasNext(); keyIt.nextKey()) 136 { 137 Element child = e.element(keyIt.currentKey()); 138 assertNotNull(child); 139 e = child; 140 } 141 142 List elems = e.elements(keyIt.currentKey()); 143 assertEquals(values.length, elems.size()); 144 Iterator it = elems.iterator(); 145 for(int i = 0; i < values.length; i++) 146 { 147 Element child = (Element) it.next(); 148 assertEquals(values[i], child.getTextTrim()); 149 } 150 } 151 152 private void check(Element root, String path, String value) 153 { 154 check(root, path, new String [] { value }); 155 } 156 157 private static class TestContentHandler extends DefaultHandler 159 { 160 public void characters(char[] ch, int start, int length) 161 throws SAXException 162 { 163 throw new SAXException ("Test exception during parsing"); 164 } 165 } 166 } 167 | Popular Tags |