View Javadoc
1   package org.codehaus.plexus.metadata.merge;
2   
3   /*
4    * The MIT License
5    *
6    * Copyright (c) 2006, The Codehaus
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of
9    * this software and associated documentation files (the "Software"), to deal in
10   * the Software without restriction, including without limitation the rights to
11   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12   * of the Software, and to permit persons to whom the Software is furnished to do
13   * so, subject to the following conditions:
14   *
15   * The above copyright notice and this permission notice shall be included in all
16   * copies or substantial portions of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   * SOFTWARE.
25   */
26  
27  import java.io.File;
28  import java.util.List;
29  
30  import org.codehaus.plexus.PlexusTestCase;
31  import org.codehaus.plexus.metadata.merge.support.AbstractMergeableElement;
32  import org.codehaus.plexus.metadata.merge.support.AbstractMergeableElementList;
33  import org.codehaus.plexus.metadata.merge.support.ComponentElement;
34  import org.codehaus.plexus.metadata.merge.support.ComponentsElement;
35  import org.codehaus.plexus.metadata.merge.support.RequirementsElement;
36  import org.codehaus.plexus.util.FileUtils;
37  import org.jdom.Document;
38  import org.jdom.Element;
39  import org.jdom.input.SAXBuilder;
40  
41  /**
42   * Tests for {@link ComponentsXmlMerger}.
43   *
44   * @author <a href='mailto:rahul.thakur.xdev@gmail.com'>Rahul Thakur</a>
45   * @version $Id$
46   */
47  public class ComponentsXmlMergerTest
48      extends PlexusTestCase
49  {
50      public void testBasic()
51          throws Exception
52      {
53          ComponentsXmlMerger merger = (ComponentsXmlMerger) lookup( Merger.class, "componentsXml" );
54          assertNotNull( merger );
55      }
56  
57      public void testComponentsXmlFileMerge()
58          throws Exception
59      {
60          File dominantXml = getTestFile( "src/test/resources/org/codehaus/plexus/metadata/merge/dominant.xml" );
61          File recessiveXml = getTestFile( "src/test/resources/org/codehaus/plexus/metadata/merge/recessive.xml" );
62          Document dDoc = new SAXBuilder().build( dominantXml );
63          Document rDoc = new SAXBuilder().build( recessiveXml );
64          // ComponentsXmlMerger merger = new ComponentsXmlMerger (dDoc);
65          Merger merger = lookup( Merger.class, "componentsXml" );
66          assertNotNull( merger );
67          merger.merge( dDoc, rDoc );
68  
69          File merged_xml = getTestFile( "target/merged.xml" );
70          if ( merged_xml.exists() )
71          {
72              FileUtils.forceDelete( merged_xml );
73          }
74          merger.writeMergedDocument( dDoc, merged_xml );
75          assertTrue( merged_xml.exists() );
76          // read merged xml and verify it was merged as expected 
77          Document mDoc = new SAXBuilder().build( merged_xml );
78          Element mRootElt = mDoc.getRootElement();
79          assertTrue( mRootElt.getName().equals( "component-set" ) );
80          assertEquals( 1, mRootElt.getChildren( "components" ).size() );
81          List componentEltList = mRootElt.getChild( "components" ).getChildren( "component" );
82          assertEquals( 2, componentEltList.size() );
83          Element cElt = (Element) componentEltList.get( 0 );
84  
85          assertEquals( "org.codehaus.plexus.metadata.component.IComponent", cElt.getChildTextTrim( "role" ) );
86          assertEquals( "org.codehaus.plexus.metadata.component.DominantComponent", cElt.getChildTextTrim( "implementation" ) );
87  
88          assertEquals( "Should only have 1 description element.", 1, cElt.getChildren( "description" ).size() );
89          assertEquals( "Description for Dominant component", cElt.getChildTextTrim( "description" ) );
90  
91          assertEquals( "Should only have 1 configuration element.", 1, cElt.getChildren( "configuration" ).size() );
92          // assert Merged configuration properties
93          Element configurationElt = cElt.getChild( "configuration" );
94          assertNotNull( configurationElt );
95          assertEquals( 1, configurationElt.getChildren( "prop1" ).size() );
96          assertEquals( "Dominant Property1 value", configurationElt.getChildTextTrim( "prop1" ) );
97          assertEquals( 1, configurationElt.getChildren( "prop2" ).size() );
98          assertEquals( 0, configurationElt.getChildren( "prop3" ).size() );
99  
100         // now for the second component
101         cElt = (Element) componentEltList.get( 1 );
102         assertEquals( "org.codehaus.plexus.metadata.component.INonConflictingComponent", cElt.getChildTextTrim( "role" ) );
103         assertEquals( "org.codehaus.plexus.metadata.component.RecessiveComponent", cElt.getChildTextTrim( "implementation" ) );
104 
105         assertEquals( 1, mRootElt.getChildren( "lifecycle-handler-manager" ).size() );
106         assertEquals( "org.codehaus.plexus.lifecycle.DefaultLifecycleHandlerManager", mRootElt
107             .getChild( "lifecycle-handler-manager" ).getAttributeValue( "implementation" ) );
108     }
109 
110     public void testInvalidMergeableElements()
111         throws Exception
112     {
113         // dominant Component Element
114         AbstractMergeableElement dCE = new ComponentElement( new Element( "component" ) );
115         Element roleElt = new Element( "role" );
116         roleElt.setText( "org.codehaus.plexus.ISampleRole" );
117         dCE.addContent( roleElt );
118 
119         AbstractMergeableElementList reqElt = new RequirementsElement( new Element( "requirement" ) );
120         // attempt and invalid merge
121         try
122         {
123             dCE.merge( reqElt );
124             fail( "Expected MergeException!" );
125         }
126         catch ( MergeException e )
127         {
128             // do nothing.
129         }
130     }
131 
132     /**
133      * Tests if &lt;component&gt; elements from two sets are being merged properly.
134      *
135      * @throws Exception if there was an unexpected error.
136      */
137     public void testComponentsMerge()
138         throws Exception
139     {
140         // dominant Components Element
141         AbstractMergeableElement dParent = new ComponentsElement( new Element( "components" ) );
142         Element dCE = new Element( "component" );
143         dParent.addContent( dCE );
144         Element roleElt = new Element( "role" );
145         roleElt.setText( "org.codehaus.plexus.ISampleRole" );
146         dCE.addContent( roleElt );
147         Element roleHintElt = new Element( "role-hint" );
148         roleHintElt.setText( "sample-role-hint" );
149         dCE.addContent( roleHintElt );
150         Element implElt = new Element( "implementation" );
151         implElt.setText( "org.codehaus.plexus.DominantImplementation" );
152         dCE.addContent( implElt );
153         Element requirementsElt = new Element( "requirements" );
154         Element reqElt = new Element( "requirement" );
155         Element reqRoleElt = new Element( "role" );
156         reqRoleElt.setText( "org.codehaus.plexus.IRequiredRole" );
157         reqElt.addContent( reqRoleElt );
158         requirementsElt.addContent( reqElt );
159         dCE.addContent( requirementsElt );
160 
161         // recessive Component Element
162         AbstractMergeableElement rParent = new ComponentsElement( new Element( "components" ) );
163         Element rCE = new Element( "component" );
164         rParent.addContent( rCE );
165         roleElt = new Element( "role" );
166         roleElt.setText( "org.codehaus.plexus.ISampleRole" );
167         rCE.addContent( roleElt );
168         roleHintElt = new Element( "role-hint" );
169         roleHintElt.setText( "sample-role-hint" );
170         rCE.addContent( roleHintElt );
171         implElt = new Element( "implementation" );
172         implElt.setText( "org.codehaus.plexus.RecessiveImplementation" );
173         rCE.addContent( implElt );
174         Element lifecycleHandlerElt = new Element( "lifecycle-handler" );
175         rCE.addContent( lifecycleHandlerElt );
176         lifecycleHandlerElt.setText( "plexus-configurable" );
177         requirementsElt = new Element( "requirements" );
178         reqElt = new Element( "requirement" );
179         reqRoleElt = new Element( "role" );
180         reqRoleElt.setText( "org.codehaus.plexus.IRequiredRole" );
181         reqElt.addContent( reqRoleElt );
182         requirementsElt.addContent( reqElt );
183         Element reqRoleHintElt = new Element( "role-hint" );
184         reqRoleHintElt.setText( "recessive-required-role-hint" );
185         reqElt.addContent( reqRoleHintElt );
186         rCE.addContent( requirementsElt );
187 
188         // attempt to merge
189         dParent.merge( rParent );
190         assertEquals( 1, dParent.getChildren( "component" ).size() );
191         assertEquals( "org.codehaus.plexus.DominantImplementation", dParent.getChild( "component" )
192             .getChildText( "implementation" ) );
193         assertEquals( 1, dParent.getChild( "component" ).getChild( "requirements" ).getChildren( "requirement" ).size() );
194     }
195 
196     /**
197      * <em>This is deprecated as we dont' want to drill to merging
198      * nested elements within a component.</em><p>
199      * <em>Keeping this around for testing MergeStrategy implmentation.</em>
200      *
201      * @throws Exception
202      */
203     public void testDeepComponentsMerge()
204         throws Exception
205     {
206         // FIXME: Review this after MergeStrategies are in place.
207         if ( true )
208         {
209             return;
210         }
211 
212         // dominant Component Element
213         AbstractMergeableElement dCE = new ComponentElement( new Element( "component" ) );
214         Element roleElt = new Element( "role" );
215         roleElt.setText( "org.codehaus.plexus.ISampleRole" );
216         dCE.addContent( roleElt );
217         Element roleHintElt;
218         // roleHintElt = new Element ("role-hint");
219         // roleHintElt.setText ("sample-hint");
220         // dCE.addContent (roleHintElt);
221         Element implElt = new Element( "implementation" );
222         implElt.setText( "org.codehaus.plexus.DominantImplementation" );
223         dCE.addContent( implElt );
224         Element requirementsElt = new Element( "requirements" );
225         Element reqElt = new Element( "requirement" );
226         Element reqRoleElt = new Element( "role" );
227         reqRoleElt.setText( "org.codehaus.plexus.IRequiredRole" );
228         reqElt.addContent( reqRoleElt );
229         requirementsElt.addContent( reqElt );
230         dCE.addContent( requirementsElt );
231 
232         // recessive Component Element
233         AbstractMergeableElement rCE = new ComponentElement( new Element( "component" ) );
234         roleElt = new Element( "role" );
235         roleElt.setText( "org.codehaus.plexus.ISampleRole" );
236         rCE.addContent( roleElt );
237         roleHintElt = new Element( "role-hint" );
238         roleHintElt.setText( "recessive-hint" );
239         rCE.addContent( roleHintElt );
240         implElt = new Element( "implementation" );
241         implElt.setText( "org.codehaus.plexus.RecessiveImplementation" );
242         rCE.addContent( implElt );
243         Element lifecycleHandlerElt = new Element( "lifecycle-handler" );
244         rCE.addContent( lifecycleHandlerElt );
245         lifecycleHandlerElt.setText( "plexus-configurable" );
246         requirementsElt = new Element( "requirements" );
247         reqElt = new Element( "requirement" );
248         reqRoleElt = new Element( "role" );
249         reqRoleElt.setText( "org.codehaus.plexus.IRequiredRole" );
250         reqElt.addContent( reqRoleElt );
251         requirementsElt.addContent( reqElt );
252         Element reqRoleHintElt = new Element( "role-hint" );
253         reqRoleHintElt.setText( "recessive-required-role-hint" );
254         reqElt.addContent( reqRoleHintElt );
255         rCE.addContent( requirementsElt );
256 
257         // attempt to merge
258         dCE.merge( rCE );
259 
260         // verify the merge
261         assertTrue( null != dCE.getChild( "role" ) );
262         assertEquals( "org.codehaus.plexus.ISampleRole", dCE.getChildText( "role" ) );
263         assertTrue( null != dCE.getChild( "role-hint" ) );
264         assertEquals( "recessive-hint", dCE.getChildText( "role-hint" ) );
265         assertTrue( null != dCE.getChild( "lifecycle-handler" ) );
266         assertEquals( "plexus-configurable", dCE.getChildText( "lifecycle-handler" ) );
267         assertTrue( null != dCE.getChild( "requirements" ) );
268         assertEquals( 1, dCE.getChild( "requirements" ).getChildren( "requirement" ).size() );
269         assertEquals( "recessive-required-role-hint", ( (Element) dCE.getChild( "requirements" )
270             .getChildren( "requirement" ).get( 0 ) ).getChildText( "role-hint" ) );
271     }
272 }