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

Merge DataProvider migration recipe from MBoegers/migrate-testngtojupiter-rewrite #38

Draft
wants to merge 51 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
c4846ab
Add TestNG dependency, so it is available on the classpath for tests
Philzen Jun 6, 2024
09d1ee0
Implement TestNG → JUnit5 @Test annotation migration recipe with tests
Philzen Jun 6, 2024
a94d4f3
Fix extraneous import added when all usages are fully qualified
Philzen Jun 7, 2024
19b3099
Implement migration of expectedExceptionsMessageRegExp annotation att…
Philzen Jun 6, 2024
2f74be1
Implement migration of TestNG enabled = false to Junit5 @Disabled
Philzen Jun 7, 2024
d939333
Add general introductory text and Java 8 compatiblity badge
Philzen Jun 7, 2024
0b63586
Implement migration of TestNG @Test(description) to Junit5 @DisplayName
Philzen Jun 7, 2024
b2b006e
Implement migration of TestNG @Test(groups) to Junit5 @Tag
Philzen Jun 7, 2024
63a3572
Remove samples content
Philzen Jun 9, 2024
48532bf
Add build & usage writeup and remove unrelated content from template
Philzen Jun 17, 2024
f78a1a8
Add rewrite-testing-frameworks dependency (enables official recipes)
Philzen Jun 9, 2024
e57948c
Cook up basic recipe list including testing.junit5.AddMissingNested
Philzen Jun 8, 2024
374cf3d
Set up code coverage statistics
Philzen Jun 9, 2024
dcb19b1
[CI] Run Jacoco during `mvn verify` and upload XML output as artifact
Philzen Jun 9, 2024
855a52d
[CI] Activate dedicated Sonar GitHub action
Philzen Jun 9, 2024
3ec4c6b
Include code coverage badge
Philzen Jun 9, 2024
474c292
Make java parser instantiation thread safe and move to utils class
Philzen Jun 9, 2024
470db0f
Put common constant strings into static final variables
Philzen Jun 9, 2024
3059a88
Factor out post visitCompilationUnit visitor
Philzen Jun 10, 2024
4eaf295
Always modify instead of creating new when annotation type J.FieldAccess
Philzen Jun 10, 2024
79bb143
Implement migration of class-level `@Test` annotation to methods
Philzen Jun 11, 2024
ff1d555
Replace GitHub and SonarCloud badges with shields.io
Philzen Jun 11, 2024
305e89b
Move maven build action into Analyze workflow
Philzen Jun 12, 2024
c751669
Add list of annotation attributes that cannot be migrated (yet)
Philzen Jun 14, 2024
ed8c4f6
Avoid 'File encoding has not been set' maven warning, defining UTF-8
Philzen Jun 15, 2024
d89aaf1
Factor out complex J.* operations into Util classes
Philzen Jun 15, 2024
bc3ebcf
Remove redundant checks and return early wherever feasible
Philzen Jun 17, 2024
c583f94
Move repetitive Comparator declarations into Util class
Philzen Jun 17, 2024
68f70fe
Improvements on the template for maven users (#56)
ammachado Jun 20, 2024
3585aaf
Disable process-sources:copy goal
Philzen Jun 22, 2024
ac7b188
Add tests and dependency for Data Provider migration
MBoegers Apr 17, 2024
98177a0
Implement data provider migrations
MBoegers Apr 17, 2024
12f6372
Fix licence headers
MBoegers Apr 17, 2024
58c0050
Add Search Visitors for annotation usage
MBoegers Apr 18, 2024
acb321d
Use preconditions and annotation search
MBoegers Apr 18, 2024
f5e4fb2
Introduce search visitor for Annotation and helper to extract annotat…
MBoegers Apr 18, 2024
58a0641
Refactor to use helpers
MBoegers Apr 18, 2024
ae36444
remove unused method
MBoegers Apr 18, 2024
423b1e2
Move DataProvider recipe one package-level and rename helper
Philzen Jun 22, 2024
e70bbbb
Refactor MigrateDataProvider recipe, add javadoc to utilities
MBoegers Apr 22, 2024
d2e4e0b
Apply best practices
MBoegers Apr 24, 2024
13f1a8d
add packages to test files to fix import order
MBoegers Apr 26, 2024
bf07f9c
apply best practices
MBoegers Apr 26, 2024
7994e9e
Introduce JDK 8 compatibility
Philzen Jun 22, 2024
942895d
Make inner classes static
Philzen Jun 22, 2024
4c5fc0f
Fix trailing whitespace in text blocks
Philzen Jun 22, 2024
34390f1
Fix typos and remove undocumented JavaDoc tags
Philzen Jun 22, 2024
c098fa7
Remove unused methods
Philzen Jun 22, 2024
27d4d09
Convert helper class to non-instantiable enum utility class
Philzen Jun 22, 2024
a72cc39
Apply finality to variables and return early where appropriate
Philzen Jun 22, 2024
20f672e
Add MigrateDataProvider to recipe list
Philzen Jun 22, 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ target/
.idea/
out/
src/main/generated/
.vscode/
*.iml
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>
8 changes: 8 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ dependencies {

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

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

// ↓ 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