1 package org.apache.maven.plugins.install;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.Reader;
24
25 import org.apache.maven.execution.MavenSession;
26 import org.apache.maven.model.Model;
27 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
28 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
29 import org.apache.maven.project.DefaultProjectBuildingRequest;
30 import org.apache.maven.project.ProjectBuildingRequest;
31 import org.codehaus.plexus.util.FileUtils;
32 import org.codehaus.plexus.util.xml.XmlStreamReader;
33 import org.eclipse.aether.DefaultRepositorySystemSession;
34 import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory;
35 import org.eclipse.aether.repository.LocalRepository;
36 import org.eclipse.aether.repository.NoLocalRepositoryManagerException;
37
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.when;
40
41
42
43
44 public class InstallFileMojoTest
45 extends AbstractMojoTestCase
46 {
47 private String groupId;
48
49 private String artifactId;
50
51 private String version;
52
53 private String packaging;
54
55 private String classifier;
56
57 private File file;
58
59 private final String LOCAL_REPO = "target/local-repo/";
60
61 public void setUp()
62 throws Exception
63 {
64 super.setUp();
65
66 FileUtils.deleteDirectory( new File( getBasedir() + "/" + LOCAL_REPO ) );
67 }
68
69 public void testInstallFileTestEnvironment()
70 throws Exception
71 {
72 File testPom = new File( getBasedir(), "target/test-classes/unit/install-file-basic-test/plugin-config.xml" );
73
74 InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
75
76 setVariableValueToObject( mojo, "session", createMavenSession() );
77
78 assertNotNull( mojo );
79 }
80
81 public void testBasicInstallFile()
82 throws Exception
83 {
84 File testPom = new File( getBasedir(), "target/test-classes/unit/install-file-basic-test/plugin-config.xml" );
85
86 InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
87
88 assertNotNull( mojo );
89
90 setVariableValueToObject( mojo, "session", createMavenSession() );
91
92 assignValuesForParameter( mojo );
93
94 mojo.execute();
95
96 File installedArtifact = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
97 artifactId + "-" + version + "." + packaging );
98
99 assertTrue( installedArtifact.exists() );
100
101 assertEquals( 5, FileUtils.getFiles( new File( LOCAL_REPO ), null, null ).size() );
102 }
103
104 public void testInstallFileWithClassifier()
105 throws Exception
106 {
107 File testPom =
108 new File( getBasedir(), "target/test-classes/unit/install-file-with-classifier/plugin-config.xml" );
109
110 InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
111
112 assertNotNull( mojo );
113
114 setVariableValueToObject( mojo, "session", createMavenSession() );
115
116 assignValuesForParameter( mojo );
117
118 assertNotNull( classifier );
119
120 mojo.execute();
121
122 File installedArtifact = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
123 artifactId + "-" + version + "-" + classifier + "." + packaging );
124
125 assertTrue( installedArtifact.exists() );
126
127 assertEquals( 5, FileUtils.getFiles( new File( LOCAL_REPO ), null, null ).size() );
128 }
129
130 public void testInstallFileWithGeneratePom()
131 throws Exception
132 {
133 File testPom =
134 new File( getBasedir(), "target/test-classes/unit/install-file-test-generatePom/plugin-config.xml" );
135
136 InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
137
138 assertNotNull( mojo );
139
140 setVariableValueToObject( mojo, "session", createMavenSession() );
141
142 assignValuesForParameter( mojo );
143
144 mojo.execute();
145
146 File installedArtifact = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
147 artifactId + "-" + version + "." + packaging );
148
149 assertTrue( (Boolean) getVariableValueFromObject( mojo, "generatePom" ) );
150
151 assertTrue( installedArtifact.exists() );
152
153 File installedPom = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
154 artifactId + "-" + version + "." + "pom" );
155
156 try ( Reader reader = new XmlStreamReader( installedPom ) ) {
157 Model model = new MavenXpp3Reader().read( reader );
158
159 assertEquals( "4.0.0", model.getModelVersion() );
160
161 assertEquals( (String) getVariableValueFromObject( mojo, "groupId" ), model.getGroupId() );
162
163 assertEquals( artifactId, model.getArtifactId() );
164
165 assertEquals( version, model.getVersion() );
166 }
167
168 assertEquals( 5, FileUtils.getFiles( new File( LOCAL_REPO ), null, null ).size() );
169 }
170
171 public void testInstallFileWithPomFile()
172 throws Exception
173 {
174 File testPom =
175 new File( getBasedir(), "target/test-classes/unit/install-file-with-pomFile-test/plugin-config.xml" );
176
177 InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
178
179 assertNotNull( mojo );
180
181 setVariableValueToObject( mojo, "session", createMavenSession() );
182
183 assignValuesForParameter( mojo );
184
185 mojo.execute();
186
187 File pomFile = (File) getVariableValueFromObject( mojo, "pomFile" );
188
189 assertTrue( pomFile.exists() );
190
191 File installedArtifact = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
192 artifactId + "-" + version + "." + packaging );
193
194 assertTrue( installedArtifact.exists() );
195
196 File installedPom = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
197 artifactId + "-" + version + "." + "pom" );
198
199 assertTrue( installedPom.exists() );
200
201 assertEquals( 5, FileUtils.getFiles( new File( LOCAL_REPO ), null, null ).size() );
202 }
203
204 public void testInstallFileWithPomAsPackaging()
205 throws Exception
206 {
207 File testPom = new File( getBasedir(),
208 "target/test-classes/unit/install-file-with-pom-as-packaging/" + "plugin-config.xml" );
209
210 InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
211
212 assertNotNull( mojo );
213
214 setVariableValueToObject( mojo, "session", createMavenSession() );
215
216 assignValuesForParameter( mojo );
217
218 assertTrue( file.exists() );
219
220 assertEquals( "pom", packaging );
221
222 mojo.execute();
223
224 File installedPom = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
225 artifactId + "-" + version + "." + "pom" );
226
227 assertTrue( installedPom.exists() );
228
229 assertEquals( 4, FileUtils.getFiles( new File( LOCAL_REPO ), null, null ).size() );
230 }
231
232 public void testInstallFile()
233 throws Exception
234 {
235 File testPom =
236 new File( getBasedir(), "target/test-classes/unit/install-file-with-checksum/" + "plugin-config.xml" );
237
238 InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
239
240 assertNotNull( mojo );
241
242 setVariableValueToObject( mojo, "session", createMavenSession() );
243
244 assignValuesForParameter( mojo );
245
246 mojo.execute();
247
248 String localPath = getBasedir() + "/" + LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
249 artifactId + "-" + version;
250
251 File installedArtifact = new File( localPath + "." + "jar" );
252
253 assertTrue( installedArtifact.exists() );
254
255 assertEquals( FileUtils.getFiles( new File( LOCAL_REPO ), null, null ).toString(), 5, FileUtils.getFiles( new File( LOCAL_REPO ), null, null ).size() );
256 }
257
258 private void assignValuesForParameter( Object obj )
259 throws Exception
260 {
261 this.groupId = dotToSlashReplacer( (String) getVariableValueFromObject( obj, "groupId" ) );
262
263 this.artifactId = (String) getVariableValueFromObject( obj, "artifactId" );
264
265 this.version = (String) getVariableValueFromObject( obj, "version" );
266
267 this.packaging = (String) getVariableValueFromObject( obj, "packaging" );
268
269 this.classifier = (String) getVariableValueFromObject( obj, "classifier" );
270
271 this.file = (File) getVariableValueFromObject( obj, "file" );
272 }
273
274 private String dotToSlashReplacer( String parameter )
275 {
276 return parameter.replace( '.', '/' );
277 }
278
279 private MavenSession createMavenSession() throws NoLocalRepositoryManagerException
280 {
281 MavenSession session = mock( MavenSession.class );
282 DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
283 repositorySession.setLocalRepositoryManager(
284 new EnhancedLocalRepositoryManagerFactory().newInstance(
285 repositorySession, new LocalRepository( LOCAL_REPO )
286 )
287 );
288 ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
289 buildingRequest.setRepositorySession( repositorySession );
290 when( session.getProjectBuildingRequest() ).thenReturn( buildingRequest );
291 when( session.getRepositorySession() ).thenReturn( repositorySession );
292 return session;
293 }
294 }