1 package org.apache.maven.project;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.artifact.repository.ArtifactRepository;
23 import org.apache.maven.artifact.repository.DefaultArtifactRepository;
24 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
25 import org.apache.maven.model.Build;
26 import org.apache.maven.model.Plugin;
27 import org.apache.maven.model.Profile;
28 import org.apache.maven.model.Repository;
29 import org.apache.maven.model.Resource;
30 import org.apache.maven.profiles.DefaultProfileManager;
31 import org.apache.maven.profiles.ProfileManager;
32 import org.codehaus.plexus.util.FileUtils;
33
34 import java.io.File;
35 import java.util.ArrayList;
36 import java.util.Iterator;
37 import java.util.List;
38 import java.util.Properties;
39
40 public class DefaultMavenProjectBuilderTest
41 extends AbstractMavenProjectTestCase
42 {
43
44 private List filesToDelete = new ArrayList();
45
46 private File localRepoDir;
47
48 public void setUp()
49 throws Exception
50 {
51 super.setUp();
52
53 localRepoDir = new File( System.getProperty( "java.io.tmpdir" ), "local-repo." + System.currentTimeMillis() );
54 localRepoDir.mkdirs();
55
56 filesToDelete.add( localRepoDir );
57 }
58
59 public void tearDown()
60 throws Exception
61 {
62 super.tearDown();
63
64 if ( !filesToDelete.isEmpty() )
65 {
66 for ( Iterator it = filesToDelete.iterator(); it.hasNext(); )
67 {
68 File file = (File) it.next();
69
70 if ( file.exists() )
71 {
72 if ( file.isDirectory() )
73 {
74 FileUtils.deleteDirectory( file );
75 }
76 else
77 {
78 file.delete();
79 }
80 }
81 }
82 }
83 }
84
85 public void testShouldInjectOneProfileToStandaloneSuperPom()
86 throws Exception
87 {
88 ProfileManager pm = new DefaultProfileManager( getContainer(), new Properties() );
89
90 String profileId = "test-profile";
91 String key = "test";
92 String value = "value";
93
94 Profile profile = new Profile();
95 profile.setId( profileId );
96 profile.addProperty( key, value );
97
98 pm.addProfile( profile );
99 pm.explicitlyActivate( profileId );
100
101 MavenProject project = projectBuilder.buildStandaloneSuperProject( getLocalRepository(), pm );
102
103 assertEquals( value, project.getProperties().getProperty( key ) );
104 }
105
106 public void testShouldInjectProfileWithRepositoryToStandaloneSuperPom()
107 throws Exception
108 {
109 ProfileManager pm = new DefaultProfileManager( getContainer(), new Properties() );
110
111 String profileId = "test-profile";
112 String repoId = "test-repo";
113
114 Profile profile = new Profile();
115 profile.setId( profileId );
116
117 Repository repo = new Repository();
118 repo.setId( repoId );
119 repo.setUrl( "http://www.google.com" );
120
121 profile.addRepository( repo );
122
123 pm.addProfile( profile );
124 pm.explicitlyActivate( profileId );
125
126 MavenProject project = projectBuilder.buildStandaloneSuperProject( getLocalRepository(), pm );
127
128 List repositories = project.getRepositories();
129
130 assertNotNull( repositories );
131
132 Repository result = null;
133
134 for ( Iterator it = repositories.iterator(); it.hasNext(); )
135 {
136 Repository candidate = (Repository) it.next();
137
138 if ( repoId.equals( candidate.getId() ) )
139 {
140 result = candidate;
141 break;
142 }
143 }
144
145 assertNotNull( "Profile-injected repository not found in super-POM.", result );
146
147 assertEquals( "Profile-injected repository was not first in repo list within super-POM", repoId,
148 ( (Repository) repositories.get( 0 ) ).getId() );
149 }
150
151
152
153
154
155 public void testBuildFromMiddlePom() throws Exception
156 {
157 File f1 = getTestFile( "src/test/resources/projects/grandchild-check/child/pom.xml");
158 File f2 = getTestFile( "src/test/resources/projects/grandchild-check/child/grandchild/pom.xml");
159
160 getProject( f1 );
161
162
163
164 getProject( f2 );
165 }
166
167 public void testDuplicatePluginDefinitionsMerged()
168 throws Exception
169 {
170 File f1 = getTestFile( "src/test/resources/projects/duplicate-plugins-merged-pom.xml" );
171
172 MavenProject project = getProject( f1 );
173
174 assertEquals( 2, ( (Plugin) project.getBuildPlugins().get( 0 ) ).getDependencies().size() );
175 }
176
177 public void testBuildDirectoryExpressionInterpolatedWithTranslatedValue()
178 throws Exception
179 {
180 File pom = getTestFile( "src/test/resources/projects/build-path-expression-pom.xml" );
181
182 MavenProject project = getProject( pom );
183 projectBuilder.calculateConcreteState( project, new DefaultProjectBuilderConfiguration() );
184
185 Build build = project.getBuild();
186 assertNotNull( "Project should have a build section containing the test resource.", build );
187
188 String sourceDirectory = build.getSourceDirectory();
189 assertNotNull( "Project build should contain a valid source directory.", sourceDirectory );
190
191 List resources = build.getResources();
192 assertNotNull( "Project should contain a build resource.", resources );
193 assertEquals( "Project should contain exactly one build resource.", 1, resources.size() );
194
195 Resource res = (Resource) resources.get( 0 );
196 assertEquals( "Project resource should be the same directory as the source directory.", sourceDirectory, res.getDirectory() );
197
198 System.out.println( "Interpolated, translated resource directory is: " + res.getDirectory() );
199 }
200
201 protected ArtifactRepository getLocalRepository()
202 throws Exception
203 {
204 ArtifactRepositoryLayout repoLayout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE,
205 "legacy" );
206
207 ArtifactRepository r = new DefaultArtifactRepository( "local", "file://" + localRepoDir.getAbsolutePath(),
208 repoLayout );
209
210 return r;
211 }
212 }