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