From 7e3c557bd8212f9b2451bf8bbfb9c6b4f094f293 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 13 Mar 2024 00:51:01 +0100 Subject: [PATCH] Add Data Faker migration recipe coming from Java Faker Fixes https://github.com/openrewrite/rewrite-testing-frameworks/issues/490 --- build.gradle.kts | 6 ++ .../resources/META-INF/rewrite/datafaker.yml | 2 +- .../datafaker/JavaFakerToDataFakerTest.java | 67 +++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/openrewrite/java/testing/datafaker/JavaFakerToDataFakerTest.java diff --git a/build.gradle.kts b/build.gradle.kts index d7b422639..6e03b6a63 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -48,6 +48,12 @@ dependencies { testRuntimeOnly("org.gradle:gradle-tooling-api:latest.release") testRuntimeOnly("com.tngtech.archunit:archunit:0.23.1") + testRuntimeOnly("com.github.javafaker:javafaker:latest.release") { + exclude(group = "org.yaml", module = "snakeyaml") + } + testRuntimeOnly("net.datafaker:datafaker:latest.release") { + exclude(group = "org.yaml", module = "snakeyaml") + } testRuntimeOnly("org.testcontainers:testcontainers:latest.release") testRuntimeOnly("org.testcontainers:nginx:latest.release") diff --git a/src/main/resources/META-INF/rewrite/datafaker.yml b/src/main/resources/META-INF/rewrite/datafaker.yml index ef0bc3067..da059092e 100644 --- a/src/main/resources/META-INF/rewrite/datafaker.yml +++ b/src/main/resources/META-INF/rewrite/datafaker.yml @@ -27,7 +27,7 @@ recipeList: newFullyQualifiedTypeName: net.datafaker.providers.base.Relationship - org.openrewrite.java.ChangeMethodName: methodPattern: com.github.javafaker.Faker crypto() - name: hashing + newMethodName: hashing - org.openrewrite.java.ChangePackage: oldPackageName: com.github.javafaker newPackageName: net.datafaker diff --git a/src/test/java/org/openrewrite/java/testing/datafaker/JavaFakerToDataFakerTest.java b/src/test/java/org/openrewrite/java/testing/datafaker/JavaFakerToDataFakerTest.java new file mode 100644 index 000000000..0701a3084 --- /dev/null +++ b/src/test/java/org/openrewrite/java/testing/datafaker/JavaFakerToDataFakerTest.java @@ -0,0 +1,67 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.java.testing.datafaker; + +import org.junit.jupiter.api.Test; +import org.openrewrite.java.JavaParser; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.java; + +class JavaFakerToDataFakerTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.recipeFromResource("/META-INF/rewrite/datafaker.yml", "org.openrewrite.java.testing.datafaker.JavaFakerToDataFaker") + .parser(JavaParser.fromJavaVersion().classpath("javafaker", "datafaker")); + } + + @Test + void javaFakerToDataFaker() { + rewriteRun( + //language=java + java( + """ + import com.github.javafaker.Faker; + class A { + void method() { + Faker faker = new Faker(); + String name = faker.name().fullName(); + String address = faker.address().fullAddress(); + + String md5 = faker.crypto().md5(); + String relationship = faker.relationships().sibling(); + } + } + """, + """ + import net.datafaker.Faker; + class A { + void method() { + Faker faker = new Faker(); + String name = faker.name().fullName(); + String address = faker.address().fullAddress(); + + String md5 = faker.hashing().md5(); + String relationship = faker.relationships().sibling(); + } + } + """ + ) + ); + } +}