Address some edge cases related to bootstrapping a fresh system.
[cfb.git] / test / net / jaekl / cfb / CFBTest.java
index de10fe7206675d49aca12856ac166456620958e0..1e70fb28ae81dd11b3d5da7eefe9d2a0cdfa2969 100644 (file)
@@ -1,7 +1,9 @@
 package net.jaekl.cfb;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
@@ -9,12 +11,14 @@ import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
+import java.io.StringWriter;
 import java.nio.charset.Charset;
 import java.sql.Connection;
 import java.sql.SQLException;
 import java.util.Locale;
 
 import net.jaekl.cfb.analyze.Analysis;
+import net.jaekl.cfb.analyze.FBMsgFileNotFoundException;
 import net.jaekl.cfb.analyze.MessageMap;
 import net.jaekl.cfb.db.CfbSchema;
 import net.jaekl.cfb.db.Column;
@@ -25,6 +29,7 @@ import net.jaekl.cfb.db.Table;
 import net.jaekl.cfb.db.TypeMismatchException;
 import net.jaekl.cfb.db.driver.DbDriverMock;
 import net.jaekl.cfb.util.Command;
+import net.jaekl.cfb.util.EnvMock;
 import net.jaekl.cfb.xml.MessagesXmlData;
 
 import org.junit.Before;
@@ -103,6 +108,53 @@ public class CFBTest {
                m_cfb = new CFBMock(Locale.getDefault(), m_driver);
        }
        
+       @Test
+       public void testParseArgs_noParams() throws IOException 
+       {
+               StringWriter sw = new StringWriter();
+               PrintWriter pw = new PrintWriter(sw);
+               String[] args = {};
+               
+               boolean result = m_cfb.parseArgs(pw, args);
+               pw.close();
+               sw.close();
+               
+               assertFalse(result);
+               assertEquals("[must.specify.fbp.file]\n[invoke.with.help.for.help]\n", sw.toString());
+       }
+       
+       @Test
+       public void testParseArgs_dropTables() throws IOException
+       {
+               StringWriter sw = new StringWriter();
+               PrintWriter pw = new PrintWriter(sw);
+               String[] args = { "--drop-tables" };
+               
+               boolean result = m_cfb.parseArgs(pw, args);
+               pw.close();
+               sw.close();
+               
+               assertTrue(result);
+               assertEquals("", sw.toString());
+       }
+       
+       @Test
+       public void testParseArgs_invalidParam() throws IOException
+       {
+               StringWriter sw = new StringWriter();
+               PrintWriter pw = new PrintWriter(sw);
+               String[] args = {"--xyzzyaoeuidhtnsl"};
+               
+               boolean result = m_cfb.parseArgs(pw, args);
+               pw.close();
+               sw.close();
+               String actual = sw.toString();
+               
+               assertFalse(result);
+               assertTrue(actual.contains("usage"));
+               assertTrue(actual.contains("--help"));
+       }
+       
        @Test
        public void testStoreAndReport_noPrior() throws IOException, SQLException, SAXException, TypeMismatchException 
        {
@@ -133,4 +185,73 @@ public class CFBTest {
                assertEquals(PROJECT_NAME, row.getValue(1));
                assertEquals(VERSION, row.getValue(2));
        }
+       
+       @Test
+       public void testTrans()
+       {
+               Object[][] data = {
+                               { "hello", new Object[] {}, "[hello]" },
+                               { "hello", new Object[] {"world"}, "[hello][world]"}    
+                       };
+               
+               for (Object[] datum : data) {
+                       String key = (String)datum[0];
+                       Object[] params = (Object[])datum[1];
+                       String expected = (String)datum[2];
+                       String actual = m_cfb.trans(key, params);
+                       assertEquals(expected, actual);
+               }
+       }
+       
+       @Test
+       public void testReportException_FBMsgFileNotFoundException() throws IOException
+       {
+               String[][] data = {
+                               { 
+                                       "/home/fred/findbugs3.0.1", "messages.xml", 
+                                       "[cannot.load.fbmsg.file][messages.xml]\n[findbugs.home.is.set.to][FINDBUGS_HOME][/home/fred/findbugs3.0.1]\n"
+                               },
+                               { 
+                                       null, "messages.xml", 
+                                       "[cannot.load.fbmsg.file][messages.xml]\n[findbugs.home.is.not.set][FINDBUGS_HOME]\n"
+                               },
+                               { 
+                                       null, null, "[cannot.load.fbmsg.file][null]\n[findbugs.home.is.not.set][FINDBUGS_HOME]\n" 
+                               }
+               };
+
+               try {
+                       EnvMock envMock = EnvMock.mock_putInstance();
+                       
+                       for (String[] datum : data) 
+                       {
+                               String fbHome = datum[0];
+                               String filename = datum[1];
+                               String expected = datum[2];
+                               
+                               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);
+                               }
+                       }
+               }
+               finally {
+                       EnvMock.mock_resetInstance();
+               }
+       }
 }