You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by dk...@apache.org on 2007/11/29 20:18:30 UTC
svn commit: r599550 - in /maven/plugins/trunk/maven-pmd-plugin/src:
main/java/org/apache/maven/plugin/pmd/CpdReport.java
test/java/org/apache/maven/plugin/pmd/CpdReportTest.java
Author: dkulp
Date: Thu Nov 29 11:18:25 2007
New Revision: 599550
URL: http://svn.apache.org/viewvc?rev=599550&view=rev
Log:
[MPMD-55] Patch from pkernevez applied to add proper UTF-8 encoding to xml file
Modified:
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdReport.java
maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/CpdReportTest.java
Modified: maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdReport.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdReport.java?rev=599550&r1=599549&r2=599550&view=diff
==============================================================================
--- maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdReport.java (original)
+++ maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdReport.java Thu Nov 29 11:18:25 2007
@@ -20,9 +20,13 @@
*/
import java.io.File;
+import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
+import java.io.OutputStreamWriter;
import java.io.Writer;
+import java.nio.charset.Charset;
+import java.nio.charset.spi.CharsetProvider;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
@@ -114,28 +118,7 @@
if ( !isHtml() )
{
- Renderer r = createRenderer();
- String buffer = r.render( cpd.getMatches() );
- try
- {
- targetDirectory.mkdirs();
- Writer writer = new FileWriter( new File( targetDirectory, "cpd." + format ) );
- writer.write( buffer, 0, buffer.length() );
- writer.close();
-
-
- File siteDir = new File( targetDirectory, "site" );
- siteDir.mkdirs();
- writer = new FileWriter( new File( siteDir,
- "cpd." + format ) );
- writer.write( buffer, 0, buffer.length() );
- writer.close();
-
- }
- catch ( IOException ioe )
- {
- throw new MavenReportException( ioe.getMessage(), ioe );
- }
+ writeNonHtml( cpd );
}
}
finally
@@ -143,6 +126,33 @@
Thread.currentThread().setContextClassLoader( origLoader );
}
+ }
+ }
+
+ void writeNonHtml( CPD cpd ) throws MavenReportException
+ {
+ Renderer r = createRenderer();
+ String buffer = r.render( cpd.getMatches() );
+ try
+ {
+ targetDirectory.mkdirs();
+ FileOutputStream tStream = new FileOutputStream(new File( targetDirectory, "cpd." + format ));
+ Writer writer = new OutputStreamWriter(tStream, Charset.forName( "UTF-8" ));
+ writer.write( buffer, 0, buffer.length() );
+ writer.close();
+
+
+ File siteDir = new File( targetDirectory, "site" );
+ siteDir.mkdirs();
+ writer = new FileWriter( new File( siteDir,
+ "cpd." + format ) );
+ writer.write( buffer, 0, buffer.length() );
+ writer.close();
+
+ }
+ catch ( IOException ioe )
+ {
+ throw new MavenReportException( ioe.getMessage(), ioe );
}
}
Modified: maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/CpdReportTest.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/CpdReportTest.java?rev=599550&r1=599549&r2=599550&view=diff
==============================================================================
--- maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/CpdReportTest.java (original)
+++ maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/CpdReportTest.java Thu Nov 29 11:18:25 2007
@@ -19,23 +19,36 @@
* under the License.
*/
-import org.apache.maven.plugin.testing.AbstractMojoTestCase;
-import org.codehaus.plexus.util.FileUtils;
-
+import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
+import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import net.sourceforge.pmd.cpd.CPD;
+import net.sourceforge.pmd.cpd.JavaLanguage;
+import net.sourceforge.pmd.cpd.Language;
+import net.sourceforge.pmd.cpd.Match;
+import net.sourceforge.pmd.cpd.TokenEntry;
+
+import org.apache.maven.plugin.testing.AbstractMojoTestCase;
+import org.codehaus.plexus.util.FileUtils;
+import org.w3c.dom.Document;
/**
* @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
*/
-public class CpdReportTest
- extends AbstractMojoTestCase
+public class CpdReportTest extends AbstractMojoTestCase
{
- protected void setUp()
- throws Exception
+ protected void setUp() throws Exception
{
super.setUp();
FileUtils.deleteDirectory( new File( getBasedir(), "target/test/unit" ) );
@@ -43,27 +56,26 @@
/**
* Test CPDReport given the default configuration
- *
+ *
* @throws Exception
*/
- public void testDefaultConfiguration()
- throws Exception
+ public void testDefaultConfiguration() throws Exception
{
- File testPom = new File( getBasedir(),
- "src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml" );
+ File testPom =
+ new File( getBasedir(),
+ "src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml" );
CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
mojo.execute();
- //check if the CPD files were generated
+ // check if the CPD files were generated
File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/cpd.xml" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/cpd.html" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
- //check the contents of cpd.html
- String str =
- readFile( new File( getBasedir(), "target/test/unit/default-configuration/target/site/cpd.html" ) );
+ // check the contents of cpd.html
+ String str = readFile( new File( getBasedir(), "target/test/unit/default-configuration/target/site/cpd.html" ) );
assertTrue( str.toLowerCase().indexOf( "AppSample.java".toLowerCase() ) != -1 );
str = readFile( new File( getBasedir(), "target/test/unit/default-configuration/target/site/cpd.html" ) );
@@ -79,18 +91,18 @@
/**
* Test CPDReport using custom configuration
- *
+ *
* @throws Exception
*/
- public void testCustomConfiguration()
- throws Exception
+ public void testCustomConfiguration() throws Exception
{
- File testPom = new File( getBasedir(),
- "src/test/resources/unit/custom-configuration/cpd-custom-configuration-plugin-config.xml" );
+ File testPom =
+ new File( getBasedir(),
+ "src/test/resources/unit/custom-configuration/cpd-custom-configuration-plugin-config.xml" );
CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
mojo.execute();
- //check if the CPD files were generated
+ // check if the CPD files were generated
File generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/cpd.csv" );
assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
@@ -112,18 +124,16 @@
assertTrue( str.toLowerCase().indexOf( "public static void main( String[] args )".toLowerCase() ) != -1 );
str = readFile( new File( getBasedir(), "target/test/unit/custom-configuration/target/site/cpd.html" ) );
- assertTrue( str.toLowerCase().indexOf(
- "private String unusedMethod( String unusedParam )".toLowerCase() ) != -1 );
+ assertTrue( str.toLowerCase().indexOf( "private String unusedMethod( String unusedParam )".toLowerCase() ) != -1 );
}
/**
* Test CPDReport with invalid format
- *
+ *
* @throws Exception
*/
- public void testInvalidFormat()
- throws Exception
+ public void testInvalidFormat() throws Exception
{
try
{
@@ -141,21 +151,20 @@
}
- protected void tearDown()
- throws Exception
+ protected void tearDown() throws Exception
{
}
/**
* Read the contents of the specified file object into a string
- *
- * @param file the file to be read
+ *
+ * @param file
+ * the file to be read
* @return a String object that contains the contents of the file
* @throws java.io.IOException
*/
- private String readFile( File file )
- throws IOException
+ private String readFile( File file ) throws IOException
{
String str = "", strTmp = "";
BufferedReader in = new BufferedReader( new FileReader( file ) );
@@ -169,5 +178,49 @@
return str;
}
+ public void testWriteNonHtml() throws Exception
+ {
+ File testPom =
+ new File( getBasedir(),
+ "src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml" );
+ CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom );
+ assertNotNull( mojo );
+
+ TokenEntry tFirstEntry = new TokenEntry( "public java", "MyClass.java", 34 );
+ TokenEntry tSecondEntry = new TokenEntry( "public java", "MyClass3.java", 55 );
+ List tList = new ArrayList();
+ Match tMatch = new Match( 2, tFirstEntry, tSecondEntry );
+ tMatch.setSourceCodeSlice( "// ----- ACCESSEURS avec éléments -----");
+ tList.add( tMatch );
+
+ CPD tCpd = new MockCpd( 100, new JavaLanguage(), tList.iterator() );
+
+ mojo.writeNonHtml( tCpd );
+
+ File tReport = new File( "target/test/unit/default-configuration/target/cpd.xml" );
+ // parseDocument( new BufferedInputStream( new FileInputStream( report ) ) );
+
+ DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ Document pmdCpdDocument = builder.parse( tReport );
+ assertNotNull( pmdCpdDocument );
+ }
+
+ public static class MockCpd extends CPD
+ {
+
+ private Iterator matches;
+
+ public MockCpd( int minimumTileSize, Language language, Iterator tMatch )
+ {
+ super( minimumTileSize, language );
+ matches = tMatch;
+ }
+
+ public Iterator getMatches()
+ {
+ return matches;
+ }
+
+ }
}