View Javadoc

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 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  import org.apache.maven.doxia.site.decoration.DecorationModel;
29  import org.apache.maven.doxia.siterenderer.RendererException;
30  import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
31  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
32  import org.apache.maven.reporting.MavenReport;
33  import org.codehaus.plexus.util.FileUtils;
34  import org.codehaus.plexus.util.IOUtil;
35  import org.codehaus.plexus.util.WriterFactory;
36  
37  /**
38   * @author Edwin Punzalan
39   * @version $Id: CheckstyleAggregateReportTest.html 816667 2012-05-08 14:02:08Z hboutemy $
40   */
41  public class CheckstyleAggregateReportTest
42      extends AbstractMojoTestCase
43  {
44      private Locale oldLocale;
45  
46      /** {@inheritDoc} */
47      protected void setUp()
48          throws Exception
49      {
50          super.setUp();
51  
52          oldLocale = Locale.getDefault();
53          Locale.setDefault( Locale.ENGLISH );
54      }
55  
56      /** {@inheritDoc} */
57      protected void tearDown()
58          throws Exception
59      {
60          super.tearDown();
61  
62          Locale.setDefault( oldLocale );
63          oldLocale = null;
64      }
65      public void testMinConfiguration()
66          throws Exception
67      {
68          generateReport( "multi-plugin-config.xml" );
69      }
70  
71      private File generateReport( String pluginXml )
72          throws Exception
73      {
74          File pluginXmlFile = new File( getBasedir(), "src/test/plugin-configs/" + pluginXml );
75          ResourceBundle bundle =
76              ResourceBundle.getBundle( "checkstyle-report", Locale.getDefault(), this.getClassLoader() );
77  
78          CheckstyleAggregateReport mojo = (CheckstyleAggregateReport) lookupMojo( "checkstyle-aggregate", pluginXmlFile );
79  
80          assertNotNull( "Mojo found.", mojo );
81  
82          mojo.execute();
83  
84          File outputFile = (File) getVariableValueFromObject( mojo, "outputFile" );
85          assertNotNull( "Test output file", outputFile );
86          assertTrue( "Test output file exists", outputFile.exists() );
87  
88          String cacheFile = (String) getVariableValueFromObject( mojo, "cacheFile" );
89          if ( cacheFile != null )
90          {
91              assertTrue( "Test cache file exists", new File( cacheFile ).exists() );
92          }
93  
94          MavenReport reportMojo = (MavenReport) mojo;
95          File outputDir = reportMojo.getReportOutputDirectory();
96  
97          Boolean rss = (Boolean) getVariableValueFromObject( mojo, "enableRSS" );
98          if ( rss.booleanValue() )
99          {
100             File rssFile = new File( outputDir, "checkstyle.rss" );
101             assertTrue( "Test rss file exists", rssFile.exists() );
102         }
103 
104         File useFile = (File) getVariableValueFromObject( mojo, "useFile" );
105         if ( useFile != null )
106         {
107             assertTrue( "Test useFile exists", useFile.exists() );
108         }
109 
110         String filename = reportMojo.getOutputName() + ".html";
111         File outputHtml = new File( outputDir, filename );
112 
113         renderer( mojo, outputHtml );
114 
115         assertTrue( outputHtml.getAbsolutePath() + " not generated!", outputHtml.exists() );
116 
117         assertTrue( outputHtml.getAbsolutePath() + " is empty!", outputHtml.length() > 0 );
118 
119         String htmlString = FileUtils.fileRead( outputHtml );
120 
121         boolean searchHeaderFound =
122             ( htmlString.indexOf( "<h2>" + bundle.getString( "report.checkstyle.rules" ) ) > 0 );
123         Boolean rules = (Boolean) getVariableValueFromObject( mojo, "enableRulesSummary" );
124         if ( rules.booleanValue() )
125         {
126             assertTrue( "Test for Rules Summary", searchHeaderFound );
127         }
128         else
129         {
130             assertFalse( "Test for Rules Summary", searchHeaderFound );
131         }
132 
133         searchHeaderFound =
134             ( htmlString.indexOf( "<h2>" + bundle.getString( "report.checkstyle.summary" )  ) > 0 );
135         Boolean severity = (Boolean) getVariableValueFromObject( mojo, "enableSeveritySummary" );
136         if ( severity.booleanValue() )
137         {
138             assertTrue( "Test for Severity Summary", searchHeaderFound );
139         }
140         else
141         {
142             assertFalse( "Test for Severity Summary", searchHeaderFound );
143         }
144 
145         searchHeaderFound =
146             ( htmlString.indexOf( "<h2>" + bundle.getString( "report.checkstyle.files" ) ) > 0 );
147         Boolean files = (Boolean) getVariableValueFromObject( mojo, "enableFilesSummary" );
148         if ( files.booleanValue() )
149         {
150             assertTrue( "Test for Files Summary", searchHeaderFound );
151         }
152         else
153         {
154             assertFalse( "Test for Files Summary", searchHeaderFound );
155         }
156 
157         return outputHtml;
158     }
159 
160     /**
161      * Renderer the sink from the report mojo.
162      *
163      * @param mojo not null
164      * @param outputHtml not null
165      * @throws RendererException if any
166      * @throws IOException if any
167      */
168     private void renderer( CheckstyleAggregateReport mojo, File outputHtml )
169         throws RendererException, Exception
170     {
171         Writer writer = null;
172         SiteRenderingContext context = new SiteRenderingContext();
173         context.setDecoration( new DecorationModel() );
174         context.setTemplateName( "org/apache/maven/doxia/siterenderer/resources/default-site.vm" );
175         context.setLocale( Locale.ENGLISH );
176 
177         try
178         {
179             outputHtml.getParentFile().mkdirs();
180             writer = WriterFactory.newXmlWriter( outputHtml );
181 
182             mojo.execute();
183 
184         }
185         finally
186         {
187             IOUtil.close( writer );
188         }
189     }
190 }