View Javadoc
1   package org.codehaus.plexus.configuration.xml;
2   
3   /*
4    * Copyright 2001-2006 Codehaus 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  import org.codehaus.plexus.configuration.ConfigurationTestHelper;
21  import org.codehaus.plexus.configuration.PlexusConfiguration;
22  
23  /**
24   * @author <a href="mailto:rantene@hotmail.com">Ran Tene</a>
25   */
26  public final class XmlPlexusConfigurationTest extends TestCase {
27      private XmlPlexusConfiguration configuration;
28  
29      public void setUp() {
30          configuration = new XmlPlexusConfiguration("a");
31      }
32  
33      public void testWithHelper() throws Exception {
34          PlexusConfiguration c = ConfigurationTestHelper.getTestConfiguration();
35  
36          ConfigurationTestHelper.testConfiguration(c);
37      }
38  
39      public void testGetValue() throws Exception {
40          String orgValue = "Original String";
41          configuration.setValue(orgValue);
42          assertEquals(orgValue, configuration.getValue());
43      }
44  
45      public void testGetAttribute() throws Exception {
46          String key = "key";
47          String value = "original value";
48          String defaultStr = "default";
49          configuration.setAttribute(key, value);
50          assertEquals(value, configuration.getAttribute(key, defaultStr));
51          assertEquals(defaultStr, configuration.getAttribute("newKey", defaultStr));
52      }
53  
54      public void testGetChild() throws Exception {
55          PlexusConfiguration child = (XmlPlexusConfiguration) configuration.getChild("child");
56  
57          assertNotNull(child);
58  
59          child.setValue("child value");
60  
61          assertEquals(1, configuration.getChildCount());
62  
63          child = (XmlPlexusConfiguration) configuration.getChild("child");
64  
65          assertNotNull(child);
66  
67          assertEquals("child value", child.getValue());
68  
69          assertEquals(1, configuration.getChildCount());
70      }
71  
72      public void testToString() throws Exception {
73          PlexusConfiguration c = ConfigurationTestHelper.getTestConfiguration();
74  
75          assertEquals(
76                  "<string string=\"string\">string</string>\n",
77                  c.getChild("string").toString());
78  
79          // TODO: uncomment once maven can test the latest plexus-utils
80          //        assertEquals( "<singleton attribute=\"attribute\"/>\n", c.getChild( "singleton" ).toString() );
81      }
82  
83      public void testProgrammaticConfigurationCreation() throws Exception {
84          String viewRoot = "/path/to/viewRoot";
85  
86          PlexusConfiguration c = new XmlPlexusConfiguration("configuration").addChild("viewRoot", viewRoot);
87  
88          assertEquals(viewRoot, c.getChild("viewRoot").getValue());
89      }
90  }