From 4bb88a7ddb60ec398fe335d472de0a7d01608005 Mon Sep 17 00:00:00 2001 From: uchitsa Date: Mon, 16 Dec 2024 20:17:02 +0300 Subject: [PATCH] fix quality violations --- .../com/jcabi/log/VerboseProcessTest.java | 83 ++++++++++--------- 1 file changed, 45 insertions(+), 38 deletions(-) diff --git a/src/test/java/com/jcabi/log/VerboseProcessTest.java b/src/test/java/com/jcabi/log/VerboseProcessTest.java index 25274bc2..9eedac93 100644 --- a/src/test/java/com/jcabi/log/VerboseProcessTest.java +++ b/src/test/java/com/jcabi/log/VerboseProcessTest.java @@ -102,29 +102,31 @@ void echosUnicodeCorrectly() { @Test void runsACommandLineScriptWithException() { Assumptions.assumeFalse(SystemUtils.IS_OS_WINDOWS, ""); - final VerboseProcess process = new VerboseProcess( - new ProcessBuilder("cat", "/non-existing-file.txt") - .redirectErrorStream(true) - ); - try { - process.stdout(); - Assertions.fail("exception expected"); - } catch (final IllegalArgumentException ex) { - MatcherAssert.assertThat( - "should be no such file exception", - ex.getMessage(), - Matchers.containsString("No such file or directory") - ); + try (VerboseProcess process = new VerboseProcess( + new ProcessBuilder("cat", "/non-existing-file.txt").redirectErrorStream(true) + )) { + try { + process.stdout(); + Assertions.fail("exception expected"); + } catch (final IllegalArgumentException ex) { + MatcherAssert.assertThat( + "should be no such file exception", + ex.getMessage(), + Matchers.containsString("No such file or directory") + ); + } } } @Test void runsACommandLineScriptWithExceptionNoRedir() throws Exception { Assumptions.assumeFalse(SystemUtils.IS_OS_WINDOWS, ""); - final VerboseProcess process = new VerboseProcess( + final VerboseProcess.Result result; + try (VerboseProcess process = new VerboseProcess( new ProcessBuilder("cat", "/non-existing-file.txt") - ); - final VerboseProcess.Result result = process.waitFor(); + )) { + result = process.waitFor(); + } MatcherAssert.assertThat( "should be 1", result.code(), @@ -140,14 +142,15 @@ void runsACommandLineScriptWithExceptionNoRedir() throws Exception { @Test void handlesLongRunningCommand() { Assumptions.assumeFalse(SystemUtils.IS_OS_WINDOWS, ""); - final VerboseProcess process = new VerboseProcess( + try (VerboseProcess process = new VerboseProcess( new ProcessBuilder("/bin/bash", "-c", "sleep 2; echo 'done'") - ); - MatcherAssert.assertThat( - "should be done", - process.stdout(), - Matchers.startsWith("done") - ); + )) { + MatcherAssert.assertThat( + "should be done", + process.stdout(), + Matchers.startsWith("done") + ); + } } @Test @@ -208,18 +211,21 @@ void rejectsStderrWithLevelAll() { void quietlyTerminatesLongRunningProcess() throws Exception { Assumptions.assumeFalse(SystemUtils.IS_OS_WINDOWS, ""); final Process proc = new ProcessBuilder("sleep", "10000").start(); - final VerboseProcess process = new VerboseProcess(proc); - final CountDownLatch start = new CountDownLatch(1); - final CountDownLatch done = new CountDownLatch(1); - new Thread( + final CountDownLatch start; + final CountDownLatch done; + try (VerboseProcess process = new VerboseProcess(proc)) { + start = new CountDownLatch(1); + done = new CountDownLatch(1); + new Thread( new VerboseRunnable( - () -> { - start.countDown(); - process.stdoutQuietly(); - done.countDown(); - } - ) - ).start(); + () -> { + start.countDown(); + process.stdoutQuietly(); + done.countDown(); + } + ) + ).start(); + } start.await(); TimeUnit.SECONDS.sleep(1L); proc.destroy(); @@ -245,10 +251,11 @@ void stdoutQuietlyLogsErrors() { "cat", String.format("/non-existing-file-%s ", message) ); } - final VerboseProcess process = new VerboseProcess( - builder, Level.OFF, Level.WARNING - ); - process.stdoutQuietly(); + try (VerboseProcess process = new VerboseProcess( + builder, Level.OFF, Level.WARNING + )) { + process.stdoutQuietly(); + } MatcherAssert.assertThat( "should be contains 'hello dear friend' message", writer.toString(),