Search in sources :

Example 1 with Match

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

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

the class CpdReportGenerator method generate.

/**
     * Method that generates the contents of the CPD report
     *
     * @param matches
     */
@SuppressWarnings("deprecation")
public void generate(Iterator<Match> matches) {
    beginDocument();
    if (!matches.hasNext()) {
        sink.paragraph();
        sink.text(bundle.getString("report.cpd.noProblems"));
        sink.paragraph_();
    }
    while (matches.hasNext()) {
        Match match = matches.next();
        String code = match.getSourceCodeSlice();
        sink.table();
        sink.tableRow();
        sink.tableHeaderCell();
        sink.text(bundle.getString("report.cpd.column.file"));
        sink.tableHeaderCell_();
        if (aggregate) {
            sink.tableHeaderCell();
            sink.text(bundle.getString("report.cpd.column.project"));
            sink.tableHeaderCell_();
        }
        sink.tableHeaderCell();
        sink.text(bundle.getString("report.cpd.column.line"));
        sink.tableHeaderCell_();
        sink.tableRow_();
        // Iterating on every token entry
        for (Iterator<Mark> occurrences = match.iterator(); occurrences.hasNext(); ) {
            TokenEntry mark = occurrences.next().getToken();
            generateFileLine(mark);
        }
        // Source snippet
        sink.tableRow();
        int colspan = 2;
        if (aggregate) {
            ++colspan;
        }
        // TODO Cleaner way to do this?
        sink.rawText("<td colspan='" + colspan + "'>");
        sink.verbatim(false);
        sink.text(code);
        sink.verbatim_();
        sink.rawText("</td>");
        sink.tableRow_();
        sink.table_();
    }
    sink.section1_();
    sink.body_();
    sink.flush();
    sink.close();
}
Also used : TokenEntry(net.sourceforge.pmd.cpd.TokenEntry) Mark(net.sourceforge.pmd.cpd.Mark) Match(net.sourceforge.pmd.cpd.Match)

Example 3 with Match

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

the class CpdReport method filterMatches.

private Iterator<Match> filterMatches(Iterator<Match> matches) {
    getLog().debug("Filtering duplications. Using " + excludeDuplicationsFromFile.countExclusions() + " configured exclusions.");
    List<Match> filteredMatches = new ArrayList<>();
    int excludedDuplications = 0;
    while (matches.hasNext()) {
        Match match = matches.next();
        if (excludeDuplicationsFromFile.isExcludedFromFailure(match)) {
            excludedDuplications++;
        } else {
            filteredMatches.add(match);
        }
    }
    getLog().debug("Excluded " + excludedDuplications + " duplications.");
    return filteredMatches.iterator();
}
Also used : ArrayList(java.util.ArrayList) Match(net.sourceforge.pmd.cpd.Match)

Aggregations

Match (net.sourceforge.pmd.cpd.Match)3 ArrayList (java.util.ArrayList)2 Mark (net.sourceforge.pmd.cpd.Mark)2 TokenEntry (net.sourceforge.pmd.cpd.TokenEntry)2 File (java.io.File)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 CPD (net.sourceforge.pmd.cpd.CPD)1 CPDConfiguration (net.sourceforge.pmd.cpd.CPDConfiguration)1 JavaLanguage (net.sourceforge.pmd.cpd.JavaLanguage)1 SourceCode (net.sourceforge.pmd.cpd.SourceCode)1 Document (org.w3c.dom.Document)1