Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement assertions migration #3

Draft
wants to merge 58 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
6c85089
Add .sdkmanrc
Philzen Jun 6, 2024
2c5767f
Add rewrite-java-8
Philzen Jun 6, 2024
aa5d2c9
Configure gradle to target Java 8
Philzen Jun 6, 2024
a04d827
Fix link to https
Philzen Jun 6, 2024
8a9dff5
Add TestNG dependency, so it is available on the classpath for tests
Philzen Jun 6, 2024
8be5629
Implement TestNG → JUnit5 @Test annotation migration recipe with tests
Philzen Jun 6, 2024
f0843b7
Fix extraneous import added when all usages are fully qualified
Philzen Jun 7, 2024
475f065
Implement migration of expectedExceptionsMessageRegExp annotation att…
Philzen Jun 6, 2024
cfe376a
Implement migration of TestNG enabled = false to Junit5 @Disabled
Philzen Jun 7, 2024
8e4c9d6
Add general introductory text and Java 8 compatiblity badge
Philzen Jun 7, 2024
0beda40
Implement migration of TestNG @Test(description) to Junit5 @DisplayName
Philzen Jun 7, 2024
496d3bc
Implement migration of TestNG @Test(groups) to Junit5 @Tag
Philzen Jun 7, 2024
665e37c
Remove samples content
Philzen Jun 9, 2024
1b8628b
Add build & usage writeup and remove unrelated content from template
Philzen Jun 17, 2024
1fbad35
Add rewrite-testing-frameworks dependency (enables official recipes)
Philzen Jun 9, 2024
244af80
Cook up basic recipe list including testing.junit5.AddMissingNested
Philzen Jun 8, 2024
495a813
Set up code coverage statistics
Philzen Jun 9, 2024
72b9e6f
[CI] Run Jacoco during `mvn verify` and upload XML output as artifact
Philzen Jun 9, 2024
a4f6a9a
[CI] Activate dedicated Sonar GitHub action
Philzen Jun 9, 2024
aea352f
Include code coverage badge
Philzen Jun 9, 2024
faf3528
Make java parser instantiation thread safe and move to utils class
Philzen Jun 9, 2024
8a8437a
Put common constant strings into static final variables
Philzen Jun 9, 2024
e8c017b
Factor out post visitCompilationUnit visitor
Philzen Jun 10, 2024
f276ad5
Always modify instead of creating new when annotation type J.FieldAccess
Philzen Jun 10, 2024
bd3734a
Implement migration of class-level `@Test` annotation to methods
Philzen Jun 11, 2024
f0ac210
Replace GitHub and SonarCloud badges with shields.io
Philzen Jun 11, 2024
3919f7c
Move maven build action into Analyze workflow
Philzen Jun 12, 2024
7f5c9f2
Add list of annotation attributes that cannot be migrated (yet)
Philzen Jun 14, 2024
640987a
Avoid 'File encoding has not been set' maven warning, defining UTF-8
Philzen Jun 15, 2024
8cad449
Factor out complex J.* operations into Util classes
Philzen Jun 15, 2024
9e38b5c
Remove redundant checks and return early wherever feasible
Philzen Jun 17, 2024
ac92a85
Move repetitive Comparator declarations into Util class
Philzen Jun 17, 2024
082a798
Remove test scope from testng and jupiter dependencies
Philzen Jun 11, 2024
c18724f
Add migration script to for basic equals assertions
MBoegers Feb 23, 2024
f8d124c
add licences
MBoegers Feb 23, 2024
2297dfb
Add migration of fail messages for Assert.assertEquals
MBoegers Feb 23, 2024
2294033
Fix Jupiter mentions everywhere
MBoegers Feb 23, 2024
eb37f63
Fix tests for EqualsInt with Message
MBoegers Feb 23, 2024
a528ad2
Enhance tests for Assert.assertEquals
MBoegers Feb 23, 2024
ba60805
Add migrations for assertTrue, assertFalse, assertNoEquals
MBoegers Feb 23, 2024
86b5f2b
Update Readme and reformat
MBoegers Mar 2, 2024
7a922cc
apply moderne best practices
MBoegers Mar 2, 2024
d7adbad
Fix trailing whitespace and wrong argument count for format method
Philzen Jun 11, 2024
1583740
Add MigrateAssertions recipe to migration recipe list
Philzen Jun 11, 2024
73b5ad4
Apply special migration for TestNG assertEquals with arrays
Philzen Jun 11, 2024
3f61525
[TEMP] Rename
Philzen Jun 18, 2024
f6b6dd0
Apply special migration for TestNG assertEquals with iterators
Philzen Jun 14, 2024
1295bad
Apply special migration for TestNG assertNotEquals with arrays
Philzen Jun 11, 2024
0f3045f
Apply custom migration for TestNG assertEquals with iterators
Philzen Jun 14, 2024
4da9bb1
Implement assertNull() and assertNotNull() migration
Philzen Jun 11, 2024
bd6e285
Implement fail() migration
Philzen Jun 11, 2024
b07a544
Implement assertEquals & assertNotEquals float/double delta migration
Philzen Jun 11, 2024
c47bd6e
Exclude Refaster template from code coverage analysis
Philzen Jun 12, 2024
00afb18
Implement special migration for float/double-array delta assertion
Philzen Jun 12, 2024
00698d1
Implement assertSame / assertNotSame migration
Philzen Jun 12, 2024
c04ca94
Add comparison tests for assertEquals with Collection, Iterable, Map …
Philzen Jun 12, 2024
de28370
Implement assertEqualsNoOrder migration
Philzen Jun 14, 2024
b777abf
Implement assertThrows & expectThrows migration
Philzen Jun 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions .github/workflows/analyse.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: SonarCloud
on:
push:
branches: [main]
pull_request:
types: [opened, synchronize, reopened]

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: 17
distribution: 'zulu' # Alternative distribution options are available.
- name: Cache SonarCloud packages
uses: actions/cache@v4
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache Maven packages
uses: actions/cache@v4
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2

- name: "[Maven] Verify & generate JaCoCo XML"
env:
MAVEN_OPTS: '-Dstyle.color=always'
MAVEN_SWITCHES: >-
--show-version
--no-transfer-progress
--update-snapshots
--fail-at-end
--batch-mode
run: mvn ${{ env.MAVEN_SWITCHES }} -P coverage verify

- name: Upload maven build result
uses: actions/upload-artifact@v4
with:
name: maven-build-target-folder
path: target

- name: List current location
run: find .

- name: Analyze project
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
MAVEN_OPTS: >-
-Dsonar.projectKey=Philzen_rewrite-recipe-testng-to-junit-jupiter
-Dsonar.organization=philzen
-Dsonar.host.url=https://sonarcloud.io
run: mvn -B org.sonarsource.scanner.maven:sonar-maven-plugin:sonar ${{ env.MAVEN_OPTS }}
4 changes: 1 addition & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
name: ci
name: Build

on:
push:
Expand Down Expand Up @@ -36,8 +36,6 @@ jobs:
uses: gradle/actions/setup-gradle@v3
with:
arguments: ${{ env.GRADLE_SWITCHES }} build test
- name: verify
run: mvn --show-version --no-transfer-progress --update-snapshots --fail-at-end --batch-mode -Dstyle.color=always verify

publish-snapshots:
needs: [build]
Expand Down
3 changes: 3 additions & 0 deletions .sdkmanrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
gradle=8.8
java=21.0.3-tem
maven=3.9.7
162 changes: 66 additions & 96 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,83 +1,93 @@
## Rewrite recipe starter
# TestNG to Junit5 recipe   [![Compatible with Java 8](https://img.shields.io/badge/Works%20on%20Java-8-seagreen?logo=openjdk&labelColor=snow&logoColor=black)](#)

This repository serves as a template for building your own recipe JARs and publishing them to a repository where they can be applied on [app.moderne.io](https://app.moderne.io) against all the public OSS code that is included there.
[![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/philzen/rewrite-recipe-testng-to-junit-jupiter/ci.yml?logo=github)](https://github.com/Philzen/rewrite-recipe-testng-to-junit-jupiter/actions/workflows/ci.yml)  
[![Sonar Coverage](https://img.shields.io/sonar/coverage/Philzen_rewrite-recipe-testng-to-junit-jupiter?server=https%3A%2F%2Fsonarcloud.io&logo=sonarcloud&label=Coverage)](https://sonarcloud.io/summary/new_code?id=Philzen_rewrite-recipe-testng-to-junit-jupiter)

We've provided a sample recipe (NoGuavaListsNewArray) and a sample test class. Both of these exist as placeholders, and they should be replaced by whatever recipe you are interested in writing.
Converts [TestNG](https://testng.org/) test annotations and assertions to
[Junit 5](https://junit.org/junit5/docs/current/user-guide/).

To begin, fork this repository and customize it by:
Inspired by the [Migrate JUnit 4 @Test annotations to JUnit 5](https://docs.openrewrite.org/recipes/java/testing/junit5/updatetestannotation) recipe

1. Changing the root project name in `settings.gradle.kts`.
2. Changing the `group` in `build.gradle.kts`.
3. Changing the package structure from `com.yourorg` to whatever you want.
## Usage

## Getting started
### Prerequisites

Familiarize yourself with the [OpenRewrite documentation](https://docs.openrewrite.org/), in particular the [concepts & explanations](https://docs.openrewrite.org/concepts-explanations) op topics like the [lossless semantic trees](https://docs.openrewrite.org/concepts-explanations/lossless-semantic-trees), [recipes](https://docs.openrewrite.org/concepts-explanations/recipes) and [visitors](https://docs.openrewrite.org/concepts-explanations/visitors).
- cloned the project to your local machine
- if you're an SDKMAN!-Andy, simply run `sdk env` from the project directory
otherwise ensure:
- JDK 17+
- Maven 3.9.x *or* Gradle 8.x

You might be interested to watch some of the [videos available on OpenRewrite and Moderne](https://www.youtube.com/@moderne-auto-remediation).
are provided on your system.

Once you want to dive into the code there is a [comprehensive getting started guide](https://docs.openrewrite.org/authoring-recipes/recipe-development-environment)
available in the OpenRewrite docs that provides more details than the below README.
### Build & install this recipe to your local repository

## Reference recipes
From the project directory, run one of the following:

* [META-INF/rewrite/stringutils.yml](./src/main/resources/META-INF/rewrite/stringutils.yml) - A declarative YAML recipe that replaces usages of `org.springframework.util.StringUtils` with `org.apache.commons.lang3.StringUtils`.
- [UseApacheStringUtilsTest](src/test/java/org/philzen/oss/UseApacheStringUtilsTest.java) - A test class for the `com.yourorg.UseApacheStringUtils` recipe.
* [NoGuavaListsNewArrayList.java](src/main/java/org/philzen/oss/NoGuavaListsNewArrayList.java) - An imperative Java recipe that replaces usages of `com.google.common.collect.Lists` with `new ArrayList<>()`.
- [NoGuavaListsNewArrayListTest.java](src/test/java/org/philzen/oss/NoGuavaListsNewArrayListTest.java) - A test class for the `NoGuavaListsNewArrayList` recipe.
* [SimplifyTernary](src/main/java/org/philzen/oss/SimplifyTernary.java) - An Refaster style recipe that simplifies ternary expressions.
- [SimplifyTernaryTest](src/test/java/org/philzen/oss/SimplifyTernaryTest.java) - A test class for the `SimplifyTernary` recipe.
* [AssertEqualsToAssertThat](src/main/java/org/philzen/oss/AssertEqualsToAssertThat.java) - An imperative Java recipe that replaces JUnit's `assertEquals` with AssertJ's `assertThat`, to show how to handle classpath dependencies.
- [AssertEqualsToAssertThatTest](src/test/java/org/philzen/oss/AssertEqualsToAssertThatTest.java) - A test class for the `AssertEqualsToAssertThat` recipe.
* [AppendToReleaseNotes](src/main/java/org/philzen/oss/AppendToReleaseNotes.java) - A ScanningRecipe that appends a message to the release notes of a project.
- [AppendToReleaseNotesTest](src/test/java/org/philzen/oss/AppendToReleaseNotesTest.java) - A test class for the `AppendToReleaseNotes` recipe.
* [ClassHierarchy](src/main/java/org/philzen/oss/ClassHierarchy.java) - A recipe that demonstrates how to produce a data table on the class hierarchy of a project.
- [ClassHierarchyTest](src/test/java/org/philzen/oss/ClassHierarchyTest.java) - A test class for the `ClassHierarchy` recipe.
* [UpdateConcoursePipeline](src/main/java/org/philzen/oss/UpdateConcoursePipeline.java) - A recipe that demonstrates how to update a Concourse pipeline, as an example of operating on Yaml files.
- [UpdateConcoursePipelineTest](src/test/java/org/philzen/oss/UpdateConcoursePipelineTest.java) - A test class for the `UpdateConcoursePipeline` recipe.
<details><summary>Maven</summary>
<p>

## Local Publishing for Testing
```bash
mvn install -DskipTests
```
</p>
</details>

<details><summary>Gradle</summary>
<p>

Before you publish your recipe module to an artifact repository, you may want to try it out locally.
To do this on the command line, run:
```bash
./gradlew publishToMavenLocal
# or ./gradlew pTML
# or mvn install
```
</p>
</details>

This will publish to your local maven repository, typically under `~/.m2/repository`.

Replace the groupId, artifactId, recipe name, and version in the below snippets with the ones that correspond to your recipe.
### Migrate a project

<details><summary>Maven</summary>
<p>

In the `pom.xml` of a different project you wish to run the recipe on,
make it a plugin dependency of rewrite-maven-plugin:

In the pom.xml of a different project you wish to test your recipe out in, make your recipe module a plugin dependency of rewrite-maven-plugin:
```xml
<project>
<build>
<plugins>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>RELEASE</version>
<configuration>
<activeRecipes>
<recipe>com.yourorg.NoGuavaListsNewArrayList</recipe>
</activeRecipes>
</configuration>
<dependencies>
<dependency>
<groupId>com.yourorg</groupId>
<artifactId>rewrite-recipe-starter</artifactId>
<version>0.1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>5.33.0</version>
<configuration>
<activeRecipes>
<recipe>org.philzen.oss.testng.MigrateToJunit5</recipe>
</activeRecipes>
</configuration>
<dependencies>
<dependency>
<groupId>org.philzen.oss</groupId>
<artifactId>rewrite-testng-to-junit5</artifactId>
<version>1.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
```
Now run the recipe via `mvn rewrite:run`.
</details>

<details><summary>Gradle</summary>
<p>

Unlike Maven, Gradle must be explicitly configured to resolve dependencies from Maven local.
The root project of your Gradle build, make your recipe module a dependency of the `rewrite` configuration:
In the root project of a gradle build that you wish to run this recipe on,
make it a dependency of the `rewrite` configuration:

```groovy
plugins {
Expand All @@ -99,45 +109,5 @@ rewrite {
}
```

Now you can run `mvn rewrite:run` or `gradlew rewriteRun` to run your recipe.

## Publishing to Artifact Repositories

This project is configured to publish to Moderne's open artifact repository (via the `publishing` task at the bottom of
the `build.gradle.kts` file). If you want to publish elsewhere, you'll want to update that task.
[app.moderne.io](https://app.moderne.io) can draw recipes from the provided repository, as well as from [Maven Central](https://search.maven.org).

Note:
Running the publish task _will not_ update [app.moderne.io](https://app.moderne.io), as only Moderne employees can
add new recipes. If you want to add your recipe to [app.moderne.io](https://app.moderne.io), please ask the
team in [Slack](https://join.slack.com/t/rewriteoss/shared_invite/zt-nj42n3ea-b~62rIHzb3Vo0E1APKCXEA) or in [Discord](https://discord.gg/xk3ZKrhWAb).

These other docs might also be useful for you depending on where you want to publish the recipe:

* Sonatype's instructions for [publishing to Maven Central](https://maven.apache.org/repository/guide-central-repository-upload.html)
* Gradle's instructions on the [Gradle Publishing Plugin](https://docs.gradle.org/current/userguide/publishing\_maven.html).

### From Github Actions

The `.github` directory contains a Github action that will push a snapshot on every successful build.

Run the release action to publish a release version of a recipe.

### From the command line

To build a snapshot, run `./gradlew snapshot publish` to build a snapshot and publish it to Moderne's open artifact repository for inclusion at [app.moderne.io](https://app.moderne.io).

To build a release, run `./gradlew final publish` to tag a release and publish it to Moderne's open artifact repository for inclusion at [app.moderne.io](https://app.moderne.io).


## Applying OpenRewrite recipe development best practices

We maintain a collection of [best practices for writing OpenRewrite recipes](https://docs.openrewrite.org/recipes/recipes/openrewritebestpractices).
You can apply these recommendations to your recipes by running the following command:
```bash
./gradlew rewriteRun -Drewrite.activeRecipe=org.openrewrite.recipes.OpenRewriteBestPractices
```
or
```bash
mvn -U org.openrewrite.maven:rewrite-maven-plugin:run -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-recommendations:RELEASE -Drewrite.activeRecipes=org.openrewrite.recipes.OpenRewriteBestPractices
```
Now run the recipe via `gradlew rewriteRun`.
</details>
18 changes: 17 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ plugins {
group = "org.philzen.oss"
description = "Rewrite recipes."

tasks.named<JavaCompile>("compileJava") {
options.release.set(8)
}

dependencies {
// The bom version can also be set to a specific version
// https://github.com/openrewrite/rewrite-recipe-bom/releases
Expand All @@ -29,6 +33,7 @@ dependencies {
implementation("org.openrewrite.recipe:rewrite-java-dependencies")
implementation("org.openrewrite:rewrite-yaml")
implementation("org.assertj:assertj-core:3.24.2")
runtimeOnly("org.openrewrite:rewrite-java-8")
runtimeOnly("org.openrewrite:rewrite-java-17")

// Refaster style recipes need the rewrite-templating annotation processor and dependency for generated recipes
Expand All @@ -44,12 +49,23 @@ dependencies {
runtimeOnly("ch.qos.logback:logback-classic:1.2.+")

// Our recipe converts Guava's `Lists` type
testRuntimeOnly("com.google.guava:guava:latest.release")
testImplementation("com.google.guava:guava:latest.release")
testRuntimeOnly("org.apache.commons:commons-lang3:latest.release")
testRuntimeOnly("org.springframework:spring-core:latest.release")

// Contains the OpenRewriteBestPractices recipe, which you can apply to your recipes
rewrite("org.openrewrite.recipe:rewrite-recommendations:latest.release")

// ↓ Classpath resource for MigrateTestNg* recipes
implementation("org.testng:testng:7.5.1") // 7.5.x is the last Java 8 compatible version: https://github.com/testng-team/testng/issues/2775

// ↓ also make jupiter available for refaster compilation
compileOnly("org.junit.jupiter:junit-jupiter-api:latest.release")

// ↓ to allow using testing recipes in our recipe list
testRuntimeOnly("org.openrewrite.recipe:rewrite-testing-frameworks:latest.release") {
exclude("org.testcontainers", "testcontainers")
}
}

signing {
Expand Down
Loading
Loading