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 junit.framework.TestCase;
23  
24  import org.apache.maven.plugin.PluginConfigurationException;
25  import org.apache.maven.plugin.PluginParameterException;
26  import org.apache.maven.plugin.descriptor.DuplicateParameterException;
27  import org.apache.maven.plugin.descriptor.MojoDescriptor;
28  import org.apache.maven.plugin.descriptor.Parameter;
29  import org.apache.maven.plugin.descriptor.PluginDescriptor;
30  import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
31  
32  import java.util.ArrayList;
33  import java.util.Collections;
34  import java.util.List;
35  
36  public class PluginErrorDiagnoserTest
37      extends TestCase
38  {
39  
40      private PluginConfigurationDiagnoser diagnoser = new PluginConfigurationDiagnoser();
41  
42      private PluginParameterException buildException( String prefix, String goal, List params )
43          throws DuplicateParameterException
44      {
45          PluginDescriptor pluginDescriptor = new PluginDescriptor();
46          pluginDescriptor.setArtifactId( "maven-test-plugin" );
47          pluginDescriptor.setGroupId( "org.apache.maven.plugins" );
48          pluginDescriptor.setVersion( "1.0" );
49  
50          pluginDescriptor.setGoalPrefix( prefix );
51  
52          MojoDescriptor mojoDescriptor = new MojoDescriptor();
53          mojoDescriptor.setGoal( goal );
54          mojoDescriptor.setPluginDescriptor( pluginDescriptor );
55  
56          mojoDescriptor.setParameters( params );
57  
58          return new PluginParameterException( mojoDescriptor, params );
59      }
60  
61      public void testShouldDiagnoseInvalidPluginConfiguration()
62      {
63          printMethodHeader();
64  
65          ComponentConfigurationException cce = new ComponentConfigurationException(
66                                                                                     "Class \'org.apache.maven.plugin.jar.JarMojo\' does not contain a field named \'addClasspath\'" );
67          
68          PluginDescriptor pd = new PluginDescriptor();
69          pd.setGroupId("testGroup");
70          pd.setArtifactId("testArtifact");
71          
72          PluginConfigurationException pce = new PluginConfigurationException( pd, "test", cce );
73  
74          assertTrue( diagnoser.canDiagnose( pce ) );
75  
76          String userMessage = diagnoser.diagnose( pce );
77  
78          System.out.println( userMessage );
79  
80          assertNotNull( userMessage );
81      }
82  
83      public void testShouldBeAbleToDiagnosePluginParameterExceptions()
84          throws DuplicateParameterException
85      {
86          Parameter param = new Parameter();
87          param.setName( "testName" );
88          param.setAlias( "testAlias" );
89          param.setExpression( "${project.build.finalName}" );
90          param.setEditable( true );
91  
92          PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) );
93  
94          assertTrue( diagnoser.canDiagnose( error ) );
95      }
96  
97      public void testParamWithOneReportsExpressionAndOneProjectBasedExpression()
98          throws DuplicateParameterException
99      {
100         printMethodHeader();
101 
102         List params = new ArrayList();
103 
104         Parameter param = new Parameter();
105 
106         param.setName( "param1" );
107 
108         param.setExpression( "${reports}" );
109 
110         param.setEditable( false );
111 
112         params.add( param );
113 
114         Parameter param2 = new Parameter();
115 
116         param2.setName( "param2" );
117 
118         param2.setExpression( "${project.build.finalName}" );
119 
120         param2.setEditable( false );
121 
122         params.add( param2 );
123 
124         PluginParameterException error = buildException( "test", "test", params );
125 
126         String userMessage = diagnoser.diagnose( error );
127 
128         System.out.println( userMessage );
129 
130         assertNotNull( userMessage );
131     }
132 
133     public void testParamWithNonActiveExpression()
134         throws DuplicateParameterException
135     {
136         printMethodHeader();
137 
138         Parameter param = new Parameter();
139         param.setName( "testName" );
140         param.setAlias( "testAlias" );
141         param.setExpression( "${project.build.finalName" );
142         param.setEditable( true );
143 
144         PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) );
145 
146         String userMessage = diagnoser.diagnose( error );
147 
148         System.out.println( userMessage );
149 
150         assertNotNull( userMessage );
151     }
152 
153     public void testParamWithoutExpression()
154         throws DuplicateParameterException
155     {
156         printMethodHeader();
157 
158         Parameter param = new Parameter();
159         param.setName( "testName" );
160         param.setAlias( "testAlias" );
161         param.setEditable( true );
162 
163         PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) );
164 
165         String userMessage = diagnoser.diagnose( error );
166 
167         System.out.println( userMessage );
168 
169         assertNotNull( userMessage );
170     }
171 
172     public void testParamWithOneLocalRepositoryExpression()
173         throws DuplicateParameterException
174     {
175         printMethodHeader();
176 
177         Parameter param = new Parameter();
178         param.setName( "testName" );
179         param.setAlias( "testAlias" );
180         param.setExpression( "${localRepository}" );
181         param.setEditable( false );
182 
183         PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) );
184 
185         String userMessage = diagnoser.diagnose( error );
186 
187         System.out.println( userMessage );
188 
189         assertNotNull( userMessage );
190     }
191 
192     public void testParamWithOneSystemPropertyExpression()
193         throws DuplicateParameterException
194     {
195         printMethodHeader();
196 
197         Parameter param = new Parameter();
198         param.setName( "testName" );
199         param.setAlias( "testAlias" );
200         param.setExpression( "${maven.mode.online}" );
201         param.setEditable( false );
202 
203         PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) );
204 
205         String userMessage = diagnoser.diagnose( error );
206 
207         System.out.println( userMessage );
208 
209         assertNotNull( userMessage );
210     }
211 
212     public void testParamWithOneProjectBasedExpression()
213         throws DuplicateParameterException
214     {
215         printMethodHeader();
216 
217         Parameter param = new Parameter();
218         param.setName( "testName" );
219         param.setAlias( "testAlias" );
220         param.setExpression( "${project.build.finalName}" );
221         param.setEditable( true );
222 
223         PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) );
224 
225         String userMessage = diagnoser.diagnose( error );
226 
227         System.out.println( userMessage );
228 
229         assertNotNull( userMessage );
230     }
231 
232     public void testParamWithOneProjectAPIBasedExpression()
233         throws DuplicateParameterException
234     {
235         printMethodHeader();
236 
237         Parameter param = new Parameter();
238         param.setName( "testName" );
239         param.setExpression( "${project.distributionManagementArtifactRepository}" );
240         param.setRequired( true );
241         param.setEditable( false );
242 
243         PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) );
244 
245         String userMessage = diagnoser.diagnose( error );
246 
247         System.out.println( userMessage );
248 
249         assertNotNull( userMessage );
250     }
251 
252     public void testNonEditableParamWithOneProjectBasedExpression()
253         throws DuplicateParameterException
254     {
255         printMethodHeader();
256 
257         Parameter param = new Parameter();
258         param.setName( "testName" );
259         param.setAlias( "testAlias" );
260         param.setExpression( "${project.build.finalName}" );
261         param.setEditable( false );
262 
263         PluginParameterException error = buildException( "test", "test", Collections.singletonList( param ) );
264 
265         String userMessage = diagnoser.diagnose( error );
266 
267         System.out.println( userMessage );
268 
269         assertNotNull( userMessage );
270     }
271 
272     private void printMethodHeader()
273     {
274         IllegalArgumentException marker = new IllegalArgumentException();
275 
276         System.out.println( "---------------------------------------------------------------------\n"
277             + "Visual output for " + marker.getStackTrace()[1].getMethodName()
278             + ":\n---------------------------------------------------------------------" );
279     }
280 
281 }