1 package org.apache.commons.configuration;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import junit.framework.TestCase;
20
21 import java.io.File;
22 import java.util.Collection;
23 import java.util.Iterator;
24
25 /***
26 * Test class for HierarchicalXMLConfiguration,
27 *
28 * @author Emmanuel Bourg
29 * @author Mark Woodman
30 * @version $Id: TestHierarchicalXMLConfiguration.java,v 1.2 2004/09/06 13:12:04 epugh Exp $
31 */
32 public class TestHierarchicalXMLConfiguration extends TestCase
33 {
34 /*** Test resources directory. */
35 private static final String TEST_DIR = "conf";
36
37 /*** Test file #1 **/
38 private static final String TEST_FILENAME = "testHierarchicalXMLConfiguration.xml";
39
40 /*** Test file #2 **/
41 private static final String TEST_FILENAME2 = "testHierarchicalXMLConfiguration2.xml";
42
43 /*** Test file path #1 **/
44 private static final String TEST_FILE = TEST_DIR + File.separator + TEST_FILENAME;
45
46 /*** Test file path #2 **/
47 private static final String TEST_FILE2 = TEST_DIR + File.separator + TEST_FILENAME2;
48
49 /*** Instance config used for tests. */
50 private HierarchicalXMLConfiguration config;
51
52 /*** Fixture setup. */
53 protected void setUp() throws Exception
54 {
55 config = new HierarchicalXMLConfiguration();
56 }
57
58 private void configTest(HierarchicalXMLConfiguration config)
59 {
60 assertEquals(1, config.getMaxIndex("tables.table"));
61 assertEquals("system", config.getProperty("tables.table(0)[@tableType]"));
62 assertEquals("application", config.getProperty("tables.table(1)[@tableType]"));
63
64 assertEquals("users", config.getProperty("tables.table(0).name"));
65 assertEquals("documents", config.getProperty("tables.table(1).name"));
66
67 Object prop = config.getProperty("tables.table.fields.field.name");
68 assertTrue(prop instanceof Collection);
69 assertEquals(10, ((Collection) prop).size());
70
71 prop = config.getProperty("tables.table(0).fields.field.type");
72 assertTrue(prop instanceof Collection);
73 assertEquals(5, ((Collection) prop).size());
74
75 prop = config.getProperty("tables.table(1).fields.field.type");
76 assertTrue(prop instanceof Collection);
77 assertEquals(5, ((Collection) prop).size());
78 }
79
80 public void testGetProperty() throws Exception
81 {
82 config.setFileName(TEST_FILE);
83 config.load();
84
85 configTest(config);
86 }
87
88 public void testLoadURL() throws Exception
89 {
90 config.load(new File(TEST_FILE).getAbsoluteFile().toURL());
91 configTest(config);
92 }
93
94 public void testLoadBasePath1() throws Exception
95 {
96 config.setBasePath(TEST_DIR);
97 config.setFileName(TEST_FILENAME);
98 config.load();
99 configTest(config);
100 }
101
102 public void testLoadBasePath2() throws Exception
103 {
104 config.setBasePath(new File(TEST_FILE).getAbsoluteFile().toURL().toString());
105 config.setFileName(TEST_FILENAME);
106 config.load();
107 configTest(config);
108 }
109
110 /***
111 * Ensure various node types are correctly processed in config.
112 * @throws Exception
113 */
114 public void testXmlNodeTypes() throws Exception
115 {
116
117 final int KEY_COUNT = 5;
118
119
120 config.load(new File(TEST_FILE2).getAbsoluteFile().toURL());
121
122
123 assertEquals("Comment in element must not change element value.", "Case1Text", config.getString("case1"));
124
125
126 assertEquals("Comment as sibling must not change element value.", "Case2Text", config.getString("case2.child"));
127
128
129 assertEquals("Comment and use of CDATA must not change element value.", "Case3Text", config.getString("case3"));
130
131
132 assertEquals("Comment and use of PI must not change element value.", "Case4Text", config.getString("case4"));
133
134
135 assertEquals("Comment must not change attribute node value.", "Case5Text", config.getString("case5[@attr]"));
136
137
138 Iterator iter = config.getKeys();
139 int count = 0;
140 while (iter.hasNext())
141 {
142 iter.next();
143 count++;
144 }
145 assertEquals("Config must contain only " + KEY_COUNT + " keys.", KEY_COUNT, count);
146 }
147
148 }