1   package org.apache.maven.usability;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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  }