From 59715b74a1ef7cb3fcdb48544b6f4f2cf8208805 Mon Sep 17 00:00:00 2001 From: Chris Jaekl Date: Sat, 9 Jan 2016 18:29:27 +0900 Subject: [PATCH] More unit testing. --- test/net/jaekl/cfb/CFBTest.java | 50 ++++++++++++++++------------ test/net/jaekl/cfb/util/EnvMock.java | 14 ++++++-- 2 files changed, 39 insertions(+), 25 deletions(-) diff --git a/test/net/jaekl/cfb/CFBTest.java b/test/net/jaekl/cfb/CFBTest.java index 08d9c39..6d44667 100644 --- a/test/net/jaekl/cfb/CFBTest.java +++ b/test/net/jaekl/cfb/CFBTest.java @@ -171,33 +171,39 @@ public class CFBTest { null, null, "[cannot.load.fbmsg.file][null]\n[findbugs.home.is.not.set][FINDBUGS_HOME]\n" } }; - EnvMock envMock = EnvMock.mock_putInstance(); - - for (String[] datum : data) - { - String fbHome = datum[0]; - String filename = datum[1]; - String expected = datum[2]; + + try { + EnvMock envMock = EnvMock.mock_putInstance(); - try ( - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - ) + for (String[] datum : data) { - envMock.mock_putEnv(CFB.FINDBUGS_HOME, fbHome); - FBMsgFileNotFoundException exc = new FBMsgFileNotFoundException(filename); - m_cfb.reportException(pw, exc); + String fbHome = datum[0]; + String filename = datum[1]; + String expected = datum[2]; - pw.close(); - sw.close(); - - String actual = sw.toString(); - boolean pass = actual.endsWith(expected); - if (!pass) { - System.out.println("Expected:\n" + expected + "\nActual:\n" + actual); + try ( + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + ) + { + envMock.mock_putEnv(CFB.FINDBUGS_HOME, fbHome); + FBMsgFileNotFoundException exc = new FBMsgFileNotFoundException(filename); + m_cfb.reportException(pw, exc); + + pw.close(); + sw.close(); + + String actual = sw.toString(); + boolean pass = actual.endsWith(expected); + if (!pass) { + System.out.println("Expected:\n" + expected + "\nActual:\n" + actual); + } + assertTrue(pass); } - assertTrue(pass); } } + finally { + EnvMock.mock_resetInstance(); + } } } diff --git a/test/net/jaekl/cfb/util/EnvMock.java b/test/net/jaekl/cfb/util/EnvMock.java index cabf42f..a09ff5f 100644 --- a/test/net/jaekl/cfb/util/EnvMock.java +++ b/test/net/jaekl/cfb/util/EnvMock.java @@ -17,13 +17,21 @@ public class EnvMock extends Env { return super.getEnv(variableName); } + public void mock_putEnv(String variableName, String value) { + m_map.put(variableName, value); + } + public static EnvMock mock_putInstance() { EnvMock mock = new EnvMock(); - m_inst = mock; + synchronized(Env.class) { + m_inst = mock; + } return mock; } - public void mock_putEnv(String variableName, String value) { - m_map.put(variableName, value); + public static void mock_resetInstance() { + synchronized(Env.class) { + m_inst = null; + } } } -- 2.39.2