Skip to content

Commit

Permalink
Rewrite TestNG Assertions to AssertJ
Browse files Browse the repository at this point in the history
Changed automatically by openrewrite recipe
openrewrite/rewrite-testing-frameworks#520
```
<plugins>
    <plugin>
        <groupId>org.openrewrite.maven</groupId>
        <artifactId>rewrite-maven-plugin</artifactId>
        <version>5.32.0</version>
        <configuration>
            <activeRecipes>
                <recipe>org.openrewrite.java.testing.assertj.TestNgToAssertj</recipe>
            </activeRecipes>
        </configuration>
        <dependencies>
            <dependency>
                <groupId>org.openrewrite.recipe</groupId>
                <artifactId>rewrite-testing-frameworks</artifactId>
                <version>2.11.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </plugin>
</plugins>
```

run
```
./mvnw rewrite:run -Dmaven.javadoc.skip=true -DskipTests=true -Dmaven
.site.skip=true -Dmaven.artifact.threads=16 -T 1C -e -Dair.check.skip-all=true --no-snapshot-updates
```

AssertJ dependency to `pom.xml` added manually.
Also, some minor compilation/compatibility errors fixed manually.
  • Loading branch information
ssheikin committed Jun 18, 2024
1 parent 24d6b27 commit 0e092c8
Show file tree
Hide file tree
Showing 138 changed files with 2,079 additions and 2,204 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
import static io.airlift.testing.Assertions.assertContains;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;
import static org.assertj.core.api.Assertions.fail;

public class TestBootstrap
{
Expand Down Expand Up @@ -118,7 +117,7 @@ public void testLoggingConfiguredOnce()
new Bootstrap().setOptionalConfigurationProperty("log.path", "tcp://0.0.0.0:0").initialize();
int configuredHandlerCount = root.getHandlers().length;
new Bootstrap().setOptionalConfigurationProperty("log.path", "tcp://0.0.0.0:0").initialize();
assertEquals(root.getHandlers().length, configuredHandlerCount);
assertThat(root.getHandlers().length).isEqualTo(configuredHandlerCount);
}

@SuppressWarnings("AssignmentToStaticFieldFromInstanceMethod")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import org.testng.annotations.Test;

import static com.google.inject.name.Names.named;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

public class TestConfigurationAwareModule
Expand All @@ -38,9 +38,9 @@ public void testConfigAvailable()
.setRequiredConfigurationProperty("bar.enabled", "true")
.initialize();

assertEquals(injector.getInstance(Key.get(String.class, named("foo"))), "fooInstance");
assertEquals(injector.getInstance(Key.get(String.class, named("bar"))), "barInstance");
assertEquals(injector.getInstance(Key.get(String.class, named("abc"))), "abcInstance");
assertThat(injector.getInstance(Key.get(String.class, named("foo")))).isEqualTo("fooInstance");
assertThat(injector.getInstance(Key.get(String.class, named("bar")))).isEqualTo("barInstance");
assertThat(injector.getInstance(Key.get(String.class, named("abc")))).isEqualTo("abcInstance");
}

@Test
Expand Down Expand Up @@ -83,8 +83,8 @@ protected void setup(Binder binder)
.setRequiredConfigurationProperty("bar.enabled", "true")
.initialize();

assertEquals(injector.getInstance(Key.get(String.class, named("foo"))), "fooInstance");
assertEquals(injector.getInstance(Key.get(String.class, named("bar"))), "barInstance");
assertThat(injector.getInstance(Key.get(String.class, named("foo")))).isEqualTo("fooInstance");
assertThat(injector.getInstance(Key.get(String.class, named("bar")))).isEqualTo("barInstance");
}

public static class FooModule
Expand All @@ -93,7 +93,7 @@ public static class FooModule
@Override
protected void setup(Binder binder)
{
assertTrue(buildConfigObject(FooConfig.class).isFoo());
assertThat(buildConfigObject(FooConfig.class).isFoo()).isTrue();
install(new BarModule());
binder.bind(String.class).annotatedWith(named("foo")).toInstance("fooInstance");
binder.install(new AbcModule());
Expand All @@ -106,7 +106,7 @@ public static class BarModule
@Override
protected void setup(Binder binder)
{
assertTrue(buildConfigObject(BarConfig.class).isBar());
assertThat(buildConfigObject(BarConfig.class).isBar()).isTrue();
binder.bind(String.class).annotatedWith(named("bar")).toInstance("barInstance");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.fail;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;

@Test(singleThreaded = true)
public class TestLifeCycleManager
Expand Down Expand Up @@ -73,7 +72,7 @@ public void testImmediateStarts()
LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
lifeCycleManager.start();

assertEquals(stateLog, ImmutableList.of("InstanceThatUsesInstanceThatRequiresStart:OK"));
assertThat(stateLog).isEqualTo(ImmutableList.of("InstanceThatUsesInstanceThatRequiresStart:OK"));
}

@Test
Expand All @@ -94,10 +93,10 @@ protected void configure()

LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
lifeCycleManager.start();
assertEquals(stateLog, ImmutableList.of("postSimpleBaseImpl"));
assertThat(stateLog).isEqualTo(ImmutableList.of("postSimpleBaseImpl"));

lifeCycleManager.stop();
assertEquals(stateLog, ImmutableList.of("postSimpleBaseImpl", "preSimpleBaseImpl"));
assertThat(stateLog).isEqualTo(ImmutableList.of("postSimpleBaseImpl", "preSimpleBaseImpl"));
}

@Test
Expand All @@ -110,11 +109,11 @@ public void testSubClassAnnotated()

LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
lifeCycleManager.start();
assertEquals(stateLog, ImmutableList.of("postSimpleBaseImpl"));
assertThat(stateLog).isEqualTo(ImmutableList.of("postSimpleBaseImpl"));

lifeCycleManager.stop();

assertEquals(stateLog, ImmutableList.of("postSimpleBaseImpl", "preSimpleBaseImpl"));
assertThat(stateLog).isEqualTo(ImmutableList.of("postSimpleBaseImpl", "preSimpleBaseImpl"));
}

@Test
Expand All @@ -130,12 +129,12 @@ public void testExecuted()
LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
lifeCycleManager.start();
instance.waitForStart();
assertEquals(stateLog, ImmutableList.of("Starting"));
assertThat(stateLog).isEqualTo(ImmutableList.of("Starting"));

lifeCycleManager.stop();
instance.waitForEnd();

assertEquals(stateLog, ImmutableList.of("Starting", "Done"));
assertThat(stateLog).isEqualTo(ImmutableList.of("Starting", "Done"));
}

@Test
Expand All @@ -154,10 +153,10 @@ public void testDeepDependency()

LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
lifeCycleManager.start();
assertEquals(stateLog, ImmutableList.of("postDependentInstance"));
assertThat(stateLog).isEqualTo(ImmutableList.of("postDependentInstance"));

lifeCycleManager.stop();
assertEquals(stateLog, ImmutableList.of("postDependentInstance", "preDependentInstance"));
assertThat(stateLog).isEqualTo(ImmutableList.of("postDependentInstance", "preDependentInstance"));
}

@Test
Expand All @@ -168,7 +167,7 @@ public void testIllegalMethods()
Stage.PRODUCTION,
binder -> binder.bind(IllegalInstance.class).in(Scopes.SINGLETON),
new LifeCycleModule());
fail();
fail("");
}
catch (CreationException dummy) {
// correct behavior
Expand All @@ -187,7 +186,7 @@ public void testDuplicateMethodNames()
lifeCycleManager.start();
lifeCycleManager.stop();

assertEquals(stateLog, ImmutableList.of("foo"));
assertThat(stateLog).isEqualTo(ImmutableList.of("foo"));
}

@Test
Expand All @@ -206,7 +205,7 @@ public void testJITInjection()
lifeCycleManager.start();
lifeCycleManager.stop();

assertEquals(stateLog, ImmutableList.of("postDependentInstance", "preDependentInstance"));
assertThat(stateLog).isEqualTo(ImmutableList.of("postDependentInstance", "preDependentInstance"));
}

@Test
Expand All @@ -222,20 +221,20 @@ public void testPreDestroySuppressedExceptionHandling()

LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
lifeCycleManager.start();
assertEquals(stateLog, ImmutableList.of("postDependentInstance"));
assertThat(stateLog).isEqualTo(ImmutableList.of("postDependentInstance"));
try {
lifeCycleManager.stopWithoutFailureLogging();
fail("Expected exception to be thrown");
}
catch (LifeCycleStopException e) {
assertEquals(e.getSuppressed().length, 2, "Expected two suppressed exceptions");
assertThat(e.getSuppressed().length).as("Expected two suppressed exceptions").isEqualTo(2);
Set<String> suppressedMessages = Arrays.stream(e.getSuppressed())
.map(Throwable::getMessage)
.collect(Collectors.toSet());
assertEquals(ImmutableSet.copyOf(suppressedMessages), ImmutableSet.of("preDestroyExceptionOne", "preDestroyExceptionTwo"));
assertThat(ImmutableSet.copyOf(suppressedMessages)).isEqualTo(ImmutableSet.of("preDestroyExceptionOne", "preDestroyExceptionTwo"));
}

assertEquals(ImmutableSet.copyOf(stateLog), ImmutableSet.of(
assertThat(ImmutableSet.copyOf(stateLog)).isEqualTo(ImmutableSet.of(
"postDependentInstance",
"preDestroyExceptionOne",
"preDestroyExceptionTwo",
Expand All @@ -255,16 +254,16 @@ public void testPreDestroyLoggingExceptionHandling()

LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
lifeCycleManager.start();
assertEquals(stateLog, ImmutableList.of("postDependentInstance"));
assertThat(stateLog).isEqualTo(ImmutableList.of("postDependentInstance"));
try {
lifeCycleManager.stop();
fail("Expected exception to be thrown");
}
catch (LifeCycleStopException e) {
assertEquals(e.getSuppressed().length, 0, "Suppressed exceptions list should be empty");
assertThat(e.getSuppressed().length).as("Suppressed exceptions list should be empty").isEqualTo(0);
}

assertEquals(ImmutableSet.copyOf(stateLog), ImmutableSet.of(
assertThat(ImmutableSet.copyOf(stateLog)).isEqualTo(ImmutableSet.of(
"postDependentInstance",
"preDestroyExceptionOne",
"preDestroyExceptionTwo",
Expand All @@ -282,19 +281,17 @@ public void testPostConstructExceptionCallsPreDestroy()
fail("Expected injector creation to fail with an exception");
}
catch (CreationException e) {
assertEquals(ImmutableSet.copyOf(stateLog), ImmutableSet.of(
assertThat(ImmutableSet.copyOf(stateLog)).isEqualTo(ImmutableSet.of(
"postConstructFailure",
"preDestroyFailureAfterPostConstructFailureOne",
"preDestroyFailureAfterPostConstructFailureTwo"));
assertEquals(e.getCause().getClass(), LifeCycleStartException.class, "Expected LifeCycleStartException to be thrown, found: " + e.getCause().getClass());
assertEquals(e.getCause().getSuppressed().length, 2, "Expected two suppressed exceptions");
assertEquals(
ImmutableSet.copyOf(
Arrays.stream(e.getCause().getSuppressed())
.map(Throwable::getCause)
.map(Throwable::getMessage)
.collect(Collectors.toSet())),
ImmutableSet.of("preDestroyFailureAfterPostConstructFailureOne", "preDestroyFailureAfterPostConstructFailureTwo"));
assertThat(e.getCause().getClass()).as("Expected LifeCycleStartException to be thrown, found: " + e.getCause().getClass()).isEqualTo(LifeCycleStartException.class);
assertThat(e.getCause().getSuppressed().length).as("Expected two suppressed exceptions").isEqualTo(2);
assertThat(ImmutableSet.copyOf(
Arrays.stream(e.getCause().getSuppressed())
.map(Throwable::getCause)
.map(Throwable::getMessage)
.collect(Collectors.toSet()))).isEqualTo(ImmutableSet.of("preDestroyFailureAfterPostConstructFailureOne", "preDestroyFailureAfterPostConstructFailureTwo"));
}
}

Expand All @@ -312,10 +309,10 @@ public void testNoPreDestroy()

LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
lifeCycleManager.start();
assertEquals(stateLog, ImmutableList.of("makeMe"));
assertThat(stateLog).isEqualTo(ImmutableList.of("makeMe"));

lifeCycleManager.stop();
assertEquals(stateLog, ImmutableList.of("makeMe", "unmakeMe"));
assertThat(stateLog).isEqualTo(ImmutableList.of("makeMe", "unmakeMe"));
}

@Test
Expand All @@ -337,16 +334,15 @@ public void testModule()
lifeCycleManager.stop();

Set<String> stateLogSet = new HashSet<>(stateLog);
assertEquals(stateLogSet,
Sets.newHashSet(
"postDependentBoundInstance",
"postDependentInstance",
"postMakeOne",
"postMakeTwo",
"preDestroyTwo",
"preDestroyOne",
"preDependentInstance",
"preDependentBoundInstance"));
assertThat(stateLogSet).isEqualTo(Sets.newHashSet(
"postDependentBoundInstance",
"postDependentInstance",
"postMakeOne",
"postMakeTwo",
"preDestroyTwo",
"preDestroyOne",
"preDependentInstance",
"preDependentBoundInstance"));
}

@Test
Expand All @@ -360,10 +356,10 @@ public void testProvider()
LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);

lifeCycleManager.start();
assertEquals(stateLog, ImmutableList.of("postBarProvider", "postBarInstance"));
assertThat(stateLog).isEqualTo(ImmutableList.of("postBarProvider", "postBarInstance"));

lifeCycleManager.stop();
assertEquals(stateLog, ImmutableList.of("postBarProvider", "postBarInstance", "preBarInstance", "preBarProvider"));
assertThat(stateLog).isEqualTo(ImmutableList.of("postBarProvider", "postBarInstance", "preBarInstance", "preBarProvider"));
}

@Test
Expand All @@ -388,10 +384,10 @@ public BarInstance create()
LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);

lifeCycleManager.start();
assertEquals(stateLog, ImmutableList.of("postBarInstance"));
assertThat(stateLog).isEqualTo(ImmutableList.of("postBarInstance"));

lifeCycleManager.stop();
assertEquals(stateLog, ImmutableList.of("postBarInstance", "preBarInstance"));
assertThat(stateLog).isEqualTo(ImmutableList.of("postBarInstance", "preBarInstance"));
}

@Test
Expand All @@ -412,13 +408,13 @@ public BarInstance createBar()
}
});

assertNull(injector.getInstance(BarInstance.class));
assertThat(injector.getInstance(BarInstance.class)).isNull();

LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);

lifeCycleManager.start();
lifeCycleManager.stop();

assertNull(injector.getInstance(BarInstance.class));
assertThat(injector.getInstance(BarInstance.class)).isNull();
}
}
Loading

0 comments on commit 0e092c8

Please sign in to comment.