Skip to content

Commit

Permalink
Apply OpenRewrite best practices to MaskCreditCardNumbers
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Jun 29, 2024
1 parent 1826c68 commit 14b5d7f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public String getDescription() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.Literal visitLiteral(J.Literal literal, ExecutionContext executionContext) {
J.Literal l = super.visitLiteral(literal, executionContext);
public J.Literal visitLiteral(J.Literal literal, ExecutionContext ctx) {
J.Literal l = super.visitLiteral(literal, ctx);
if(l.getValue() instanceof String) {
String value = (String) l.getValue();
Matcher m = CC_PATTERN.matcher(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.staticanalysis;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

Expand All @@ -29,37 +30,42 @@ public void defaults(RecipeSpec spec) {
spec.recipe(new MaskCreditCardNumbers());
}

@DocumentExample
@Test
void noSpaces() {
rewriteRun(
//language=java
java("""
class A {
String cc = "1234567890123456";
}
""",
java(
"""
class A {
String cc = "12345678XXXXXXXX";
}
""")
class A {
String cc = "1234567890123456";
}
""",
"""
class A {
String cc = "12345678XXXXXXXX";
}
"""
)
);
}

@Test
void withSpaces() {
rewriteRun(
//language=java
java("""
class A {
String cc = "1234 5678 9012 3456";
}
""",
"""
class A {
String cc = "1234 5678 XXXX XXXX";
}
""")
java(
"""
class A {
String cc = "1234 5678 9012 3456";
}
""",
"""
class A {
String cc = "1234 5678 XXXX XXXX";
}
"""
)
);
}
}

0 comments on commit 14b5d7f

Please sign in to comment.