Skip to content

Commit 9c20bb1

Browse files
Anmol202005rdiachenko
authored andcommitted
Implemented openrewrite testing framework for UpperEll Recipe
1 parent 44f96fe commit 9c20bb1

File tree

6 files changed

+309
-0
lines changed

6 files changed

+309
-0
lines changed

.github/workflows/ci.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
strategy:
12+
matrix:
13+
platform: [ubuntu-latest, macos-latest, windows-latest]
14+
runs-on: ${{ matrix.platform }}
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- name: Setup Maven cache
20+
uses: actions/cache@v4
21+
with:
22+
path: ~/.m2/repository
23+
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
24+
restore-keys: |
25+
${{ runner.os }}-maven-
26+
27+
- name: Set up JDK 11
28+
uses: actions/setup-java@v4
29+
with:
30+
java-version: '11'
31+
distribution: 'corretto'
32+
33+
- name: Build with Maven
34+
run: mvn -e --no-transfer-progress clean install

.gitignore

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Eclipse project files
2+
.settings
3+
.externalToolBuilders
4+
.classpath
5+
.project
6+
7+
# m2e-code-quality Eclipse IDE plugin temporary configuration files for Eclipse CS Checkstyle / PMD / SpotBugs Plug-Ins
8+
.checkstyle
9+
.pmd
10+
.pmdruleset.xml
11+
.fbExcludeFilterFile
12+
13+
# NetBeans project files
14+
nbactions.xml
15+
nb-configuration.xml
16+
17+
# Maven build folder
18+
target
19+
bin
20+
21+
# Maven Wrapper (modern setup — do not include jar/downloader)
22+
.mvn/wrapper/maven-wrapper.jar
23+
.mvn/wrapper/MavenWrapperDownloader.java
24+
25+
# IDEA project files
26+
checkstyle.iml
27+
checkstyle.ipr
28+
checkstyle.iws
29+
.idea
30+
31+
# Vscode project files
32+
.vscode
33+
34+
# Temp files
35+
*~
36+
37+
# Java Virtual machine crash logs
38+
hs_err_pid*
39+
replay_pid*
40+
41+
# Apple MAC OSX hidden file
42+
.DS_Store
43+
44+
# NonDex files
45+
.nondex
46+
47+
# temp folder for files/folders of ci validations
48+
.ci-temp
49+
50+
# OpenRewrite-generated impl files
51+
*.iml

pom.xml

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<groupId>org.checkstyle.autofix</groupId>
9+
<artifactId>checkstyle-openrewrite-recipes</artifactId>
10+
<version>1.0.0</version>
11+
<packaging>jar</packaging>
12+
13+
<name>checkstyle openrewrite recipes</name>
14+
<description>Automatically fix Checkstyle violations with OpenRewrite</description>
15+
<url>https://github.com/checkstyle/checkstyle-openrewrite-recipes</url>
16+
17+
<properties>
18+
<maven.compiler.source>11</maven.compiler.source>
19+
<maven.compiler.target>11</maven.compiler.target>
20+
<maven.compiler.plugin>3.11.0</maven.compiler.plugin>
21+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22+
23+
<rewrite.version>8.54.0</rewrite.version>
24+
<recipe.bom.version>3.9.0</recipe.bom.version>
25+
<junit.version>5.13.0</junit.version>
26+
<assertj.version>3.24.2</assertj.version>
27+
</properties>
28+
29+
<dependencyManagement>
30+
<dependencies>
31+
<dependency>
32+
<groupId>org.openrewrite.recipe</groupId>
33+
<artifactId>rewrite-recipe-bom</artifactId>
34+
<version>${recipe.bom.version}</version>
35+
<type>pom</type>
36+
<scope>import</scope>
37+
</dependency>
38+
</dependencies>
39+
</dependencyManagement>
40+
41+
<dependencies>
42+
<!-- OpenRewrite core dependencies - using consistent versions -->
43+
<dependency>
44+
<groupId>org.openrewrite</groupId>
45+
<artifactId>rewrite-java</artifactId>
46+
<version>${rewrite.version}</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.openrewrite</groupId>
50+
<artifactId>rewrite-java-11</artifactId>
51+
<version>${rewrite.version}</version>
52+
</dependency>
53+
54+
<!-- Test dependencies -->
55+
<dependency>
56+
<groupId>org.openrewrite</groupId>
57+
<artifactId>rewrite-test</artifactId>
58+
<version>${rewrite.version}</version>
59+
<scope>test</scope>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.junit.jupiter</groupId>
63+
<artifactId>junit-jupiter</artifactId>
64+
<version>${junit.version}</version>
65+
<scope>test</scope>
66+
</dependency>
67+
<dependency>
68+
<groupId>org.assertj</groupId>
69+
<artifactId>assertj-core</artifactId>
70+
<version>${assertj.version}</version>
71+
<scope>test</scope>
72+
</dependency>
73+
</dependencies>
74+
75+
<build>
76+
<plugins>
77+
<!-- Compiler plugin -->
78+
<plugin>
79+
<groupId>org.apache.maven.plugins</groupId>
80+
<artifactId>maven-compiler-plugin</artifactId>
81+
<version>${maven.compiler.plugin}</version>
82+
<configuration>
83+
<source>${maven.compiler.source}</source>
84+
<target>${maven.compiler.target}</target>
85+
<encoding>${project.build.sourceEncoding}</encoding>
86+
</configuration>
87+
</plugin>
88+
89+
<!-- Surefire plugin for running tests -->
90+
<plugin>
91+
<groupId>org.apache.maven.plugins</groupId>
92+
<artifactId>maven-surefire-plugin</artifactId>
93+
<version>3.2.2</version>
94+
<configuration>
95+
<systemPropertyVariables>
96+
<org.slf4j.simpleLogger.log.org.openrewrite>debug</org.slf4j.simpleLogger.log.org.openrewrite>
97+
</systemPropertyVariables>
98+
</configuration>
99+
</plugin>
100+
</plugins>
101+
</build>
102+
103+
104+
105+
</project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.checkstyle.autofix;
2+
3+
import org.openrewrite.Recipe;
4+
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
import org.checkstyle.autofix.recipe.UpperEllRecipe;
9+
10+
/**
11+
* Main recipe that automatically fixes all supported Checkstyle violations
12+
*/
13+
public class CheckstyleAutoFix extends Recipe {
14+
15+
@Override
16+
public String getDisplayName() {
17+
return "Checkstyle autoFix";
18+
}
19+
20+
@Override
21+
public String getDescription() {
22+
return "Automatically fixes Checkstyle violations.";
23+
}
24+
25+
@Override
26+
public List<Recipe> getRecipeList() {
27+
return Collections.singletonList(
28+
29+
new UpperEllRecipe()
30+
);
31+
}
32+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.checkstyle.autofix.recipe;
2+
3+
import org.openrewrite.ExecutionContext;
4+
import org.openrewrite.Recipe;
5+
import org.openrewrite.TreeVisitor;
6+
import org.openrewrite.java.JavaIsoVisitor;
7+
import org.openrewrite.java.tree.J;
8+
import org.openrewrite.java.tree.JavaType;
9+
10+
/**
11+
* Fixes Checkstyle UpperEll violations by replacing lowercase 'l' suffix
12+
* in long literals with uppercase 'L'.
13+
*/
14+
public class UpperEllRecipe extends Recipe {
15+
16+
@Override
17+
public String getDisplayName() {
18+
return "UpperEll recipe";
19+
}
20+
21+
@Override
22+
public String getDescription() {
23+
return "Replace lowercase 'l' suffix in long literals with uppercase 'L' to improve readability.";
24+
}
25+
26+
@Override
27+
public TreeVisitor<?, ExecutionContext> getVisitor() {
28+
return new UpperEllVisitor();
29+
}
30+
31+
private static class UpperEllVisitor extends JavaIsoVisitor<ExecutionContext> {
32+
@Override
33+
public J.Literal visitLiteral(J.Literal literal, ExecutionContext ctx) {
34+
J.Literal result = super.visitLiteral(literal, ctx);
35+
String valueSource = result.getValueSource();
36+
37+
if (valueSource != null && valueSource.endsWith("l")
38+
&& result.getType() == JavaType.Primitive.Long) {
39+
String numericPart = valueSource.substring(0, valueSource.length() - 1);
40+
String newValueSource = numericPart + "L";
41+
result = result.withValueSource(newValueSource);
42+
}
43+
return result;
44+
}
45+
46+
}
47+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.checkstyle.autofix.recipe;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.openrewrite.test.RecipeSpec;
5+
import org.openrewrite.test.RewriteTest;
6+
7+
import static org.junit.jupiter.api.Assertions.assertTrue;
8+
import static org.openrewrite.java.Assertions.java;
9+
10+
public class UpperEllRecipeTest implements RewriteTest {
11+
12+
@Override
13+
public void defaults(RecipeSpec spec) {
14+
spec.recipe(new UpperEllRecipe());
15+
}
16+
17+
@Test
18+
void fixesLowercaseLInLongLiteralsFromResources() {
19+
rewriteRun(
20+
21+
java(
22+
"class Test {\n" +
23+
" int value1 = 123l;\n" +
24+
" long value2 = 0x123l;\n" +
25+
" long value3 = 0123l;\n" +
26+
" long value4 = 0b101l;\n" +
27+
" String value5 = null;\n" +
28+
"}\n",
29+
"class Test {\n" +
30+
" int value1 = 123L;\n" +
31+
" long value2 = 0x123L;\n" +
32+
" long value3 = 0123L;\n" +
33+
" long value4 = 0b101L;\n" +
34+
" String value5 = null;\n" +
35+
"}\n"
36+
)
37+
);
38+
assertTrue(true, "Test completed successfully");
39+
}
40+
}

0 commit comments

Comments
 (0)