1 package org.apache.maven.project.inheritance.t02;
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 java.io.File;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.apache.maven.model.Build;
29 import org.apache.maven.model.MailingList;
30 import org.apache.maven.model.Plugin;
31 import org.apache.maven.project.MavenProject;
32 import org.apache.maven.project.inheritance.AbstractProjectInheritanceTestCase;
33
34 /**
35 * A test which demonstrates maven's recursive inheritance where
36 * a distinct value is taken from each parent contributing to the
37 * the final model of the project being assembled. There is no
38 * overriding going on amongst the models being used in this test:
39 * each model in the lineage is providing a value that is not present
40 * anywhere else in the lineage. We are just making sure that values
41 * down in the lineage are bubbling up where they should.
42 *
43 * @author Jason van Zyl
44 * @version $Id: ProjectInheritanceTest.java 770390 2009-04-30 18:49:42Z jvanzyl $
45 */
46 public class ProjectInheritanceTest
47 extends AbstractProjectInheritanceTestCase
48 {
49 // ----------------------------------------------------------------------
50 //
51 // p4 inherits from p3
52 // p3 inherits from p2
53 // p2 inherits from p1
54 // p1 inherits from p0
55 // p0 inhertis from super model
56 //
57 // or we can show it graphically as:
58 //
59 // p4 ---> p3 ---> p2 ---> p1 ---> p0 --> super model
60 //
61 // ----------------------------------------------------------------------
62
63 public void testProjectInheritance()
64 throws Exception
65 {
66 File localRepo = getLocalRepositoryPath();
67
68 System.out.println( "Local repository is at: " + localRepo.getAbsolutePath() );
69
70 File pom0 = new File( localRepo, "p0/pom.xml" );
71 File pom1 = new File( pom0.getParentFile(), "p1/pom.xml" );
72 File pom2 = new File( pom1.getParentFile(), "p2/pom.xml" );
73 File pom3 = new File( pom2.getParentFile(), "p3/pom.xml" );
74 File pom4 = new File( pom3.getParentFile(), "p4/pom.xml" );
75 File pom5 = new File( pom4.getParentFile(), "p5/pom.xml" );
76
77 System.out.println( "Location of project-4's POM: " + pom4.getPath() );
78
79 // load everything...
80 MavenProject project0 = getProject( pom0 );
81 MavenProject project1 = getProject( pom1 );
82 MavenProject project2 = getProject( pom2 );
83 MavenProject project3 = getProject( pom3 );
84 MavenProject project4 = getProject( pom4 );
85 MavenProject project5 = getProject( pom5 );
86
87 assertEquals( "p4", project4.getName() );
88
89 // ----------------------------------------------------------------------
90 // Value inherited from p3
91 // ----------------------------------------------------------------------
92
93 assertEquals( "2000", project4.getInceptionYear() );
94
95 // ----------------------------------------------------------------------
96 // Value taken from p2
97 // ----------------------------------------------------------------------
98
99 assertEquals( "mailing-list", ( (MailingList) project4.getMailingLists().get( 0 ) ).getName() );
100
101 // ----------------------------------------------------------------------
102 // Value taken from p1
103 // ----------------------------------------------------------------------
104
105 assertEquals( "scm-url/p2/p3/p4", project4.getScm().getUrl() );
106
107 // ----------------------------------------------------------------------
108 // Value taken from p4
109 // ----------------------------------------------------------------------
110
111 assertEquals( "Codehaus", project4.getOrganization().getName() );
112
113 // ----------------------------------------------------------------------
114 // Value taken from super model
115 // ----------------------------------------------------------------------
116
117 assertEquals( "4.0.0", project4.getModelVersion() );
118
119 Build build = project4.getBuild();
120 List plugins = build.getPlugins();
121
122 Map validPluginCounts = new HashMap();
123
124 String testPluginArtifactId = "maven-compiler-plugin";
125
126 // this is the plugin we're looking for.
127 validPluginCounts.put( testPluginArtifactId, new Integer( 0 ) );
128
129 // these are injected if -DperformRelease=true
130 validPluginCounts.put( "maven-deploy-plugin", new Integer( 0 ) );
131 validPluginCounts.put( "maven-javadoc-plugin", new Integer( 0 ) );
132 validPluginCounts.put( "maven-source-plugin", new Integer( 0 ) );
133
134 Plugin testPlugin = null;
135
136 for ( Iterator it = plugins.iterator(); it.hasNext(); )
137 {
138 Plugin plugin = (Plugin) it.next();
139
140 String pluginArtifactId = plugin.getArtifactId();
141
142 if ( !validPluginCounts.containsKey( pluginArtifactId ) )
143 {
144 fail( "Illegal plugin found: " + pluginArtifactId );
145 }
146 else
147 {
148 if ( pluginArtifactId.equals( testPluginArtifactId ) )
149 {
150 testPlugin = plugin;
151 }
152
153 Integer count = (Integer) validPluginCounts.get( pluginArtifactId );
154
155 if ( count.intValue() > 0 )
156 {
157 fail( "Multiple copies of plugin: " + pluginArtifactId + " found in POM." );
158 }
159 else
160 {
161 count = new Integer( count.intValue() + 1 );
162
163 validPluginCounts.put( pluginArtifactId, count );
164 }
165 }
166 }
167
168 List executions = testPlugin.getExecutions();
169
170 assertEquals( 1, executions.size() );
171 }
172 }