1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugin.eclipse;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.net.URL;
24 import java.util.Collections;
25
26 import junit.framework.Assert;
27 import junit.framework.TestCase;
28
29 import org.apache.maven.artifact.Artifact;
30 import org.apache.maven.artifact.repository.ArtifactRepository;
31 import org.apache.maven.model.Model;
32 import org.apache.maven.plugin.MojoExecutionException;
33 import org.apache.maven.plugin.MojoFailureException;
34 import org.apache.maven.plugin.logging.Log;
35 import org.apache.maven.plugin.logging.SystemStreamLog;
36 import org.apache.maven.project.MavenProject;
37 import org.apache.maven.project.MavenProjectBuilder;
38 import org.apache.maven.project.ProjectBuildingException;
39 import org.apache.maven.shared.tools.easymock.MockManager;
40 import org.apache.maven.shared.tools.easymock.TestFileManager;
41 import org.codehaus.plexus.archiver.manager.ArchiverManager;
42 import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
43 import org.codehaus.plexus.archiver.zip.ZipUnArchiver;
44 import org.codehaus.plexus.components.interactivity.InputHandler;
45 import org.easymock.MockControl;
46
47 public class InstallPluginsMojoTest
48 extends TestCase
49 {
50
51 private static final String GROUP_ID = "org.codehaus.m2eclipse";
52
53 private static final String ARTIFACT_ID = "org.maven.ide.eclipse";
54
55 private static final String VERSION = "0.0.9";
56
57 private static final String SOURCE_PATH =
58 "/org/codehaus/m2eclipse/" + ARTIFACT_ID + "/" + VERSION + "/" + ARTIFACT_ID + "-" + VERSION + ".jar";
59
60 private File sourceFile;
61
62 private TestFileManager fileManager;
63
64 private MockManager mm = new MockManager();
65
66 private File eclipseDir;
67
68 private File pluginsDir;
69
70 public void testShouldInstallAsJarWhenPropertyNotSpecified()
71 throws MojoExecutionException, MojoFailureException
72 {
73 File pluginsDir = performTestInstall( null, false, "eclipse-plugin", "eclipse-plugin" );
74
75 File installedFile = new File( pluginsDir, ARTIFACT_ID + "_" + VERSION + ".jar" );
76
77 File installedDir = new File( pluginsDir, ARTIFACT_ID + "_" + VERSION );
78
79 assertTrue( installedFile + " should exist.", installedFile.exists() );
80 assertFalse( installedDir + " should not exist.", installedDir.exists() );
81
82 mm.verifyAll();
83 }
84
85 public void testShouldInstallAsJarWhenPropertyIsTrue()
86 throws MojoExecutionException, MojoFailureException
87 {
88 File pluginsDir = performTestInstall( Boolean.TRUE, false, "eclipse-plugin", "eclipse-plugin" );
89
90 File installedFile = new File( pluginsDir, ARTIFACT_ID + "_" + VERSION + ".jar" );
91
92 File installedDir = new File( pluginsDir, ARTIFACT_ID + "_" + VERSION );
93
94 assertTrue( installedFile + " should exist.", installedFile.exists() );
95 assertFalse( installedDir + " should not exist.", installedDir.exists() );
96
97 mm.verifyAll();
98 }
99
100 public void testShouldInstallAsDirWhenPropertyIsFalse()
101 throws MojoExecutionException, MojoFailureException
102 {
103 File pluginsDir = performTestInstall( Boolean.FALSE, false, "eclipse-plugin", "eclipse-plugin" );
104
105 File installedFile = new File( pluginsDir, ARTIFACT_ID + "_" + VERSION + ".jar" );
106
107 File installedDir = new File( pluginsDir, ARTIFACT_ID + "_" + VERSION );
108
109 assertFalse( installedFile + " should not exist.", installedFile.exists() );
110 assertTrue( installedDir + " should exist.", installedDir.exists() );
111
112 mm.verifyAll();
113 }
114
115 public void testShouldInstallWhenTypeContainedInPluginTypesListWithMultipleValues()
116 throws MojoExecutionException, MojoFailureException
117 {
118 File pluginsDir = performTestInstall( null, false, "eclipse-plugin", "osgi-bundle,eclipse-plugin" );
119
120 File installedFile = new File( pluginsDir, ARTIFACT_ID + "_" + VERSION + ".jar" );
121
122 File installedDir = new File( pluginsDir, ARTIFACT_ID + "_" + VERSION );
123
124 assertTrue( installedFile + " should exist.", installedFile.exists() );
125 assertFalse( installedDir + " should not exist.", installedDir.exists() );
126
127 mm.verifyAll();
128 }
129
130 public void testShouldNotInstallWhenTypeNotContainedInPluginTypesList()
131 throws MojoExecutionException, MojoFailureException
132 {
133 File pluginsDir = performTestInstall( null, false, "jar", "eclipse-plugin" );
134
135 File installedFile = new File( pluginsDir, ARTIFACT_ID + "_" + VERSION + ".jar" );
136
137 File installedDir = new File( pluginsDir, ARTIFACT_ID + "_" + VERSION );
138
139 assertFalse( installedFile + " should not exist.", installedFile.exists() );
140 assertFalse( installedDir + " should not exist.", installedDir.exists() );
141
142 mm.verifyAll();
143 }
144
145 public void testShouldRemoveOldDirectoryBeforeInstallingNewJarWhenOverwriteIsFalse()
146 throws MojoExecutionException, MojoFailureException
147 {
148 createPluginsDir();
149
150 File installedDir = new File( pluginsDir, ARTIFACT_ID + "_" + VERSION );
151
152 installedDir.mkdir();
153
154 assertTrue( installedDir + " should have been created prior to running the test.", installedDir.exists() );
155
156 performTestInstall( Boolean.FALSE, true, "eclipse-plugin", "eclipse-plugin" );
157
158 File installedFile = new File( pluginsDir, ARTIFACT_ID + "_" + VERSION + ".jar" );
159
160 assertFalse( installedFile + " should not exist.", installedFile.exists() );
161 assertTrue( installedDir + " should still exist.", installedDir.exists() );
162
163 mm.verifyAll();
164 }
165
166 public void testShouldRemoveOldDirectoryBeforeInstallingNewJarWhenOverwriteIsTrue()
167 throws MojoExecutionException, MojoFailureException
168 {
169 createPluginsDir();
170
171 File installedDir = new File( pluginsDir, ARTIFACT_ID + "_" + VERSION );
172
173 installedDir.mkdir();
174
175 assertTrue( installedDir + " should have been created prior to running the test.", installedDir.exists() );
176
177 performTestInstall( null, true, "eclipse-plugin", "eclipse-plugin" );
178
179 File installedFile = new File( pluginsDir, ARTIFACT_ID + "_" + VERSION + ".jar" );
180
181 assertTrue( installedFile + " should exist.", installedFile.exists() );
182 assertFalse( installedDir + " should not exist.", installedDir.exists() );
183
184 mm.verifyAll();
185 }
186
187 public void setUp()
188 {
189 fileManager = new TestFileManager( "InstallPluginsMojo.test.", "" );
190
191 URL resource = null;
192
193 resource = Thread.currentThread().getContextClassLoader().getResource( "m2repo" + SOURCE_PATH );
194
195 if ( resource == null )
196 {
197 resource = Thread.currentThread().getContextClassLoader().getResource( "M2REPO" + SOURCE_PATH );
198 }
199
200 if ( resource == null )
201 {
202 throw new IllegalStateException( "Cannot find test source jar: (m2repo|M2REPO)" + SOURCE_PATH +
203 " in context classloader!" );
204 }
205
206 sourceFile = new File( resource.getPath() );
207 }
208
209 public void tearDown()
210 throws IOException
211 {
212 fileManager.cleanUp();
213 }
214
215
216 private File performTestInstall( Boolean installAsJar, boolean overwrite, String type, String typeList )
217 throws MojoExecutionException, MojoFailureException
218 {
219 createPluginsDir();
220
221 Artifact dep = createDependencyArtifact( type );
222
223 ArtifactRepository localRepo = createLocalRepository();
224 MavenProjectBuilder projectBuilder = createProjectBuilder( typeList.indexOf( type ) > -1, installAsJar );
225 ArchiverManager archiverManager = createArchiverManager( typeList.indexOf( type ) > -1, installAsJar );
226 InputHandler inputHandler = createInputHandler();
227
228 Log log = new SystemStreamLog();
229
230 mm.replayAll();
231
232 InstallPluginsMojo mojo =
233 new InstallPluginsMojo( eclipseDir, overwrite, Collections.singletonList( dep ), typeList, localRepo,
234 projectBuilder, archiverManager, inputHandler, log );
235
236 mojo.execute();
237
238 return pluginsDir;
239 }
240
241 private File createPluginsDir()
242 {
243 if ( eclipseDir == null )
244 {
245 eclipseDir = fileManager.createTempDir();
246 }
247
248 if ( pluginsDir == null )
249 {
250 pluginsDir = new File( eclipseDir, "plugins" );
251
252 pluginsDir.mkdir();
253 }
254
255 return pluginsDir;
256 }
257
258 private InputHandler createInputHandler()
259 {
260 MockControl control = MockControl.createControl( InputHandler.class );
261
262 mm.add( control );
263
264 InputHandler handler = (InputHandler) control.getMock();
265
266 return handler;
267 }
268
269 private ArchiverManager createArchiverManager( boolean isReachable, Boolean installAsJar )
270 {
271 MockControl control = MockControl.createControl( ArchiverManager.class );
272
273 mm.add( control );
274
275 ArchiverManager manager = (ArchiverManager) control.getMock();
276
277 if ( isReachable && installAsJar == Boolean.FALSE )
278 {
279 try
280 {
281 manager.getUnArchiver( (File) null );
282 control.setMatcher( MockControl.ALWAYS_MATCHER );
283 control.setReturnValue( new ZipUnArchiver(), MockControl.ONE_OR_MORE );
284 }
285 catch ( NoSuchArchiverException e )
286 {
287 Assert.fail( "Should never happen." );
288 }
289 }
290
291 return manager;
292 }
293
294 private MavenProjectBuilder createProjectBuilder( boolean expectBuildFromRepository, Boolean installAsJar )
295 {
296 MockControl control = MockControl.createControl( MavenProjectBuilder.class );
297
298 mm.add( control );
299
300 MavenProjectBuilder projectBuilder = (MavenProjectBuilder) control.getMock();
301
302 if ( expectBuildFromRepository )
303 {
304 try
305 {
306 Model model = new Model();
307
308 if ( installAsJar != null )
309 {
310 model.addProperty( InstallPluginsMojo.PROP_UNPACK_PLUGIN, "" + ( !installAsJar.booleanValue() ) );
311 }
312
313 MavenProject project = new MavenProject( model );
314
315 projectBuilder.buildFromRepository( null, null, null, true );
316 control.setMatcher( MockControl.ALWAYS_MATCHER );
317 control.setReturnValue( project, MockControl.ONE_OR_MORE );
318 }
319 catch ( ProjectBuildingException e )
320 {
321 Assert.fail( "should never happen." );
322 }
323 }
324
325 return projectBuilder;
326 }
327
328 private ArtifactRepository createLocalRepository()
329 {
330 MockControl control = MockControl.createControl( ArtifactRepository.class );
331
332 mm.add( control );
333
334 ArtifactRepository repo = (ArtifactRepository) control.getMock();
335
336 return repo;
337 }
338
339 private Artifact createDependencyArtifact( String type )
340 {
341 MockControl control = MockControl.createControl( Artifact.class );
342
343 mm.add( control );
344
345 Artifact artifact = (Artifact) control.getMock();
346
347 artifact.getFile();
348 control.setReturnValue( sourceFile, MockControl.ZERO_OR_MORE );
349
350 artifact.getArtifactId();
351 control.setReturnValue( ARTIFACT_ID, MockControl.ZERO_OR_MORE );
352
353 artifact.getVersion();
354 control.setReturnValue( VERSION, MockControl.ZERO_OR_MORE );
355
356 artifact.getType();
357 control.setReturnValue( type, MockControl.ZERO_OR_MORE );
358
359 artifact.getId();
360 control.setReturnValue( GROUP_ID + ":" + ARTIFACT_ID + ":" + type + ":" + VERSION, MockControl.ZERO_OR_MORE );
361
362 return artifact;
363 }
364
365 }