Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ out/
bin/
*.log
.vscode/
src/main/generated/
src/main/generated/
working-set*/
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public class SimplifyRedundantAssertJChains extends Recipe {
new MethodMatcher("org.assertj.core.api..* isNotPresent()"),
new MethodMatcher("org.assertj.core.api..* isTrue()"),
new MethodMatcher("org.assertj.core.api..* isFalse()"),
new MethodMatcher("org.assertj.core.api..* isNotEqualTo(..)"),
new MethodMatcher("org.assertj.core.api..* isNotSameAs(..)"),
// Note: isNotEqualTo and isNotSameAs are NOT here because they pass when actual is null
new MethodMatcher("org.assertj.core.api..* isInstanceOf(..)"),
new MethodMatcher("org.assertj.core.api..* hasSameClassAs(..)"),
new MethodMatcher("org.assertj.core.api..* hasToString(..)"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public class TruthThrowableAssertions extends Recipe {
private static final MethodMatcher HAS_MESSAGE_THAT = new MethodMatcher("com.google.common.truth.ThrowableSubject hasMessageThat()");
private static final MethodMatcher HAS_CAUSE_THAT = new MethodMatcher("com.google.common.truth.ThrowableSubject hasCauseThat()");
private static final MethodMatcher CONTAINS = new MethodMatcher("com.google.common.truth.StringSubject contains(..)");
private static final MethodMatcher CONTAINS_MATCH = new MethodMatcher("com.google.common.truth.StringSubject containsMatch(..)");
private static final MethodMatcher DOES_NOT_CONTAIN = new MethodMatcher("com.google.common.truth.StringSubject doesNotContain(..)");
private static final MethodMatcher STARTS_WITH = new MethodMatcher("com.google.common.truth.StringSubject startsWith(..)");
private static final MethodMatcher ENDS_WITH = new MethodMatcher("com.google.common.truth.StringSubject endsWith(..)");
private static final MethodMatcher IS_EQUAL_TO = new MethodMatcher("com.google.common.truth.Subject isEqualTo(..)");
private static final MethodMatcher IS_INSTANCE_OF = new MethodMatcher("com.google.common.truth.Subject isInstanceOf(..)");

Expand Down Expand Up @@ -75,6 +79,66 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
mi.getArguments().get(0));
}

// Handle hasMessageThat().containsMatch(regex)
if (CONTAINS_MATCH.matches(mi) && HAS_MESSAGE_THAT.matches(hasMethod)) {
maybeRemoveImport("com.google.common.truth.Truth");
maybeRemoveImport("com.google.common.truth.Truth.assertThat");
maybeAddImport("org.assertj.core.api.Assertions", "assertThat");
return JavaTemplate.builder("assertThat(#{any()}).hasMessageMatching(#{any()})")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3"))
.staticImports("org.assertj.core.api.Assertions.assertThat")
.build()
.apply(getCursor(),
mi.getCoordinates().replace(),
assertThat.getArguments().get(0),
mi.getArguments().get(0));
}

// Handle hasMessageThat().doesNotContain(text)
if (DOES_NOT_CONTAIN.matches(mi) && HAS_MESSAGE_THAT.matches(hasMethod)) {
maybeRemoveImport("com.google.common.truth.Truth");
maybeRemoveImport("com.google.common.truth.Truth.assertThat");
maybeAddImport("org.assertj.core.api.Assertions", "assertThat");
return JavaTemplate.builder("assertThat(#{any()}).hasMessageNotContaining(#{any()})")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3"))
.staticImports("org.assertj.core.api.Assertions.assertThat")
.build()
.apply(getCursor(),
mi.getCoordinates().replace(),
assertThat.getArguments().get(0),
mi.getArguments().get(0));
}

// Handle hasMessageThat().startsWith(prefix)
if (STARTS_WITH.matches(mi) && HAS_MESSAGE_THAT.matches(hasMethod)) {
maybeRemoveImport("com.google.common.truth.Truth");
maybeRemoveImport("com.google.common.truth.Truth.assertThat");
maybeAddImport("org.assertj.core.api.Assertions", "assertThat");
return JavaTemplate.builder("assertThat(#{any()}).hasMessageStartingWith(#{any()})")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3"))
.staticImports("org.assertj.core.api.Assertions.assertThat")
.build()
.apply(getCursor(),
mi.getCoordinates().replace(),
assertThat.getArguments().get(0),
mi.getArguments().get(0));
}

// Handle hasMessageThat().endsWith(suffix)
if (ENDS_WITH.matches(mi) && HAS_MESSAGE_THAT.matches(hasMethod)) {
maybeRemoveImport("com.google.common.truth.Truth");
maybeRemoveImport("com.google.common.truth.Truth.assertThat");
maybeAddImport("org.assertj.core.api.Assertions", "assertThat");
return JavaTemplate.builder("assertThat(#{any()}).hasMessageEndingWith(#{any()})")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "assertj-core-3"))
.staticImports("org.assertj.core.api.Assertions.assertThat")
.build()
.apply(getCursor(),
mi.getCoordinates().replace(),
assertThat.getArguments().get(0),
mi.getArguments().get(0));
}

// Handle hasMessageThat().isEqualTo(text)
if (IS_EQUAL_TO.matches(mi) && HAS_MESSAGE_THAT.matches(hasMethod)) {
maybeRemoveImport("com.google.common.truth.Truth");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,6 @@ void test(Object obj, Object other) {
assertThat(obj).isNotNull().isEqualTo(other);
assertThat(obj).isNotNull().isSameAs(other);

assertThat(obj).isNotNull().isNotEqualTo(other);
assertThat(obj).isNotNull().isNotSameAs(other);
assertThat(obj).isNotNull().isInstanceOf(String.class);
assertThat(obj).isNotNull().hasSameClassAs(other);
assertThat(obj).isNotNull().hasToString("text");
Expand All @@ -298,8 +296,6 @@ void test(Object obj, Object other) {
assertThat(obj).isNotNull().isEqualTo(other);
assertThat(obj).isNotNull().isSameAs(other);

assertThat(obj).isNotEqualTo(other);
assertThat(obj).isNotSameAs(other);
assertThat(obj).isInstanceOf(String.class);
assertThat(obj).hasSameClassAs(other);
assertThat(obj).hasToString("text");
Expand All @@ -310,6 +306,27 @@ void test(Object obj, Object other) {
);
}

@Test
void doesNotSimplifyIsNotNullBeforeNegatedAssertions() {
rewriteRun(
//language=java
java(
"""
import static org.assertj.core.api.Assertions.assertThat;

class Test {
void test(Object obj, Object other) {
// isNotEqualTo passes when actual is null, so isNotNull is NOT redundant
assertThat(obj).isNotNull().isNotEqualTo(other);
// isNotSameAs passes when actual is null, so isNotNull is NOT redundant
assertThat(obj).isNotNull().isNotSameAs(other);
}
}
"""
)
);
}

@Test
void simplifyArrayAssertions() {
rewriteRun(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,122 @@ void test() {
);
}

@Test
void hasMessageThatContainsMatch() {
rewriteRun(
//language=java
java(
"""
import static com.google.common.truth.Truth.assertThat;

class Test {
void test() {
Exception e = new IllegalArgumentException("Invalid argument provided");
assertThat(e).hasMessageThat().containsMatch("(?i:invalid)");
}
}
""",
"""
import static org.assertj.core.api.Assertions.assertThat;

class Test {
void test() {
Exception e = new IllegalArgumentException("Invalid argument provided");
assertThat(e).hasMessageMatching("(?i:invalid)");
}
}
"""
)
);
}

@Test
void hasMessageThatDoesNotContain() {
rewriteRun(
//language=java
java(
"""
import static com.google.common.truth.Truth.assertThat;

class Test {
void test() {
Exception e = new RuntimeException("Error occurred");
assertThat(e).hasMessageThat().doesNotContain("success");
}
}
""",
"""
import static org.assertj.core.api.Assertions.assertThat;

class Test {
void test() {
Exception e = new RuntimeException("Error occurred");
assertThat(e).hasMessageNotContaining("success");
}
}
"""
)
);
}

@Test
void hasMessageThatStartsWith() {
rewriteRun(
//language=java
java(
"""
import static com.google.common.truth.Truth.assertThat;

class Test {
void test() {
Exception e = new RuntimeException("Error occurred");
assertThat(e).hasMessageThat().startsWith("Error");
}
}
""",
"""
import static org.assertj.core.api.Assertions.assertThat;

class Test {
void test() {
Exception e = new RuntimeException("Error occurred");
assertThat(e).hasMessageStartingWith("Error");
}
}
"""
)
);
}

@Test
void hasMessageThatEndsWith() {
rewriteRun(
//language=java
java(
"""
import static com.google.common.truth.Truth.assertThat;

class Test {
void test() {
Exception e = new RuntimeException("Error occurred");
assertThat(e).hasMessageThat().endsWith("occurred");
}
}
""",
"""
import static org.assertj.core.api.Assertions.assertThat;

class Test {
void test() {
Exception e = new RuntimeException("Error occurred");
assertThat(e).hasMessageEndingWith("occurred");
}
}
"""
)
);
}

@Test
void multipleThrowableAssertions() {
rewriteRun(
Expand Down
Loading