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.ResourceBundle;
26  
27  import org.apache.maven.artifact.DependencyResolutionRequiredException;
28  import org.apache.maven.doxia.tools.SiteTool;
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          File generatedReport = generateReport(getGoal(), "no-source-plugin-config.xml");
39          assertFalse(FileUtils.fileExists(generatedReport.getAbsolutePath()));
40      }
41  
42      public void testMinConfiguration() throws Exception {
43          generateReport("min-plugin-config.xml");
44      }
45  
46      public void testCustomConfiguration() throws Exception {
47          generateReport("custom-plugin-config.xml");
48      }
49  
50      public void testUseFile() throws Exception {
51          generateReport("useFile-plugin-config.xml");
52      }
53  
54      public void testNoRulesSummary() throws Exception {
55          generateReport("no-rules-plugin-config.xml");
56      }
57  
58      public void testNoSeveritySummary() throws Exception {
59          generateReport("no-severity-plugin-config.xml");
60      }
61  
62      public void testNoFilesSummary() throws Exception {
63          generateReport("no-files-plugin-config.xml");
64      }
65  
66      public void testFailOnError() {
67          try {
68              generateReport("fail-on-error-plugin-config.xml");
69  
70              fail("Must throw exception on errors");
71          } catch (Exception e) {
72              // expected
73          }
74      }
75  
76      public void testDependencyResolutionException() {
77          try {
78              generateReport("dep-resolution-exception-plugin-config.xml");
79  
80              fail("Must throw exception on errors");
81          } catch (Exception e) {
82              if (!(e.getCause().getCause().getCause() instanceof DependencyResolutionRequiredException)) {
83                  e.printStackTrace();
84                  fail("Must throw exception DependencyResolutionRequiredException on errors and not "
85                          + e.getClass().getName() + ", " + e.getMessage());
86              }
87          }
88      }
89  
90      public void testTestSourceDirectory() throws Exception {
91          generateReport("test-source-directory-plugin-config.xml");
92      }
93  
94      /**
95       * Read the contents of the specified file object into a string
96       *
97       * @param file the file to be read
98       * @return a String object that contains the contents of the file
99       * @throws java.io.IOException
100      */
101     private String readFile(File file) throws IOException {
102         String strTmp;
103         StringBuilder str = new StringBuilder((int) file.length());
104         try (BufferedReader in = new BufferedReader(new FileReader(file))) {
105             while ((strTmp = in.readLine()) != null) {
106                 str.append(' ');
107                 str.append(strTmp);
108             }
109         }
110 
111         return str.toString();
112     }
113 
114     private void generateReport(String pluginXml) throws Exception {
115         File pluginXmlFile = new File(getBasedir(), "src/test/resources/plugin-configs/" + pluginXml);
116         ResourceBundle bundle =
117                 ResourceBundle.getBundle("checkstyle-report", SiteTool.DEFAULT_LOCALE, this.getClassLoader());
118 
119         CheckstyleReport mojo = createReportMojo(getGoal(), pluginXmlFile);
120 
121         PluginDescriptor descriptorStub = new PluginDescriptor();
122         descriptorStub.setGroupId("org.apache.maven.plugins");
123         descriptorStub.setArtifactId("maven-checkstyle-plugin");
124         setVariableValueToObject(mojo, "plugin", descriptorStub);
125 
126         File generatedReport = generateReport(mojo, pluginXmlFile);
127         assertTrue(FileUtils.fileExists(generatedReport.getAbsolutePath()));
128 
129         File outputFile = (File) getVariableValueFromObject(mojo, "outputFile");
130         assertNotNull("Test output file", outputFile);
131         assertTrue("Test output file exists", outputFile.exists());
132 
133         String cacheFile = (String) getVariableValueFromObject(mojo, "cacheFile");
134         if (cacheFile != null) {
135             assertTrue("Test cache file exists", new File(cacheFile).exists());
136         }
137 
138         File outputDir = mojo.getReportOutputDirectory();
139 
140         File useFile = (File) getVariableValueFromObject(mojo, "useFile");
141         if (useFile != null) {
142             assertTrue("Test useFile exists", useFile.exists());
143         }
144 
145         String str = readFile(generatedReport);
146 
147         boolean searchHeaderFound = str.contains(getHtmlHeader(bundle.getString("report.checkstyle.rules")));
148         Boolean rules = (Boolean) getVariableValueFromObject(mojo, "enableRulesSummary");
149         if (rules) {
150             assertTrue("Test for Rules Summary", searchHeaderFound);
151         } else {
152             assertFalse("Test for Rules Summary", searchHeaderFound);
153         }
154 
155         searchHeaderFound = str.contains(getHtmlHeader(bundle.getString("report.checkstyle.summary")));
156         Boolean severity = (Boolean) getVariableValueFromObject(mojo, "enableSeveritySummary");
157         if (severity) {
158             assertTrue("Test for Severity Summary", searchHeaderFound);
159         } else {
160             assertFalse("Test for Severity Summary", searchHeaderFound);
161         }
162 
163         searchHeaderFound = str.contains(getHtmlHeader(bundle.getString("report.checkstyle.files")));
164         Boolean files = (Boolean) getVariableValueFromObject(mojo, "enableFilesSummary");
165         if (files) {
166             assertTrue("Test for Files Summary", searchHeaderFound);
167         } else {
168             assertFalse("Test for Files Summary", searchHeaderFound);
169         }
170     }
171 
172     private static String getHtmlHeader(String s) {
173         return ">" + s + "</h2>";
174     }
175 
176     @Override
177     protected String getGoal() {
178         return "checkstyle";
179     }
180 }