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