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