1 package org.apache.maven.plugin.pmd;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.codehaus.plexus.util.FileUtils;
23
24 import java.io.BufferedReader;
25 import java.io.File;
26 import java.io.FileReader;
27 import java.io.IOException;
28 import java.net.URL;
29 import java.util.Locale;
30
31
32
33
34
35 public class PmdReportTest
36 extends AbstractPmdReportTest
37 {
38
39 protected void setUp()
40 throws Exception
41 {
42 super.setUp();
43 FileUtils.deleteDirectory( new File( getBasedir(), "target/test/unit" ) );
44 }
45
46 public void testDefaultConfiguration()
47 throws Exception
48 {
49 FileUtils.copyDirectoryStructure(
50 new File( getBasedir(), "src/test/resources/unit/default-configuration/jxr-files" ),
51 new File( getBasedir(), "target/test/unit/default-configuration/target/site" ) );
52
53 File testPom = new File( getBasedir(),
54 "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" );
55 PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
56 mojo.execute();
57
58
59 File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd.xml" );
60 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
61
62 generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/basic.xml" );
63 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
64
65 generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/imports.xml" );
66 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
67
68 generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/unusedcode.xml" );
69 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
70
71 generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" );
72 renderer( mojo, generatedFile );
73 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
74
75
76 String str =
77 readFile( new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" ) );
78
79
80 assertTrue( str.indexOf( "/xref/def/configuration/App.html#31" ) != -1 );
81
82 assertTrue( str.indexOf( "/xref/def/configuration/AppSample.html#45" ) != -1 );
83 }
84
85
86 public void testFileURL()
87 throws Exception
88 {
89 FileUtils.copyDirectoryStructure(
90 new File( getBasedir(), "src/test/resources/unit/default-configuration/jxr-files" ),
91 new File( getBasedir(), "target/test/unit/default-configuration/target/site" ) );
92
93 File testPom = new File( getBasedir(),
94 "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" );
95 PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
96
97 URL url = getClass().getClassLoader().getResource( "rulesets/basic.xml" );
98 URL url2 = getClass().getClassLoader().getResource( "rulesets/unusedcode.xml" );
99 URL url3 = getClass().getClassLoader().getResource( "rulesets/imports.xml" );
100 mojo.setRulesets( new String[] { url.toString(), url2.toString() , url3.toString() } );
101
102 mojo.execute();
103
104
105 File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd.xml" );
106 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
107
108 generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/basic.xml" );
109 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
110
111 generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/imports.xml" );
112 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
113
114 generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/unusedcode.xml" );
115 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
116
117 generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" );
118 renderer( mojo, generatedFile );
119 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
120
121
122 String str =
123 readFile( new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" ) );
124
125
126 assertTrue( str.indexOf( "/xref/def/configuration/App.html#31" ) != -1 );
127
128 assertTrue( str.indexOf( "/xref/def/configuration/AppSample.html#45" ) != -1 );
129 }
130
131
132
133
134
135
136 public void testCustomConfiguration()
137 throws Exception
138 {
139 File testPom = new File( getBasedir(),
140 "src/test/resources/unit/custom-configuration/custom-configuration-plugin-config.xml" );
141
142 PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
143 mojo.execute();
144
145
146 File generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/pmd.csv" );
147 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
148
149 generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/custom.xml" );
150 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
151
152 generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/site/pmd.html" );
153 renderer( mojo, generatedFile );
154 renderer( mojo, generatedFile );
155 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
156
157
158 String str = readFile( new File( getBasedir(), "target/test/unit/custom-configuration/target/site/pmd.html" ) );
159 assertTrue( str.toLowerCase().indexOf( "Avoid using if statements without curly braces".toLowerCase() ) != -1 );
160
161 assertTrue(
162 str.toLowerCase().indexOf( "Avoid using if...else statements without curly braces".toLowerCase() ) != -1 );
163
164 assertTrue("unnecessary constructor should not be triggered because of low priority",
165 str.toLowerCase().indexOf( "Avoid unnecessary constructors - the compiler will generate these for you".toLowerCase() ) == -1 );
166
167 }
168
169
170
171
172
173
174 public void testSkipConfiguration()
175 throws Exception
176 {
177 File testPom = new File( getBasedir(),
178 "src/test/resources/unit/custom-configuration/skip-plugin-config.xml" );
179 PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
180 mojo.execute();
181
182 File basedir = new File( getBasedir(), "target/test/unit/skip-configuration" );
183
184
185 File generatedFile = new File( getBasedir(), "target/test/unit/skip-configuration/target/pmd.csv" );
186 assertFalse( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
187
188 generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/custom.xml" );
189 assertFalse( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
190
191 generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/site/pmd.html" );
192 assertFalse( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
193 }
194
195 public void testInvalidFormat()
196 throws Exception
197 {
198 try
199 {
200 File testPom =
201 new File( getBasedir(), "src/test/resources/unit/invalid-format/invalid-format-plugin-config.xml" );
202 PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
203 setVariableValueToObject( mojo, "compileSourceRoots", mojo.project.getCompileSourceRoots() );
204 mojo.executeReport( Locale.ENGLISH );
205
206 fail( "Must throw MavenReportException." );
207 }
208 catch ( Exception e )
209 {
210 assertTrue( true );
211 }
212 }
213
214 public void testInvalidTargetJdk()
215 throws Exception
216 {
217 try
218 {
219 File testPom =
220 new File( getBasedir(), "src/test/resources/unit/invalid-format/invalid-target-jdk-plugin-config.xml" );
221 PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
222 mojo.execute();
223
224 fail( "Must throw MavenReportException." );
225 }
226 catch ( Exception e )
227 {
228 assertTrue( true );
229 }
230 }
231
232
233
234
235
236
237
238
239
240
241 private String readFile( File file )
242 throws IOException
243 {
244 String strTmp;
245 StringBuffer str = new StringBuffer( (int) file.length() );
246 BufferedReader in = new BufferedReader( new FileReader( file ) );
247
248 while ( ( strTmp = in.readLine() ) != null )
249 {
250 str.append( ' ' );
251 str.append( strTmp );
252 }
253 in.close();
254
255 return str.toString();
256 }
257
258
259
260
261
262
263 public void testLocationTemp()
264 throws Exception
265 {
266
267 File testPom = new File( getBasedir(),
268 "src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" );
269 PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom );
270
271 assertEquals( "locationTemp is not correctly encoding filename",
272 "export_format_pmd_language_java_name_some_2520name", mojo.getLocationTemp(
273 "http://nemo.sonarsource.org/sonar/profiles/export?format=pmd&language=java&name=some%2520name" ) );
274
275 }
276
277 }