View Javadoc
1   package org.apache.maven.it;
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.it.util.ResourceExtractor;
23  
24  import java.io.File;
25  import java.io.IOException;
26  import java.util.List;
27  import java.util.regex.Pattern;
28  
29  import junit.framework.TestCase;
30  
31  /**
32   * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-2690">MNG-2690</a>.
33   *
34   * It checks, at the most basic level possible, that the plugin manager is intercepting things like
35   * {@link NoClassDefFoundError} and ComponntLookupException, then throwing user-friendly errors when loading and
36   * configuring a mojo.
37   *
38   * @author jdcasey
39   */
40  public class MavenITmng2690MojoLoadingErrorsTest
41      extends AbstractMavenIntegrationTestCase
42  {
43  
44      public MavenITmng2690MojoLoadingErrorsTest()
45      {
46          super( "(2.1.0-M1,)" );
47      }
48  
49      public void testNoClassDefFromMojoLoad()
50          throws IOException, VerificationException
51      {
52          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-2690/noclassdef-mojo" );
53  
54          Verifier verifier = newVerifier( testDir.getAbsolutePath() );
55          verifier.setAutoclean( false );
56  
57          try
58          {
59              verifier.executeGoal( "validate" );
60  
61              fail( "should throw an error during execution." );
62          }
63          catch ( VerificationException e )
64          {
65              // expected...it'd be nice if we could get the specifics of the exception right here...
66          }
67          finally
68          {
69              verifier.resetStreams();
70          }
71  
72          List<String> lines = verifier.loadFile( new File( testDir, "log.txt" ), false );
73  
74          int msg = indexOf( lines, "(?i).*required class is missing.*" );
75          assertTrue( "User-friendly message was not found in output.", msg >= 0 );
76  
77          int cls = lines.get( msg ).toString().replace( '/', '.' ).indexOf( TestCase.class.getName() );
78          assertTrue( "Missing class name was not found in output.", cls >= 0 );
79      }
80  
81      public void testNoClassDefFromMojoConfiguration()
82          throws IOException, VerificationException
83      {
84          File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-2690/noclassdef-param" );
85  
86          Verifier verifier = newVerifier( testDir.getAbsolutePath() );
87          verifier.setAutoclean( false );
88  
89          try
90          {
91              verifier.executeGoal( "validate" );
92  
93              fail( "should throw an error during execution." );
94          }
95          catch ( VerificationException e )
96          {
97              // expected...it'd be nice if we could get the specifics of the exception right here...
98          }
99          finally
100         {
101             verifier.resetStreams();
102         }
103 
104         List<String> lines = verifier.loadFile( new File( testDir, "log.txt" ), false );
105 
106         int msg = indexOf( lines, "(?i).*required class (i|wa)s missing( during (mojo )?configuration)?.*" );
107         assertTrue( "User-friendly message was not found in output.", msg >= 0 );
108 
109         int cls = lines.get( msg ).toString().replace( '/', '.' ).indexOf( TestCase.class.getName() );
110         assertTrue( "Missing class name was not found in output.", cls >= 0 );
111     }
112 
113     public void testMojoComponentLookupException()
114         throws IOException, VerificationException
115     {
116         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-2690/mojo-complookup" );
117 
118         Verifier verifier = newVerifier( testDir.getAbsolutePath() );
119         verifier.setAutoclean( false );
120 
121         try
122         {
123             verifier.executeGoal( "validate" );
124 
125             fail( "should throw an error during execution." );
126         }
127         catch ( VerificationException e )
128         {
129             // expected...it'd be nice if we could get the specifics of the exception right here...
130         }
131         finally
132         {
133             verifier.resetStreams();
134         }
135 
136         List<String> lines = verifier.loadFile( new File( testDir, "log.txt" ), false );
137 
138         String compLookupMsg =
139             "(?i).*unable to .* mojo 'mojo-component-lookup-exception' .* plugin "
140                 + "'org\\.apache\\.maven\\.its\\.plugins:maven-it-plugin-error.*";
141 
142         assertTrue( "User-friendly message was not found in output.", indexOf( lines, compLookupMsg ) > 0 );
143     }
144 
145     public void testMojoRequirementComponentLookupException()
146         throws IOException, VerificationException
147     {
148         File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-2690/requirement-complookup" );
149 
150         Verifier verifier = newVerifier( testDir.getAbsolutePath() );
151         verifier.setAutoclean( false );
152 
153         try
154         {
155             verifier.executeGoal( "validate" );
156 
157             fail( "should throw an error during execution." );
158         }
159         catch ( VerificationException e )
160         {
161             // expected...it'd be nice if we could get the specifics of the exception right here...
162         }
163         finally
164         {
165             verifier.resetStreams();
166         }
167 
168         List<String> lines = verifier.loadFile( new File( testDir, "log.txt" ), false );
169 
170         String compLookupMsg =
171             "(?i).*unable to .* mojo 'requirement-component-lookup-exception' .* plugin "
172                 + "'org\\.apache\\.maven\\.its\\.plugins:maven-it-plugin-error.*";
173 
174         assertTrue( "User-friendly message was not found in output.", indexOf( lines, compLookupMsg ) > 0 );
175     }
176 
177     private int indexOf( List<String> logLines, String regex )
178     {
179         Pattern pattern = Pattern.compile( regex );
180 
181         for ( int i = 0; i < logLines.size(); i++ )
182         {
183             String logLine = logLines.get( i );
184 
185             if ( pattern.matcher( logLine ).matches() )
186             {
187                 return i;
188             }
189         }
190 
191         return -1;
192     }
193 
194 }