1   package org.apache.commons.configuration;
2   
3   /*
4    * Copyright 2001-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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         // Number of keys expected from test configuration file
117         final int KEY_COUNT = 5;
118 
119         // Load the configuration file
120         config.load(new File(TEST_FILE2).getAbsoluteFile().toURL());
121 
122         // Validate comment in element ignored
123         assertEquals("Comment in element must not change element value.", "Case1Text", config.getString("case1"));
124 
125         // Validate sibling comment ignored
126         assertEquals("Comment as sibling must not change element value.", "Case2Text", config.getString("case2.child"));
127 
128         // Validate comment ignored, CDATA processed
129         assertEquals("Comment and use of CDATA must not change element value.", "Case3Text", config.getString("case3"));
130 
131         // Validate comment and processing instruction ignored
132         assertEquals("Comment and use of PI must not change element value.", "Case4Text", config.getString("case4"));
133 
134         // Validate comment ignored in parent attribute
135         assertEquals("Comment must not change attribute node value.", "Case5Text", config.getString("case5[@attr]"));
136 
137         // Validate non-text nodes haven't snuck in as keys
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 }