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(".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 setVariableValueToObject( mojo, "compileSourceRoots", mojo.project.getCompileSourceRoots() );
152 mojo.execute();
153
154 fail( "MavenReportException must be thrown" );
155 }
156 catch ( Exception e )
157 {
158 assertTrue( true );
159 }
160
161 }
162
163
164
165
166
167
168
169
170
171 private String readFile( File file )
172 throws IOException
173 {
174 String strTmp;
175 StringBuffer str = new StringBuffer( (int) file.length() );
176 BufferedReader in = new BufferedReader( new FileReader( file ) );
177
178 while ( ( strTmp = in.readLine() ) != null )
179 {
180 str.append( ' ' );
181 str.append( strTmp );
182 }
183 in.close();
184
185 return str.toString();
186 }
187
188 public void testWriteNonHtml()
189 throws Exception
190 {
191 File testPom =
192 new File( getBasedir(),
193 "src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml" );
194 CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
195 assertNotNull( mojo );
196
197 TokenEntry tFirstEntry = new TokenEntry( "public java", "MyClass.java", 34 );
198 TokenEntry tSecondEntry = new TokenEntry( "public java", "MyClass3.java", 55 );
199 List<Match> tList = new ArrayList<Match>();
200 Match tMatch = new Match( 2, tFirstEntry, tSecondEntry );
201 tMatch.setSourceCodeSlice( "// ----- ACCESSEURS avec �l�ments -----" );
202 tList.add( tMatch );
203
204 CPD tCpd = new MockCpd( 100, new JavaLanguage(), tList.iterator() );
205
206 tCpd.go();
207 mojo.writeNonHtml( tCpd );
208
209 File tReport = new File( "target/test/unit/default-configuration/target/cpd.xml" );
210
211
212 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
213 Document pmdCpdDocument = builder.parse( tReport );
214 assertNotNull( pmdCpdDocument );
215 }
216
217 public static class MockCpd
218 extends CPD
219 {
220
221 private Iterator<Match> matches;
222
223 public MockCpd( int minimumTileSize, Language language, Iterator<Match> tMatch )
224 {
225 super( minimumTileSize, language );
226 matches = tMatch;
227 }
228
229 public Iterator<Match> getMatches()
230 {
231 return matches;
232 }
233
234 }
235
236 }