Clean up container stop logic 49/110449/4
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 3 Mar 2024 07:00:10 +0000 (08:00 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 3 Mar 2024 07:07:45 +0000 (08:07 +0100)
Introduce nested try/catch blocks, so that the we do not need an
AtomicBoolean to track whether the container has been started or not.

JIRA: ODLPARENT-262
Change-Id: I9807cfeacd4d4e57f9d4b376e592233d2de70aba
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
features-test-plugin/src/main/java/org/opendaylight/odlparent/features/test/plugin/PaxExamExecution.java

index e94d385f31f1c1ca717732788b49b69f8518b673..cf8b1b3d14cbd00a72f7a3ba1d21ce28a1a6b148 100644 (file)
@@ -13,7 +13,6 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.io.PrintStream;
 import java.nio.charset.StandardCharsets;
-import java.util.concurrent.atomic.AtomicBoolean;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.ops4j.pax.exam.ExamSystem;
 import org.ops4j.pax.exam.TestContainer;
@@ -40,27 +39,29 @@ final class PaxExamExecution {
             final var stdout = System.out;
             System.setOut(new PrintStream(OutputStream.nullOutputStream(), true, StandardCharsets.UTF_8));
 
-            final var containerStarted = new AtomicBoolean(false);
             try {
-                container.start();
-                containerStarted.set(true);
-
-                // build probe
-                final var probeBuilder = examSystem.createProbe();
-                final var address = probeBuilder.addTest(TestProbe.class, "testFeature");
-                probeBuilder.addTest(TestProbe.CheckResult.class);
+                try {
+                    container.start();
+                } catch (RuntimeException e) {
+                    throw new MojoExecutionException(e);
+                }
 
-                // install probe bundle
-                container.install(probeBuilder.build().getStream());
-                // execute probe testMethod
-                container.call(address);
+                try {
+                    // build probe
+                    final var probeBuilder = examSystem.createProbe();
+                    final var address = probeBuilder.addTest(TestProbe.class, "testFeature");
+                    probeBuilder.addTest(TestProbe.CheckResult.class);
 
-            } catch (RuntimeException | IOException e) {
-                throw new MojoExecutionException(e);
-            } finally {
-                if (containerStarted.get()) {
+                    // install probe bundle
+                    container.install(probeBuilder.build().getStream());
+                    // execute probe testMethod
+                    container.call(address);
+                } catch (RuntimeException | IOException e) {
+                    throw new MojoExecutionException(e);
+                } finally {
                     container.stop();
                 }
+            } finally {
                 // restore stdout
                 System.setOut(stdout);
             }