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