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 static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
23
24 import java.io.IOException;
25 import java.util.Arrays;
26 import java.util.Iterator;
27
28 import javax.xml.transform.Transformer;
29 import javax.xml.transform.TransformerFactory;
30 import javax.xml.transform.dom.DOMResult;
31 import javax.xml.transform.sax.SAXSource;
32
33 import org.apache.commons.jxpath.JXPathContext;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.w3c.dom.Document;
37 import org.w3c.dom.Node;
38 import org.xml.sax.InputSource;
39 import org.xml.sax.SAXException;
40 import org.xml.sax.helpers.DefaultHandler;
41
42
43
44
45
46
47 public class TestBaseConfigurationXMLReader
48 {
49 private static final String[] CONTINENTS =
50 {
51 "Africa", "America", "Asia", "Australia", "Europe"
52 };
53
54 private BaseConfiguration config;
55 private BaseConfigurationXMLReader configReader;
56
57 @Before
58 public void setUp() throws Exception
59 {
60 config = new BaseConfiguration();
61 config.addProperty("world.continents.continent", Arrays.asList(CONTINENTS));
62 config.addProperty("world.greeting", "Hello");
63 config.addProperty("world.greeting", "Salute");
64 config.addProperty("world.wish", "Peace");
65 config.addProperty("application.mail.smtp", "smtp.mymail.org");
66 config.addProperty("application.mail.pop", "pop3.mymail.org");
67 config.addProperty("application.mail.account.type", "pop3");
68 config.addProperty("application.mail.account.user", "postmaster");
69 config.addProperty("application.mail.account.pwd", "?.-gulp*#");
70 config.addProperty("application.mail.timeout", new Integer(42));
71 config.addProperty("test", Boolean.TRUE);
72
73 configReader = new BaseConfigurationXMLReader(config);
74 }
75
76 @Test
77 public void testParse() throws Exception
78 {
79 checkDocument(configReader, "config");
80 }
81
82 @Test(expected = SAXException.class)
83 public void testParseSAXException() throws IOException, SAXException
84 {
85 configReader.setContentHandler(new TestContentHandler());
86 configReader.parse("systemID");
87 }
88
89 @Test(expected = IOException.class)
90 public void testParseIOException() throws SAXException, IOException
91 {
92 BaseConfigurationXMLReader reader = new BaseConfigurationXMLReader();
93 reader.parse("document");
94 }
95
96 @Test
97 public void testSetRootName() throws Exception
98 {
99 BaseConfigurationXMLReader reader = new BaseConfigurationXMLReader(config);
100 reader.setRootName("apache");
101 checkDocument(reader, "apache");
102 }
103
104 private void checkDocument(BaseConfigurationXMLReader creader,
105 String rootName) throws Exception
106 {
107 SAXSource source = new SAXSource(creader, new InputSource());
108 DOMResult result = new DOMResult();
109 Transformer trans = TransformerFactory.newInstance().newTransformer();
110 trans.transform(source, result);
111 Node root = ((Document) result.getNode()).getDocumentElement();
112 JXPathContext ctx = JXPathContext.newContext(root);
113
114 assertEquals("Wrong root name", rootName, root.getNodeName());
115 assertEquals("Wrong number of children", 3, ctx.selectNodes("/*").size());
116
117 check(ctx, "world/continents/continent", CONTINENTS);
118 check(ctx, "world/greeting", new String[] { "Hello", "Salute" });
119 check(ctx, "world/wish", "Peace");
120 check(ctx, "application/mail/smtp", "smtp.mymail.org");
121 check(ctx, "application/mail/timeout", "42");
122 check(ctx, "application/mail/account/type", "pop3");
123 check(ctx, "application/mail/account/user", "postmaster");
124 check(ctx, "test", "true");
125 }
126
127
128
129
130
131
132
133
134 private void check(JXPathContext ctx, String path, String[] values)
135 {
136 Iterator<?> it = ctx.iterate(path);
137 for (int i = 0; i < values.length; i++)
138 {
139 assertTrue("Too few values", it.hasNext());
140 assertEquals("Wrong property value", values[i], it.next());
141 }
142 assertFalse("Too many values", it.hasNext());
143 }
144
145 private void check(JXPathContext ctx, String path, String value)
146 {
147 check(ctx, path, new String[]
148 { value });
149 }
150
151
152 private static class TestContentHandler extends DefaultHandler
153 {
154 @Override
155 public void characters(char[] ch, int start, int length)
156 throws SAXException
157 {
158 throw new SAXException("Test exception during parsing");
159 }
160 }
161 }