View Javadoc
1   package org.codehaus.plexus.configuration;
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  
21  /**
22   * @author <a href="mailto:rantene@hotmail.com">Ran Tene</a>
23   */
24  public final class DefaultPlexusConfigurationTest extends TestCase {
25      private DefaultPlexusConfiguration configuration;
26  
27      public void setUp() {
28          configuration = new DefaultPlexusConfiguration("a");
29      }
30  
31      public void testWithHelper() throws Exception {
32          PlexusConfiguration c = ConfigurationTestHelper.getTestConfiguration();
33  
34          ConfigurationTestHelper.testConfiguration(c);
35      }
36  
37      public void testGetValue() throws Exception {
38          String orgValue = "Original String";
39          configuration.setValue(orgValue);
40          assertEquals(orgValue, configuration.getValue());
41      }
42  
43      public void testGetAttribute() throws Exception {
44          String key = "key";
45          String value = "original value";
46          String defaultStr = "default";
47          configuration.setAttribute(key, value);
48          assertEquals(value, configuration.getAttribute(key, defaultStr));
49          assertEquals(defaultStr, configuration.getAttribute("newKey", defaultStr));
50      }
51  
52      public void testGetChild() throws Exception {
53          DefaultPlexusConfiguration child = (DefaultPlexusConfiguration) configuration.getChild("child");
54  
55          assertNotNull(child);
56  
57          child.setValue("child value");
58  
59          assertEquals(1, configuration.getChildCount());
60  
61          child = (DefaultPlexusConfiguration) configuration.getChild("child");
62  
63          assertNotNull(child);
64  
65          assertEquals("child value", child.getValue());
66  
67          assertEquals(1, configuration.getChildCount());
68      }
69  
70      public void testToString() throws Exception {
71          // TODO: this currently works since getTestConfiguration() invokes PlexusTools.buildConfiguration()
72          // and it returns XmlPlexusConfiguration actually.
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(
81                  "<singleton attribute=\"attribute\"/>\n",
82                  c.getChild("singleton").toString());
83      }
84  
85      public void testProgrammaticConfigurationCreation() throws Exception {
86          String viewRoot = "/path/to/viewRoot";
87  
88          PlexusConfiguration c = new DefaultPlexusConfiguration("configuration").addChild("viewRoot", viewRoot);
89  
90          assertEquals(viewRoot, c.getChild("viewRoot").getValue());
91      }
92  
93      public void testChildOrdering() throws Exception {
94          PlexusConfiguration child0 = new DefaultPlexusConfiguration("child");
95          PlexusConfiguration child1 = new DefaultPlexusConfiguration("child");
96          PlexusConfiguration child2 = new DefaultPlexusConfiguration("special-child");
97          PlexusConfiguration child3 = new DefaultPlexusConfiguration("child");
98          PlexusConfiguration child4 = new DefaultPlexusConfiguration("child");
99  
100         configuration.addChild(child0);
101         configuration.addChild(child1);
102         configuration.addChild(child2);
103         configuration.addChild(child3);
104         configuration.addChild(child4);
105 
106         assertEquals(5, configuration.getChildCount());
107         assertSame(child0, configuration.getChild(0));
108         assertSame(child1, configuration.getChild(1));
109         assertSame(child2, configuration.getChild(2));
110         assertSame(child3, configuration.getChild(3));
111         assertSame(child4, configuration.getChild(4));
112     }
113 }