1 package org.apache.maven.usability;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.artifact.Artifact;
23 import org.apache.maven.artifact.factory.ArtifactFactory;
24 import org.codehaus.plexus.PlexusTestCase;
25
26 public class InvalidArtifactDiagnoserTest
27 extends PlexusTestCase
28 {
29 private InvalidArtifactDiagnoser diagnoser = new InvalidArtifactDiagnoser();
30
31 public void testShouldDiagnoseArtifactWithMissingGroupId() throws Throwable
32 {
33 testDiagnosis( "Test diagnosis for missing groupId", null, "test-artifact", "1.0", "jar" );
34 }
35
36 public void testShouldDiagnoseArtifactWithMissingArtifactId() throws Throwable
37 {
38 testDiagnosis( "Test diagnosis for missing artifactId", "test.group.id", null, "1.0", "jar" );
39 }
40
41 public void testShouldDiagnoseArtifactWithMissingVersion() throws Throwable
42 {
43 testDiagnosis( "Test diagnosis for missing version", "test.group.id", "test-artifact", null, "jar" );
44 }
45
46 public void testShouldDiagnoseArtifactWithMissingType() throws Throwable
47 {
48 testDiagnosis( "Test diagnosis for missing type", "test.group.id", "test-artifact", "1.0", null );
49 }
50
51 public void testShouldDiagnoseArtifactWithMissingGroupIdAndArtifactId() throws Throwable
52 {
53 testDiagnosis( "Test diagnosis for missing groupId and artifactId", null, null, "1.0", "jar" );
54 }
55
56 private void testDiagnosis( String testHeader, String groupId, String artifactId, String version, String type )
57 throws Throwable
58 {
59 System.out.println( "------------------------------------------------------------" );
60 System.out.println( "| " + testHeader );
61 System.out.println( "------------------------------------------------------------" );
62 System.out.println();
63
64 try
65 {
66 createArtifact( groupId, artifactId, version, type );
67
68 fail( "artifact creation did not fail; nothing to diagnose." );
69 }
70 catch ( Throwable error )
71 {
72 assertTrue( "Unexpected error while constructing artifact: " + error, diagnoser.canDiagnose( error ) );
73
74 if ( diagnoser.canDiagnose( error ) )
75 {
76 System.out.println( diagnoser.diagnose( error ) );
77 }
78 else
79 {
80 throw error;
81 }
82 }
83 }
84
85 private Artifact createArtifact( String groupId, String artifactId, String version, String type )
86 throws Exception
87 {
88 ArtifactFactory artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
89 return artifactFactory.createBuildArtifact( groupId, artifactId, version, type );
90 }
91
92 }