Search in sources :

Example 1 with CPD

use of net.sourceforge.pmd.cpd.CPD in project maven-plugins by apache.

the class CpdReportTest method testWriteNonHtml.

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", 2);
    TokenEntry tSecondEntry = new TokenEntry("public java", "MyClass3.java", 2);
    String duplicatedCodeFragment = "// ----- duplicated code example -----";
    SourceCode sourceCodeFirst = new SourceCode(new SourceCode.StringCodeLoader(PMD.EOL + duplicatedCodeFragment + PMD.EOL, "MyClass.java"));
    SourceCode sourceCodeSecond = new SourceCode(new SourceCode.StringCodeLoader(PMD.EOL + duplicatedCodeFragment + PMD.EOL, "MyClass3.java"));
    List<Match> tList = new ArrayList<>();
    Mark tFirstMark = new Mark(tFirstEntry);
    tFirstMark.setSourceCode(sourceCodeFirst);
    tFirstMark.setLineCount(1);
    Mark tSecondMark = new Mark(tSecondEntry);
    tSecondMark.setSourceCode(sourceCodeSecond);
    tSecondMark.setLineCount(1);
    Match tMatch = new Match(2, tFirstMark, tSecondMark);
    tList.add(tMatch);
    CPDConfiguration cpdConfiguration = new CPDConfiguration();
    cpdConfiguration.setMinimumTileSize(100);
    cpdConfiguration.setLanguage(new JavaLanguage());
    cpdConfiguration.setEncoding("UTF-8");
    CPD tCpd = new MockCpd(cpdConfiguration, tList.iterator());
    tCpd.go();
    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);
    String str = readFile(new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"));
    assertTrue(lowerCaseContains(str, "MyClass.java"));
    assertTrue(lowerCaseContains(str, "MyClass3.java"));
    assertTrue(lowerCaseContains(str, duplicatedCodeFragment));
}
Also used : SourceCode(net.sourceforge.pmd.cpd.SourceCode) CPDConfiguration(net.sourceforge.pmd.cpd.CPDConfiguration) ArrayList(java.util.ArrayList) Mark(net.sourceforge.pmd.cpd.Mark) JavaLanguage(net.sourceforge.pmd.cpd.JavaLanguage) Document(org.w3c.dom.Document) Match(net.sourceforge.pmd.cpd.Match) TokenEntry(net.sourceforge.pmd.cpd.TokenEntry) DocumentBuilder(javax.xml.parsers.DocumentBuilder) CPD(net.sourceforge.pmd.cpd.CPD) File(java.io.File)

Example 2 with CPD

use of net.sourceforge.pmd.cpd.CPD in project maven-plugins by apache.

the class CpdReport method executeCpd.

private void executeCpd() throws MavenReportException {
    if (cpd != null) {
        // CPD has already been run
        getLog().debug("CPD has already been run - skipping redundant execution.");
        return;
    }
    Properties p = new Properties();
    if (ignoreLiterals) {
        p.setProperty(JavaTokenizer.IGNORE_LITERALS, "true");
    }
    if (ignoreIdentifiers) {
        p.setProperty(JavaTokenizer.IGNORE_IDENTIFIERS, "true");
    }
    try {
        if (filesToProcess == null) {
            filesToProcess = getFilesToProcess();
        }
        try {
            excludeDuplicationsFromFile.loadExcludeFromFailuresData(excludeFromFailureFile);
        } catch (MojoExecutionException e) {
            throw new MavenReportException("Error loading exclusions", e);
        }
        String encoding = determineEncoding(!filesToProcess.isEmpty());
        Language cpdLanguage;
        if ("java".equals(language) || null == language) {
            cpdLanguage = new JavaLanguage(p);
        } else if ("javascript".equals(language)) {
            cpdLanguage = new EcmascriptLanguage();
        } else if ("jsp".equals(language)) {
            cpdLanguage = new JSPLanguage();
        } else {
            cpdLanguage = LanguageFactory.createLanguage(language, p);
        }
        CPDConfiguration cpdConfiguration = new CPDConfiguration();
        cpdConfiguration.setMinimumTileSize(minimumTokens);
        cpdConfiguration.setLanguage(cpdLanguage);
        cpdConfiguration.setSourceEncoding(encoding);
        cpd = new CPD(cpdConfiguration);
        for (File file : filesToProcess.keySet()) {
            cpd.add(file);
        }
    } catch (UnsupportedEncodingException e) {
        throw new MavenReportException("Encoding '" + getSourceEncoding() + "' is not supported.", e);
    } catch (IOException e) {
        throw new MavenReportException(e.getMessage(), e);
    }
    getLog().debug("Executing CPD...");
    cpd.go();
    getLog().debug("CPD finished.");
    // so the "check" goals can check for violations
    if (isXml()) {
        writeNonHtml(cpd);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) JSPLanguage(net.sourceforge.pmd.cpd.JSPLanguage) CPDConfiguration(net.sourceforge.pmd.cpd.CPDConfiguration) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Properties(java.util.Properties) JavaLanguage(net.sourceforge.pmd.cpd.JavaLanguage) EcmascriptLanguage(net.sourceforge.pmd.cpd.EcmascriptLanguage) JSPLanguage(net.sourceforge.pmd.cpd.JSPLanguage) Language(net.sourceforge.pmd.cpd.Language) JavaLanguage(net.sourceforge.pmd.cpd.JavaLanguage) CPD(net.sourceforge.pmd.cpd.CPD) EcmascriptLanguage(net.sourceforge.pmd.cpd.EcmascriptLanguage) File(java.io.File) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 3 with CPD

use of net.sourceforge.pmd.cpd.CPD in project maven-plugins by apache.

the class CpdReport method executeCpd.

private void executeCpd() throws MavenReportException {
    if (cpd != null) {
        // CPD has already been run
        getLog().debug("CPD has already been run - skipping redundant execution.");
        return;
    }
    Properties p = new Properties();
    if (ignoreLiterals) {
        p.setProperty(JavaTokenizer.IGNORE_LITERALS, "true");
    }
    if (ignoreIdentifiers) {
        p.setProperty(JavaTokenizer.IGNORE_IDENTIFIERS, "true");
    }
    try {
        if (filesToProcess == null) {
            filesToProcess = getFilesToProcess();
        }
        try {
            excludeDuplicationsFromFile.loadExcludeFromFailuresData(excludeFromFailureFile);
        } catch (MojoExecutionException e) {
            throw new MavenReportException("Error loading exclusions", e);
        }
        String encoding = determineEncoding(!filesToProcess.isEmpty());
        Language cpdLanguage;
        if ("java".equals(language) || null == language) {
            cpdLanguage = new JavaLanguage(p);
        } else if ("javascript".equals(language)) {
            cpdLanguage = new EcmascriptLanguage();
        } else if ("jsp".equals(language)) {
            cpdLanguage = new JSPLanguage();
        } else {
            cpdLanguage = LanguageFactory.createLanguage(language, p);
        }
        CPDConfiguration cpdConfiguration = new CPDConfiguration();
        cpdConfiguration.setMinimumTileSize(minimumTokens);
        cpdConfiguration.setLanguage(cpdLanguage);
        cpdConfiguration.setSourceEncoding(encoding);
        cpd = new CPD(cpdConfiguration);
        for (File file : filesToProcess.keySet()) {
            cpd.add(file);
        }
    } catch (UnsupportedEncodingException e) {
        throw new MavenReportException("Encoding '" + getSourceEncoding() + "' is not supported.", e);
    } catch (IOException e) {
        throw new MavenReportException(e.getMessage(), e);
    }
    getLog().debug("Executing CPD...");
    cpd.go();
    getLog().debug("CPD finished.");
    // so the "check" goals can check for violations
    if (isXml()) {
        writeNonHtml(cpd);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) JSPLanguage(net.sourceforge.pmd.cpd.JSPLanguage) CPDConfiguration(net.sourceforge.pmd.cpd.CPDConfiguration) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Properties(java.util.Properties) JavaLanguage(net.sourceforge.pmd.cpd.JavaLanguage) EcmascriptLanguage(net.sourceforge.pmd.cpd.EcmascriptLanguage) JSPLanguage(net.sourceforge.pmd.cpd.JSPLanguage) Language(net.sourceforge.pmd.cpd.Language) JavaLanguage(net.sourceforge.pmd.cpd.JavaLanguage) CPD(net.sourceforge.pmd.cpd.CPD) EcmascriptLanguage(net.sourceforge.pmd.cpd.EcmascriptLanguage) File(java.io.File) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 4 with CPD

use of net.sourceforge.pmd.cpd.CPD in project maven-plugins by apache.

the class CpdReportTest method testWriteNonHtml.

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", 2);
    TokenEntry tSecondEntry = new TokenEntry("public java", "MyClass3.java", 2);
    String duplicatedCodeFragment = "// ----- duplicated code example -----";
    SourceCode sourceCodeFirst = new SourceCode(new SourceCode.StringCodeLoader(PMD.EOL + duplicatedCodeFragment + PMD.EOL, "MyClass.java"));
    SourceCode sourceCodeSecond = new SourceCode(new SourceCode.StringCodeLoader(PMD.EOL + duplicatedCodeFragment + PMD.EOL, "MyClass3.java"));
    List<Match> tList = new ArrayList<>();
    Mark tFirstMark = new Mark(tFirstEntry);
    tFirstMark.setSourceCode(sourceCodeFirst);
    tFirstMark.setLineCount(1);
    Mark tSecondMark = new Mark(tSecondEntry);
    tSecondMark.setSourceCode(sourceCodeSecond);
    tSecondMark.setLineCount(1);
    Match tMatch = new Match(2, tFirstMark, tSecondMark);
    tList.add(tMatch);
    CPDConfiguration cpdConfiguration = new CPDConfiguration();
    cpdConfiguration.setMinimumTileSize(100);
    cpdConfiguration.setLanguage(new JavaLanguage());
    cpdConfiguration.setEncoding("UTF-8");
    CPD tCpd = new MockCpd(cpdConfiguration, tList.iterator());
    tCpd.go();
    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);
    String str = readFile(new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"));
    assertTrue(lowerCaseContains(str, "MyClass.java"));
    assertTrue(lowerCaseContains(str, "MyClass3.java"));
    assertTrue(lowerCaseContains(str, duplicatedCodeFragment));
}
Also used : SourceCode(net.sourceforge.pmd.cpd.SourceCode) CPDConfiguration(net.sourceforge.pmd.cpd.CPDConfiguration) ArrayList(java.util.ArrayList) Mark(net.sourceforge.pmd.cpd.Mark) JavaLanguage(net.sourceforge.pmd.cpd.JavaLanguage) Document(org.w3c.dom.Document) Match(net.sourceforge.pmd.cpd.Match) TokenEntry(net.sourceforge.pmd.cpd.TokenEntry) DocumentBuilder(javax.xml.parsers.DocumentBuilder) CPD(net.sourceforge.pmd.cpd.CPD) File(java.io.File)

Example 5 with CPD

use of net.sourceforge.pmd.cpd.CPD in project pmd-eclipse-plugin by pmd.

the class DetectCutAndPasteCmd method execute.

/**
 * @see name.herlin.command.AbstractProcessableCommand#execute()
 */
@Override
public void execute() throws CommandException {
    try {
        List<File> files = findCandidateFiles();
        if (files.isEmpty()) {
            logInfo("No files found for specified language.");
        } else {
            logInfo("Found " + files.size() + " files for the specified language. Performing CPD.");
        }
        setStepCount(files.size());
        beginTask("Finding suspect Cut And Paste", getStepCount() * 2);
        if (!isCanceled()) {
            final CPD cpd = detectCutAndPaste(files);
            if (!isCanceled()) {
                if (createReport) {
                    renderReport(cpd.getMatches());
                }
                notifyListeners(cpd);
            }
        }
    } catch (CoreException e) {
        LOG.debug("Core Exception: " + e.getMessage(), e);
        throw new CommandException(e);
    } catch (PropertiesException e) {
        LOG.debug("Properties Exception: " + e.getMessage(), e);
        throw new CommandException(e);
    } finally {
        setTerminated(true);
    }
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) PropertiesException(net.sourceforge.pmd.eclipse.runtime.properties.PropertiesException) CPD(net.sourceforge.pmd.cpd.CPD) CommandException(name.herlin.command.CommandException) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Aggregations

CPD (net.sourceforge.pmd.cpd.CPD)7 File (java.io.File)6 CPDConfiguration (net.sourceforge.pmd.cpd.CPDConfiguration)5 JavaLanguage (net.sourceforge.pmd.cpd.JavaLanguage)4 IOException (java.io.IOException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 EcmascriptLanguage (net.sourceforge.pmd.cpd.EcmascriptLanguage)2 JSPLanguage (net.sourceforge.pmd.cpd.JSPLanguage)2 Language (net.sourceforge.pmd.cpd.Language)2 Mark (net.sourceforge.pmd.cpd.Mark)2 Match (net.sourceforge.pmd.cpd.Match)2 SourceCode (net.sourceforge.pmd.cpd.SourceCode)2 TokenEntry (net.sourceforge.pmd.cpd.TokenEntry)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 MavenReportException (org.apache.maven.reporting.MavenReportException)2 IFile (org.eclipse.core.resources.IFile)2 Document (org.w3c.dom.Document)2