Skip to content

Commit

Permalink
Add Data Faker migration recipe coming from Java Faker
Browse files Browse the repository at this point in the history
Fixes #490
  • Loading branch information
timtebeek committed Mar 12, 2024
1 parent fad1832 commit 7e3c557
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
6 changes: 6 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/rewrite/datafaker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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();
}
}
"""
)
);
}
}

0 comments on commit 7e3c557

Please sign in to comment.