View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.checkstyle;
20  
21  import java.io.BufferedReader;
22  import java.io.File;
23  import java.io.FileReader;
24  import java.io.IOException;
25  import java.util.Locale;
26  import java.util.ResourceBundle;
27  
28  import org.apache.maven.artifact.DependencyResolutionRequiredException;
29  import org.apache.maven.plugin.descriptor.PluginDescriptor;
30  import org.codehaus.plexus.util.FileUtils;
31  
32  /**
33   * @author Edwin Punzalan
34   *
35   */
36  public class CheckstyleReportTest extends AbstractCheckstyleTestCase {
37      public void testNoSource() throws Exception {
38          // clean up after earlier runs
39          File report = new File("target/test-harness/checkstyle/no-source/checkstyle.html");
40          report.delete();
41          File generatedReport = generateReport("checkstyle", "no-source-plugin-config.xml");
42          assertFalse(report + " exists", generatedReport.exists());
43      }
44  
45      public void testMinConfiguration() throws Exception {
46          generateReport("min-plugin-config.xml");
47      }
48  
49      public void testCustomConfiguration() throws Exception {
50          generateReport("custom-plugin-config.xml");
51      }
52  
53      public void testUseFile() throws Exception {
54          generateReport("useFile-plugin-config.xml");
55      }
56  
57      public void testNoRulesSummary() throws Exception {
58          generateReport("no-rules-plugin-config.xml");
59      }
60  
61      public void testNoSeveritySummary() throws Exception {
62          generateReport("no-severity-plugin-config.xml");
63      }
64  
65      public void testNoFilesSummary() throws Exception {
66          generateReport("no-files-plugin-config.xml");
67      }
68  
69      public void testFailOnError() {
70          try {
71              generateReport("fail-on-error-plugin-config.xml");
72  
73              fail("Must throw exception on errors");
74          } catch (Exception e) {
75              // expected
76          }
77      }
78  
79      public void testDependencyResolutionException() {
80          try {
81              generateReport("dep-resolution-exception-plugin-config.xml");
82  
83              fail("Must throw exception on errors");
84          } catch (Exception e) {
85              if (!(e.getCause().getCause().getCause() instanceof DependencyResolutionRequiredException)) {
86                  e.printStackTrace();
87                  fail("Must throw exception DependencyResolutionRequiredException on errors and not "
88                          + e.getClass().getName() + ", " + e.getMessage());
89              }
90          }
91      }
92  
93      public void testTestSourceDirectory() throws Exception {
94          generateReport("test-source-directory-plugin-config.xml");
95      }
96  
97      /**
98       * Read the contents of the specified file object into a string
99       *
100      * @param file the file to be read
101      * @return a String object that contains the contents of the file
102      * @throws java.io.IOException
103      */
104     private String readFile(File file) throws IOException {
105         String strTmp;
106         StringBuilder str = new StringBuilder((int) file.length());
107         try (BufferedReader in = new BufferedReader(new FileReader(file))) {
108             while ((strTmp = in.readLine()) != null) {
109                 str.append(' ');
110                 str.append(strTmp);
111             }
112         }
113 
114         return str.toString();
115     }
116 
117     private void generateReport(String pluginXml) throws Exception {
118         File pluginXmlFile = new File(getBasedir(), "src/test/resources/plugin-configs/" + pluginXml);
119         ResourceBundle bundle =
120                 ResourceBundle.getBundle("checkstyle-report", Locale.getDefault(), this.getClassLoader());
121 
122         CheckstyleReport mojo = createReportMojo("checkstyle", pluginXmlFile);
123 
124         PluginDescriptor descriptorStub = new PluginDescriptor();
125         descriptorStub.setGroupId("org.apache.maven.plugins");
126         descriptorStub.setArtifactId("maven-checkstyle-plugin");
127         setVariableValueToObject(mojo, "plugin", descriptorStub);
128 
129         File generatedReport = generateReport(mojo, pluginXmlFile);
130         assertTrue(FileUtils.fileExists(generatedReport.getAbsolutePath()));
131 
132         File outputFile = (File) getVariableValueFromObject(mojo, "outputFile");
133         assertNotNull("Test output file", outputFile);
134         assertTrue("Test output file exists", outputFile.exists());
135 
136         String cacheFile = (String) getVariableValueFromObject(mojo, "cacheFile");
137         if (cacheFile != null) {
138             assertTrue("Test cache file exists", new File(cacheFile).exists());
139         }
140 
141         File outputDir = mojo.getReportOutputDirectory();
142 
143         File useFile = (File) getVariableValueFromObject(mojo, "useFile");
144         if (useFile != null) {
145             assertTrue("Test useFile exists", useFile.exists());
146         }
147 
148         String str = readFile(generatedReport);
149 
150         boolean searchHeaderFound = str.contains(getHtmlHeader(bundle.getString("report.checkstyle.rules")));
151         Boolean rules = (Boolean) getVariableValueFromObject(mojo, "enableRulesSummary");
152         if (rules) {
153             assertTrue("Test for Rules Summary", searchHeaderFound);
154         } else {
155             assertFalse("Test for Rules Summary", searchHeaderFound);
156         }
157 
158         searchHeaderFound = str.contains(getHtmlHeader(bundle.getString("report.checkstyle.summary")));
159         Boolean severity = (Boolean) getVariableValueFromObject(mojo, "enableSeveritySummary");
160         if (severity) {
161             assertTrue("Test for Severity Summary", searchHeaderFound);
162         } else {
163             assertFalse("Test for Severity Summary", searchHeaderFound);
164         }
165 
166         searchHeaderFound = str.contains(getHtmlHeader(bundle.getString("report.checkstyle.files")));
167         Boolean files = (Boolean) getVariableValueFromObject(mojo, "enableFilesSummary");
168         if (files) {
169             assertTrue("Test for Files Summary", searchHeaderFound);
170         } else {
171             assertFalse("Test for Files Summary", searchHeaderFound);
172         }
173     }
174 
175     private static String getHtmlHeader(String s) {
176         return ">" + s + "</h3>";
177     }
178 }