1 package org.apache.maven.plugins.checkstyle;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.io.BufferedReader;
24 import java.io.File;
25 import java.io.FileReader;
26 import java.io.IOException;
27 import java.util.Locale;
28 import java.util.ResourceBundle;
29
30 import org.apache.maven.artifact.DependencyResolutionRequiredException;
31 import org.apache.maven.plugin.descriptor.PluginDescriptor;
32 import org.codehaus.plexus.util.FileUtils;
33
34
35
36
37
38 public class CheckstyleReportTest
39 extends AbstractCheckstyleTestCase
40 {
41 public void testNoSource()
42 throws Exception
43 {
44 File generatedReport = generateReport( "checkstyle", "no-source-plugin-config.xml" );
45 assertFalse( FileUtils.fileExists( generatedReport.getAbsolutePath() ) );
46 }
47
48 public void testMinConfiguration()
49 throws Exception
50 {
51 generateReport( "min-plugin-config.xml" );
52 }
53
54 public void testCustomConfiguration()
55 throws Exception
56 {
57 generateReport( "custom-plugin-config.xml" );
58 }
59
60 public void testUseFile()
61 throws Exception
62 {
63 generateReport( "useFile-plugin-config.xml" );
64 }
65
66 public void testNoRulesSummary()
67 throws Exception
68 {
69 generateReport( "no-rules-plugin-config.xml" );
70 }
71
72 public void testNoSeveritySummary()
73 throws Exception
74 {
75 generateReport( "no-severity-plugin-config.xml" );
76 }
77
78 public void testNoFilesSummary()
79 throws Exception
80 {
81 generateReport( "no-files-plugin-config.xml" );
82 }
83
84 public void testFailOnError()
85 {
86 try
87 {
88 generateReport( "fail-on-error-plugin-config.xml" );
89
90 fail( "Must throw exception on errors" );
91 }
92 catch ( Exception e )
93 {
94
95 }
96 }
97
98 public void testDependencyResolutionException()
99 {
100 try
101 {
102 generateReport( "dep-resolution-exception-plugin-config.xml" );
103
104 fail( "Must throw exception on errors" );
105 }
106 catch ( Exception e )
107 {
108 if ( !( e.getCause().getCause().getCause() instanceof DependencyResolutionRequiredException ) )
109 {
110 e.printStackTrace();
111 fail( "Must throw exception DependencyResolutionRequiredException on errors and not " + e.getClass().getName() + ", " + e.getMessage() );
112 }
113 }
114 }
115
116 public void testTestSourceDirectory()
117 throws Exception
118 {
119 generateReport( "test-source-directory-plugin-config.xml" );
120 }
121
122
123
124
125
126
127
128
129 private String readFile( File file )
130 throws IOException
131 {
132 String strTmp;
133 StringBuilder str = new StringBuilder( (int) file.length() );
134 try ( BufferedReader in = new BufferedReader( new FileReader( file ) ) )
135 {
136 while ( ( strTmp = in.readLine() ) != null )
137 {
138 str.append( ' ' );
139 str.append( strTmp );
140 }
141 }
142
143 return str.toString();
144 }
145
146 private void generateReport( String pluginXml )
147 throws Exception
148 {
149 File pluginXmlFile = new File( getBasedir(), "src/test/resources/plugin-configs/" + pluginXml );
150 ResourceBundle bundle =
151 ResourceBundle.getBundle( "checkstyle-report", Locale.getDefault(), this.getClassLoader() );
152
153 CheckstyleReport mojo = createReportMojo( "checkstyle", pluginXmlFile );
154
155 PluginDescriptor descriptorStub = new PluginDescriptor();
156 descriptorStub.setGroupId( "org.apache.maven.plugins" );
157 descriptorStub.setArtifactId( "maven-checkstyle-plugin" );
158 setVariableValueToObject( mojo, "plugin", descriptorStub );
159
160 File generatedReport = generateReport( mojo, pluginXmlFile );
161 assertTrue( FileUtils.fileExists( generatedReport.getAbsolutePath() ) );
162
163 File outputFile = (File) getVariableValueFromObject( mojo, "outputFile" );
164 assertNotNull( "Test output file", outputFile );
165 assertTrue( "Test output file exists", outputFile.exists() );
166
167 String cacheFile = (String) getVariableValueFromObject( mojo, "cacheFile" );
168 if ( cacheFile != null )
169 {
170 assertTrue( "Test cache file exists", new File( cacheFile ).exists() );
171 }
172
173 File outputDir = mojo.getReportOutputDirectory();
174
175 Boolean rss = (Boolean) getVariableValueFromObject( mojo, "enableRSS" );
176 if (rss)
177 {
178 File rssFile = new File( outputDir, "checkstyle.rss" );
179 assertTrue( "Test rss file exists", rssFile.exists() );
180 }
181
182 File useFile = (File) getVariableValueFromObject( mojo, "useFile" );
183 if ( useFile != null )
184 {
185 assertTrue( "Test useFile exists", useFile.exists() );
186 }
187
188 String str = readFile( generatedReport );
189
190 boolean searchHeaderFound =
191 str.contains( getHtmlHeader( bundle.getString( "report.checkstyle.rules" ) ) );
192 Boolean rules = (Boolean) getVariableValueFromObject( mojo, "enableRulesSummary" );
193 if (rules)
194 {
195 assertTrue( "Test for Rules Summary", searchHeaderFound );
196 }
197 else
198 {
199 assertFalse( "Test for Rules Summary", searchHeaderFound );
200 }
201
202 searchHeaderFound = str.contains( getHtmlHeader( bundle.getString( "report.checkstyle.summary" ) ) );
203 Boolean severity = (Boolean) getVariableValueFromObject( mojo, "enableSeveritySummary" );
204 if (severity)
205 {
206 assertTrue( "Test for Severity Summary", searchHeaderFound );
207 }
208 else
209 {
210 assertFalse( "Test for Severity Summary", searchHeaderFound );
211 }
212
213 searchHeaderFound = str.contains( getHtmlHeader( bundle.getString( "report.checkstyle.files" ) ) );
214 Boolean files = (Boolean) getVariableValueFromObject( mojo, "enableFilesSummary" );
215 if (files)
216 {
217 assertTrue( "Test for Files Summary", searchHeaderFound );
218 }
219 else
220 {
221 assertFalse( "Test for Files Summary", searchHeaderFound );
222 }
223 }
224
225 private static String getHtmlHeader( String s )
226 {
227 return ">" + s + "</h2>";
228 }
229 }