]> jaekl.net Git - cfb.git/commitdiff
More unit testing.
authorChris Jaekl <cejaekl@yahoo.com>
Sat, 9 Jan 2016 09:29:27 +0000 (18:29 +0900)
committerChris Jaekl <cejaekl@yahoo.com>
Sat, 9 Jan 2016 09:29:27 +0000 (18:29 +0900)
test/net/jaekl/cfb/CFBTest.java
test/net/jaekl/cfb/util/EnvMock.java

index 08d9c393f626e431214536a7001933d6adce3301..6d446676e0e32d1243830f7c537fc1f44a0fc673 100644 (file)
@@ -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();
+               }
        }
 }
index cabf42fec8bd6e50b8595135992204422e8a975a..a09ff5ffefa11dd4923e11d69aa9d58378362a7f 100644 (file)
@@ -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;
+               }
        }
 }