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   * @version $Id$
26   */
27  public final class XmlPlexusConfigurationTest
28      extends TestCase
29  {
30      private XmlPlexusConfiguration configuration;
31  
32      public void setUp()
33      {
34          configuration = new XmlPlexusConfiguration( "a" );
35      }
36  
37      public void testWithHelper()
38          throws Exception
39      {
40          PlexusConfiguration c = ConfigurationTestHelper.getTestConfiguration();
41  
42          ConfigurationTestHelper.testConfiguration( c );
43      }
44  
45      public void testGetValue()
46          throws Exception
47      {
48          String orgValue = "Original String";
49          configuration.setValue( orgValue );
50          assertEquals( orgValue, configuration.getValue() );
51      }
52  
53      public void testGetAttribute()
54          throws Exception
55      {
56          String key = "key";
57          String value = "original value";
58          String defaultStr = "default";
59          configuration.setAttribute( key, value );
60          assertEquals( value, configuration.getAttribute( key, defaultStr ) );
61          assertEquals( defaultStr, configuration.getAttribute( "newKey", defaultStr ) );
62      }
63  
64      public void testGetChild()
65          throws Exception
66      {
67          PlexusConfiguration child = (XmlPlexusConfiguration) configuration.getChild( "child" );
68  
69          assertNotNull( child );
70  
71          child.setValue( "child value" );
72  
73          assertEquals( 1, configuration.getChildCount() );
74  
75          child = (XmlPlexusConfiguration) configuration.getChild( "child" );
76  
77          assertNotNull( child );
78  
79          assertEquals( "child value", child.getValue() );
80  
81          assertEquals( 1, configuration.getChildCount() );
82      }
83  
84      public void testToString()
85         throws Exception
86      {
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 XmlPlexusConfiguration( "configuration" )
101             .addChild( "viewRoot", viewRoot );
102 
103         assertEquals( viewRoot, c.getChild( "viewRoot" ).getValue() );
104     }
105 }
106