KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > usability > PluginErrorDiagnoserTest


1 package org.apache.maven.usability;
2
3 /*
4  * Copyright 2001-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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