More unit testing.
[cfb.git] / test / net / jaekl / cfb / CFBTest.java
1 package net.jaekl.cfb;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6
7 import java.io.ByteArrayInputStream;
8 import java.io.ByteArrayOutputStream;
9 import java.io.FileNotFoundException;
10 import java.io.IOException;
11 import java.io.OutputStreamWriter;
12 import java.io.PrintWriter;
13 import java.io.StringWriter;
14 import java.nio.charset.Charset;
15 import java.sql.Connection;
16 import java.sql.SQLException;
17 import java.util.Locale;
18
19 import net.jaekl.cfb.analyze.Analysis;
20 import net.jaekl.cfb.analyze.FBMsgFileNotFoundException;
21 import net.jaekl.cfb.analyze.MessageMap;
22 import net.jaekl.cfb.db.CfbSchema;
23 import net.jaekl.cfb.db.Column;
24 import net.jaekl.cfb.db.Condition;
25 import net.jaekl.cfb.db.Operation;
26 import net.jaekl.cfb.db.Row;
27 import net.jaekl.cfb.db.Table;
28 import net.jaekl.cfb.db.TypeMismatchException;
29 import net.jaekl.cfb.db.driver.DbDriverMock;
30 import net.jaekl.cfb.util.Command;
31 import net.jaekl.cfb.util.EnvMock;
32 import net.jaekl.cfb.xml.MessagesXmlData;
33
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.xml.sax.InputSource;
37 import org.xml.sax.SAXException;
38
39 public class CFBTest {
40         private CFBMock m_cfb;
41         private DbDriverMock m_driver;
42         
43         private static final String BUG_COLLECTION_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
44                         + "<BugCollection version=\"2.0.3\" sequence=\"0\" timestamp=\"1425655198000\" analysisTimestamp=\"1451305502231\" release=\"\">"
45                         + "<Project projectName=\"JUnit\">"
46                         + "<Jar>/data/prog/findbugs-3.0.1/lib/junit.jar</Jar>"
47                         + "</Project>"
48                         + "<BugInstance type=\"VO_VOLATILE_INCREMENT\" priority=\"2\" abbrev=\"VO\" category=\"MT_CORRECTNESS\">"
49                         + "<Class classname=\"junit.extensions.ActiveTestSuite\">"
50                         + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
51                         + "</Class>"
52                         + "<Method classname=\"junit.extensions.ActiveTestSuite\" name=\"runFinished\" signature=\"()V\" isStatic=\"false\">"
53                         + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" start=\"67\" end=\"69\" startBytecode=\"0\" endBytecode=\"64\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
54                         + "</Method>"
55                         + "<Field classname=\"junit.extensions.ActiveTestSuite\" name=\"fActiveTestDeathCount\" signature=\"I\" isStatic=\"false\">"
56                         + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
57                         + "</Field>"
58                         + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" start=\"67\" end=\"67\" startBytecode=\"7\" endBytecode=\"7\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
59                         + "</BugInstance>"
60                         + "<BugInstance type=\"DM_NUMBER_CTOR\" priority=\"2\" abbrev=\"Bx\" category=\"PERFORMANCE\">"
61                         + "<Class classname=\"junit.framework.Assert\">"
62                         + "<SourceLine classname=\"junit.framework.Assert\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
63                         + "</Class>"
64                         + "<Method classname=\"junit.framework.Assert\" name=\"assertEquals\" signature=\"(Ljava/lang/String;BB)V\" isStatic=\"true\">"
65                         + "<SourceLine classname=\"junit.framework.Assert\" start=\"156\" end=\"157\" startBytecode=\"0\" endBytecode=\"86\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
66                         + "</Method>"
67                         + "<Method classname=\"java.lang.Byte\" name=\"&lt;init&gt;\" signature=\"(B)V\" isStatic=\"false\" role=\"METHOD_CALLED\">"
68                         + "<SourceLine classname=\"java.lang.Byte\" start=\"307\" end=\"309\" startBytecode=\"0\" endBytecode=\"41\" sourcefile=\"Byte.java\" sourcepath=\"java/lang/Byte.java\"/>"
69                         + "</Method>"
70                         + "<Method classname=\"java.lang.Byte\" name=\"valueOf\" signature=\"(B)Ljava/lang/Byte;\" isStatic=\"true\" role=\"SHOULD_CALL\">"
71                         + "<SourceLine classname=\"java.lang.Byte\" start=\"87\" end=\"87\" startBytecode=\"0\" endBytecode=\"33\" sourcefile=\"Byte.java\" sourcepath=\"java/lang/Byte.java\"/>"
72                         + "</Method>"
73                         + "<SourceLine classname=\"junit.framework.Assert\" start=\"156\" end=\"156\" startBytecode=\"6\" endBytecode=\"6\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
74                         + "</BugInstance>"
75                         + "<BugInstance type=\"DM_NUMBER_CTOR\" priority=\"2\" abbrev=\"Bx\" category=\"PERFORMANCE\">"
76                         + "<Class classname=\"junit.framework.Assert\">"
77                         + "<SourceLine classname=\"junit.framework.Assert\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
78                         + "</Class>"
79                         + "<Method classname=\"junit.framework.Assert\" name=\"assertEquals\" signature=\"(Ljava/lang/String;CC)V\" isStatic=\"true\">"
80                         + "<SourceLine classname=\"junit.framework.Assert\" start=\"169\" end=\"170\" startBytecode=\"0\" endBytecode=\"86\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
81                         + "</Method>"
82                         + "<Method classname=\"java.lang.Character\" name=\"&lt;init&gt;\" signature=\"(C)V\" isStatic=\"false\" role=\"METHOD_CALLED\">"
83                         + "<SourceLine classname=\"java.lang.Character\" start=\"2056\" end=\"2058\" startBytecode=\"0\" endBytecode=\"41\" sourcefile=\"Character.java\" sourcepath=\"java/lang/Character.java\"/>"
84                         + "</Method>"
85                         + "<Method classname=\"java.lang.Character\" name=\"valueOf\" signature=\"(C)Ljava/lang/Character;\" isStatic=\"true\" role=\"SHOULD_CALL\">"
86                         + "<SourceLine classname=\"java.lang.Character\" start=\"2085\" end=\"2088\" startBytecode=\"0\" endBytecode=\"52\" sourcefile=\"Character.java\" sourcepath=\"java/lang/Character.java\"/>"
87                         + "</Method>"
88                         + "<SourceLine classname=\"junit.framework.Assert\" start=\"169\" end=\"169\" startBytecode=\"6\" endBytecode=\"6\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
89                         + "</BugInstance>"
90                         + "</BugCollection>";
91
92         private Analysis analysisFromXml(String xml, String projectName, String version) 
93                         throws FileNotFoundException, IOException, SAXException 
94         {
95                 Analysis analysis = new Analysis(projectName, version);
96                 ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes(Command.UTF_8));
97                 InputSource inputSource = new InputSource(bais); 
98                 analysis.parse(inputSource);
99                 
100                 return analysis;
101         }
102                 
103         @Before
104         public void setUp()
105         {
106                 m_driver = new DbDriverMock();
107                 m_cfb = new CFBMock(Locale.getDefault(), m_driver);
108         }
109         
110         @Test
111         public void testStoreAndReport_noPrior() throws IOException, SQLException, SAXException, TypeMismatchException 
112         {
113                 final String PROJECT_NAME = "ProjectName";
114                 final String VERSION = "1.0.1";
115                 
116                 try (
117                                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
118                                 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, Charset.forName(Command.UTF_8)));
119                         )
120                 {
121                         MessageMap msgMap = new MessageMap();
122                         msgMap.parse(new InputSource(new ByteArrayInputStream(MessagesXmlData.XML.getBytes(Command.UTF_8))));
123                         
124                         Analysis analysis = analysisFromXml(BUG_COLLECTION_XML, PROJECT_NAME, VERSION);
125                         
126                         m_cfb.ensureDbInitialized(pw, msgMap);
127                         m_cfb.storeAndReport(pw, msgMap, analysis);
128                 }
129                 
130                 Connection con = m_driver.connect("host", 1234, "dbName", "dbUser", "dbPass");
131                 Column[] columns = { CfbSchema.RUNID, CfbSchema.PROJNAME, CfbSchema.VERSION };
132                 Table[] tables = { CfbSchema.RUNS };
133                 Condition[] conditions = { new Condition(CfbSchema.RUNID, Operation.NOT_NULL) };
134                 
135                 Row row = m_driver.selectExactlyOne(con, columns, tables, conditions);
136                 assertNotNull(row);
137                 assertEquals(PROJECT_NAME, row.getValue(1));
138                 assertEquals(VERSION, row.getValue(2));
139         }
140         
141         @Test
142         public void testTrans()
143         {
144                 Object[][] data = {
145                                 { "hello", new Object[] {}, "[hello]" },
146                                 { "hello", new Object[] {"world"}, "[hello][world]"}    
147                         };
148                 
149                 for (Object[] datum : data) {
150                         String key = (String)datum[0];
151                         Object[] params = (Object[])datum[1];
152                         String expected = (String)datum[2];
153                         String actual = m_cfb.trans(key, params);
154                         assertEquals(expected, actual);
155                 }
156         }
157         
158         @Test
159         public void testReportException_FBMsgFileNotFoundException() throws IOException
160         {
161                 String[][] data = {
162                                 { 
163                                         "/home/fred/findbugs3.0.1", "messages.xml", 
164                                         "[cannot.load.fbmsg.file][messages.xml]\n[findbugs.home.is.set.to][FINDBUGS_HOME][/home/fred/findbugs3.0.1]\n"
165                                 },
166                                 { 
167                                         null, "messages.xml", 
168                                         "[cannot.load.fbmsg.file][messages.xml]\n[findbugs.home.is.not.set][FINDBUGS_HOME]\n"
169                                 },
170                                 { 
171                                         null, null, "[cannot.load.fbmsg.file][null]\n[findbugs.home.is.not.set][FINDBUGS_HOME]\n" 
172                                 }
173                 };
174
175                 try {
176                         EnvMock envMock = EnvMock.mock_putInstance();
177                         
178                         for (String[] datum : data) 
179                         {
180                                 String fbHome = datum[0];
181                                 String filename = datum[1];
182                                 String expected = datum[2];
183                                 
184                                 try (
185                                                 StringWriter sw = new StringWriter();
186                                                 PrintWriter pw = new PrintWriter(sw);
187                                         )
188                                 {
189                                         envMock.mock_putEnv(CFB.FINDBUGS_HOME, fbHome);
190                                         FBMsgFileNotFoundException exc = new FBMsgFileNotFoundException(filename);
191                                         m_cfb.reportException(pw, exc);
192                                         
193                                         pw.close();
194                                         sw.close();
195                                         
196                                         String actual = sw.toString();
197                                         boolean pass = actual.endsWith(expected);
198                                         if (!pass) {
199                                                 System.out.println("Expected:\n" + expected + "\nActual:\n" + actual);
200                                         }
201                                         assertTrue(pass);
202                                 }
203                         }
204                 }
205                 finally {
206                         EnvMock.mock_resetInstance();
207                 }
208         }
209 }