Skip to content

Commit

Permalink
Rewrite TestNG Assertions to AssertJ
Browse files Browse the repository at this point in the history
and improve existing ones

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.
Occurrences, not rewritten by recipe were rewrote manually.

Co-authored-by: Mateusz Gajewski <[email protected]>
Co-authored-by: Sasha Sheikin <[email protected]>
  • Loading branch information
ssheikin and wendigo committed Jun 18, 2024
1 parent 24d6b27 commit 2635948
Show file tree
Hide file tree
Showing 140 changed files with 2,252 additions and 2,338 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()).hasSize(configuredHandlerCount);
}

@SuppressWarnings("AssignmentToStaticFieldFromInstanceMethod")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +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 +37,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 All @@ -63,7 +62,7 @@ public void testCombine()
@Override
protected void setup(Binder binder)
{
assertTrue(buildConfigObject(FooConfig.class).isFoo());
assertThat(buildConfigObject(FooConfig.class).isFoo()).isTrue();
binder.bind(String.class).annotatedWith(named("foo")).toInstance("fooInstance");
}
},
Expand All @@ -72,7 +71,7 @@ protected void setup(Binder binder)
@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 All @@ -83,8 +82,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 +92,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 +105,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
108 changes: 58 additions & 50 deletions bootstrap/src/test/java/io/airlift/bootstrap/TestLifeCycleManager.java
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 Down Expand Up @@ -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,23 @@ 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())
.as("Expected two suppressed exceptions")
.hasSize(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 +257,18 @@ 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())
.as("Suppressed exceptions list should be empty")
.hasSize(0);
}

assertEquals(ImmutableSet.copyOf(stateLog), ImmutableSet.of(
assertThat(ImmutableSet.copyOf(stateLog)).isEqualTo(ImmutableSet.of(
"postDependentInstance",
"preDestroyExceptionOne",
"preDestroyExceptionTwo",
Expand All @@ -275,26 +279,29 @@ public void testPreDestroyLoggingExceptionHandling()
public void testPostConstructExceptionCallsPreDestroy()
{
try {
Injector injector = Guice.createInjector(
Guice.createInjector(
Stage.PRODUCTION,
new LifeCycleModule(),
binder -> binder.bind(PostConstructExceptionInstance.class).in(Scopes.SINGLETON));
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())
.as("Expected two suppressed exceptions")
.hasSize(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 +319,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 +344,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 +366,12 @@ 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 +396,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 +420,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 2635948

Please sign in to comment.