1 package org.apache.archiva.metadata.repository.storage.maven2;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.archiva.model.ArtifactReference;
23 import org.apache.archiva.repository.AbstractRepositoryLayerTestCase;
24 import org.apache.archiva.repository.layout.LayoutException;
25 import org.apache.commons.lang.StringUtils;
26 import org.junit.Test;
27
28 import static org.junit.Assert.*;
29
30
31
32
33 public abstract class AbstractDefaultRepositoryContentTestCase
34 extends AbstractRepositoryLayerTestCase
35 {
36 @Test
37 public void testBadPathMissingType()
38 {
39 assertBadPath( "invalid/invalid/1/invalid-1", "missing type" );
40 }
41
42 @Test
43 public void testBadPathReleaseInSnapshotDir()
44 {
45 assertBadPath( "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar",
46 "non snapshot artifact inside of a snapshot dir" );
47 }
48
49 @Test
50 public void testBadPathTimestampedSnapshotNotInSnapshotDir()
51 {
52 assertBadPath( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar",
53 "Timestamped Snapshot artifact not inside of an Snapshot dir" );
54 }
55
56 @Test
57 public void testBadPathTooShort()
58 {
59 assertBadPath( "invalid/invalid-1.0.jar", "path is too short" );
60 }
61
62 @Test
63 public void testBadPathVersionMismatchA()
64 {
65 assertBadPath( "invalid/invalid/1.0/invalid-2.0.jar", "version mismatch between path and artifact" );
66 }
67
68 @Test
69 public void testBadPathVersionMismatchB()
70 {
71 assertBadPath( "invalid/invalid/1.0/invalid-1.0b.jar", "version mismatch between path and artifact" );
72 }
73
74 @Test
75 public void testBadPathWrongArtifactId()
76 {
77 assertBadPath( "org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar",
78 "wrong artifact id" );
79 }
80
81
82
83
84
85
86
87
88 @Test
89 public void testGoodButOddVersionSpecGanymedSsh2()
90 throws LayoutException
91 {
92 String groupId = "ch.ethz.ganymed";
93 String artifactId = "ganymed-ssh2";
94 String version = "build210";
95 String classifier = null;
96 String type = "jar";
97 String path = "ch/ethz/ganymed/ganymed-ssh2/build210/ganymed-ssh2-build210.jar";
98
99 assertLayout( path, groupId, artifactId, version, classifier, type );
100 }
101
102
103
104
105
106
107
108
109 @Test
110 public void testGoodButOddVersionSpecJavaxComm()
111 throws LayoutException
112 {
113 String groupId = "javax";
114 String artifactId = "comm";
115 String version = "3.0-u1";
116 String classifier = null;
117 String type = "jar";
118 String path = "javax/comm/3.0-u1/comm-3.0-u1.jar";
119
120 assertLayout( path, groupId, artifactId, version, classifier, type );
121 }
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151 @Test
152 public void testGoodButOddVersionSpecJavaxPersistence()
153 throws LayoutException
154 {
155 String groupId = "javax.persistence";
156 String artifactId = "ejb";
157 String version = "3.0-public_review";
158 String classifier = null;
159 String type = "jar";
160 String path = "javax/persistence/ejb/3.0-public_review/ejb-3.0-public_review.jar";
161
162
163
164
165
166
167
168
169 assertLayout( path, groupId, artifactId, version, classifier, type );
170 }
171
172 @Test
173 public void testGoodComFooTool()
174 throws LayoutException
175 {
176 String groupId = "com.foo";
177 String artifactId = "foo-tool";
178 String version = "1.0";
179 String classifier = null;
180 String type = "jar";
181 String path = "com/foo/foo-tool/1.0/foo-tool-1.0.jar";
182
183 assertLayout( path, groupId, artifactId, version, classifier, type );
184 }
185
186 @Test
187 public void testGoodCommonsLang()
188 throws LayoutException
189 {
190 String groupId = "commons-lang";
191 String artifactId = "commons-lang";
192 String version = "2.1";
193 String classifier = null;
194 String type = "jar";
195 String path = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
196
197 assertLayout( path, groupId, artifactId, version, classifier, type );
198 }
199
200
201
202
203 @Test
204 public void testGoodDashedArtifactId()
205 throws LayoutException
206 {
207 String groupId = "test.maven-arch";
208 String artifactId = "test-arch";
209 String version = "2.0.3-SNAPSHOT";
210 String classifier = null;
211 String type = "pom";
212 String path = "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.pom";
213
214 assertLayout( path, groupId, artifactId, version, classifier, type );
215 }
216
217
218
219
220 @Test
221 public void testGoodDotNotationArtifactId()
222 throws LayoutException
223 {
224 String groupId = "com.company.department";
225 String artifactId = "com.company.department";
226 String version = "0.2";
227 String classifier = null;
228 String type = "pom";
229 String path = "com/company/department/com.company.department/0.2/com.company.department-0.2.pom";
230
231 assertLayout( path, groupId, artifactId, version, classifier, type );
232 }
233
234
235
236
237 @Test
238 public void testGoodDotNotationSameGroupIdAndArtifactId()
239 throws LayoutException
240 {
241 String groupId = "com.company.department";
242 String artifactId = "com.company.department.project";
243 String version = "0.3";
244 String classifier = null;
245 String type = "pom";
246 String path =
247 "com/company/department/com.company.department.project/0.3/com.company.department.project-0.3.pom";
248
249 assertLayout( path, groupId, artifactId, version, classifier, type );
250 }
251
252
253
254
255
256
257
258 @Test
259 public void testGoodFooLibSources()
260 throws LayoutException
261 {
262 String groupId = "com.foo.lib";
263 String artifactId = "foo-lib";
264 String version = "2.1-alpha-1";
265 String classifier = "sources";
266 String type = "java-source";
267 String path = "com/foo/lib/foo-lib/2.1-alpha-1/foo-lib-2.1-alpha-1-sources.jar";
268
269 assertLayout( path, groupId, artifactId, version, classifier, type );
270 }
271
272
273
274
275
276
277
278 @Test
279 public void testGoodSnapshotMavenTest()
280 throws LayoutException
281 {
282 String groupId = "org.apache.archiva.test";
283 String artifactId = "redonkulous";
284 String version = "3.1-beta-1-20050831.101112-42";
285 String classifier = null;
286 String type = "jar";
287 String path =
288 "org/apache/archiva/test/redonkulous/3.1-beta-1-SNAPSHOT/redonkulous-3.1-beta-1-20050831.101112-42.jar";
289
290 assertLayout( path, groupId, artifactId, version, classifier, type );
291 }
292
293
294
295
296
297 @Test
298 public void testGoodVersionKeywordInArtifactId()
299 throws LayoutException
300 {
301 String groupId = "maven";
302 String artifactId = "maven-test-plugin";
303 String version = "1.8.2";
304 String classifier = null;
305 String type = "pom";
306 String path = "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.pom";
307
308 assertLayout( path, groupId, artifactId, version, classifier, type );
309 }
310
311
312
313
314
315 @Test
316 public void testGoodDetectMavenTestPlugin()
317 throws LayoutException
318 {
319 String groupId = "maven";
320 String artifactId = "maven-test-plugin";
321 String version = "1.8.2";
322 String classifier = null;
323 String type = "maven-plugin";
324 String path = "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.jar";
325
326 assertLayout( path, groupId, artifactId, version, classifier, type );
327 }
328
329
330
331
332 @Test
333 public void testGoodDetectCoberturaMavenPlugin()
334 throws LayoutException
335 {
336 String groupId = "org.codehaus.mojo";
337 String artifactId = "cobertura-maven-plugin";
338 String version = "2.1";
339 String classifier = null;
340 String type = "maven-plugin";
341 String path = "org/codehaus/mojo/cobertura-maven-plugin/2.1/cobertura-maven-plugin-2.1.jar";
342
343 assertLayout( path, groupId, artifactId, version, classifier, type );
344 }
345
346 @Test
347 public void testToArtifactOnEmptyPath()
348 {
349 try
350 {
351 toArtifactReference( "" );
352 fail( "Should have failed due to empty path." );
353 }
354 catch ( LayoutException e )
355 {
356
357 }
358 }
359
360 @Test
361 public void testToArtifactOnNullPath()
362 {
363 try
364 {
365 toArtifactReference( null );
366 fail( "Should have failed due to null path." );
367 }
368 catch ( LayoutException e )
369 {
370
371 }
372 }
373
374 @Test
375 public void testToArtifactReferenceOnEmptyPath()
376 {
377 try
378 {
379 toArtifactReference( "" );
380 fail( "Should have failed due to empty path." );
381 }
382 catch ( LayoutException e )
383 {
384
385 }
386 }
387
388 @Test
389 public void testToArtifactReferenceOnNullPath()
390 {
391 try
392 {
393 toArtifactReference( null );
394 fail( "Should have failed due to null path." );
395 }
396 catch ( LayoutException e )
397 {
398
399 }
400 }
401
402 @Test
403 public void testToPathOnNullArtifactReference()
404
405 {
406 try
407 {
408 ArtifactReference reference = null;
409 toPath( reference );
410 fail( "Should have failed due to null artifact reference." );
411 }
412 catch ( IllegalArgumentException e )
413 {
414
415 }
416 }
417
418 private void assertArtifactReference( ArtifactReference actualReference, String groupId, String artifactId,
419 String version, String classifier, String type )
420 {
421 String expectedId =
422 "ArtifactReference - " + groupId + ":" + artifactId + ":" + version + ":" + classifier + ":" + type;
423
424 assertNotNull( expectedId + " - Should not be null.", actualReference );
425
426 assertEquals( expectedId + " - Group ID", groupId, actualReference.getGroupId() );
427 assertEquals( expectedId + " - Artifact ID", artifactId, actualReference.getArtifactId() );
428 if ( StringUtils.isNotBlank( classifier ) )
429 {
430 assertEquals( expectedId + " - Classifier", classifier, actualReference.getClassifier() );
431 }
432 assertEquals( expectedId + " - Version ID", version, actualReference.getVersion() );
433 assertEquals( expectedId + " - Type", type, actualReference.getType() );
434 }
435
436 private void assertBadPath( String path, String reason )
437 {
438 try
439 {
440 toArtifactReference( path );
441 fail(
442 "Should have thrown a LayoutException on the invalid path [" + path + "] because of [" + reason + "]" );
443 }
444 catch ( LayoutException e )
445 {
446
447 }
448 }
449
450
451
452
453 private void assertLayout( String path, String groupId, String artifactId, String version, String classifier,
454 String type )
455 throws LayoutException
456 {
457 ArtifactReference expectedArtifact = createArtifact( groupId, artifactId, version, classifier, type );
458
459
460
461
462 assertEquals( "Artifact <" + expectedArtifact + "> to path:", path, toPath( expectedArtifact ) );
463
464
465
466
467 ArtifactReference testReference = toArtifactReference( path );
468 assertArtifactReference( testReference, groupId, artifactId, version, classifier, type );
469
470
471 assertEquals( "Artifact <" + expectedArtifact + "> to path:", path, toPath( testReference ) );
472 }
473
474 protected ArtifactReference createArtifact( String groupId, String artifactId, String version, String classifier,
475 String type )
476 {
477 ArtifactReference artifact = new ArtifactReference();
478 artifact.setGroupId( groupId );
479 artifact.setArtifactId( artifactId );
480 artifact.setVersion( version );
481 artifact.setClassifier( classifier );
482 artifact.setType( type );
483 assertNotNull( artifact );
484 return artifact;
485 }
486
487 protected abstract ArtifactReference toArtifactReference( String path )
488 throws LayoutException;
489
490 protected abstract String toPath( ArtifactReference reference );
491 }