Search in sources :

Example 1 with CPDConfiguration

use of net.sourceforge.pmd.cpd.CPDConfiguration 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 CPDConfiguration

use of net.sourceforge.pmd.cpd.CPDConfiguration 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)

Aggregations

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