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 java.io.BufferedReader;
23 import java.io.File;
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 import java.util.List;
29
30 import javax.xml.parsers.DocumentBuilder;
31 import javax.xml.parsers.DocumentBuilderFactory;
32
33 import net.sourceforge.pmd.cpd.CPD;
34 import net.sourceforge.pmd.cpd.JavaLanguage;
35 import net.sourceforge.pmd.cpd.Language;
36 import net.sourceforge.pmd.cpd.Match;
37 import net.sourceforge.pmd.cpd.TokenEntry;
38
39 import org.codehaus.plexus.util.FileUtils;
40 import org.w3c.dom.Document;
41
42
43
44
45
46 public class CpdReportTest
47 extends AbstractPmdReportTest
48 {
49
50 protected void setUp()
51 throws Exception
52 {
53 super.setUp();
54 FileUtils.deleteDirectory( new File( getBasedir(), "target/test/unit" ) );
55 }
56
57
58
59
60
61
62 public void testDefaultConfiguration()
63 throws Exception
64 {
65 File testPom =
66 new File( getBasedir(),
67 "src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml" );
68 CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
69 mojo.execute();
70
71
72 File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
73 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
74
75 generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/cpd.html" );
76 renderer( mojo, generatedFile );
77 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
78
79
80 String str =
81 readFile( new File( getBasedir(), "target/test/unit/default-configuration/target/site/cpd.html" ) );
82 assertTrue( str.toLowerCase().indexOf( "AppSample.java".toLowerCase() ) != -1 );
83
84 str = readFile( new File( getBasedir(), "target/test/unit/default-configuration/target/site/cpd.html" ) );
85 assertTrue( str.toLowerCase().indexOf( "App.java".toLowerCase() ) != -1 );
86
87 str = readFile( new File( getBasedir(), "target/test/unit/default-configuration/target/site/cpd.html" ) );
88 assertTrue( str.toLowerCase().indexOf( "public String dup( String str )".toLowerCase() ) != -1 );
89
90 str = readFile( new File( getBasedir(), "target/test/unit/default-configuration/target/site/cpd.html" ) );
91 assertTrue( str.toLowerCase().indexOf( "tmp = tmp + str.substring( i, i + 1);".toLowerCase() ) != -1 );
92
93 }
94
95
96
97
98
99
100 public void testCustomConfiguration()
101 throws Exception
102 {
103 File testPom =
104 new File( getBasedir(),
105 "src/test/resources/unit/custom-configuration/cpd-custom-configuration-plugin-config.xml" );
106 CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
107 mojo.execute();
108
109
110 File generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/cpd.csv" );
111 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
112
113 generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/site/cpd.html" );
114 renderer( mojo, generatedFile );
115 assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
116
117
118 String str =
119 readFile( new File( getBasedir(), "target/test/unit/custom-configuration/target/site/cpd.html" ) );
120 assertTrue( str.toLowerCase().indexOf( "/Sample.java".toLowerCase() ) == -1 );
121
122 str = readFile( new File( getBasedir(), "target/test/unit/custom-configuration/target/site/cpd.html" ) );
123 assertTrue( str.toLowerCase().indexOf( "public void duplicateMethod( int i )".toLowerCase() ) == -1 );
124
125
126 str = readFile( new File( getBasedir(), "target/test/unit/custom-configuration/target/site/cpd.html" ) );
127 assertTrue( str.toLowerCase().indexOf( "AnotherSample.java".toLowerCase() ) != -1 );
128
129 str = readFile( new File( getBasedir(), "target/test/unit/custom-configuration/target/site/cpd.html" ) );
130 assertTrue( str.toLowerCase().indexOf( "public static void main( String[] args )".toLowerCase() ) != -1 );
131
132 str = readFile( new File( getBasedir(), "target/test/unit/custom-configuration/target/site/cpd.html" ) );
133 assertTrue( str.toLowerCase().indexOf( "private String unusedMethod( String unusedParam )".toLowerCase() ) != -1 );
134
135 }
136
137
138
139
140
141
142 public void testInvalidFormat()
143 throws Exception
144 {
145 try
146 {
147 File testPom =
148 new File( getBasedir(),
149 "src/test/resources/unit/invalid-format/cpd-invalid-format-plugin-config.xml" );
150 CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
151 mojo.execute();
152
153 fail( "MavenReportException must be thrown" );
154 }
155 catch ( Exception e )
156 {
157 assertTrue( true );
158 }
159
160 }
161
162
163
164
165
166
167
168
169
170 private String readFile( File file )
171 throws IOException
172 {
173 String str = "", strTmp = "";
174 BufferedReader in = new BufferedReader( new FileReader( file ) );
175
176 while ( ( strTmp = in.readLine() ) != null )
177 {
178 str = str + " " + strTmp;
179 }
180 in.close();
181
182 return str;
183 }
184
185 public void testWriteNonHtml()
186 throws Exception
187 {
188 File testPom =
189 new File( getBasedir(),
190 "src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml" );
191 CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
192 assertNotNull( mojo );
193
194 TokenEntry tFirstEntry = new TokenEntry( "public java", "MyClass.java", 34 );
195 TokenEntry tSecondEntry = new TokenEntry( "public java", "MyClass3.java", 55 );
196 List tList = new ArrayList();
197 Match tMatch = new Match( 2, tFirstEntry, tSecondEntry );
198 tMatch.setSourceCodeSlice( "// ----- ACCESSEURS avec ?l?ments -----" );
199 tList.add( tMatch );
200
201 CPD tCpd = new MockCpd( 100, new JavaLanguage(), tList.iterator() );
202
203 tCpd.go();
204 mojo.writeNonHtml( tCpd );
205
206 File tReport = new File( "target/test/unit/default-configuration/target/cpd.xml" );
207
208
209 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
210 Document pmdCpdDocument = builder.parse( tReport );
211 assertNotNull( pmdCpdDocument );
212 }
213
214 public static class MockCpd
215 extends CPD
216 {
217
218 private Iterator matches;
219
220 public MockCpd( int minimumTileSize, Language language, Iterator tMatch )
221 {
222 super( minimumTileSize, language );
223 matches = tMatch;
224 }
225
226 public Iterator getMatches()
227 {
228 return matches;
229 }
230
231 }
232
233 }