Skip to content

Commit d247386

Browse files
Anmol202005rdiachenko
authored andcommitted
Issue #41: add violation comment violation
1 parent 9389bf1 commit d247386

File tree

8 files changed

+76
-42
lines changed

8 files changed

+76
-42
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@
6565
</dependency>
6666

6767
<!-- Test dependencies -->
68+
<dependency>
69+
<groupId>com.google.truth</groupId>
70+
<artifactId>truth</artifactId>
71+
<version>1.4.4</version>
72+
<scope>test</scope>
73+
</dependency>
6874
<dependency>
6975
<groupId>com.puppycrawl.tools</groupId>
7076
<artifactId>checkstyle</artifactId>

src/test/java/org/checkstyle/autofix/recipe/AbstractRecipeTestSupport.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,23 @@ protected String getPackageLocation() {
5757
}
5858

5959
protected void verify(String testCaseName) throws Exception {
60-
verify(getCheckConfigurations(getInputFilePath(testCaseName)), testCaseName);
60+
61+
final String inputPath = getInputFilePath(testCaseName);
62+
final String outputPath = getOutputFilePath(testCaseName);
63+
final Configuration config = getCheckConfigurations(inputPath);
64+
verifyOutputFile(outputPath, config);
65+
66+
final List<CheckstyleViolation> violations = runCheckstyle(inputPath, config);
67+
68+
verifyWithInlineConfigParser(getPath(inputPath), convertToExpectedMessages(violations));
69+
70+
final String beforeCode = readFile(getPath(inputPath));
71+
final String expectedAfterCode = readFile(getPath(outputPath));
72+
final CheckConfiguration checkConfig = ConfigurationLoader.mapConfiguration(config);
73+
final Recipe mainRecipe = createRecipe(violations, checkConfig);
74+
testRecipe(beforeCode, expectedAfterCode,
75+
getPath(inputPath), new InputClassRenamer(),
76+
mainRecipe, new RemoveViolationComments());
6177
}
6278

6379
protected void verify(Configuration config, String testCaseName) throws Exception {
@@ -74,8 +90,7 @@ protected void verify(Configuration config, String testCaseName) throws Exceptio
7490
final CheckConfiguration checkConfig = ConfigurationLoader.mapConfiguration(config);
7591
final Recipe mainRecipe = createRecipe(violations, checkConfig);
7692
testRecipe(beforeCode, expectedAfterCode,
77-
getPath(inputPath), new InputClassRenamer(),
78-
new RemoveViolationComments(), mainRecipe);
93+
getPath(inputPath), new InputClassRenamer(), mainRecipe);
7994
}
8095

8196
private List<CheckstyleViolation> runCheckstyle(String inputPath,
@@ -120,6 +135,17 @@ private void verifyOutputFile(String outputPath, Configuration config) throws Ex
120135
}
121136
}
122137

138+
private String[] convertToExpectedMessages(List<CheckstyleViolation> violations) {
139+
return violations.stream()
140+
.map(violation -> {
141+
final String message;
142+
message = violation.getLine() + ":"
143+
+ violation.getColumn() + ": " + violation.getMessage();
144+
return message;
145+
})
146+
.toArray(String[]::new);
147+
}
148+
123149
private Configuration getCheckConfigurations(String inputPath) throws Exception {
124150
final String configFilePath = getPath(inputPath);
125151
final TestInputConfiguration testInputConfiguration =

src/test/resources/org/checkstyle/autofix/recipe/upperell/complexlongliterals/InputComplexLongLiterals.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,47 +15,49 @@
1515
package org.checkstyle.autofix.recipe.upperell.complexlongliterals;
1616

1717
public class InputComplexLongLiterals {
18-
private long withUnderscores = 1_000_000l;
19-
private long multipleUnderscores = 1_234_567_890l;
18+
private long withUnderscores = 1_000_000l; // violation 'Should use uppercase 'L'.'
19+
private long multipleUnderscores = 1_234_567_890l; // violation 'Should use uppercase 'L'.'
2020

21-
private long maxLong = 9223372036854775807l; //suppressed violation
22-
private long minLong = -9223372036854775808l; //suppressed violation
21+
private long maxLong = 9223372036854775807l; // suppressed violation
22+
private long minLong = -9223372036854775808l; // suppressed violation
2323

2424
private Long nullLong = null;
25-
private Long simpleLong = 1234l;
26-
private Long negativeLong = -5678l;
27-
private Long underscoreLong = 1_000_000l;
28-
Long maxLongObject = 9223372036854775807l; //suppressed violation
29-
Long minLongObject = -9223372036854775808l; //suppressed violation
25+
private Long simpleLong = 1234l; // violation 'Should use uppercase 'L'.'
26+
private Long negativeLong = -5678l; // violation 'Should use uppercase 'L'.'
27+
private Long underscoreLong = 1_000_000l; // violation 'Should use uppercase 'L'.'
28+
Long maxLongObject = 9223372036854775807l; // suppressed violation
29+
Long minLongObject = -9223372036854775808l; // suppressed violation
3030

3131
public long calculate(long input1, long input2) {
32-
return input1 + input2 + 1000l;
32+
return input1 + input2 + 1000l; // violation 'Should use uppercase 'L'.'
3333
}
3434

3535
public void lambdaUsage() {
36+
// violation below, 'Should use uppercase 'L'.'
3637
java.util.function.LongSupplier supplier = () -> 42l;
38+
// 3 violations below
3739
java.util.Arrays.asList(1l, 2l, 3l).forEach(System.out::println);
3840
}
3941

4042
public java.util.List<Long> getNumbers() {
41-
return java.util.Arrays.asList(100l, 200l, 300l);
43+
return java.util.Arrays.asList(100l, 200l, 300l); // 3 violations
4244
}
4345

4446
public void longObjectOperations() {
4547
Long a = null;
46-
Long b = 1234l;
47-
Long c = Long.valueOf(5678l);
48-
Long d = new Long(9999l);
48+
Long b = 1234l; // violation 'Should use uppercase 'L'.'
49+
Long c = Long.valueOf(5678l); // violation 'Should use uppercase 'L'.'
50+
Long d = new Long(9999l); // violation 'Should use uppercase 'L'.'
4951

50-
Long conditional = (a != null) ? a : 0l;
51-
long primitive = b != null ? b : 0l;
52-
Long boxed = primitive + 1000l;
52+
Long conditional = (a != null) ? a : 0l; // violation 'Should use uppercase 'L'.'
53+
long primitive = b != null ? b : 0l; // violation 'Should use uppercase 'L'.'
54+
Long boxed = primitive + 1000l; // violation 'Should use uppercase 'L'.'
5355
}
5456

5557
public Long methodReturningLong(Long param) {
5658
if (param == null) {
57-
return 12345l;
59+
return 12345l; // violation 'Should use uppercase 'L'.'
5860
}
59-
return param + 6789l;
61+
return param + 6789l; // violation 'Should use uppercase 'L'.'
6062
}
6163
}

src/test/resources/org/checkstyle/autofix/recipe/upperell/complexlongliterals/OutputComplexLongLiterals.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ public class OutputComplexLongLiterals {
1818
private long withUnderscores = 1_000_000L;
1919
private long multipleUnderscores = 1_234_567_890L;
2020

21-
private long maxLong = 9223372036854775807l; //suppressed violation
22-
private long minLong = -9223372036854775808l; //suppressed violation
21+
private long maxLong = 9223372036854775807l; // suppressed violation
22+
private long minLong = -9223372036854775808l; // suppressed violation
2323

2424
private Long nullLong = null;
2525
private Long simpleLong = 1234L;
2626
private Long negativeLong = -5678L;
2727
private Long underscoreLong = 1_000_000L;
28-
Long maxLongObject = 9223372036854775807l; //suppressed violation
29-
Long minLongObject = -9223372036854775808l; //suppressed violation
28+
Long maxLongObject = 9223372036854775807l; // suppressed violation
29+
Long minLongObject = -9223372036854775808l; // suppressed violation
3030

3131
public long calculate(long input1, long input2) {
3232
return input1 + input2 + 1000L;

src/test/resources/org/checkstyle/autofix/recipe/upperell/hexoctalliteral/InputHexOctalLiteral.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@
1414
package org.checkstyle.autofix.recipe.upperell.hexoctalliteral;
1515

1616
public class InputHexOctalLiteral {
17-
private long hexLower = 0x1ABCl;
18-
private long hexUpper = 0X2DEFl;
19-
private long octal = 0777l;
20-
private long binary = 0b1010l;
21-
private long decimal = 12345l;
17+
private long hexLower = 0x1ABCl; // violation 'Should use uppercase 'L'.'
18+
private long hexUpper = 0X2DEFl; // violation 'Should use uppercase 'L'.'
19+
private long octal = 0777l; // violation 'Should use uppercase 'L'.'
20+
private long binary = 0b1010l; // violation 'Should use uppercase 'L'.'
21+
private long decimal = 12345l; // violation 'Should use uppercase 'L'.'
2222

2323
public void calculateValues() {
24+
// violation below, 'Should use uppercase 'L'.'
2425
long hexResult = 0xDEADBEEFl + 0xDEADBEFl; //suppressed violation for 0xDEADBEFl
26+
// violation below, 'Should use uppercase 'L'.'
2527
long octalResult = 01234l + 0xDEADBEEFl; //suppressed violation for 01234l
26-
long binaryResult = 0b11110000l;
28+
long binaryResult = 0b11110000l; // violation 'Should use uppercase 'L'.'
2729
}
2830
}

src/test/resources/org/checkstyle/autofix/recipe/upperell/stringandcomments/InputStringAndComments.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ public class InputStringAndComments {
1616
private String message = "The value 456l should not change in strings";
1717
private String code = "long value = 789l;"; // This 789l in string should not change
1818

19-
// Only this actual long literal should change
20-
private long actualLong = 999l;
19+
private long actualLong = 999l; // violation 'Should use uppercase 'L'.'
2120

2221
/*
2322
* Multi-line comment with 111l should not change
2423
*/
2524
public void method() {
2625
// Single line comment with 222l should not change
27-
long value = 333l; // This should change
26+
long value = 333l; // violation 'Should use uppercase 'L'.'
2827
}
2928
}

src/test/resources/org/checkstyle/autofix/recipe/upperell/stringandcomments/OutputStringAndComments.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ public class OutputStringAndComments {
1616
private String message = "The value 456l should not change in strings";
1717
private String code = "long value = 789l;"; // This 789l in string should not change
1818

19-
// Only this actual long literal should change
2019
private long actualLong = 999L;
2120

2221
/*
2322
* Multi-line comment with 111l should not change
2423
*/
2524
public void method() {
2625
// Single line comment with 222l should not change
27-
long value = 333L; // This should change
26+
long value = 333L;
2827
}
2928
}

src/test/resources/org/checkstyle/autofix/recipe/upperell/symbolicliterals/InputSymbolicLiterals.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
package org.checkstyle.autofix.recipe.upperell.symbolicliterals;
1111

1212
public class InputSymbolicLiterals {
13-
private long minLong = -9223372036854775808l;
14-
private Long negativeLong = -5678l;
15-
long a = -0xAl;
16-
long b = +-12l;
17-
long c = +-(-(-(-4l)));;
13+
private long minLong = -9223372036854775808l; // violation 'Should use uppercase 'L'.'
14+
private Long negativeLong = -5678l; // violation 'Should use uppercase 'L'.'
15+
long a = -0xAl; // violation 'Should use uppercase 'L'.'
16+
long b = +-12l; // violation 'Should use uppercase 'L'.'
17+
long c = +-(-(-(-4l)));; // violation 'Should use uppercase 'L'.'
1818
}

0 commit comments

Comments
 (0)