1   package org.apache.maven.project.inheritance;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.model.Build;
23  import org.apache.maven.model.Dependency;
24  import org.apache.maven.model.DependencyManagement;
25  import org.apache.maven.model.DeploymentRepository;
26  import org.apache.maven.model.DistributionManagement;
27  import org.apache.maven.model.Model;
28  import org.apache.maven.model.Parent;
29  import org.apache.maven.model.Plugin;
30  import org.apache.maven.model.PluginExecution;
31  import org.apache.maven.model.Relocation;
32  import org.apache.maven.model.ReportPlugin;
33  import org.apache.maven.model.ReportSet;
34  import org.apache.maven.model.Reporting;
35  import org.apache.maven.model.Repository;
36  import org.apache.maven.model.RepositoryBase;
37  import org.apache.maven.model.Resource;
38  import org.apache.maven.model.Scm;
39  import org.apache.maven.model.Site;
40  
41  import java.util.ArrayList;
42  import java.util.Collections;
43  import java.util.Iterator;
44  import java.util.List;
45  import java.util.Map;
46  
47  import junit.framework.TestCase;
48  
49  /**
50   * @author jdcasey
51   */
52  public class DefaultModelInheritanceAssemblerTest
53      extends TestCase
54  {
55      private ModelInheritanceAssembler assembler = new DefaultModelInheritanceAssembler();
56  
57      public void testShouldAdjustChildUrlBasedOnParentAndModulePathInSiblingDir()
58      {
59          Model parent = makeBaseModel( "parent" );
60  
61          parent.setUrl( "http://www.google.com/parent" );
62  
63          Model child = makeBaseModel( "child" );
64  
65          // TODO: this is probably what we should be appending...
66  //        child.setUrl( "/child.dir" );
67  
68          parent.addModule( "../child" );
69  
70          assembler.assembleModelInheritance( child, parent, ".." );
71  
72          String resultingUrl = child.getUrl();
73  
74          System.out.println( resultingUrl );
75  
76          assertEquals( "http://www.google.com/child", resultingUrl );
77      }
78  
79      public void testShouldAdjustPathsThreeLevelsDeepAncestryInRepoAndNonStandardModulePaths()
80      {
81          Model top = makeBaseModel( "top" );
82  
83          top.setUrl( "http://www.google.com/top" );
84  
85          Model middle = makeBaseModel( "middle" );
86  
87          top.addModule( "../middle" );
88  
89          Model bottom = makeBaseModel( "bottom" );
90  
91          middle.addModule( "../bottom" );
92  
93          assembler.assembleModelInheritance( middle, top, ".." );
94          assembler.assembleModelInheritance( bottom, middle, ".." );
95  
96          String resultingUrl = bottom.getUrl();
97  
98          System.out.println( resultingUrl );
99  
100         assertEquals( "http://www.google.com/bottom", resultingUrl );
101     }
102 
103     public void testShouldMergeSuccessiveDependencyManagementSectionsOverThreeLevels()
104     {
105         Model top = makeBaseModel( "top" );
106 
107         DependencyManagement topMgmt = new DependencyManagement();
108 
109         topMgmt.addDependency( makeDep( "top-dep" ) );
110 
111         top.setDependencyManagement( topMgmt );
112 
113         Model mid = makeBaseModel( "mid" );
114 
115         DependencyManagement midMgmt = new DependencyManagement();
116 
117         midMgmt.addDependency( makeDep( "mid-dep" ) );
118 
119         mid.setDependencyManagement( midMgmt );
120 
121         Model bottom = makeBaseModel( "bottom" );
122 
123         DependencyManagement bottomMgmt = new DependencyManagement();
124 
125         bottomMgmt.addDependency( makeDep( "bottom-dep" ) );
126 
127         bottom.setDependencyManagement( bottomMgmt );
128 
129         assembler.assembleModelInheritance( mid, top );
130 
131         assembler.assembleModelInheritance( bottom, mid );
132 
133         DependencyManagement result = bottom.getDependencyManagement();
134 
135         List resultDeps = result.getDependencies();
136 
137         assertEquals( 3, resultDeps.size() );
138     }
139 
140     public void testShouldMergeDependencyManagementSectionsFromTopTwoLevelsToBottomLevel()
141     {
142         Model top = makeBaseModel( "top" );
143 
144         DependencyManagement topMgmt = new DependencyManagement();
145 
146         topMgmt.addDependency( makeDep( "top-dep" ) );
147 
148         top.setDependencyManagement( topMgmt );
149 
150         Model mid = makeBaseModel( "mid" );
151 
152         DependencyManagement midMgmt = new DependencyManagement();
153 
154         midMgmt.addDependency( makeDep( "mid-dep" ) );
155 
156         mid.setDependencyManagement( midMgmt );
157 
158         Model bottom = makeBaseModel( "bottom" );
159 
160         assembler.assembleModelInheritance( mid, top );
161 
162         assembler.assembleModelInheritance( bottom, mid );
163 
164         DependencyManagement result = bottom.getDependencyManagement();
165 
166         List resultDeps = result.getDependencies();
167 
168         assertEquals( 2, resultDeps.size() );
169     }
170 
171     private Dependency makeDep( String artifactId )
172     {
173         Dependency dep = new Dependency();
174 
175         dep.setGroupId( "maven" );
176         dep.setArtifactId( artifactId );
177         dep.setVersion( "1.0" );
178 
179         return dep;
180     }
181 
182     public void testShouldAppendChildPathAdjustmentWithNoChildPartAndNoParentPart()
183     {
184         String parentPath = "";
185         String childPath = null;
186         String pathAdjustment = "../file-management";
187 
188         String result =
189             ( (DefaultModelInheritanceAssembler) assembler ).appendPath( parentPath, childPath, pathAdjustment, true );
190 
191         System.out.println( "Resulting path is: \'" + result + "\'" );
192 
193         assertEquals( "Append with path adjustment failed.", "/file-management", result );
194     }
195 
196     public void testShouldAppendChildPathAdjustmentWithNoChildPart()
197     {
198         String parentPath = "http://maven.apache.org/shared/maven-shared-parent";
199         String childPath = null;
200         String pathAdjustment = "../file-management";
201 
202         String result =
203             ( (DefaultModelInheritanceAssembler) assembler ).appendPath( parentPath, childPath, pathAdjustment, true );
204 
205         System.out.println( "Resulting path is: \'" + result + "\'" );
206 
207         assertEquals( "Append with path adjustment failed.", "http://maven.apache.org/shared/file-management", result );
208     }
209 
210     public void testShouldAppendPathWithChildPathAdjustment()
211     {
212         String parentPath = "http://maven.apache.org/shared/maven-shared-parent";
213         String childPath = "file-management";
214         String pathAdjustment = "..";
215 
216         String result =
217             ( (DefaultModelInheritanceAssembler) assembler ).appendPath( parentPath, childPath, pathAdjustment, true );
218 
219         System.out.println( "Resulting path is: \'" + result + "\'" );
220 
221         assertEquals( "Append with path adjustment failed.", "http://maven.apache.org/shared/file-management", result );
222     }
223 
224     public void testAppendPathUNC()
225     {
226         String parentPath = "file:////host/shared/maven-shared-parent";
227         String childPath = "file-management";
228         String pathAdjustment = null;
229 
230         String result =
231             ( (DefaultModelInheritanceAssembler) assembler ).appendPath( parentPath, childPath, pathAdjustment, true );
232 
233         assertEquals( "file:////host/shared/maven-shared-parent/file-management", result );
234     }
235 
236     public void testDistributionManagementInheritance()
237     {
238         Model parent = makeBaseModel( "parent" );
239         Model child = makeBaseModel( "child" );
240 
241         DistributionManagement distributionManagement = new DistributionManagement();
242         distributionManagement.setDownloadUrl( "downloadUrl" );
243         distributionManagement.setRelocation( new Relocation() );
244         distributionManagement.setStatus( "deployed" );
245 
246         DeploymentRepository repository = new DeploymentRepository();
247         repository.setId( "apache.releases" );
248         repository.setUrl( "scp://minotaur.apache.org/www/www.apache.org/dist/java-repository" );
249         repository.setName( "name" );
250         repository.setLayout( "legacy" );
251         distributionManagement.setRepository( repository );
252 
253         DeploymentRepository snapshotRepository = new DeploymentRepository();
254         snapshotRepository.setId( "apache.snapshots" );
255         snapshotRepository.setUrl( "scp://minotaur.apache.org/www/cvs.apache.org/repository" );
256         snapshotRepository.setName( "name" );
257         snapshotRepository.setLayout( "legacy" );
258         snapshotRepository.setUniqueVersion( false );
259         distributionManagement.setSnapshotRepository( snapshotRepository );
260 
261         Site site = new Site();
262         site.setId( "apache.website" );
263         site.setUrl( "scp://minotaur.apache.org/www/maven.apache.org/" );
264         site.setName( "name3" );
265         distributionManagement.setSite( site );
266 
267         parent.setDistributionManagement( distributionManagement );
268 
269         assembler.assembleModelInheritance( child, parent );
270 
271         DistributionManagement childDistMgmt = child.getDistributionManagement();
272         assertNotNull( "Check distMgmt inherited", childDistMgmt );
273         assertNull( "Check status NOT inherited", childDistMgmt.getStatus() );
274         assertNull( "Check relocation NOT inherited", childDistMgmt.getRelocation() );
275         assertEquals( "Check downloadUrl inherited", distributionManagement.getDownloadUrl(),
276                       childDistMgmt.getDownloadUrl() );
277 
278         Site childSite = childDistMgmt.getSite();
279         assertNotNull( "Check site inherited", childSite );
280         assertEquals( "Check id matches", site.getId(), childSite.getId() );
281         assertEquals( "Check name matches", site.getName(), childSite.getName() );
282         assertEquals( "Check url matches with appended path", site.getUrl() + "child", childSite.getUrl() );
283 
284         assertRepositoryBase( childDistMgmt.getRepository(), repository );
285         assertRepositoryBase( childDistMgmt.getSnapshotRepository(), snapshotRepository );
286         assertEquals( "Check uniqueVersion is inherited", snapshotRepository.isUniqueVersion(),
287                       childDistMgmt.getSnapshotRepository().isUniqueVersion() );
288     }
289 
290     private static void assertRepositoryBase( RepositoryBase childRepository, RepositoryBase repository )
291     {
292         assertNotNull( "Check repository inherited", childRepository );
293         assertEquals( "Check id matches", repository.getId(), childRepository.getId() );
294         assertEquals( "Check name matches", repository.getName(), childRepository.getName() );
295         assertEquals( "Check url matches", repository.getUrl(), childRepository.getUrl() );
296         assertEquals( "Check layout matches", repository.getLayout(), childRepository.getLayout() );
297     }
298 
299     public void testShouldOverrideUnitTestExcludesOnly()
300     {
301         Model parent = new Model();
302 
303         parent.setGroupId( "test" );
304         parent.setArtifactId( "test" );
305         parent.setVersion( "0.0" );
306 
307         Build parentBuild = new Build();
308 
309         parentBuild.setSourceDirectory( "src/main/java" );
310         parentBuild.setTestSourceDirectory( "src/test/java" );
311 
312         Resource parentResource = new Resource();
313 
314         parentResource.setDirectory( "src/main/resources" );
315 
316         parentBuild.addResource( parentResource );
317 
318         Resource parentUTResource = new Resource();
319 
320         parentUTResource.setDirectory( "src/test/resources" );
321 
322         parentBuild.addTestResource( parentUTResource );
323 
324         parent.setBuild( parentBuild );
325 
326         Model child = new Model();
327 
328         Parent parentElement = new Parent();
329         parentElement.setArtifactId( parent.getArtifactId() );
330         parentElement.setGroupId( parent.getGroupId() );
331         parentElement.setVersion( parent.getVersion() );
332         child.setParent( parentElement );
333 
334         child.setPackaging( "plugin" );
335 
336         Build childBuild = new Build();
337         child.setBuild( childBuild );
338 
339         assembler.assembleModelInheritance( child, parent );
340 
341         assertEquals( "source directory should be from parent", "src/main/java",
342                       child.getBuild().getSourceDirectory() );
343         assertEquals( "unit test source directory should be from parent", "src/test/java",
344                       child.getBuild().getTestSourceDirectory() );
345 
346 // TODO: test inheritence/super pom?
347 //        List childExcludesTest = child.getBuild().getUnitTest().getExcludes();
348 //
349 //        assertTrue( "unit test excludes should have **/*AspectTest.java", childExcludesTest
350 //            .contains( "**/*AspectTest.java" ) );
351 //        assertTrue( "unit test excludes should have **/*Abstract*.java", childExcludesTest
352 //            .contains( "**/*Abstract*.java" ) );
353 //
354 
355         List resources = child.getBuild().getResources();
356 
357         assertEquals( "build resources inherited from parent should be of size 1", 1, resources.size() );
358         assertEquals( "first resource should have dir == src/main/resources", "src/main/resources",
359                       ( (Resource) resources.get( 0 ) ).getDirectory() );
360 
361         List utResources = child.getBuild().getTestResources();
362 
363         assertEquals( "UT resources inherited from parent should be of size 1", 1, utResources.size() );
364         assertEquals( "first UT resource should have dir == src/test/resources", "src/test/resources",
365                       ( (Resource) utResources.get( 0 ) ).getDirectory() );
366 
367         assertEquals( "plugin", child.getPackaging() );
368         assertEquals( "jar", parent.getPackaging() );
369     }
370 
371     /**
372      * <pre>
373      * root
374      *   |--artifact1
375      *   |         |
376      *   |         |--artifact1-1
377      *   |
378      *   |--artifact2 (in another directory called a2 so it has it's own scm section)
379      *             |
380      *             |--artifact2-1
381      * </pre>
382      */
383     public void testScmInheritance()
384         throws Exception
385     {
386         // Make the models
387         Model root = makeScmModel( "root", "scm:foo:/scm-root", "scm:foo:/scm-dev-root", null );
388 
389         Model artifact1 = makeScmModel( "artifact1" );
390 
391         Model artifact1_1 = makeScmModel( "artifact1-1" );
392 
393         Model artifact2 =
394             makeScmModel( "artifact2", "scm:foo:/scm-root/yay-artifact2", "scm:foo:/scm-dev-root/yay-artifact2", null );
395 
396         Model artifact2_1 = makeScmModel( "artifact2-1" );
397 
398         // Assemble
399         assembler.assembleModelInheritance( artifact1, root );
400 
401         assembler.assembleModelInheritance( artifact1_1, artifact1 );
402 
403         assembler.assembleModelInheritance( artifact2, root );
404 
405         assembler.assembleModelInheritance( artifact2_1, artifact2 );
406 
407         // --- -- -
408 
409         assertConnection( "scm:foo:/scm-root/artifact1", "scm:foo:/scm-dev-root/artifact1", artifact1 );
410 
411         assertConnection( "scm:foo:/scm-root/artifact1/artifact1-1", "scm:foo:/scm-dev-root/artifact1/artifact1-1",
412                           artifact1_1 );
413 
414         assertConnection( "scm:foo:/scm-root/yay-artifact2", "scm:foo:/scm-dev-root/yay-artifact2", artifact2 );
415 
416         assertConnection( "scm:foo:/scm-root/yay-artifact2/artifact2-1",
417                           "scm:foo:/scm-dev-root/yay-artifact2/artifact2-1", artifact2_1 );
418     }
419 
420     public void testScmInheritanceWhereParentHasConnectionAndTheChildDoesnt()
421     {
422         Model parent = makeScmModel( "parent", "scm:foo:bar:/scm-root", null, null );
423 
424         Model child = makeScmModel( "child" );
425 
426         assembler.assembleModelInheritance( child, parent );
427 
428         assertScm( "scm:foo:bar:/scm-root/child", null, null, child.getScm() );
429     }
430 
431     public void testScmInheritanceWhereParentHasConnectionAndTheChildDoes()
432     {
433         Model parent = makeScmModel( "parent", "scm:foo:bar:/scm-root", null, null );
434 
435         Model child = makeScmModel( "child", "scm:foo:bar:/another-root", null, null );
436 
437         assembler.assembleModelInheritance( child, parent );
438 
439         assertScm( "scm:foo:bar:/another-root", null, null, child.getScm() );
440     }
441 
442     public void testScmInheritanceWhereParentHasDeveloperConnectionAndTheChildDoesnt()
443     {
444         Model parent = makeScmModel( "parent", null, "scm:foo:bar:/scm-dev-root", null );
445 
446         Model child = makeScmModel( "child" );
447 
448         assembler.assembleModelInheritance( child, parent );
449 
450         assertScm( null, "scm:foo:bar:/scm-dev-root/child", null, child.getScm() );
451     }
452 
453     public void testScmInheritanceWhereParentHasDeveloperConnectionAndTheChildDoes()
454     {
455         Model parent = makeScmModel( "parent", null, "scm:foo:bar:/scm-dev-root", null );
456 
457         Model child = makeScmModel( "child", null, "scm:foo:bar:/another-dev-root", null );
458 
459         assembler.assembleModelInheritance( child, parent );
460 
461         assertScm( null, "scm:foo:bar:/another-dev-root", null, child.getScm() );
462     }
463 
464     public void testScmInheritanceWhereParentHasUrlAndTheChildDoesnt()
465     {
466         Model parent = makeScmModel( "parent", null, null, "http://foo/bar" );
467 
468         Model child = makeScmModel( "child" );
469 
470         assembler.assembleModelInheritance( child, parent );
471 
472         assertScm( null, null, "http://foo/bar/child", child.getScm() );
473     }
474 
475     public void testScmInheritanceWhereParentHasUrlAndTheChildDoes()
476     {
477         Model parent = makeScmModel( "parent", null, null, "http://foo/bar/" );
478 
479         Model child = makeScmModel( "child", null, null, "http://bar/foo/" );
480 
481         assembler.assembleModelInheritance( child, parent );
482 
483         assertScm( null, null, "http://bar/foo/", child.getScm() );
484     }
485 
486     public void testRepositoryInheritenceWhereParentHasRepositoryAndTheChildDoesnt()
487     {
488         Model parent = makeRepositoryModel( "parent", "central", "http://repo1.maven.org/maven/" );
489 
490         List repos = new ArrayList( parent.getRepositories() );
491 
492         Model child = makeBaseModel( "child" );
493 
494         assembler.assembleModelInheritance( child, parent );
495 
496         // TODO: a lot easier if modello generated equals() :)
497         assertRepositories( repos, child.getRepositories() );
498     }
499 
500     public void testRepositoryInheritenceWhereParentHasRepositoryAndTheChildHasDifferent()
501     {
502         Model parent = makeRepositoryModel( "parent", "central", "http://repo1.maven.org/maven/" );
503 
504         List repos = new ArrayList( parent.getRepositories() );
505 
506         Model child = makeRepositoryModel( "child", "workplace", "http://repository.mycompany.com/maven/" );
507 
508         repos.addAll( child.getRepositories() );
509 
510         assembler.assembleModelInheritance( child, parent );
511 
512         // TODO: a lot easier if modello generated equals() :)
513         assertRepositories( repos, child.getRepositories() );
514     }
515 
516     public void testRepositoryInheritenceWhereParentHasRepositoryAndTheChildHasSameId()
517     {
518         Model parent = makeRepositoryModel( "parent", "central", "http://repo1.maven.org/maven/" );
519 
520         Model child = makeRepositoryModel( "child", "central", "http://repo2.maven.org/maven/" );
521 
522         // We want to get the child repository here.
523         List repos = new ArrayList( child.getRepositories() );
524 
525         assembler.assembleModelInheritance( child, parent );
526 
527         // TODO: a lot easier if modello generated equals() :)
528         assertRepositories( repos, child.getRepositories() );
529     }
530 
531     public void testPluginInheritanceWhereParentPluginWithoutInheritFlagAndChildHasNoPlugins()
532     {
533         Model parent = makeBaseModel( "parent" );
534 
535         Model child = makeBaseModel( "child" );
536 
537         Plugin parentPlugin = new Plugin();
538         parentPlugin.setArtifactId( "maven-testInheritance-plugin" );
539         parentPlugin.setGroupId( "org.apache.maven.plugins" );
540         parentPlugin.setVersion( "1.0" );
541 
542         List parentPlugins = Collections.singletonList( parentPlugin );
543 
544         Build parentBuild = new Build();
545         parentBuild.setPlugins( parentPlugins );
546 
547         parent.setBuild( parentBuild );
548 
549         assembler.assembleModelInheritance( child, parent );
550 
551         assertPlugins( parentPlugins, child );
552     }
553 
554     public void testPluginInheritanceWhereParentPluginWithTrueInheritFlagAndChildHasNoPlugins()
555     {
556         Model parent = makeBaseModel( "parent" );
557 
558         Model child = makeBaseModel( "child" );
559 
560         Plugin parentPlugin = new Plugin();
561         parentPlugin.setArtifactId( "maven-testInheritance2-plugin" );
562         parentPlugin.setGroupId( "org.apache.maven.plugins" );
563         parentPlugin.setVersion( "1.0" );
564         parentPlugin.setInherited( "true" );
565 
566         List parentPlugins = Collections.singletonList( parentPlugin );
567 
568         Build parentBuild = new Build();
569         parentBuild.setPlugins( parentPlugins );
570 
571         parent.setBuild( parentBuild );
572 
573         assembler.assembleModelInheritance( child, parent );
574 
575         assertPlugins( parentPlugins, child );
576     }
577 
578     public void testPluginInheritanceWhereParentPluginWithFalseInheritFlagAndChildHasNoPlugins()
579     {
580         Model parent = makeBaseModel( "parent" );
581 
582         Model child = makeBaseModel( "child" );
583 
584         Plugin parentPlugin = new Plugin();
585         parentPlugin.setArtifactId( "maven-testInheritance3-plugin" );
586         parentPlugin.setGroupId( "org.apache.maven.plugins" );
587         parentPlugin.setVersion( "1.0" );
588         parentPlugin.setInherited( "false" );
589 
590         List parentPlugins = Collections.singletonList( parentPlugin );
591 
592         Build parentBuild = new Build();
593         parentBuild.setPlugins( parentPlugins );
594 
595         parent.setBuild( parentBuild );
596 
597         assembler.assembleModelInheritance( child, parent );
598 
599         assertPlugins( new ArrayList(), child );
600     }
601 
602     private void assertPlugins( List expectedPlugins, Model child )
603     {
604         Build childBuild = child.getBuild();
605 
606         if ( ( expectedPlugins != null ) && !expectedPlugins.isEmpty() )
607         {
608             assertNotNull( childBuild );
609 
610             Map childPluginsMap = childBuild.getPluginsAsMap();
611 
612             if ( childPluginsMap != null )
613             {
614                 assertEquals( expectedPlugins.size(), childPluginsMap.size() );
615 
616                 for ( Iterator it = expectedPlugins.iterator(); it.hasNext(); )
617                 {
618                     Plugin expectedPlugin = (Plugin) it.next();
619 
620                     Plugin childPlugin = (Plugin) childPluginsMap.get( expectedPlugin.getKey() );
621 
622                     assertPluginsEqual( expectedPlugin, childPlugin );
623                 }
624             }
625             else
626             {
627                 fail( "child plugins collection is null, but expectations map is not." );
628             }
629         }
630         else
631         {
632             assertTrue( ( childBuild == null ) || ( childBuild.getPlugins() == null ) || childBuild.getPlugins().isEmpty() );
633         }
634     }
635 
636     private void assertPluginsEqual( Plugin reference, Plugin test )
637     {
638         assertEquals( "Plugin keys don't match", reference.getKey(), test.getKey() );
639         assertEquals( "Plugin configurations don't match", reference.getConfiguration(), test.getConfiguration() );
640 
641         List referenceExecutions = reference.getExecutions();
642         Map testExecutionsMap = test.getExecutionsAsMap();
643 
644         if ( ( referenceExecutions != null ) && !referenceExecutions.isEmpty() )
645         {
646             assertTrue( "Missing goals specification", ( ( testExecutionsMap != null ) && !testExecutionsMap.isEmpty() ) );
647 
648             for ( Iterator it = referenceExecutions.iterator(); it.hasNext(); )
649             {
650                 PluginExecution referenceExecution = (PluginExecution) it.next();
651                 PluginExecution testExecution = (PluginExecution) testExecutionsMap.get( referenceExecution.getId() );
652 
653                 assertNotNull( "Goal from reference not found in test", testExecution );
654 
655                 assertEquals( "Goal IDs don't match", referenceExecution.getId(), testExecution.getId() );
656                 assertEquals( "Goal configurations don't match", referenceExecution.getConfiguration(),
657                               testExecution.getConfiguration() );
658                 assertEquals( "Goal lists don't match", referenceExecution.getGoals(), testExecution.getGoals() );
659             }
660         }
661         else
662         {
663             assertTrue( "Unexpected goals specification",
664                         ( ( testExecutionsMap == null ) || testExecutionsMap.isEmpty() ) );
665         }
666     }
667 
668     public void testReportingExcludeDefaultsInheritance()
669     {
670         // parent and child have no reporting section
671         Model parent = makeBaseModel( "parent" );
672         Model child = makeBaseModel( "child" );
673 
674         assembler.assembleModelInheritance( child, parent );
675         assertNull( child.getReporting() );
676 
677         // parent is default, and child has no reporting section
678         parent = makeBaseModel( "parent" );
679         parent.setReporting( new Reporting() );
680         child = makeBaseModel( "child" );
681 
682         assembler.assembleModelInheritance( child, parent );
683         assertFalse( child.getReporting().isExcludeDefaults() );
684 
685         // parent is false, and child has no reporting section
686         parent = makeBaseModel( "parent" );
687         parent.setReporting( createReportingWithExcludeDefaults( false ) );
688         child = makeBaseModel( "child" );
689 
690         assembler.assembleModelInheritance( child, parent );
691         assertFalse( child.getReporting().isExcludeDefaults() );
692 
693         // parent is true, and child has no reporting section
694         parent = makeBaseModel( "parent" );
695         parent.setReporting( createReportingWithExcludeDefaults( true ) );
696         child = makeBaseModel( "child" );
697 
698         assembler.assembleModelInheritance( child, parent );
699         assertTrue( child.getReporting().isExcludeDefaults() );
700 
701         // parent has no reporting section, child is false
702         parent = makeBaseModel( "parent" );
703         child = makeBaseModel( "child" );
704         child.setReporting( createReportingWithExcludeDefaults( false ) );
705 
706         assembler.assembleModelInheritance( child, parent );
707         assertFalse( child.getReporting().isExcludeDefaults() );
708 
709         // parent is default, and child is false
710         parent = makeBaseModel( "parent" );
711         parent.setReporting( new Reporting() );
712         child = makeBaseModel( "child" );
713         child.setReporting( createReportingWithExcludeDefaults( false ) );
714 
715         assembler.assembleModelInheritance( child, parent );
716         assertFalse( child.getReporting().isExcludeDefaults() );
717 
718         // parent is false, and child is false
719         parent = makeBaseModel( "parent" );
720         parent.setReporting( createReportingWithExcludeDefaults( false ) );
721         child = makeBaseModel( "child" );
722         child.setReporting( createReportingWithExcludeDefaults( false ) );
723 
724         assembler.assembleModelInheritance( child, parent );
725         assertFalse( child.getReporting().isExcludeDefaults() );
726 
727         // parent is true, and child is false
728         parent = makeBaseModel( "parent" );
729         parent.setReporting( createReportingWithExcludeDefaults( true ) );
730         child = makeBaseModel( "child" );
731         child.setReporting( createReportingWithExcludeDefaults( false ) );
732 
733         assembler.assembleModelInheritance( child, parent );
734         assertFalse( child.getReporting().isExcludeDefaults() );
735 
736         // parent has no reporting section, child is true
737         parent = makeBaseModel( "parent" );
738         child = makeBaseModel( "child" );
739         child.setReporting( createReportingWithExcludeDefaults( true ) );
740 
741         assembler.assembleModelInheritance( child, parent );
742         assertTrue( child.getReporting().isExcludeDefaults() );
743 
744         // parent is default, and child is true
745         parent = makeBaseModel( "parent" );
746         parent.setReporting( new Reporting() );
747         child = makeBaseModel( "child" );
748         child.setReporting( createReportingWithExcludeDefaults( true ) );
749 
750         assembler.assembleModelInheritance( child, parent );
751         assertTrue( child.getReporting().isExcludeDefaults() );
752 
753         // parent is false, and child is true
754         parent = makeBaseModel( "parent" );
755         parent.setReporting( createReportingWithExcludeDefaults( false ) );
756         child = makeBaseModel( "child" );
757         child.setReporting( createReportingWithExcludeDefaults( true ) );
758 
759         assembler.assembleModelInheritance( child, parent );
760         assertTrue( child.getReporting().isExcludeDefaults() );
761 
762         // parent is true, and child is true
763         parent = makeBaseModel( "parent" );
764         parent.setReporting( createReportingWithExcludeDefaults( true ) );
765         child = makeBaseModel( "child" );
766         child.setReporting( createReportingWithExcludeDefaults( true ) );
767 
768         assembler.assembleModelInheritance( child, parent );
769         assertTrue( child.getReporting().isExcludeDefaults() );
770 
771         // parent has no reporting section, child is default
772         parent = makeBaseModel( "parent" );
773         child = makeBaseModel( "child" );
774         child.setReporting( new Reporting() );
775 
776         assembler.assembleModelInheritance( child, parent );
777         assertFalse( child.getReporting().isExcludeDefaults() );
778 
779         // parent is default, and child is default
780         parent = makeBaseModel( "parent" );
781         parent.setReporting( new Reporting() );
782         child = makeBaseModel( "child" );
783         child.setReporting( new Reporting() );
784 
785         assembler.assembleModelInheritance( child, parent );
786         assertFalse( child.getReporting().isExcludeDefaults() );
787 
788         // parent is false, and child is default
789         parent = makeBaseModel( "parent" );
790         parent.setReporting( createReportingWithExcludeDefaults( false ) );
791         child = makeBaseModel( "child" );
792         child.setReporting( new Reporting() );
793 
794         assembler.assembleModelInheritance( child, parent );
795         assertFalse( child.getReporting().isExcludeDefaults() );
796 
797         // parent is true, and child is default
798         parent = makeBaseModel( "parent" );
799         parent.setReporting( createReportingWithExcludeDefaults( true ) );
800         child = makeBaseModel( "child" );
801         child.setReporting( new Reporting() );
802 
803         assembler.assembleModelInheritance( child, parent );
804         assertTrue( child.getReporting().isExcludeDefaults() );
805     }
806 
807     private Reporting createReportingWithExcludeDefaults( boolean excludeDefaults )
808     {
809         Reporting reporting = new Reporting();
810         reporting.setExcludeDefaults( excludeDefaults );
811         return reporting;
812     }
813 
814     public void testReportInheritanceWhereParentReportWithoutInheritFlagAndChildHasNoReports()
815     {
816         Model parent = makeBaseModel( "parent" );
817 
818         Model child = makeBaseModel( "child" );
819 
820         ReportPlugin parentReport = new ReportPlugin();
821         parentReport.setArtifactId( "maven-testInheritance-report-plugin" );
822         parentReport.setGroupId( "org.apache.maven.plugins" );
823         parentReport.setVersion( "1.0" );
824 
825         List parentPlugins = Collections.singletonList( parentReport );
826 
827         Reporting parentBuild = new Reporting();
828         parentBuild.setPlugins( parentPlugins );
829 
830         parent.setReporting( parentBuild );
831 
832         assembler.assembleModelInheritance( child, parent );
833 
834         assertReports( parentPlugins, child );
835     }
836 
837     public void testReportInheritanceWhereParentReportWithTrueInheritFlagAndChildHasNoReports()
838     {
839         Model parent = makeBaseModel( "parent" );
840 
841         Model child = makeBaseModel( "child" );
842 
843         ReportPlugin parentPlugin = new ReportPlugin();
844         parentPlugin.setArtifactId( "maven-testInheritance2-report-plugin" );
845         parentPlugin.setGroupId( "org.apache.maven.plugins" );
846         parentPlugin.setVersion( "1.0" );
847         parentPlugin.setInherited( "true" );
848 
849         List parentPlugins = Collections.singletonList( parentPlugin );
850 
851         Reporting parentBuild = new Reporting();
852         parentBuild.setPlugins( parentPlugins );
853 
854         parent.setReporting( parentBuild );
855 
856         assembler.assembleModelInheritance( child, parent );
857 
858         assertReports( parentPlugins, child );
859     }
860 
861     public void testReportInheritanceWhereParentReportWithFalseInheritFlagAndChildHasNoReports()
862     {
863         Model parent = makeBaseModel( "parent" );
864 
865         Model child = makeBaseModel( "child" );
866 
867         ReportPlugin parentPlugin = new ReportPlugin();
868         parentPlugin.setArtifactId( "maven-testInheritance3-report-plugin" );
869         parentPlugin.setGroupId( "org.apache.maven.plugins" );
870         parentPlugin.setVersion( "1.0" );
871         parentPlugin.setInherited( "false" );
872 
873         List parentPlugins = Collections.singletonList( parentPlugin );
874 
875         Reporting parentBuild = new Reporting();
876         parentBuild.setPlugins( parentPlugins );
877 
878         parent.setReporting( parentBuild );
879 
880         assembler.assembleModelInheritance( child, parent );
881 
882         assertReports( new ArrayList(), child );
883     }
884 
885     public void testPluginExecInheritanceWhereExecInheritedSetToFalse()
886     {
887         String testId = "test";
888         String gid = "group";
889         String aid = "artifact";
890         String ver = "1";
891 
892         Model child = makeBaseModel( "child" );
893 
894         Plugin pChild = new Plugin();
895         pChild.setGroupId( gid );
896         pChild.setArtifactId( aid );
897         pChild.setVersion( ver );
898 
899         PluginExecution eChild = new PluginExecution();
900         eChild.setId( "normal" );
901         eChild.addGoal( "run" );
902 
903         pChild.addExecution( eChild );
904 
905         Build bChild = new Build();
906         bChild.addPlugin( pChild );
907 
908         child.setBuild( bChild );
909 
910         Model parent = makeBaseModel( "parent" );
911 
912         Plugin pParent = new Plugin();
913         pParent.setGroupId( gid );
914         pParent.setArtifactId( aid );
915         pParent.setVersion( ver );
916 
917         pParent.setInherited( Boolean.toString( true ) );
918 
919         PluginExecution eParent = new PluginExecution();
920         eParent.setId( testId );
921         eParent.addGoal( "test" );
922         eParent.setInherited( Boolean.toString( false ) );
923 
924         pParent.addExecution( eParent );
925 
926         Build bParent = new Build();
927         bParent.addPlugin( pParent );
928 
929         parent.setBuild( bParent );
930 
931         assembler.assembleModelInheritance( child, parent );
932 
933         Map pluginMap = bChild.getPluginsAsMap();
934         assertNotNull( pluginMap );
935 
936         Plugin plugin = (Plugin) pluginMap.get( gid + ":" + aid );
937         assertNotNull( plugin );
938 
939         Map executionMap = plugin.getExecutionsAsMap();
940         assertNotNull( executionMap );
941 
942         assertNull( "test execution with inherited == false should NOT be inherited to child model.", executionMap.get( testId ) );
943     }
944 
945     public void testPluginExecInheritanceWhereExecInheritedSetToFalseAndPluginInheritedNotSet()
946     {
947         String testId = "test";
948         String gid = "group";
949         String aid = "artifact";
950         String ver = "1";
951 
952         Model child = makeBaseModel( "child" );
953 
954         Plugin pChild = new Plugin();
955         pChild.setGroupId( gid );
956         pChild.setArtifactId( aid );
957         pChild.setVersion( ver );
958 
959         PluginExecution eChild = new PluginExecution();
960         eChild.setId( "normal" );
961         eChild.addGoal( "run" );
962 
963         pChild.addExecution( eChild );
964 
965         Build bChild = new Build();
966         bChild.addPlugin( pChild );
967 
968         child.setBuild( bChild );
969 
970         Model parent = makeBaseModel( "parent" );
971 
972         Plugin pParent = new Plugin();
973         pParent.setGroupId( gid );
974         pParent.setArtifactId( aid );
975         pParent.setVersion( ver );
976 
977         PluginExecution eParent = new PluginExecution();
978         eParent.setId( testId );
979         eParent.addGoal( "test" );
980         eParent.setInherited( Boolean.toString( false ) );
981 
982         pParent.addExecution( eParent );
983 
984         Build bParent = new Build();
985         bParent.addPlugin( pParent );
986 
987         parent.setBuild( bParent );
988 
989         assembler.assembleModelInheritance( child, parent );
990 
991         Map pluginMap = bChild.getPluginsAsMap();
992         assertNotNull( pluginMap );
993 
994         Plugin plugin = (Plugin) pluginMap.get( gid + ":" + aid );
995         assertNotNull( plugin );
996 
997         Map executionMap = plugin.getExecutionsAsMap();
998         assertNotNull( executionMap );
999 
1000         assertNull( "test execution with inherited == false should NOT be inherited to child model.", executionMap.get( testId ) );
1001     }
1002 
1003     private void assertReports( List expectedPlugins, Model child )
1004     {
1005         Reporting childBuild = child.getReporting();
1006 
1007         if ( ( expectedPlugins != null ) && !expectedPlugins.isEmpty() )
1008         {
1009             assertNotNull( childBuild );
1010 
1011             Map childPluginsMap = childBuild.getReportPluginsAsMap();
1012 
1013             if ( childPluginsMap != null )
1014             {
1015                 assertEquals( expectedPlugins.size(), childPluginsMap.size() );
1016 
1017                 for ( Iterator it = expectedPlugins.iterator(); it.hasNext(); )
1018                 {
1019                     ReportPlugin expectedPlugin = (ReportPlugin) it.next();
1020 
1021                     ReportPlugin childPlugin = (ReportPlugin) childPluginsMap.get( expectedPlugin.getKey() );
1022 
1023                     assertReportsEqual( expectedPlugin, childPlugin );
1024                 }
1025             }
1026             else
1027             {
1028                 fail( "child plugins collection is null, but expectations map is not." );
1029             }
1030         }
1031         else
1032         {
1033             assertTrue( ( childBuild == null ) || ( childBuild.getPlugins() == null ) || childBuild.getPlugins().isEmpty() );
1034         }
1035     }
1036 
1037     private void assertReportsEqual( ReportPlugin reference, ReportPlugin test )
1038     {
1039         assertEquals( "Plugin keys don't match", reference.getKey(), test.getKey() );
1040         assertEquals( "Plugin configurations don't match", reference.getConfiguration(), test.getConfiguration() );
1041 
1042         List referenceReportSets = reference.getReportSets();
1043         Map testReportSetsMap = test.getReportSetsAsMap();
1044 
1045         if ( ( referenceReportSets != null ) && !referenceReportSets.isEmpty() )
1046         {
1047             assertTrue( "Missing goals specification", ( ( testReportSetsMap != null ) && !testReportSetsMap.isEmpty() ) );
1048 
1049             for ( Iterator it = referenceReportSets.iterator(); it.hasNext(); )
1050             {
1051                 ReportSet referenceReportSet = (ReportSet) it.next();
1052                 ReportSet testReportSet = (ReportSet) testReportSetsMap.get( referenceReportSet.getId() );
1053 
1054                 assertNotNull( "Goal from reference not found in test", testReportSet );
1055 
1056                 assertEquals( "Goal IDs don't match", referenceReportSet.getId(), testReportSet.getId() );
1057                 assertEquals( "Goal configurations don't match", referenceReportSet.getConfiguration(),
1058                               testReportSet.getConfiguration() );
1059                 assertEquals( "Reports don't match", referenceReportSet.getReports(), testReportSet.getReports() );
1060             }
1061         }
1062         else
1063         {
1064             assertTrue( "Unexpected goals specification",
1065                         ( ( testReportSetsMap == null ) || testReportSetsMap.isEmpty() ) );
1066         }
1067     }
1068 
1069     // ----------------------------------------------------------------------
1070     //
1071     // ----------------------------------------------------------------------
1072 
1073     private void assertConnection( String expectedConnection, String expectedDeveloperConnection, Model model )
1074     {
1075         String connection = model.getScm().getConnection();
1076 
1077         assertNotNull( connection );
1078 
1079         assertEquals( expectedConnection, connection );
1080 
1081         String developerConnection = model.getScm().getDeveloperConnection();
1082 
1083         assertNotNull( developerConnection );
1084 
1085         assertEquals( expectedDeveloperConnection, developerConnection );
1086     }
1087 
1088     public void assertScm( String connection, String developerConnection, String url, Scm scm )
1089     {
1090         assertNotNull( scm );
1091 
1092         assertEquals( connection, scm.getConnection() );
1093 
1094         assertEquals( developerConnection, scm.getDeveloperConnection() );
1095 
1096         assertEquals( url, scm.getUrl() );
1097     }
1098 
1099     private static Model makeScmModel( String artifactId )
1100     {
1101         return makeScmModel( artifactId, null, null, null );
1102     }
1103 
1104     private static Model makeScmModel( String artifactId, String connection, String developerConnection, String url )
1105     {
1106         Model model = makeBaseModel( artifactId );
1107 
1108         if ( ( connection != null ) || ( developerConnection != null ) || ( url != null ) )
1109         {
1110             Scm scm = new Scm();
1111 
1112             scm.setConnection( connection );
1113 
1114             scm.setDeveloperConnection( developerConnection );
1115 
1116             scm.setUrl( url );
1117 
1118             model.setScm( scm );
1119         }
1120 
1121         return model;
1122     }
1123 
1124     private static Model makeBaseModel( String artifactId )
1125     {
1126         Model model = new Model();
1127 
1128         model.setModelVersion( "4.0.0" );
1129 
1130         model.setGroupId( "maven" );
1131 
1132         model.setArtifactId( artifactId );
1133 
1134         model.setVersion( "1.0" );
1135         return model;
1136     }
1137 
1138     private static Model makeRepositoryModel( String artifactId, String id, String url )
1139     {
1140         Model model = makeBaseModel( artifactId );
1141 
1142         Repository repository = makeRepository( id, url );
1143 
1144         model.setRepositories( new ArrayList( Collections.singletonList( repository ) ) );
1145 
1146         return model;
1147     }
1148 
1149     private static Repository makeRepository( String id, String url )
1150     {
1151         Repository repository = new Repository();
1152         repository.setId( id );
1153         repository.setUrl( url );
1154         return repository;
1155     }
1156 
1157     private void assertRepositories( List expected, List actual )
1158     {
1159         assertEquals( "Repository list sizes don't match", expected.size(), actual.size() );
1160 
1161         for ( Iterator i = expected.iterator(); i.hasNext(); )
1162         {
1163             Repository expectedRepository = (Repository) i.next();
1164             boolean found = false;
1165             for ( Iterator j = actual.iterator(); j.hasNext() && !found; )
1166             {
1167                 Repository actualRepository = (Repository) j.next();
1168 
1169                 if ( actualRepository.getId().equals( expectedRepository.getId() ) )
1170                 {
1171                     assertEquals( "Repository URLs don't match", expectedRepository.getUrl(),
1172                                   actualRepository.getUrl() );
1173                     found = true;
1174                 }
1175             }
1176             assertTrue( "Repository with ID " + expectedRepository.getId() + " not found", found );
1177         }
1178     }
1179 
1180 }