View Javadoc
1   package org.codehaus.plexus.util.xml;
2   
3   /*
4    * Copyright The 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 org.codehaus.plexus.util.xml.pull.XmlPullParserException;
20  
21  import java.io.IOException;
22  import java.io.StringReader;
23  import java.util.HashMap;
24  
25  import junit.framework.TestCase;
26  
27  public class Xpp3DomTest
28      extends TestCase
29  {
30  
31      public void testShouldPerformAppendAtFirstSubElementLevel()
32      {
33          // create the dominant DOM
34          Xpp3Dom t1 = new Xpp3Dom( "top" );
35          t1.setAttribute( Xpp3Dom.CHILDREN_COMBINATION_MODE_ATTRIBUTE, Xpp3Dom.CHILDREN_COMBINATION_APPEND );
36  
37          Xpp3Dom t1s1 = new Xpp3Dom( "topsub1" );
38          t1s1.setValue( "t1s1Value" );
39  
40          t1.addChild( t1s1 );
41  
42          // create the recessive DOM
43          Xpp3Dom t2 = new Xpp3Dom( "top" );
44  
45          Xpp3Dom t2s1 = new Xpp3Dom( "topsub1" );
46          t2s1.setValue( "t2s1Value" );
47  
48          t2.addChild( t2s1 );
49  
50          // merge and check results.
51          Xpp3Dom result = Xpp3Dom.mergeXpp3Dom( t1, t2 );
52  
53          assertEquals( 2, result.getChildren( "topsub1" ).length );
54      }
55  
56      public void testShouldOverrideAppendAndDeepMerge()
57      {
58          // create the dominant DOM
59          Xpp3Dom t1 = new Xpp3Dom( "top" );
60          t1.setAttribute( Xpp3Dom.CHILDREN_COMBINATION_MODE_ATTRIBUTE, Xpp3Dom.CHILDREN_COMBINATION_APPEND );
61  
62          Xpp3Dom t1s1 = new Xpp3Dom( "topsub1" );
63          t1s1.setValue( "t1s1Value" );
64  
65          t1.addChild( t1s1 );
66  
67          // create the recessive DOM
68          Xpp3Dom t2 = new Xpp3Dom( "top" );
69  
70          Xpp3Dom t2s1 = new Xpp3Dom( "topsub1" );
71          t2s1.setValue( "t2s1Value" );
72  
73          t2.addChild( t2s1 );
74  
75          // merge and check results.
76          Xpp3Dom result = Xpp3Dom.mergeXpp3Dom( t1, t2, Boolean.TRUE );
77  
78          assertEquals( 1, result.getChildren( "topsub1" ).length );
79      }
80  
81      public void testShouldPerformSelfOverrideAtTopLevel()
82      {
83          // create the dominant DOM
84          Xpp3Dom t1 = new Xpp3Dom( "top" );
85          t1.setAttribute( "attr", "value" );
86  
87          t1.setAttribute( Xpp3Dom.SELF_COMBINATION_MODE_ATTRIBUTE, Xpp3Dom.SELF_COMBINATION_OVERRIDE );
88  
89          // create the recessive DOM
90          Xpp3Dom t2 = new Xpp3Dom( "top" );
91          t2.setAttribute( "attr2", "value2" );
92          t2.setValue( "t2Value" );
93  
94          // merge and check results.
95          Xpp3Dom result = Xpp3Dom.mergeXpp3Dom( t1, t2 );
96  
97          assertEquals( 2, result.getAttributeNames().length );
98          assertNull( result.getValue() );
99      }
100 
101     public void testShouldMergeValuesAtTopLevelByDefault()
102     {
103         // create the dominant DOM
104         Xpp3Dom t1 = new Xpp3Dom( "top" );
105         t1.setAttribute( "attr", "value" );
106 
107         // create the recessive DOM
108         Xpp3Dom t2 = new Xpp3Dom( "top" );
109         t2.setAttribute( "attr2", "value2" );
110         t2.setValue( "t2Value" );
111 
112         // merge and check results.
113         Xpp3Dom result = Xpp3Dom.mergeXpp3Dom( t1, t2 );
114 
115         // this is still 2, since we're not using the merge-control attribute.
116         assertEquals( 2, result.getAttributeNames().length );
117 
118         assertEquals( result.getValue(), t2.getValue() );
119     }
120 
121     public void testShouldMergeValuesAtTopLevel()
122     {
123         // create the dominant DOM
124         Xpp3Dom t1 = new Xpp3Dom( "top" );
125         t1.setAttribute( "attr", "value" );
126 
127         t1.setAttribute( Xpp3Dom.SELF_COMBINATION_MODE_ATTRIBUTE, Xpp3Dom.SELF_COMBINATION_MERGE );
128 
129         // create the recessive DOM
130         Xpp3Dom t2 = new Xpp3Dom( "top" );
131         t2.setAttribute( "attr2", "value2" );
132         t2.setValue( "t2Value" );
133 
134         // merge and check results.
135         Xpp3Dom result = Xpp3Dom.mergeXpp3Dom( t1, t2 );
136 
137         assertEquals( 3, result.getAttributeNames().length );
138         assertEquals( result.getValue(), t2.getValue() );
139     }
140 
141     public void testNullAttributeNameOrValue()
142     {
143         Xpp3Dom t1 = new Xpp3Dom( "top" );
144         try
145         {
146             t1.setAttribute( "attr", null );
147             fail( "null attribute values shouldn't be allowed" );
148         }
149         catch ( NullPointerException e )
150         {
151         }
152         t1.toString();
153         try
154         {
155             t1.setAttribute( null, "value" );
156             fail( "null attribute names shouldn't be allowed" );
157         }
158         catch ( NullPointerException e )
159         {
160         }
161         t1.toString();
162     }
163 
164     public void testEquals()
165     {
166         Xpp3Dom dom = new Xpp3Dom( "top" );
167 
168         assertEquals( dom, dom );
169         assertFalse( dom.equals( null ) );
170         assertFalse( dom.equals( new Xpp3Dom( (String) null ) ) );
171     }
172 
173     public void testEqualsIsNullSafe()
174         throws XmlPullParserException, IOException
175     {
176         String testDom = "<configuration><items thing='blah'><item>one</item><item>two</item></items></configuration>";
177         Xpp3Dom dom = Xpp3DomBuilder.build( new StringReader( testDom ) );
178         Xpp3Dom dom2 = Xpp3DomBuilder.build( new StringReader( testDom ) );
179 
180         try
181         {
182             dom2.attributes = new HashMap();
183             dom2.attributes.put( "nullValue", null );
184             dom2.attributes.put( null, "nullKey" );
185             dom2.childList.clear();
186             dom2.childList.add( null );
187 
188             assertFalse( dom.equals( dom2 ) );
189             assertFalse( dom2.equals( dom ) );
190 
191         }
192         catch ( NullPointerException ex )
193         {
194             ex.printStackTrace();
195             fail( "\nNullPointerExceptions should not be thrown." );
196         }
197     }
198 
199     public void testShouldOverwritePluginConfigurationSubItemsByDefault()
200         throws XmlPullParserException, IOException
201     {
202         String parentConfigStr = "<configuration><items><item>one</item><item>two</item></items></configuration>";
203         Xpp3Dom parentConfig = Xpp3DomBuilder.build( new StringReader( parentConfigStr ) );
204 
205         String childConfigStr = "<configuration><items><item>three</item></items></configuration>";
206         Xpp3Dom childConfig = Xpp3DomBuilder.build( new StringReader( childConfigStr ) );
207 
208         Xpp3Dom result = Xpp3Dom.mergeXpp3Dom( childConfig, parentConfig );
209         Xpp3Dom items = result.getChild( "items" );
210 
211         assertEquals( 1, items.getChildCount() );
212 
213         Xpp3Dom item = items.getChild( 0 );
214         assertEquals( "three", item.getValue() );
215     }
216 
217     public void testShouldMergePluginConfigurationSubItemsWithMergeAttributeSet()
218         throws XmlPullParserException, IOException
219     {
220         String parentConfigStr = "<configuration><items><item>one</item><item>two</item></items></configuration>";
221         Xpp3Dom parentConfig = Xpp3DomBuilder.build( new StringReader( parentConfigStr ) );
222 
223         String childConfigStr =
224             "<configuration><items combine.children=\"append\"><item>three</item></items></configuration>";
225         Xpp3Dom childConfig = Xpp3DomBuilder.build( new StringReader( childConfigStr ) );
226 
227         Xpp3Dom result = Xpp3Dom.mergeXpp3Dom( childConfig, parentConfig );
228         Xpp3Dom items = result.getChild( "items" );
229 
230         assertEquals( 3, items.getChildCount() );
231 
232         Xpp3Dom[] item = items.getChildren();
233 
234         assertEquals( "one", item[0].getValue() );
235         assertEquals( "two", item[1].getValue() );
236         assertEquals( "three", item[2].getValue() );
237     }
238 
239     public void testShouldNotChangeUponMergeWithItselfWhenFirstOrLastSubItemIsEmpty()
240         throws Exception
241     {
242         String configStr = "<configuration><items><item/><item>test</item><item/></items></configuration>";
243         Xpp3Dom dominantConfig = Xpp3DomBuilder.build( new StringReader( configStr ) );
244         Xpp3Dom recessiveConfig = Xpp3DomBuilder.build( new StringReader( configStr ) );
245 
246         Xpp3Dom result = Xpp3Dom.mergeXpp3Dom( dominantConfig, recessiveConfig );
247         Xpp3Dom items = result.getChild( "items" );
248 
249         assertEquals( 3, items.getChildCount() );
250 
251         assertEquals( null, items.getChild( 0 ).getValue() );
252         assertEquals( "test", items.getChild( 1 ).getValue() );
253         assertEquals( null, items.getChild( 2 ).getValue() );
254     }
255 
256     public void testShouldCopyRecessiveChildrenNotPresentInTarget()
257         throws Exception
258     {
259         String dominantStr = "<configuration><foo>x</foo></configuration>";
260         String recessiveStr = "<configuration><bar>y</bar></configuration>";
261         Xpp3Dom dominantConfig = Xpp3DomBuilder.build( new StringReader( dominantStr ) );
262         Xpp3Dom recessiveConfig = Xpp3DomBuilder.build( new StringReader( recessiveStr ) );
263 
264         Xpp3Dom result = Xpp3Dom.mergeXpp3Dom( dominantConfig, recessiveConfig );
265 
266         assertEquals( 2, result.getChildCount() );
267 
268         assertEquals( "x", result.getChild( "foo" ).getValue() );
269         assertEquals( "y", result.getChild( "bar" ).getValue() );
270         assertNotSame( result.getChild( "bar" ), recessiveConfig.getChild( "bar" ) );
271     }
272 
273     public void testDupeChildren()
274         throws IOException, XmlPullParserException
275     {
276         String dupes = "<configuration><foo>x</foo><foo>y</foo></configuration>";
277         Xpp3Dom dom = Xpp3DomBuilder.build( new StringReader( dupes ) );
278         assertNotNull( dom );
279         assertEquals( "y", dom.getChild( "foo" ).getValue() );
280     }
281 }