Code duplication check for java does not seem to work

I cannot seem to trigger the code duplication check of sonar.
Here is the output saying no code duplications:
https://sonarcloud.io/dashboard?id=org.rulez.demokracia%3APDEngine&pullRequest=273
Here is the build output: https://app.shippable.com/github/edemo/PDEngine/runs/548/summary/console
Here is the pull request with all the code: https://github.com/edemo/PDEngine/pull/273

I am using sonar-maven-plugin:3.6.0.1398:sonar

The code executing it:
mvn sonar:sonar -Dsonar.organization=edemo
-Dsonar.pullrequest.provider=github
-Dsonar.pullrequest.github.repository={REPO_FULL_NAME} \ -Dsonar.pullrequest.branch={HEAD_BRANCH}
-Dsonar.pullrequest.key={PULL_REQUEST}\ -Dsonar.pullrequest.base={BASE_BRANCH}
-Dsonar.github.oauth={GITHUB_OAUTH}\ -Dsonar.github.repository={REPO_FULL_NAME}
-Dsonar.issuesReport.console.enable=true
-Dsonar.github.login=magwas
-Dsonar.externalIssuesReportPaths=issuesReport.json
-Dsonar.cpd.{language}.minimumTokens=2\ -Dsonar.cpd.{language}.minimumLines=2

The project contains this code:

@tested_feature("Vote")
@tested_operation("Cast vote")
@tested_behaviour("if there was a cast vote from the same user, the old one is deleted")
@Test 
public void cast_vote_records_the_vote_with_different_user_votesCast() {
	String ballot = voteManager.obtainBallot(adminInfo.voteId, adminInfo.adminKey);
	List<RankedChoice> theCastVote = new ArrayList<RankedChoice>();
	Vote vote = getTheVote();
	vote.canVote = true;
	
	vote.votesCast.clear();
	vote.addCastVote("OtherUser", theCastVote, "OtherSecret");
	vote.votesCast.clear();
	vote.addCastVote("OtherUser", theCastVote, "OtherSecret");
	vote.votesCast.clear();
	vote.addCastVote("OtherUser", theCastVote, "OtherSecret");
	vote.votesCast.clear();
	vote.addCastVote("OtherUser", theCastVote, "OtherSecret");
	vote.votesCast.clear();
	vote.addCastVote("OtherUser", theCastVote, "OtherSecret");
	
	voteManager.castVote(adminInfo.voteId, ballot, theCastVote);
	CastVote voteCast = new CastVote("test_user_in_ws_context", theCastVote, "Secret");
	
	assertTrue(voteCast.preferences.containsAll(vote.votesCast.get(0).preferences));
}

@tested_feature("Vote")
@tested_operation("Cast vote")
@tested_behaviour("if there was a cast vote from the same user, the old one is deleted")
@Test 
public void this_is_the_same_test_as_above() {
	String ballot = voteManager.obtainBallot(adminInfo.voteId, adminInfo.adminKey);
	List<RankedChoice> theCastVote = new ArrayList<RankedChoice>();
	Vote vote = getTheVote();
	vote.canVote = true;
	
	vote.votesCast.clear();
	vote.addCastVote("OtherUser", theCastVote, "OtherSecret");
	vote.votesCast.clear();
	vote.addCastVote("OtherUser", theCastVote, "OtherSecret");
	vote.votesCast.clear();
	vote.addCastVote("OtherUser", theCastVote, "OtherSecret");
	vote.votesCast.clear();
	vote.addCastVote("OtherUser", theCastVote, "OtherSecret");
	vote.votesCast.clear();
	vote.addCastVote("OtherUser", theCastVote, "OtherSecret");
	
	voteManager.castVote(adminInfo.voteId, ballot, theCastVote);
	CastVote voteCast = new CastVote("test_user_in_ws_context", theCastVote, "Secret");
	
	assertTrue(voteCast.preferences.containsAll(vote.votesCast.get(0).preferences));
}

Hi,

You may be interested in how duplicated blocks are detected (expand the ‘Language-specific details’ block for the info you’re after). Off-hand, it looks like your block isn’t long enough to be noticed by the duplication detection engine.

Also, you’re supposed to replace the .{language}. part of the parameters with an actual language name, e.g. sonar.cpd.java.minimumTokens (altho to be honest, I’m not entirely sure it’s tunable for Java).

 
Ann

The documentation says that in case of java we need at least 10 tokens. For me it seems that the duplicated code is much more longer than 10 tokens.

Hi,

The docs say (emphasis mine):

There should be at least 10 successive and duplicated statements whatever the number of tokens and lines.

 
Ann

And the code is more than 10 successive and duplicated statements.

Hi,

Could you just try adding a few more pairs in there?

 
Ann

I decided to use PMD for duplication analysis.

1 Like