Further unit tests: CFB and Delta
[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
6 import java.io.ByteArrayInputStream;
7 import java.io.ByteArrayOutputStream;
8 import java.io.FileNotFoundException;
9 import java.io.IOException;
10 import java.io.OutputStreamWriter;
11 import java.io.PrintWriter;
12 import java.nio.charset.Charset;
13 import java.sql.Connection;
14 import java.sql.SQLException;
15 import java.util.Locale;
16
17 import net.jaekl.cfb.analyze.Analysis;
18 import net.jaekl.cfb.analyze.MessageMap;
19 import net.jaekl.cfb.db.CfbSchema;
20 import net.jaekl.cfb.db.Column;
21 import net.jaekl.cfb.db.Condition;
22 import net.jaekl.cfb.db.Operation;
23 import net.jaekl.cfb.db.Row;
24 import net.jaekl.cfb.db.Table;
25 import net.jaekl.cfb.db.TypeMismatchException;
26 import net.jaekl.cfb.db.driver.DbDriverMock;
27 import net.jaekl.cfb.util.Command;
28 import net.jaekl.cfb.xml.MessagesXmlData;
29
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.xml.sax.InputSource;
33 import org.xml.sax.SAXException;
34
35 public class CFBTest {
36         private CFBMock m_cfb;
37         private DbDriverMock m_driver;
38         
39         private static final String BUG_COLLECTION_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
40                         + "<BugCollection version=\"2.0.3\" sequence=\"0\" timestamp=\"1425655198000\" analysisTimestamp=\"1451305502231\" release=\"\">"
41                         + "<Project projectName=\"JUnit\">"
42                         + "<Jar>/data/prog/findbugs-3.0.1/lib/junit.jar</Jar>"
43                         + "</Project>"
44                         + "<BugInstance type=\"VO_VOLATILE_INCREMENT\" priority=\"2\" abbrev=\"VO\" category=\"MT_CORRECTNESS\">"
45                         + "<Class classname=\"junit.extensions.ActiveTestSuite\">"
46                         + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
47                         + "</Class>"
48                         + "<Method classname=\"junit.extensions.ActiveTestSuite\" name=\"runFinished\" signature=\"()V\" isStatic=\"false\">"
49                         + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" start=\"67\" end=\"69\" startBytecode=\"0\" endBytecode=\"64\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
50                         + "</Method>"
51                         + "<Field classname=\"junit.extensions.ActiveTestSuite\" name=\"fActiveTestDeathCount\" signature=\"I\" isStatic=\"false\">"
52                         + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
53                         + "</Field>"
54                         + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" start=\"67\" end=\"67\" startBytecode=\"7\" endBytecode=\"7\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
55                         + "</BugInstance>"
56                         + "<BugInstance type=\"DM_NUMBER_CTOR\" priority=\"2\" abbrev=\"Bx\" category=\"PERFORMANCE\">"
57                         + "<Class classname=\"junit.framework.Assert\">"
58                         + "<SourceLine classname=\"junit.framework.Assert\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
59                         + "</Class>"
60                         + "<Method classname=\"junit.framework.Assert\" name=\"assertEquals\" signature=\"(Ljava/lang/String;BB)V\" isStatic=\"true\">"
61                         + "<SourceLine classname=\"junit.framework.Assert\" start=\"156\" end=\"157\" startBytecode=\"0\" endBytecode=\"86\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
62                         + "</Method>"
63                         + "<Method classname=\"java.lang.Byte\" name=\"&lt;init&gt;\" signature=\"(B)V\" isStatic=\"false\" role=\"METHOD_CALLED\">"
64                         + "<SourceLine classname=\"java.lang.Byte\" start=\"307\" end=\"309\" startBytecode=\"0\" endBytecode=\"41\" sourcefile=\"Byte.java\" sourcepath=\"java/lang/Byte.java\"/>"
65                         + "</Method>"
66                         + "<Method classname=\"java.lang.Byte\" name=\"valueOf\" signature=\"(B)Ljava/lang/Byte;\" isStatic=\"true\" role=\"SHOULD_CALL\">"
67                         + "<SourceLine classname=\"java.lang.Byte\" start=\"87\" end=\"87\" startBytecode=\"0\" endBytecode=\"33\" sourcefile=\"Byte.java\" sourcepath=\"java/lang/Byte.java\"/>"
68                         + "</Method>"
69                         + "<SourceLine classname=\"junit.framework.Assert\" start=\"156\" end=\"156\" startBytecode=\"6\" endBytecode=\"6\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
70                         + "</BugInstance>"
71                         + "<BugInstance type=\"DM_NUMBER_CTOR\" priority=\"2\" abbrev=\"Bx\" category=\"PERFORMANCE\">"
72                         + "<Class classname=\"junit.framework.Assert\">"
73                         + "<SourceLine classname=\"junit.framework.Assert\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
74                         + "</Class>"
75                         + "<Method classname=\"junit.framework.Assert\" name=\"assertEquals\" signature=\"(Ljava/lang/String;CC)V\" isStatic=\"true\">"
76                         + "<SourceLine classname=\"junit.framework.Assert\" start=\"169\" end=\"170\" startBytecode=\"0\" endBytecode=\"86\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
77                         + "</Method>"
78                         + "<Method classname=\"java.lang.Character\" name=\"&lt;init&gt;\" signature=\"(C)V\" isStatic=\"false\" role=\"METHOD_CALLED\">"
79                         + "<SourceLine classname=\"java.lang.Character\" start=\"2056\" end=\"2058\" startBytecode=\"0\" endBytecode=\"41\" sourcefile=\"Character.java\" sourcepath=\"java/lang/Character.java\"/>"
80                         + "</Method>"
81                         + "<Method classname=\"java.lang.Character\" name=\"valueOf\" signature=\"(C)Ljava/lang/Character;\" isStatic=\"true\" role=\"SHOULD_CALL\">"
82                         + "<SourceLine classname=\"java.lang.Character\" start=\"2085\" end=\"2088\" startBytecode=\"0\" endBytecode=\"52\" sourcefile=\"Character.java\" sourcepath=\"java/lang/Character.java\"/>"
83                         + "</Method>"
84                         + "<SourceLine classname=\"junit.framework.Assert\" start=\"169\" end=\"169\" startBytecode=\"6\" endBytecode=\"6\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
85                         + "</BugInstance>"
86                         + "</BugCollection>";
87
88         private Analysis analysisFromXml(String xml, String projectName, String version) 
89                         throws FileNotFoundException, IOException, SAXException 
90         {
91                 Analysis analysis = new Analysis(projectName, version);
92                 ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes(Command.UTF_8));
93                 InputSource inputSource = new InputSource(bais); 
94                 analysis.parse(inputSource);
95                 
96                 return analysis;
97         }
98                 
99         @Before
100         public void setUp()
101         {
102                 m_driver = new DbDriverMock();
103                 m_cfb = new CFBMock(Locale.getDefault(), m_driver);
104         }
105         
106         @Test
107         public void testStoreAndReport_noPrior() throws IOException, SQLException, SAXException, TypeMismatchException 
108         {
109                 final String PROJECT_NAME = "ProjectName";
110                 final String VERSION = "1.0.1";
111                 
112                 try (
113                                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
114                                 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, Charset.forName(Command.UTF_8)));
115                         )
116                 {
117                         MessageMap msgMap = new MessageMap();
118                         msgMap.parse(new InputSource(new ByteArrayInputStream(MessagesXmlData.XML.getBytes(Command.UTF_8))));
119                         
120                         Analysis analysis = analysisFromXml(BUG_COLLECTION_XML, PROJECT_NAME, VERSION);
121                         
122                         m_cfb.ensureDbInitialized(pw, msgMap);
123                         m_cfb.storeAndReport(pw, msgMap, analysis);
124                 }
125                 
126                 Connection con = m_driver.connect("host", 1234, "dbName", "dbUser", "dbPass");
127                 Column[] columns = { CfbSchema.RUNID, CfbSchema.PROJNAME, CfbSchema.VERSION };
128                 Table[] tables = { CfbSchema.RUNS };
129                 Condition[] conditions = { new Condition(CfbSchema.RUNID, Operation.NOT_NULL) };
130                 
131                 Row row = m_driver.selectExactlyOne(con, columns, tables, conditions);
132                 assertNotNull(row);
133                 assertEquals(PROJECT_NAME, row.getValue(1));
134                 assertEquals(VERSION, row.getValue(2));
135         }
136 }