View Javadoc

1   package org.apache.maven.plugin.pmd;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
42   * @version $Id: CpdReportTest.html 853015 2013-03-04 21:10:54Z olamy $
43   */
44  public class CpdReportTest
45      extends AbstractPmdReportTest
46  {
47      /**
48       * {@inheritDoc}
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       * Test CPDReport given the default configuration
59       *
60       * @throws Exception
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          // check if the CPD files were generated
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          // check the contents of cpd.html
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       * Test CPDReport using custom configuration
96       *
97       * @throws Exception
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         // check if the CPD files were generated
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         // Contents that should NOT be in the report
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         // Contents that should be in the report
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      * Test CPDReport with invalid format
136      *
137      * @throws Exception
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      * Read the contents of the specified file object into a string
161      *
162      * @param file the file to be read
163      * @return a String object that contains the contents of the file
164      * @throws java.io.IOException
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         // parseDocument( new BufferedInputStream( new FileInputStream( report ) ) );
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 }