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 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   * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
44   * @version $Id$
45   */
46  public class CpdReportTest
47      extends AbstractPmdReportTest
48  {
49      /** {@inheritDoc} */
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 =
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          // check if the CPD files were generated
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          // check the contents of cpd.html
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       * Test CPDReport using custom configuration
97       *
98       * @throws Exception
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         // check if the CPD files were generated
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         // Contents that should NOT be in the report
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         // Contents that should be in the report
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      * Test CPDReport with invalid format
139      *
140      * @throws Exception
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      * Read the contents of the specified file object into a string
165      *
166      * @param file
167      *            the file to be read
168      * @return a String object that contains the contents of the file
169      * @throws java.io.IOException
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         // parseDocument( new BufferedInputStream( new FileInputStream( report ) ) );
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 }