Skip to content

SONARJAVA-6196 S1451 should provide a default template for headers#5578

Merged
NoemieBenard merged 4 commits intomasterfrom
nb/sonarjava-6196
Apr 22, 2026
Merged

SONARJAVA-6196 S1451 should provide a default template for headers#5578
NoemieBenard merged 4 commits intomasterfrom
nb/sonarjava-6196

Conversation

@NoemieBenard
Copy link
Copy Markdown
Contributor

Add default template for headerFormat in S1451.

@hashicorp-vault-sonar-prod
Copy link
Copy Markdown
Contributor

hashicorp-vault-sonar-prod Bot commented Apr 17, 2026

SONARJAVA-6196

@sonar-review-alpha
Copy link
Copy Markdown
Contributor

sonar-review-alpha Bot commented Apr 17, 2026

Summary

This PR adds a meaningful default template for the headerFormat property in rule S1451 (file header check). Previously, the default was an empty string, which meant users had to manually configure the header format from scratch. The new default provides a complete copyright header template with placeholders (<Your-Product-Name>, <Year-From>, <Year-To>, <Your-Company-Name>) and guidance, allowing users to customize it in their SonarCloud/SonarQube quality profile rather than starting from nothing.

What reviewers should know

Review focus: This is a straightforward constant update to the rule's default behavior. The change affects how the rule behaves when enabled without explicit configuration.

Key points:

  • The template is a standard Java multi-line copyright comment block with helpful placeholders
  • This improves the out-of-the-box experience for users enabling S1451 — they'll see a concrete example to work from
  • The rule logic at line 67-69 checks if (headerFormat.isEmpty()) and short-circuits if true. With this change, the rule now runs with the template by default instead of being a no-op
  • The template includes a message ("Please configure this header...") to guide users toward customization

Testing to verify: Confirm that rules using this default now flag files that don't match the template pattern (regression test), and that customized headers still work as expected.


  • Generate Walkthrough
  • Generate Diagram

🗣️ Give feedback

Copy link
Copy Markdown
Contributor

@sonar-review-alpha sonar-review-alpha Bot left a comment

Choose a reason for hiding this comment

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

One real test coverage gap needs fixing before merge — the rest of the implementation is correct.

🗣️ Give feedback

Comment on lines +116 to +120
check = new FileHeaderCheck();
CheckVerifier.newVerifier()
.onFile(mainCodeSourcesPath("checks/FileHeaderCheck/Class6.java"))
.withCheck(check)
.verifyNoIssues();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The test verifies that Class6.java (which contains the default header) is compliant — but there is no negative test asserting that a file without the default header gets flagged.

This gap matters concretely: if DEFAULT_HEADER_FORMAT ever became empty (e.g., due to a text-block indentation accident), the headerFormat.isEmpty() branch would fire, set expectedLines = {}, and matches() would return true for any file. The verifyNoIssues() call would still pass, giving a false sense of correctness.

Add a complementary case that runs the default FileHeaderCheck() against a file that lacks the header and asserts an issue is raised — something like:

check = new FileHeaderCheck();
CheckVerifier.newVerifier()
  .onFile(mainCodeSourcesPath("checks/FileHeaderCheck/Class1.java"))
  .withCheck(check)
  .verifyIssueOnFile("Add or update the header of this file.");
  • Mark as noise

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's use desciprive file names for test samples.

private String getHeaderFormat() {
String format = headerFormat;
if(format.charAt(0) != '^') {
if (format.charAt(0) != '^') {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

IMO it's a bad practice to do unintentional chages, because they make it hard to understand what is happening, if someone is reading a PR for some reason in the future.

.onFile(mainCodeSourcesPath("checks/FileHeaderCheck/Class3.java"))
.withCheck(check)
.verifyNoIssues();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Are these a part of this PR or is it a copied from the other changes?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's remove expectedLines.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's remove this method.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should be extracted to checkExpectedLines

@sonarqube-next
Copy link
Copy Markdown

Copy link
Copy Markdown
Contributor

@sonar-review-alpha sonar-review-alpha Bot left a comment

Choose a reason for hiding this comment

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

LGTM! ✅

The text block is correct — the 4 spaces of incidental whitespace strip cleanly to produce the expected header, matching ClassDefaultHeader.java exactly. The @RuleProperty(defaultValue = DEFAULT_HEADER_FORMAT) annotation compiles fine since text blocks are compile-time constants. The previously flagged missing negative test has not been addressed.

🗣️ Give feedback

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM. I think we should take a look at the experience in local SQ to see how changing this rule will affect the users.

@NoemieBenard NoemieBenard merged commit b2a8eb4 into master Apr 22, 2026
17 checks passed
@NoemieBenard NoemieBenard deleted the nb/sonarjava-6196 branch April 22, 2026 12:49
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.

2 participants