Skip to content

[scala] chore: Add test for #2153 - #7087

Merged
UncleOwen merged 2 commits into
pmd:mainfrom
lukasgraef:issue2153
Sep 26, 2026
Merged

UncleOwen merged 2 commits into
pmd:mainfrom
lukasgraef:issue2153

Conversation

@lukasgraef

@lukasgraef lukasgraef commented Sep 18, 2026 •

Copy link
Copy Markdown
Member

Describe the PR

The CPD failure in #2153 has already been fixed with PMD 6.25 (in 3df4506365).
This PR adds a regression test to close this issue.

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

@lukasgraef
lukasgraef marked this pull request as ready for review September 18, 2026 12:17
@pmd-actions-helper

pmd-actions-helper Bot commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

No relevant source code has been changed, pmdtester skipped.

(comment created at 2026-09-26 11:46:07+00:00 for 0cd3540)

@UncleOwen

Copy link
Copy Markdown
Member

I backported your test to PMD-6.20.0. I had to do some changes because of the old API. But the only material change I had to make the test pass is the addition of .trim() in the last line.

I don't think the test reproduces the failure.

/*
 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
 */

package net.sourceforge.pmd.cpd;

import static org.junit.Assert.assertTrue;

import java.io.StringWriter;

import org.junit.Test;

public class ScalaCpdTest {

    private static final String CODE = "object Foo {\n"
            + "    void foo() {\n"
            + "        String[] array = null;\n"
            + "        array = {\"abc\", \"def\", \"ghi\"};\n"
            + "    }\n"
            + "}\n";

    /**
     * @see <a href="https://github.com/pmd/pmd/issues/2153">GitHub Issue #2153</a>
     */
    @Test
    public void smallMinimumTokensRendersXml() throws Exception {
        CPDConfiguration configuration = new CPDConfiguration();
        configuration.setMinimumTileSize(20);
        configuration.setLanguage(new ScalaLanguage());
        CPD cpd = new CPD(configuration);
        cpd.add(new SourceCode(new SourceCode.StringCodeLoader(CODE, "Foo.scala")));
        cpd.add(new SourceCode(new SourceCode.StringCodeLoader(CODE, "Bar.scala")));
        cpd.go();
        assertTrue(cpd.getMatches().hasNext());
        CPDReport report = cpd.toReport();
        StringWriter xml = new StringWriter();
        new XMLRenderer().render(report, xml);
        // the XML serializer writes newlines as the platform line separator
        String rendered = xml.toString().replace("\r\n", "\n");
        assertTrue(rendered.contains("<codefragment><![CDATA[" + CODE.trim() + "]]></codefragment>"));
    }
}

@lukasgraef

Copy link
Copy Markdown
Member Author

I will later try out writing a test in PMD 6.20, but what I don't understand about your test sample is that CpdReport was only added with PMD 6.48 in 9fc8a56#diff-a81a6ecd467896b826edf1a7da74bdf4406be26578c33af208f4e8a884ee8b73 ?

Or am I missing something?

@UncleOwen

UncleOwen commented Sep 26, 2026 •

Copy link
Copy Markdown
Member

No, I must have made a mistake yesterday. The code I posted above only works for 6.48.0 and 6.55.0

Sorry about that.

Now I know what my mistake was. I first created the test in 6.55.0. Then I checked out 6.20.0. In that version, pmd-scala-modules doesn't exist, so my test wasn't executed at all.

@UncleOwen

Copy link
Copy Markdown
Member

Here's the new test code, which indeed fails in 6.20.0, with a very similar stacktrace to that in #2153

/*
 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
 */

package net.sourceforge.pmd.cpd;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class ScalaCpdTest {

    private static final String CODE = "object Foo {\n"
            + "    void foo() {\n"
            + "        String[] array = null;\n"
            + "        array = {\"abc\", \"def\", \"ghi\"};\n"
            + "    }\n"
            + "}\n";

    /**
     * @see <a href="https://github.com/pmd/pmd/issues/2153">GitHub Issue #2153</a>
     */
    @Test
    public void smallMinimumTokensRendersXml() throws Exception {
        CPDConfiguration configuration = new CPDConfiguration();
        configuration.setMinimumTileSize(20);
        configuration.setLanguage(new ScalaLanguage());
        CPD cpd = new CPD(configuration);
        cpd.add(new SourceCode(new SourceCode.StringCodeLoader(CODE, "Foo.scala")));
        cpd.add(new SourceCode(new SourceCode.StringCodeLoader(CODE, "Bar.scala")));
        cpd.go();
        assertTrue(cpd.getMatches().hasNext());
        String rendered = new XMLRenderer().render(cpd.getMatches()).replace("\r\n", "\n");
        assertTrue(rendered.contains("<codefragment><![CDATA[" + CODE.trim() + "]]></codefragment>"));
    }
}

@UncleOwen

Copy link
Copy Markdown
Member

The test fails in 6.24.0, but succeeds in 6.25.0 (when moved to pmd-scala-modules/pmd-scala-common/src/test/java/net/sourceforge/pmd/cpd)

@lukasgraef

Copy link
Copy Markdown
Member Author

Yes, it seems like the issue was actually fixed in 3df4506365

Sorry for the wrong attribution, I just speculated that the issue was resolved by the rewrite of CPD in PMD 7.

@UncleOwen UncleOwen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@UncleOwen
UncleOwen merged commit ef27e2a into pmd:main Sep 26, 2026
12 checks passed
@UncleOwen UncleOwen added this to the 7.29.0 milestone Sep 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[scala] CPD terminates with an exception when a small value is set for --minimum-tokens

2 participants