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