From 25904c907dcd5ba941f4191f5c8894852930cd82 Mon Sep 17 00:00:00 2001 From: Emmanuel Bigeon Date: Tue, 6 Dec 2016 14:23:31 -0500 Subject: [PATCH] upgrade test to avoid memory leaks --- gclc/pom.xml | 4 +-- .../bigeon/gclc/ConsoleApplicationTest.java | 18 ++++++++---- .../manager/SystemConsoleManagerTest.java | 29 ++++++++++++------- 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/gclc/pom.xml b/gclc/pom.xml index 6cc4ecd..0dd8e7e 100644 --- a/gclc/pom.xml +++ b/gclc/pom.xml @@ -35,7 +35,7 @@ 4.0.0 gclc - 1.3.5-SNAPSHOT + 1.3.6-SNAPSHOT jar http://www.bigeon.fr/emmanuel @@ -83,6 +83,6 @@ scm:git:gogs@git.code.bigeon.net:emmanuel/gclc.git - HEAD + gclc-1.3.5 diff --git a/gclc/src/test/java/fr/bigeon/gclc/ConsoleApplicationTest.java b/gclc/src/test/java/fr/bigeon/gclc/ConsoleApplicationTest.java index 5cdc62d..5c8c03c 100644 --- a/gclc/src/test/java/fr/bigeon/gclc/ConsoleApplicationTest.java +++ b/gclc/src/test/java/fr/bigeon/gclc/ConsoleApplicationTest.java @@ -43,6 +43,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.io.IOException; @@ -55,7 +56,6 @@ import fr.bigeon.gclc.exception.CommandRunExceptionType; import fr.bigeon.gclc.exception.InvalidCommandName; import fr.bigeon.gclc.i18n.Messages; import fr.bigeon.gclc.manager.ConsoleManager; -import fr.bigeon.gclc.manager.SystemConsoleManager; import fr.bigeon.gclc.manager.PipedConsoleManager; /** Test class for ConsoleApplication @@ -70,9 +70,14 @@ public class ConsoleApplicationTest { /** Test the base of a console application */ @Test public void test() { - ConsoleTestApplication app = new ConsoleTestApplication( - new SystemConsoleManager()); - app.exit(); + + try (PipedConsoleManager manager = new PipedConsoleManager()) { + ConsoleTestApplication app = new ConsoleTestApplication(manager); + app.exit(); + } catch (IOException e) { + fail("System Console Manager failed"); + } + } @Test @@ -156,7 +161,10 @@ public class ConsoleApplicationTest { manager.type("exit"); th.join(); - } catch (IOException | InvalidCommandName | InterruptedException e) { + } catch (IOException | InvalidCommandName | + + InterruptedException e) { + assertNull(e); } diff --git a/gclc/src/test/java/fr/bigeon/gclc/manager/SystemConsoleManagerTest.java b/gclc/src/test/java/fr/bigeon/gclc/manager/SystemConsoleManagerTest.java index 0210670..b2b2712 100644 --- a/gclc/src/test/java/fr/bigeon/gclc/manager/SystemConsoleManagerTest.java +++ b/gclc/src/test/java/fr/bigeon/gclc/manager/SystemConsoleManagerTest.java @@ -65,13 +65,12 @@ public class SystemConsoleManagerTest { @Test public final void testPrompt() { - try { - PipedOutputStream outStream = new PipedOutputStream(); - InputStream in = new PipedInputStream(outStream); - final PrintStream out = new PrintStream(outStream); - final String test = "test"; - SystemConsoleManager manager = new SystemConsoleManager(System.out, - in, Charset.forName("UTF-8")); + final String test = "test"; + try (PipedOutputStream outStream = new PipedOutputStream(); + InputStream in = new PipedInputStream(outStream); + final PrintStream out = new PrintStream(outStream); + SystemConsoleManager manager = new SystemConsoleManager(System.out, + in, Charset.forName("UTF-8"))) { Thread th = new Thread(new Runnable() { @@ -95,10 +94,18 @@ public class SystemConsoleManagerTest { * {@link fr.bigeon.gclc.manager.SystemConsoleManager#setPrompt(java.lang.String)}. */ @Test public final void testSetPrompt() { - SystemConsoleManager manager = new SystemConsoleManager(); - String prt = "++"; - manager.setPrompt(prt); - assertEquals(prt, manager.getPrompt()); + try (PipedOutputStream outStream = new PipedOutputStream(); + InputStream in = new PipedInputStream(outStream); + final PrintStream out = new PrintStream(outStream); + SystemConsoleManager manager = new SystemConsoleManager(System.out, + in, Charset.forName("UTF-8"))) { + + String prt = "++"; + manager.setPrompt(prt); + assertEquals(prt, manager.getPrompt()); + } catch (IOException e) { + assertNull(e); + } } /** Test method for