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   * @version $Id$
24   */
25  public final class DefaultPlexusConfigurationTest
26      extends TestCase
27  {
28      private DefaultPlexusConfiguration configuration;
29  
30      public void setUp()
31      {
32          configuration = new DefaultPlexusConfiguration( "a" );
33      }
34  
35      public void testWithHelper()
36          throws Exception
37      {
38          PlexusConfiguration c = ConfigurationTestHelper.getTestConfiguration();
39  
40          ConfigurationTestHelper.testConfiguration( c );
41      }
42  
43      public void testGetValue()
44          throws Exception
45      {
46          String orgValue = "Original String";
47          configuration.setValue( orgValue );
48          assertEquals( orgValue, configuration.getValue() );
49      }
50  
51      public void testGetAttribute()
52          throws Exception
53      {
54          String key = "key";
55          String value = "original value";
56          String defaultStr = "default";
57          configuration.setAttribute( key, value );
58          assertEquals( value, configuration.getAttribute( key, defaultStr ) );
59          assertEquals( defaultStr, configuration.getAttribute( "newKey", defaultStr ) );
60      }
61  
62      public void testGetChild()
63          throws Exception
64      {
65          DefaultPlexusConfiguration child = (DefaultPlexusConfiguration) configuration.getChild( "child" );
66  
67          assertNotNull( child );
68  
69          child.setValue( "child value" );
70  
71          assertEquals( 1, configuration.getChildCount() );
72  
73          child = (DefaultPlexusConfiguration) configuration.getChild( "child" );
74  
75          assertNotNull( child );
76  
77          assertEquals( "child value", child.getValue() );
78  
79          assertEquals( 1, configuration.getChildCount() );
80      }
81  
82      public void testToString()
83          throws Exception
84      {
85          // TODO: this currently works since getTestConfiguration() invokes PlexusTools.buildConfiguration()
86          // and it returns XmlPlexusConfiguration actually.
87          PlexusConfiguration c = ConfigurationTestHelper.getTestConfiguration();
88  
89          assertEquals( "<string string=\"string\">string</string>\n", c.getChild( "string" ).toString() );
90  
91          // TODO: uncomment once maven can test the latest plexus-utils
92          assertEquals( "<singleton attribute=\"attribute\"/>\n", c.getChild( "singleton" ).toString() );
93      }
94  
95      public void testProgrammaticConfigurationCreation()
96          throws Exception
97      {
98          String viewRoot = "/path/to/viewRoot";
99  
100         PlexusConfiguration c = new DefaultPlexusConfiguration( "configuration" ).addChild( "viewRoot", viewRoot );
101 
102         assertEquals( viewRoot, c.getChild( "viewRoot" ).getValue() );
103     }
104 
105     public void testChildOrdering()
106         throws Exception
107     {
108         PlexusConfiguration child0 = new DefaultPlexusConfiguration( "child" );
109         PlexusConfiguration child1 = new DefaultPlexusConfiguration( "child" );
110         PlexusConfiguration child2 = new DefaultPlexusConfiguration( "special-child" );
111         PlexusConfiguration child3 = new DefaultPlexusConfiguration( "child" );
112         PlexusConfiguration child4 = new DefaultPlexusConfiguration( "child" );
113 
114         configuration.addChild( child0 );
115         configuration.addChild( child1 );
116         configuration.addChild( child2 );
117         configuration.addChild( child3 );
118         configuration.addChild( child4 );
119 
120         assertEquals( 5, configuration.getChildCount() );
121         assertSame( child0, configuration.getChild( 0 ) );
122         assertSame( child1, configuration.getChild( 1 ) );
123         assertSame( child2, configuration.getChild( 2 ) );
124         assertSame( child3, configuration.getChild( 3 ) );
125         assertSame( child4, configuration.getChild( 4 ) );
126     }
127 
128 }