1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.configuration;
19
20 import java.io.IOException;
21 import java.util.Arrays;
22 import java.util.Iterator;
23
24 import javax.xml.transform.Transformer;
25 import javax.xml.transform.TransformerFactory;
26 import javax.xml.transform.dom.DOMResult;
27 import javax.xml.transform.sax.SAXSource;
28
29 import org.apache.commons.jxpath.JXPathContext;
30 import org.w3c.dom.Document;
31 import org.w3c.dom.Node;
32 import org.xml.sax.InputSource;
33 import org.xml.sax.SAXException;
34 import org.xml.sax.helpers.DefaultHandler;
35
36 import junit.framework.TestCase;
37
38 /***
39 * Test class for BaseConfigurationXMLReader.
40 *
41 * @version $Id: TestBaseConfigurationXMLReader.java 439648 2006-09-02 20:42:10Z oheger $
42 */
43 public class TestBaseConfigurationXMLReader extends TestCase
44 {
45 private static final String[] CONTINENTS =
46 {
47 "Africa", "America", "Asia", "Australia", "Europe"
48 };
49
50 private BaseConfiguration config;
51 private BaseConfigurationXMLReader configReader;
52
53 protected void setUp() throws Exception
54 {
55 config = new BaseConfiguration();
56 config.addProperty("world.continents.continent", Arrays.asList(CONTINENTS));
57 config.addProperty("world.greeting", "Hello");
58 config.addProperty("world.greeting", "Salute");
59 config.addProperty("world.wish", "Peace");
60 config.addProperty("application.mail.smtp", "smtp.mymail.org");
61 config.addProperty("application.mail.pop", "pop3.mymail.org");
62 config.addProperty("application.mail.account.type", "pop3");
63 config.addProperty("application.mail.account.user", "postmaster");
64 config.addProperty("application.mail.account.pwd", "?.-gulp*#");
65 config.addProperty("application.mail.timeout", new Integer(42));
66 config.addProperty("test", Boolean.TRUE);
67
68 configReader = new BaseConfigurationXMLReader(config);
69 }
70
71 public void testParse() throws Exception
72 {
73 checkDocument(configReader, "config");
74 }
75
76 public void testParseSAXException() throws IOException
77 {
78 configReader.setContentHandler(new TestContentHandler());
79 try
80 {
81 configReader.parse("systemID");
82 fail("Expected exception was not thrown!");
83 }
84 catch(SAXException ex)
85 {
86 }
87 }
88
89 public void testParseIOException() throws SAXException
90 {
91 BaseConfigurationXMLReader reader = new BaseConfigurationXMLReader();
92 try
93 {
94 reader.parse("document");
95 fail("Expected exception was not thrown!");
96 }
97 catch(IOException ex)
98 {
99 }
100 }
101
102 public void testSetRootName() throws Exception
103 {
104 BaseConfigurationXMLReader reader = new BaseConfigurationXMLReader(config);
105 reader.setRootName("apache");
106 checkDocument(reader, "apache");
107 }
108
109 private void checkDocument(BaseConfigurationXMLReader creader,
110 String rootName) throws Exception
111 {
112 SAXSource source = new SAXSource(creader, new InputSource());
113 DOMResult result = new DOMResult();
114 Transformer trans = TransformerFactory.newInstance().newTransformer();
115 try
116 {
117
118
119 trans.transform(source, result);
120 }
121 catch(NoSuchMethodError ex)
122 {
123 return;
124 }
125 Node root = ((Document) result.getNode()).getDocumentElement();
126 JXPathContext ctx = JXPathContext.newContext(root);
127
128 assertEquals("Wrong root name", rootName, root.getNodeName());
129 assertEquals("Wrong number of children", 3, ctx.selectNodes("/*").size());
130
131 check(ctx, "world/continents/continent", CONTINENTS);
132 check(ctx, "world/greeting", new String[] { "Hello", "Salute" });
133 check(ctx, "world/wish", "Peace");
134 check(ctx, "application/mail/smtp", "smtp.mymail.org");
135 check(ctx, "application/mail/timeout", "42");
136 check(ctx, "application/mail/account/type", "pop3");
137 check(ctx, "application/mail/account/user", "postmaster");
138 check(ctx, "test", "true");
139 }
140
141 /***
142 * Helper method for checking values in the created document.
143 *
144 * @param ctx the JXPath context
145 * @param path the path to be checked
146 * @param values the expected element values
147 */
148 private void check(JXPathContext ctx, String path, String[] values)
149 {
150 Iterator it = ctx.iterate(path);
151 for (int i = 0; i < values.length; i++)
152 {
153 assertTrue("Too few values", it.hasNext());
154 assertEquals("Wrong property value", values[i], it.next());
155 }
156 assertFalse("Too many values", it.hasNext());
157 }
158
159 private void check(JXPathContext ctx, String path, String value)
160 {
161 check(ctx, path, new String[]
162 { value });
163 }
164
165
166 private static class TestContentHandler extends DefaultHandler
167 {
168 public void characters(char[] ch, int start, int length)
169 throws SAXException
170 {
171 throw new SAXException("Test exception during parsing");
172 }
173 }
174 }