1 | |
package org.apache.maven.plugin.testing; |
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.IOException; |
24 | |
import java.lang.reflect.Field; |
25 | |
import java.util.HashSet; |
26 | |
import java.util.Set; |
27 | |
|
28 | |
import org.apache.maven.artifact.Artifact; |
29 | |
import org.apache.maven.artifact.DefaultArtifact; |
30 | |
import org.apache.maven.artifact.handler.ArtifactHandler; |
31 | |
import org.apache.maven.artifact.versioning.VersionRange; |
32 | |
import org.apache.maven.plugin.testing.stubs.DefaultArtifactHandlerStub; |
33 | |
import org.codehaus.plexus.archiver.Archiver; |
34 | |
import org.codehaus.plexus.archiver.ArchiverException; |
35 | |
import org.codehaus.plexus.archiver.manager.ArchiverManager; |
36 | |
import org.codehaus.plexus.archiver.manager.NoSuchArchiverException; |
37 | |
import org.codehaus.plexus.archiver.war.WarArchiver; |
38 | |
import org.codehaus.plexus.util.FileUtils; |
39 | |
import org.codehaus.plexus.util.ReflectionUtils; |
40 | |
import org.codehaus.plexus.util.StringUtils; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
public class ArtifactStubFactory |
53 | |
{ |
54 | |
private File workingDir; |
55 | |
|
56 | |
private boolean createFiles; |
57 | |
|
58 | |
private File srcFile; |
59 | |
|
60 | |
private boolean createUnpackableFile; |
61 | |
|
62 | |
private ArchiverManager archiverManager; |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
public ArtifactStubFactory() |
68 | 4 | { |
69 | 4 | this.workingDir = null; |
70 | 4 | this.createFiles = false; |
71 | 4 | } |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public ArtifactStubFactory( File workingDir, boolean createFiles ) |
80 | 0 | { |
81 | 0 | this.workingDir = new File( workingDir, "localTestRepo" ); |
82 | 0 | this.createFiles = createFiles; |
83 | 0 | } |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
public void setUnpackableFile( ArchiverManager archiverManager ) |
92 | |
{ |
93 | 0 | this.createUnpackableFile = true; |
94 | 0 | this.archiverManager = archiverManager; |
95 | 0 | } |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
public Artifact createArtifact( String groupId, String artifactId, String version ) |
106 | |
throws IOException |
107 | |
{ |
108 | 8 | return createArtifact( groupId, artifactId, version, Artifact.SCOPE_COMPILE, "jar", "" ); |
109 | |
} |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
public Artifact createArtifact( String groupId, String artifactId, String version, String scope ) |
121 | |
throws IOException |
122 | |
{ |
123 | 0 | return createArtifact( groupId, artifactId, version, scope, "jar", "" ); |
124 | |
} |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public Artifact createArtifact( String groupId, String artifactId, String version, String scope, String type, |
138 | |
String classifier ) |
139 | |
throws IOException |
140 | |
{ |
141 | 8 | VersionRange vr = VersionRange.createFromVersion( version ); |
142 | 8 | return createArtifact( groupId, artifactId, vr, scope, type, classifier, false ); |
143 | |
} |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
public Artifact createArtifact( String groupId, String artifactId, VersionRange versionRange, String scope, |
157 | |
String type, String classifier, boolean optional ) |
158 | |
throws IOException |
159 | |
{ |
160 | 8 | ArtifactHandler ah = new DefaultArtifactHandlerStub( type, classifier ); |
161 | |
|
162 | 8 | Artifact artifact = |
163 | |
new DefaultArtifact( groupId, artifactId, versionRange, scope, type, classifier, ah, optional ); |
164 | |
|
165 | |
|
166 | 8 | artifact.setRelease( !artifact.isSnapshot() ); |
167 | |
|
168 | 8 | if ( createFiles ) |
169 | |
{ |
170 | 0 | setArtifactFile( artifact, this.workingDir, this.srcFile, this.createUnpackableFile ); |
171 | |
} |
172 | 8 | return artifact; |
173 | |
} |
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
public void setArtifactFile( Artifact artifact, File workingDir ) |
183 | |
throws IOException |
184 | |
{ |
185 | 0 | setArtifactFile( artifact, workingDir, null, false ); |
186 | 0 | } |
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
public void setArtifactFile( Artifact artifact, File workingDir, File srcFile ) |
198 | |
throws IOException |
199 | |
{ |
200 | 0 | setArtifactFile( artifact, workingDir, srcFile, false ); |
201 | 0 | } |
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
public void setUnpackableArtifactFile( Artifact artifact, File workingDir ) |
211 | |
throws IOException |
212 | |
{ |
213 | 0 | setArtifactFile( artifact, workingDir, null, true ); |
214 | 0 | } |
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
public void setUnpackableArtifactFile( Artifact artifact, File workingDir, File srcFile ) |
226 | |
throws IOException |
227 | |
{ |
228 | 0 | setArtifactFile( artifact, workingDir, srcFile, true ); |
229 | 0 | } |
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
private void setArtifactFile( Artifact artifact, File workingDir, File srcFile, boolean createUnpackableFile ) |
241 | |
throws IOException |
242 | |
{ |
243 | 0 | if ( workingDir == null ) |
244 | |
{ |
245 | 0 | throw new IllegalArgumentException( |
246 | |
"The workingDir must be set." ); |
247 | |
} |
248 | |
|
249 | 0 | String fileName = getFormattedFileName( artifact, false ); |
250 | |
|
251 | 0 | File theFile = new File( workingDir, fileName ); |
252 | 0 | theFile.getParentFile().mkdirs(); |
253 | |
|
254 | 0 | if ( srcFile == null ) |
255 | |
{ |
256 | 0 | theFile.createNewFile(); |
257 | |
} |
258 | 0 | else if ( createUnpackableFile ) |
259 | |
{ |
260 | |
try |
261 | |
{ |
262 | 0 | createUnpackableFile( artifact, theFile ); |
263 | |
} |
264 | 0 | catch ( NoSuchArchiverException e ) |
265 | |
{ |
266 | 0 | throw new IOException( "NoSuchArchiverException: " + e.getMessage() ); |
267 | |
} |
268 | 0 | catch ( ArchiverException e ) |
269 | |
{ |
270 | 0 | throw new IOException( "ArchiverException: " + e.getMessage() ); |
271 | 0 | } |
272 | |
} |
273 | |
else |
274 | |
{ |
275 | 0 | FileUtils.copyFile( srcFile, theFile ); |
276 | |
} |
277 | |
|
278 | 0 | artifact.setFile( theFile ); |
279 | 0 | } |
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
public static String getUnpackableFileName( Artifact artifact ) |
286 | |
{ |
287 | 0 | return "" + artifact.getGroupId() + "-" + artifact.getArtifactId() + "-" + artifact.getVersion() + "-" |
288 | |
+ artifact.getClassifier() + "-" + artifact.getType() + ".txt"; |
289 | |
} |
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
public void createUnpackableFile( Artifact artifact, File destFile ) |
299 | |
throws NoSuchArchiverException, ArchiverException, IOException |
300 | |
{ |
301 | 0 | Archiver archiver = archiverManager.getArchiver( destFile ); |
302 | |
|
303 | 0 | archiver.setDestFile( destFile ); |
304 | 0 | archiver.addFile( srcFile, getUnpackableFileName( artifact ) ); |
305 | |
|
306 | |
try |
307 | |
{ |
308 | 0 | setVariableValueToObject( archiver, "logger", new SilentLog() ); |
309 | |
} |
310 | 0 | catch ( IllegalAccessException e ) |
311 | |
{ |
312 | 0 | System.out.println( "Unable to override logger with silent log." ); |
313 | 0 | e.printStackTrace(); |
314 | 0 | } |
315 | 0 | if ( archiver instanceof WarArchiver ) |
316 | |
{ |
317 | 0 | WarArchiver war = (WarArchiver) archiver; |
318 | |
|
319 | |
|
320 | 0 | war.setIgnoreWebxml( false ); |
321 | |
} |
322 | 0 | archiver.createArchive(); |
323 | 0 | } |
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
public Artifact getReleaseArtifact() |
330 | |
throws IOException |
331 | |
{ |
332 | 4 | return createArtifact( "testGroupId", "release", "1.0" ); |
333 | |
} |
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
public Artifact getSnapshotArtifact() |
340 | |
throws IOException |
341 | |
{ |
342 | 4 | return createArtifact( "testGroupId", "snapshot", "2.0-SNAPSHOT" ); |
343 | |
} |
344 | |
|
345 | |
|
346 | |
|
347 | |
|
348 | |
|
349 | |
|
350 | |
|
351 | |
|
352 | |
public Set<Artifact> getReleaseAndSnapshotArtifacts() |
353 | |
throws IOException |
354 | |
{ |
355 | 0 | Set<Artifact> set = new HashSet<Artifact>(); |
356 | 0 | set.add( getReleaseArtifact() ); |
357 | 0 | set.add( getSnapshotArtifact() ); |
358 | 0 | return set; |
359 | |
} |
360 | |
|
361 | |
|
362 | |
|
363 | |
|
364 | |
|
365 | |
|
366 | |
public Set<Artifact> getScopedArtifacts() |
367 | |
throws IOException |
368 | |
{ |
369 | 0 | Set<Artifact> set = new HashSet<Artifact>(); |
370 | 0 | set.add( createArtifact( "g", "compile", "1.0", Artifact.SCOPE_COMPILE ) ); |
371 | 0 | set.add( createArtifact( "g", "provided", "1.0", Artifact.SCOPE_PROVIDED ) ); |
372 | 0 | set.add( createArtifact( "g", "test", "1.0", Artifact.SCOPE_TEST ) ); |
373 | 0 | set.add( createArtifact( "g", "runtime", "1.0", Artifact.SCOPE_RUNTIME ) ); |
374 | 0 | set.add( createArtifact( "g", "system", "1.0", Artifact.SCOPE_SYSTEM ) ); |
375 | 0 | return set; |
376 | |
} |
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
public Set<Artifact> getTypedArtifacts() |
384 | |
throws IOException |
385 | |
{ |
386 | 0 | Set<Artifact> set = new HashSet<Artifact>(); |
387 | 0 | set.add( createArtifact( "g", "a", "1.0", Artifact.SCOPE_COMPILE, "war", null ) ); |
388 | 0 | set.add( createArtifact( "g", "b", "1.0", Artifact.SCOPE_COMPILE, "jar", null ) ); |
389 | 0 | set.add( createArtifact( "g", "c", "1.0", Artifact.SCOPE_COMPILE, "sources", null ) ); |
390 | 0 | set.add( createArtifact( "g", "d", "1.0", Artifact.SCOPE_COMPILE, "zip", null ) ); |
391 | 0 | set.add( createArtifact( "g", "e", "1.0", Artifact.SCOPE_COMPILE, "rar", null ) ); |
392 | 0 | return set; |
393 | |
} |
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
|
400 | |
public Set<Artifact> getClassifiedArtifacts() |
401 | |
throws IOException |
402 | |
{ |
403 | 0 | Set<Artifact> set = new HashSet<Artifact>(); |
404 | 0 | set.add( createArtifact( "g", "a", "1.0", Artifact.SCOPE_COMPILE, "jar", "one" ) ); |
405 | 0 | set.add( createArtifact( "g", "b", "1.0", Artifact.SCOPE_COMPILE, "jar", "two" ) ); |
406 | 0 | set.add( createArtifact( "g", "c", "1.0", Artifact.SCOPE_COMPILE, "jar", "three" ) ); |
407 | 0 | set.add( createArtifact( "g", "d", "1.0", Artifact.SCOPE_COMPILE, "jar", "four" ) ); |
408 | 0 | return set; |
409 | |
} |
410 | |
|
411 | |
|
412 | |
|
413 | |
|
414 | |
|
415 | |
|
416 | |
public Set<Artifact> getTypedArchiveArtifacts() |
417 | |
throws IOException |
418 | |
{ |
419 | 0 | Set<Artifact> set = new HashSet<Artifact>(); |
420 | 0 | set.add( createArtifact( "g", "a", "1.0", Artifact.SCOPE_COMPILE, "war", null ) ); |
421 | 0 | set.add( createArtifact( "g", "b", "1.0", Artifact.SCOPE_COMPILE, "jar", null ) ); |
422 | 0 | set.add( createArtifact( "g", "d", "1.0", Artifact.SCOPE_COMPILE, "zip", null ) ); |
423 | 0 | set.add( createArtifact( "g", "e", "1.0", Artifact.SCOPE_COMPILE, "rar", null ) ); |
424 | 0 | return set; |
425 | |
} |
426 | |
|
427 | |
|
428 | |
|
429 | |
|
430 | |
|
431 | |
|
432 | |
public Set<Artifact> getArtifactArtifacts() |
433 | |
throws IOException |
434 | |
{ |
435 | 0 | Set<Artifact> set = new HashSet<Artifact>(); |
436 | 0 | set.add( createArtifact( "g", "one", "1.0", Artifact.SCOPE_COMPILE, "jar", "a" ) ); |
437 | 0 | set.add( createArtifact( "g", "two", "1.0", Artifact.SCOPE_COMPILE, "jar", "a" ) ); |
438 | 0 | set.add( createArtifact( "g", "three", "1.0", Artifact.SCOPE_COMPILE, "jar", "a" ) ); |
439 | 0 | set.add( createArtifact( "g", "four", "1.0", Artifact.SCOPE_COMPILE, "jar", "a" ) ); |
440 | 0 | return set; |
441 | |
} |
442 | |
|
443 | |
|
444 | |
|
445 | |
|
446 | |
|
447 | |
|
448 | |
|
449 | |
public Set<Artifact> getGroupIdArtifacts() |
450 | |
throws IOException |
451 | |
{ |
452 | 0 | Set<Artifact> set = new HashSet<Artifact>(); |
453 | 0 | set.add( createArtifact( "one", "group-one", "1.0", Artifact.SCOPE_COMPILE, "jar", "a" ) ); |
454 | 0 | set.add( createArtifact( "two", "group-two", "1.0", Artifact.SCOPE_COMPILE, "jar", "a" ) ); |
455 | 0 | set.add( createArtifact( "three", "group-three", "1.0", Artifact.SCOPE_COMPILE, "jar", "a" ) ); |
456 | 0 | set.add( createArtifact( "four", "group-four", "1.0", Artifact.SCOPE_COMPILE, "jar", "a" ) ); |
457 | 0 | return set; |
458 | |
} |
459 | |
|
460 | |
|
461 | |
|
462 | |
|
463 | |
|
464 | |
|
465 | |
|
466 | |
|
467 | |
public Set<Artifact> getMixedArtifacts() |
468 | |
throws IOException |
469 | |
{ |
470 | 0 | Set<Artifact> set = new HashSet<Artifact>(); |
471 | 0 | set.addAll( getTypedArtifacts() ); |
472 | 0 | set.addAll( getScopedArtifacts() ); |
473 | 0 | set.addAll( getReleaseAndSnapshotArtifacts() ); |
474 | 0 | return set; |
475 | |
} |
476 | |
|
477 | |
|
478 | |
|
479 | |
|
480 | |
public boolean isCreateFiles() |
481 | |
{ |
482 | 2 | return this.createFiles; |
483 | |
} |
484 | |
|
485 | |
|
486 | |
|
487 | |
|
488 | |
public void setCreateFiles( boolean createFiles ) |
489 | |
{ |
490 | 0 | this.createFiles = createFiles; |
491 | 0 | } |
492 | |
|
493 | |
|
494 | |
|
495 | |
|
496 | |
public File getWorkingDir() |
497 | |
{ |
498 | 0 | return this.workingDir; |
499 | |
} |
500 | |
|
501 | |
|
502 | |
|
503 | |
|
504 | |
public void setWorkingDir( File workingDir ) |
505 | |
{ |
506 | 0 | this.workingDir = workingDir; |
507 | 0 | } |
508 | |
|
509 | |
|
510 | |
|
511 | |
|
512 | |
public File getSrcFile() |
513 | |
{ |
514 | 0 | return this.srcFile; |
515 | |
} |
516 | |
|
517 | |
|
518 | |
|
519 | |
|
520 | |
public void setSrcFile( File srcFile ) |
521 | |
{ |
522 | 0 | this.srcFile = srcFile; |
523 | 0 | } |
524 | |
|
525 | |
|
526 | |
|
527 | |
|
528 | |
|
529 | |
|
530 | |
|
531 | |
|
532 | |
|
533 | |
public static void setVariableValueToObject( Object object, String variable, Object value ) |
534 | |
throws IllegalAccessException |
535 | |
{ |
536 | 0 | Field field = ReflectionUtils.getFieldByNameIncludingSuperclasses( variable, object.getClass() ); |
537 | |
|
538 | 0 | field.setAccessible( true ); |
539 | |
|
540 | 0 | field.set( object, value ); |
541 | 0 | } |
542 | |
|
543 | |
|
544 | |
|
545 | |
|
546 | |
|
547 | |
|
548 | |
|
549 | |
|
550 | |
|
551 | |
public static String getFormattedFileName( Artifact artifact, boolean removeVersion ) |
552 | |
{ |
553 | 0 | String destFileName = null; |
554 | |
|
555 | |
|
556 | |
|
557 | 0 | if ( artifact.getFile() != null && !removeVersion ) |
558 | |
{ |
559 | 0 | destFileName = artifact.getFile().getName(); |
560 | |
} |
561 | |
else |
562 | |
|
563 | |
{ |
564 | 0 | String versionString = null; |
565 | 0 | if ( !removeVersion ) |
566 | |
{ |
567 | 0 | versionString = "-" + artifact.getVersion(); |
568 | |
} |
569 | |
else |
570 | |
{ |
571 | 0 | versionString = ""; |
572 | |
} |
573 | |
|
574 | 0 | String classifierString = ""; |
575 | |
|
576 | 0 | if ( StringUtils.isNotEmpty( artifact.getClassifier() ) ) |
577 | |
{ |
578 | 0 | classifierString = "-" + artifact.getClassifier(); |
579 | |
} |
580 | |
|
581 | 0 | destFileName = artifact.getArtifactId() + versionString + classifierString + "." |
582 | |
+ artifact.getArtifactHandler().getExtension(); |
583 | |
} |
584 | 0 | return destFileName; |
585 | |
} |
586 | |
|
587 | |
} |