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( String unusedParam )".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             mojo.execute();
152 
153             fail( "MavenReportException must be thrown" );
154         }
155         catch ( Exception e )
156         {
157             assertTrue( true );
158         }
159 
160     }
161 
162     /**
163      * Read the contents of the specified file object into a string
164      *
165      * @param file
166      *            the file to be read
167      * @return a String object that contains the contents of the file
168      * @throws java.io.IOException
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         // parseDocument( new BufferedInputStream( new FileInputStream( report ) ) );
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 }