3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
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;
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;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.xml.sax.InputSource;
37 import org.xml.sax.SAXException;
39 public class CFBTest {
40 private CFBMock m_cfb;
41 private DbDriverMock m_driver;
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>"
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\"/>"
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\"/>"
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\"/>"
58 + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" start=\"67\" end=\"67\" startBytecode=\"7\" endBytecode=\"7\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
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\"/>"
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\"/>"
67 + "<Method classname=\"java.lang.Byte\" name=\"<init>\" 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\"/>"
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\"/>"
73 + "<SourceLine classname=\"junit.framework.Assert\" start=\"156\" end=\"156\" startBytecode=\"6\" endBytecode=\"6\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
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\"/>"
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\"/>"
82 + "<Method classname=\"java.lang.Character\" name=\"<init>\" 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\"/>"
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\"/>"
88 + "<SourceLine classname=\"junit.framework.Assert\" start=\"169\" end=\"169\" startBytecode=\"6\" endBytecode=\"6\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
92 private Analysis analysisFromXml(String xml, String projectName, String version)
93 throws FileNotFoundException, IOException, SAXException
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);
106 m_driver = new DbDriverMock();
107 m_cfb = new CFBMock(Locale.getDefault(), m_driver);
111 public void testStoreAndReport_noPrior() throws IOException, SQLException, SAXException, TypeMismatchException
113 final String PROJECT_NAME = "ProjectName";
114 final String VERSION = "1.0.1";
117 ByteArrayOutputStream baos = new ByteArrayOutputStream();
118 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, Charset.forName(Command.UTF_8)));
121 MessageMap msgMap = new MessageMap();
122 msgMap.parse(new InputSource(new ByteArrayInputStream(MessagesXmlData.XML.getBytes(Command.UTF_8))));
124 Analysis analysis = analysisFromXml(BUG_COLLECTION_XML, PROJECT_NAME, VERSION);
126 m_cfb.ensureDbInitialized(pw, msgMap);
127 m_cfb.storeAndReport(pw, msgMap, analysis);
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) };
135 Row row = m_driver.selectExactlyOne(con, columns, tables, conditions);
137 assertEquals(PROJECT_NAME, row.getValue(1));
138 assertEquals(VERSION, row.getValue(2));
142 public void testTrans()
145 { "hello", new Object[] {}, "[hello]" },
146 { "hello", new Object[] {"world"}, "[hello][world]"}
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);
159 public void testReportException_FBMsgFileNotFoundException() throws IOException
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"
167 null, "messages.xml",
168 "[cannot.load.fbmsg.file][messages.xml]\n[findbugs.home.is.not.set][FINDBUGS_HOME]\n"
171 null, null, "[cannot.load.fbmsg.file][null]\n[findbugs.home.is.not.set][FINDBUGS_HOME]\n"
176 EnvMock envMock = EnvMock.mock_putInstance();
178 for (String[] datum : data)
180 String fbHome = datum[0];
181 String filename = datum[1];
182 String expected = datum[2];
185 StringWriter sw = new StringWriter();
186 PrintWriter pw = new PrintWriter(sw);
189 envMock.mock_putEnv(CFB.FINDBUGS_HOME, fbHome);
190 FBMsgFileNotFoundException exc = new FBMsgFileNotFoundException(filename);
191 m_cfb.reportException(pw, exc);
196 String actual = sw.toString();
197 boolean pass = actual.endsWith(expected);
199 System.out.println("Expected:\n" + expected + "\nActual:\n" + actual);
206 EnvMock.mock_resetInstance();