Skip to content

Commit f74679d

Browse files
SONARJAVA-4951 Replace sonar-java specific verifier with analyzer-commons library (#4786)
Use the CheckVerifier provided by the sonar-analyzer-commons testing module instead of the sonar-java-specific implementation to improve testing and allow for better maintanance and updates of the verifier logic.
1 parent 308fcf3 commit f74679d

899 files changed

Lines changed: 8164 additions & 4648 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/CUSTOM_RULES_101.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -682,14 +682,25 @@ See: [pom.xml#L137-L197](./java-custom-rules-example/pom_SQ_9_9_LTS.xml#L137-L19
682682
You can raise an issue on a given line, but you can also raise it at a specific Token.
683683
Because of that, you may want to specify, in your sample code used by your Unit Tests, the exact location, i.e. in between which 2 specific Columns, where you are expecting the issue to be raised.
684684

685-
This can be achieved using the special keywords `sc` (start-column) and `ec` (end-column) in the `// Noncompliant` comment.
686-
In the following example, we are expecting to have the issue being raised between the columns 27 and 32 (i.e. exactly on the "Order" variable type):
687-
685+
> **Deprecated**
686+
>
687+
>This can be achieved using the special keywords `sc` (start-column) and `ec` (end-column) in the `// Noncompliant` comment.
688+
>In the following example, we are expecting to have the issue being raised between the columns 27 and 32 (i.e. exactly on the "Order" variable type):
689+
> ```java
690+
> public String updateOrder(Order order){ // Noncompliant [[sc=27;ec=32]] {{Don't use Order here because it's an @Entity}}
691+
> // ...
692+
> }
693+
> ```
694+
695+
Assert precise primary location of an issue with the comment on a line below containing required number of `^`.
688696
```java
689-
public String updateOrder(Order order){ // Noncompliant [[sc=27;ec=32]] {{Don't use Order here because it's an @Entity}}
697+
public String updateOrder(Order order){ // Noncompliant {{Don't use Order here because it's an @Entity}}
698+
// ^^^^^
690699
// ...
691700
}
692701
```
702+
More information on how to test precise issue location can be found in: [SonarSource Analyzer Test Commons](https://github.com/SonarSource/sonar-analyzer-commons/tree/master/test-commons) library.
703+
693704

694705
### How to test the Source Version in a rule
695706

docs/java-custom-rules-example/src/test/files/NoIfStatementInTestsRule.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ public class ATest {
99

1010
@Test
1111
public void myTest() {
12-
if (someCondition) { // Noncompliant [[sc=5;ec=7]] {{Remove this 'if' statement from this test.}}
12+
if (someCondition) { // Noncompliant {{Remove this 'if' statement from this test.}}
13+
// ^^
1314
// verify something
1415
}
1516
}

docs/java-custom-rules-example/src/test/files/SecurityAnnotationMandatoryRule.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ class BusinessClassDelegate implements MySecurityInterface, SecondInterface {
1212
public void aMethod() { }
1313

1414
@org.foo.MyOtherSecurityAnnotation
15-
public void anotherMethod() { } // Noncompliant [[sc=15;ec=28]] {{Mandatory Annotation not set @MySecurityAnnotation}}
15+
public void anotherMethod() { } // Noncompliant {{Mandatory Annotation not set @MySecurityAnnotation}}
16+
// ^^^^^^^^^^^^^
1617

1718
@VariousAnnotation
18-
public void differentMethod() { } // Noncompliant [[startColumn=15;endColumn=30]] {{Mandatory Annotation not set @MySecurityAnnotation}}
19+
public void differentMethod() { } // Noncompliant {{Mandatory Annotation not set @MySecurityAnnotation}}
20+
// ^^^^^^^^^^^^^^^
1921
}
2022

2123
class OtherClass implements FirstInterface, SecondInterface {

docs/java-custom-rules-example/src/test/files/SpringControllerRequestMappingEntityRule.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
public class HelloController {
77

88
@RequestMapping("/updateOrder")
9-
public String updateOrder(Order order, Client client) { // Noncompliant [[sc=29;ec=34]] {{Don't use Order here because it's an @Entity}}
9+
public String updateOrder(Order order, Client client) { // Noncompliant {{Don't use Order here because it's an @Entity}}
10+
// ^^^^^
1011
return null;
1112
}
1213

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ruleKey": "S1128",
33
"hasTruePositives": true,
4-
"falseNegatives": 34,
4+
"falseNegatives": 33,
55
"falsePositives": 0
6-
}
6+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ruleKey": "S2699",
33
"hasTruePositives": true,
4-
"falseNegatives": 151,
4+
"falseNegatives": 150,
55
"falsePositives": 1
6-
}
6+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ruleKey": "S5786",
33
"hasTruePositives": true,
4-
"falseNegatives": 63,
4+
"falseNegatives": 62,
55
"falsePositives": 0
66
}

java-checks-test-sources/aws/src/main/files/non-compiling/checks/aws/AwsConsumerBuilderUsageCheckSample.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ public class AwsConsumerBuilderUsageCheckSample {
77

88
public void test() {
99
AwsConsumerBuilderUsageCheckSample.builder()
10-
.validDestination(Destination.builder() // Noncompliant [[sc=8;ec=24]] {{Consider using the Consumer Builder method instead of creating this nested builder.}}
10+
.validDestination(Destination.builder() // Noncompliant {{Consider using the Consumer Builder method instead of creating this nested builder.}}
11+
// ^^^^^^^^^^^^^^^^
1112
.toAddresses("to-email@domain.com")
1213
.bccAddresses("bcc-email@domain.com")
1314
.build())

java-checks-test-sources/aws/src/main/java/checks/aws/AwsConsumerBuilderUsageCheckSample.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public class AwsConsumerBuilderUsageCheckSample {
1818
void test_destination(Destination destination1) {
1919

2020
software.amazon.awssdk.services.sesv2.model.SendEmailRequest.builder()
21-
.destination(Destination.builder() // Noncompliant [[sc=8;ec=19]] {{Consider using the Consumer Builder method instead of creating this nested builder.}}
21+
.destination(Destination.builder() // Noncompliant {{Consider using the Consumer Builder method instead of creating this nested builder.}}
22+
// ^^^^^^^^^^^
2223
.toAddresses("to-email@domain.com")
2324
.bccAddresses("bcc-email@domain.com")
2425
.build())

java-checks-test-sources/aws/src/main/java/checks/aws/AwsCredentialsShouldBeSetExplicitlyCheckSample.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,28 @@ public class AwsCredentialsShouldBeSetExplicitlyCheckSample {
1212
public static final AwsClientBuilder BUILDER = S3Client.builder(); // Compliant FN - Could be configured closer to build call
1313

1414
void nonCompliant() {
15-
S3Client.builder().build(); // Noncompliant [[sc=5;ec=31]] {{Set the credentials provider explicitly on this builder.}}
16-
S3Client client = S3Client.builder().build(); // Noncompliant [[sc=23;ec=49]] {{Set the credentials provider explicitly on this builder.}}
17-
S3Client otherClient = S3Client.builder() // Noncompliant [[sl=+0;sc=28;el=+2;ec=15]] {{Set the credentials provider explicitly on this builder.}}
15+
S3Client.builder().build(); // Noncompliant {{Set the credentials provider explicitly on this builder.}}
16+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^
17+
S3Client client = S3Client.builder().build(); // Noncompliant {{Set the credentials provider explicitly on this builder.}}
18+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^
19+
S3Client otherClient = S3Client.builder() // Noncompliant {{Set the credentials provider explicitly on this builder.}}
20+
//^[sc=28;ec=14;el=+3]
1821
.region(Region.of(System.getenv(SdkSystemSetting.AWS_REGION.environmentVariable())))
1922
.build();
2023
}
2124

2225
void nonCompliantBuilderAsVariable() {
23-
S3ClientBuilder builder = S3Client.builder(); // Noncompliant [[sc=5;ec=50]] {{Set the credentials provider explicitly on this builder.}}
26+
S3ClientBuilder builder = S3Client.builder(); // Noncompliant {{Set the credentials provider explicitly on this builder.}}
27+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2428
builder.build();
2529

26-
S3ClientBuilder secondBuilder = S3Client.builder(); // Noncompliant [[sc=5;ec=56]] {{Set the credentials provider explicitly on this builder.}}
30+
S3ClientBuilder secondBuilder = S3Client.builder(); // Noncompliant {{Set the credentials provider explicitly on this builder.}}
31+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2732
secondBuilder.region(Region.of(System.getenv(SdkSystemSetting.AWS_REGION.environmentVariable())));
2833
secondBuilder.build();
2934

30-
S3ClientBuilder thirdBuilder = S3Client.builder() // Noncompliant [[sl=+0;sc=5;el=+1;ec=92]] {{Set the credentials provider explicitly on this builder.}}
35+
S3ClientBuilder thirdBuilder = S3Client.builder() // Noncompliant {{Set the credentials provider explicitly on this builder.}}
36+
//^[sc=5;ec=91;el=+2]
3137
.region(Region.of(System.getenv(SdkSystemSetting.AWS_REGION.environmentVariable())));
3238
thirdBuilder.build();
3339
}

0 commit comments

Comments
 (0)