KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > project > inheritance > t02 > ProjectInheritanceTest


1 package org.apache.maven.project.inheritance.t02;
2
3 /*
4  * Copyright 2001-2005 The Apache Software 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 java.io.File JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.maven.model.Build;
27 import org.apache.maven.model.MailingList;
28 import org.apache.maven.model.Plugin;
29 import org.apache.maven.project.MavenProject;
30 import org.apache.maven.project.inheritance.AbstractProjectInheritanceTestCase;
31
32 /**
33  * A test which demonstrates maven's recursive inheritance where
34  * a distinct value is taken from each parent contributing to the
35  * the final model of the project being assembled. There is no
36  * overriding going on amongst the models being used in this test:
37  * each model in the lineage is providing a value that is not present
38  * anywhere else in the lineage. We are just making sure that values
39  * down in the lineage are bubbling up where they should.
40  *
41  * @author <a HREF="mailto:jason@maven.org">Jason van Zyl</a>
42  * @version $Id: ProjectInheritanceTest.java 393058 2006-04-10 20:47:28Z jdcasey $
43  */

44 public class ProjectInheritanceTest
45     extends AbstractProjectInheritanceTestCase
46 {
47     // ----------------------------------------------------------------------
48
//
49
// p4 inherits from p3
50
// p3 inherits from p2
51
// p2 inherits from p1
52
// p1 inherits from p0
53
// p0 inhertis from super model
54
//
55
// or we can show it graphically as:
56
//
57
// p4 ---> p3 ---> p2 ---> p1 ---> p0 --> super model
58
//
59
// ----------------------------------------------------------------------
60

61     public void testProjectInheritance()
62         throws Exception JavaDoc
63     {
64         File JavaDoc localRepo = getLocalRepositoryPath();
65         
66         System.out.println( "Local repository is at: " + localRepo.getAbsolutePath() );
67         
68         File JavaDoc pom0 = new File JavaDoc( localRepo, "p0/pom.xml" );
69         File JavaDoc pom1 = new File JavaDoc( pom0.getParentFile(), "p1/pom.xml" );
70         File JavaDoc pom2 = new File JavaDoc( pom1.getParentFile(), "p2/pom.xml" );
71         File JavaDoc pom3 = new File JavaDoc( pom2.getParentFile(), "p3/pom.xml" );
72         File JavaDoc pom4 = new File JavaDoc( pom3.getParentFile(), "p4/pom.xml" );
73         File JavaDoc pom5 = new File JavaDoc( pom4.getParentFile(), "p5/pom.xml" );
74         
75         System.out.println( "Location of project-4's POM: " + pom4.getPath() );
76
77         // load everything...
78
MavenProject project0 = getProject( pom0 );
79         MavenProject project1 = getProject( pom1 );
80         MavenProject project2 = getProject( pom2 );
81         MavenProject project3 = getProject( pom3 );
82         MavenProject project4 = getProject( pom4 );
83         MavenProject project5 = getProject( pom5 );
84
85         assertEquals( "p4", project4.getName() );
86
87         // ----------------------------------------------------------------------
88
// Value inherited from p3
89
// ----------------------------------------------------------------------
90

91         assertEquals( "2000", project4.getInceptionYear() );
92
93         // ----------------------------------------------------------------------
94
// Value taken from p2
95
// ----------------------------------------------------------------------
96

97         assertEquals( "mailing-list", ( (MailingList) project4.getMailingLists().get( 0 ) ).getName() );
98
99         // ----------------------------------------------------------------------
100
// Value taken from p1
101
// ----------------------------------------------------------------------
102

103         assertEquals( "scm-url/p2/p3/p4", project4.getScm().getUrl() );
104
105         // ----------------------------------------------------------------------
106
// Value taken from p4
107
// ----------------------------------------------------------------------
108

109         assertEquals( "Codehaus", project4.getOrganization().getName() );
110
111         // ----------------------------------------------------------------------
112
// Value taken from super model
113
// ----------------------------------------------------------------------
114

115         assertEquals( "4.0.0", project4.getModelVersion() );
116         
117         Build build = project4.getBuild();
118         List JavaDoc plugins = build.getPlugins();
119         
120         Map JavaDoc validPluginCounts = new HashMap JavaDoc();
121         
122         String JavaDoc testPluginArtifactId = "maven-compiler-plugin";
123         
124         // this is the plugin we're looking for.
125
validPluginCounts.put( testPluginArtifactId, new Integer JavaDoc( 0 ) );
126         
127         // these are injected if -DperformRelease=true
128
validPluginCounts.put( "maven-deploy-plugin", new Integer JavaDoc( 0 ) );
129         validPluginCounts.put( "maven-javadoc-plugin", new Integer JavaDoc( 0 ) );
130         validPluginCounts.put( "maven-source-plugin", new Integer JavaDoc( 0 ) );
131         
132         Plugin testPlugin = null;
133         
134         for ( Iterator JavaDoc it = plugins.iterator(); it.hasNext(); )
135         {
136             Plugin plugin = (Plugin) it.next();
137             
138             String JavaDoc pluginArtifactId = plugin.getArtifactId();
139             
140             if ( !validPluginCounts.containsKey( pluginArtifactId ) )
141             {
142                 fail( "Illegal plugin found: " + pluginArtifactId );
143             }
144             else
145             {
146                 if ( pluginArtifactId.equals( testPluginArtifactId ) )
147                 {
148                     testPlugin = plugin;
149                 }
150                 
151                 Integer JavaDoc count = (Integer JavaDoc) validPluginCounts.get( pluginArtifactId );
152                 
153                 if ( count.intValue() > 0 )
154                 {
155                     fail( "Multiple copies of plugin: " + pluginArtifactId + " found in POM." );
156                 }
157                 else
158                 {
159                     count = new Integer JavaDoc( count.intValue() + 1 );
160                     
161                     validPluginCounts.put( pluginArtifactId, count );
162                 }
163             }
164         }
165         
166         List JavaDoc executions = testPlugin.getExecutions();
167         
168         assertEquals( 1, executions.size() );
169     }
170 }
171
Popular Tags