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