1 package org.apache.maven.plugin.checkstyle;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.Writer;
25 import java.util.Locale;
26 import java.util.ResourceBundle;
27
28
29
30 import org.apache.maven.artifact.DependencyResolutionRequiredException;
31 import org.apache.maven.doxia.site.decoration.DecorationModel;
32 import org.apache.maven.doxia.siterenderer.RendererException;
33 import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
34 import org.apache.maven.plugin.descriptor.PluginDescriptor;
35 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
36 import org.apache.maven.reporting.MavenReport;
37 import org.codehaus.plexus.util.FileUtils;
38 import org.codehaus.plexus.util.IOUtil;
39 import org.codehaus.plexus.util.WriterFactory;
40
41
42
43
44
45 public class CheckstyleReportTest
46 extends AbstractMojoTestCase
47 {
48 private Locale oldLocale;
49
50
51 protected void setUp()
52 throws Exception
53 {
54 super.setUp();
55
56 oldLocale = Locale.getDefault();
57 Locale.setDefault( Locale.ENGLISH );
58 }
59
60
61 protected void tearDown()
62 throws Exception
63 {
64 super.tearDown();
65
66 Locale.setDefault( oldLocale );
67 oldLocale = null;
68 }
69
70 public void testNoSource()
71 throws Exception
72 {
73 File pluginXmlFile = new File( getBasedir(), "src/test/plugin-configs/no-source-plugin-config.xml" );
74
75 CheckstyleReport mojo = (CheckstyleReport) lookupMojo( "checkstyle", pluginXmlFile );
76 assertNotNull( "Mojo found.", mojo );
77 mojo.execute();
78
79 File outputFile = (File) getVariableValueFromObject( mojo, "outputFile" );
80
81 renderer( mojo, outputFile );
82
83 assertTrue( outputFile.getAbsolutePath() + " not generated!", outputFile.exists() );
84
85 assertTrue( outputFile.getAbsolutePath() + " is empty!", outputFile.length() <= 0 );
86 }
87
88 public void testMinConfiguration()
89 throws Exception
90 {
91 generateReport( "min-plugin-config.xml" );
92 }
93
94 public void testCustomConfiguration()
95 throws Exception
96 {
97 generateReport( "custom-plugin-config.xml" );
98 }
99
100 public void testUseFile()
101 throws Exception
102 {
103 generateReport( "useFile-plugin-config.xml" );
104 }
105
106 public void testNoRulesSummary()
107 throws Exception
108 {
109 generateReport( "no-rules-plugin-config.xml" );
110 }
111
112 public void testNoSeveritySummary()
113 throws Exception
114 {
115 generateReport( "no-severity-plugin-config.xml" );
116 }
117
118 public void testNoFilesSummary()
119 throws Exception
120 {
121 generateReport( "no-files-plugin-config.xml" );
122 }
123
124 public void testFailOnError()
125 {
126 try
127 {
128 generateReport( "fail-on-error-plugin-config.xml" );
129
130 fail( "Must throw exception on errors" );
131 }
132 catch ( Exception e )
133 {
134
135 }
136 }
137
138 public void testDependencyResolutionException()
139 {
140 try
141 {
142 generateReport( "dep-resolution-exception-plugin-config.xml" );
143
144 fail( "Must throw exception on errors" );
145 }
146 catch ( Exception e )
147 {
148 if ( !( e.getCause().getCause().getCause() instanceof DependencyResolutionRequiredException ) )
149 {
150 e.printStackTrace();
151 fail( "Must throw exception DependencyResolutionRequiredException on errors and not " + e.getClass().getName() + ", " + e.getMessage() );
152 }
153 }
154 }
155
156 public void testTestSourceDirectory()
157 throws Exception
158 {
159 generateReport( "test-source-directory-plugin-config.xml" );
160 }
161
162 private File generateReport( String pluginXml )
163 throws Exception
164 {
165 File pluginXmlFile = new File( getBasedir(), "src/test/plugin-configs/" + pluginXml );
166 ResourceBundle bundle =
167 ResourceBundle.getBundle( "checkstyle-report", Locale.getDefault(), this.getClassLoader() );
168
169 CheckstyleReport mojo = (CheckstyleReport) lookupMojo( "checkstyle", pluginXmlFile );
170
171 assertNotNull( "Mojo found.", mojo );
172
173 PluginDescriptor descriptorStub = new PluginDescriptor();
174 descriptorStub.setGroupId( "org.apache.maven.plugins" );
175 descriptorStub.setArtifactId( "maven-checkstyle-plugin" );
176 setVariableValueToObject( mojo, "plugin", descriptorStub );
177
178 mojo.execute();
179
180 File outputFile = (File) getVariableValueFromObject( mojo, "outputFile" );
181 assertNotNull( "Test output file", outputFile );
182 assertTrue( "Test output file exists", outputFile.exists() );
183
184 String cacheFile = (String) getVariableValueFromObject( mojo, "cacheFile" );
185 if ( cacheFile != null )
186 {
187 assertTrue( "Test cache file exists", new File( cacheFile ).exists() );
188 }
189
190 MavenReport reportMojo = mojo;
191 File outputDir = reportMojo.getReportOutputDirectory();
192
193 Boolean rss = (Boolean) getVariableValueFromObject( mojo, "enableRSS" );
194 if (rss)
195 {
196 File rssFile = new File( outputDir, "checkstyle.rss" );
197 assertTrue( "Test rss file exists", rssFile.exists() );
198 }
199
200 File useFile = (File) getVariableValueFromObject( mojo, "useFile" );
201 if ( useFile != null )
202 {
203 assertTrue( "Test useFile exists", useFile.exists() );
204 }
205
206 String filename = reportMojo.getOutputName() + ".html";
207 File outputHtml = new File( outputDir, filename );
208
209 renderer( mojo, outputHtml );
210
211 assertTrue( outputHtml.getAbsolutePath() + " not generated!", outputHtml.exists() );
212
213 assertTrue( outputHtml.getAbsolutePath() + " is empty!", outputHtml.length() > 0 );
214
215 String htmlString = FileUtils.fileRead( outputHtml );
216
217 boolean searchHeaderFound =
218 ( htmlString.indexOf( "<h2>" + bundle.getString( "report.checkstyle.rules" ) ) > 0 );
219 Boolean rules = (Boolean) getVariableValueFromObject( mojo, "enableRulesSummary" );
220 if (rules)
221 {
222 assertTrue( "Test for Rules Summary", searchHeaderFound );
223 }
224 else
225 {
226 assertFalse( "Test for Rules Summary", searchHeaderFound );
227 }
228
229 searchHeaderFound =
230 ( htmlString.indexOf( "<h2>" + bundle.getString( "report.checkstyle.summary" ) ) > 0 );
231 Boolean severity = (Boolean) getVariableValueFromObject( mojo, "enableSeveritySummary" );
232 if (severity)
233 {
234 assertTrue( "Test for Severity Summary", searchHeaderFound );
235 }
236 else
237 {
238 assertFalse( "Test for Severity Summary", searchHeaderFound );
239 }
240
241 searchHeaderFound =
242 ( htmlString.indexOf( "<h2>" + bundle.getString( "report.checkstyle.files" ) ) > 0 );
243 Boolean files = (Boolean) getVariableValueFromObject( mojo, "enableFilesSummary" );
244 if (files)
245 {
246 assertTrue( "Test for Files Summary", searchHeaderFound );
247 }
248 else
249 {
250 assertFalse( "Test for Files Summary", searchHeaderFound );
251 }
252
253 return outputHtml;
254 }
255
256
257
258
259
260
261
262
263
264 private void renderer( CheckstyleReport mojo, File outputHtml )
265 throws RendererException, Exception
266 {
267 Writer writer = null;
268 SiteRenderingContext context = new SiteRenderingContext();
269 context.setDecoration( new DecorationModel() );
270 context.setTemplateName( "org/apache/maven/doxia/siterenderer/resources/default-site.vm" );
271 context.setLocale( Locale.ENGLISH );
272
273 try
274 {
275 outputHtml.getParentFile().mkdirs();
276 writer = WriterFactory.newXmlWriter( outputHtml );
277
278 mojo.execute();
279
280 }
281 finally
282 {
283 IOUtil.close( writer );
284 }
285 }
286 }