methods that make debugging variable dumps more useful.
m_buildNumber = buildNumber;
m_start = new Date().getTime();
m_end = 0;
+ m_bugCollection = new BugCollection();
}
public BugCollection getBugCollection() { return m_bugCollection; }
public Column getColumn() { return m_column; }
public Object getValue() { return m_value; }
public Operation getOperation() { return m_operation; }
+
+ @Override
+ public String toString() {
+ return ("(" + m_column.getName() + " " + m_operation + " " + m_value + ")");
+ }
}
public int getNumColumns() { return m_columns.length; }
public Column getColumn(int idx) { return m_columns[idx]; }
+ public Object getValue(int index) {
+ return m_values[index];
+ }
+
public String getString(int index) throws TypeMismatchException {
checkType(index, Column.Type.VARCHAR);
return (String)m_values[index];
return (java.util.Date)m_values[index];
}
+ @Override
+ public String toString()
+ {
+ StringBuilder sb = new StringBuilder("[");
+ for (int idx = 0; idx < m_columns.length; ++idx) {
+ if (idx > 0) {
+ sb.append(", ");
+ }
+ sb.append("" + m_columns[idx].getName() + "=" + m_values[idx]);
+ }
+ sb.append("]");
+ return sb.toString();
+ }
+
protected void checkType(int index, Column.Type type) throws TypeMismatchException {
Column column = m_columns[index];
Column.Type columnType = column.getType();
import java.io.PrintWriter;
import java.io.StringWriter;
+import java.util.Iterator;
+import java.util.List;
public class Util {
- public static String stringify(Throwable thr)
+ // Returns true iff. a and b contain equal items in the same order
+ public static boolean listsAreEqual(List<?> a, List<?> b)
{
- StringWriter sw = new StringWriter();
- PrintWriter pw = new PrintWriter(sw);
- thr.printStackTrace(pw);
- return sw.toString();
+ if ((null == a) || (null == b)) {
+ return (a == b);
+ }
+
+ if (0 == a.size()) {
+ return (0 == b.size());
+ }
+
+ if (a.size() != b.size()) {
+ return false;
+ }
+
+ Iterator<?> iterA = a.iterator();
+ Iterator<?> iterB = b.iterator();
+
+ while (iterA.hasNext()) {
+ Object elemA = iterA.next();
+ Object elemB = iterB.next();
+
+ if (! objsAreEqual(elemA, elemB)) {
+ return false;
+ }
+ }
+
+ return true;
}
// Test for equality, while taking care to avoid
}
return obj.hashCode();
}
+
+ // Convert a Throwable to the string representation
+ // that is generated by printStackTrace().
+ public static String stringify(Throwable thr)
+ {
+ StringWriter sw = new StringWriter();
+ PrintWriter pw = new PrintWriter(sw);
+ thr.printStackTrace(pw);
+ return sw.toString();
+ }
}
import java.util.Collections;
import java.util.List;
+import net.jaekl.cfb.util.Util;
import net.jaekl.qd.xml.ParseResult;
import net.jaekl.qd.xml.XmlParseException;
}
}
+ @Override
+ public boolean equals(Object obj) {
+ if (null == obj) {
+ return false;
+ }
+ if (! (obj instanceof BugCollection)) {
+ return false;
+ }
+ BugCollection other = (BugCollection)obj;
+
+ return Util.listsAreEqual(this.m_bugs, other.m_bugs);
+ }
}
package net.jaekl.cfb.db;
import java.util.ArrayList;
-
import static org.junit.Assert.*;
public class TableMock extends Table {
this(table.m_name, table.m_columns.toArray(new Column[table.m_columns.size()]));
}
- protected ArrayList<Row> mock_getRows() { return m_rows; }
+ public ArrayList<Row> mock_getRows() { return m_rows; }
public boolean mock_hasColumn(Column expectedCol) {
for (Column col : m_columns) {
}
m_rows.add(row);
}
+
+ @Override
+ public String toString()
+ {
+ StringBuilder sb = new StringBuilder("" + getName() + "={\n");
+ for (Row row : m_rows) {
+ sb.append("" + row + "\n");
+ }
+ sb.append("}");
+ return sb.toString();
+ }
}
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
+import java.util.Date;
import java.util.HashMap;
import java.util.List;
import net.jaekl.cfb.db.Sort;
import net.jaekl.cfb.db.Table;
import net.jaekl.cfb.db.TableMock;
+import net.jaekl.cfb.db.TypeMismatchException;
public class DbDriverMock extends DbDriver {
assertNotNull(columns);
assertNotNull(tables);
- // TODO: produce sensible output
- return new ArrayList<Row>();
+ if (1 != tables.length) {
+ throw new UnsupportedOperationException("DbDriverMock does not (yet) implement table joins");
+ }
+
+ Table schemaTable = tables[0];
+ TableMock table = m_tables.get(schemaTable.getName());
+ if (null == table) {
+ throw new SQLException("Could not find table \"" + schemaTable.getName() + "\" in schema.");
+ }
+
+ ArrayList<Row> rows = table.mock_getRows();
+ if (null != conditions) {
+ ArrayList<Row> filteredRows = new ArrayList<Row>();
+ for (Row row : rows) {
+ boolean match = true;
+ for (Condition condition : conditions) {
+ try {
+ if (!conditionSatisfied(condition, row)) {
+ match = false;
+ }
+ }
+ catch (TypeMismatchException exc) {
+ throw new SQLException(exc);
+ }
+ }
+ if (match) {
+ filteredRows.add(row);
+ }
+ }
+ rows = filteredRows;
+ }
+
+ ArrayList<Row> result = new ArrayList<Row>();
+
+ // Now, convert the rows from select(*) to select(specified columns)
+ for (Row row : rows) {
+ Object[] values = new Object[columns.length];
+
+ for (int outIdx = 0; outIdx < columns.length; ++outIdx) {
+ for (int inIdx = 0; inIdx < row.getNumColumns(); ++inIdx) {
+ if (row.getColumn(inIdx).getName().equals(columns[outIdx].getName())) {
+ values[outIdx] = row.getValue(inIdx);
+ }
+ }
+ }
+ result.add(new Row(columns, values));
+ }
+
+ return result;
+ }
+
+ private boolean aLessThanB(Object a, Object b)
+ {
+ if ((null == a) || (null == b)) {
+ return false;
+ }
+
+ if (a instanceof Number) {
+ if (! (b instanceof Number)) {
+ throw new UnsupportedOperationException("Incompatible types");
+ }
+ Number first = (Number)a;
+ Number second = (Number)b;
+ return first.doubleValue() < second.doubleValue();
+ }
+
+ if (a instanceof Date) {
+ if (! (b instanceof Date)) {
+ throw new UnsupportedOperationException("Incompatible types");
+ }
+ Date first = (Date)a;
+ Date second = (Date)b;
+ int comp = first.compareTo(second);
+ boolean result = (comp < 0);
+
+ return (result);
+ }
+
+ throw new UnsupportedOperationException("Incompatible types");
+ }
+
+ private boolean conditionSatisfied(Condition condition, Row row) throws TypeMismatchException, SQLException
+ {
+ for (int idx = 0; idx < row.getNumColumns(); ++idx) {
+ Column col = row.getColumn(idx);
+ if (condition.getColumn().equals(col)) {
+ switch(condition.getOperation()) {
+ case EQUAL:
+ return (condition.getValue().equals(row.getValue(idx)));
+ case GREATER_THAN:
+ return (aLessThanB(condition.getValue(), row.getValue(idx)));
+ case LESS_THAN:
+ return (aLessThanB(row.getValue(idx), condition.getValue()));
+ case NOT_NULL:
+ return (null != row.getValue(idx));
+ case NULL:
+ return (null == row.getValue(idx));
+ default:
+ throw new UnsupportedOperationException("This condition operation is not supported by DbDriverMock");
+ }
+ }
+ }
+ throw new SQLException("Could not locate column \"" + condition.getColumn().getName() + "\" in TableMock.");
}
// Returns the number of rows inserted
--- /dev/null
+package net.jaekl.cfb.store;
+
+import static org.junit.Assert.*;
+
+import java.io.ByteArrayInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.sql.SQLException;
+import java.util.Date;
+
+import net.jaekl.cfb.analyze.Analysis;
+import net.jaekl.cfb.analyze.MessageMap;
+import net.jaekl.cfb.db.CfbSchema;
+import net.jaekl.cfb.db.TypeMismatchException;
+import net.jaekl.cfb.db.driver.ConnectionMock;
+import net.jaekl.cfb.db.driver.DbDriverMock;
+import net.jaekl.cfb.xml.MessagesXmlData;
+import net.jaekl.cfb.xml.messages.MessageCollection;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+public class DbStoreTest {
+ private DbStore m_store;
+
+ @Before
+ public void setUp() throws SQLException, FileNotFoundException, UnsupportedEncodingException, IOException, SAXException
+ {
+ MessageMap msgMap = new MessageMap();
+ msgMap.parse(new InputSource(new ByteArrayInputStream(MessagesXmlData.XML.getBytes("UTF-8"))));
+
+ DbDriverMock driver = new DbDriverMock();
+ CfbSchema schema = new CfbSchema(driver);
+ ConnectionMock con = new ConnectionMock();
+
+ schema.setMessageMap(msgMap);
+ schema.ensureDbInitialized(con);
+
+ MessageCollection msgColl = new MessageCollection();
+ m_store = new DbStore(con, driver, msgColl);
+ }
+
+ @Test
+ public void testGetPrior_withNoEntries() throws SQLException, TypeMismatchException {
+ // First test: getPrior(null) should return null
+ Analysis actual = m_store.getPrior(null);
+ assertNull(actual);
+
+ // Second test: getPrior(current) with no data in the DB should return null
+ String projName = "ProjectName";
+ String version = "1.2.3";
+ Date start = new Date(1234567890);
+ Date end = new Date(1234567900);
+ Analysis current = new Analysis(projName, version);
+ current.setStart(start);
+ current.setEnd(end);
+ actual = m_store.getPrior(current);
+ assertNull(actual);
+ }
+
+ @Test
+ public void testPut_thenGetPrior() throws SQLException, TypeMismatchException {
+ String projName = "ProjectName";
+ String firstVersion = "1.0.1";
+ Date firstStart = new Date(100);
+ Date firstEnd = new Date(200);
+ Analysis firstAnalysis = new Analysis(projName, firstVersion);
+ firstAnalysis.setStart(firstStart);
+ firstAnalysis.setEnd(firstEnd);
+
+ boolean result = m_store.put(firstAnalysis);
+ assertTrue(result);
+
+ String secondVersion = "1.0.2";
+ Date secondStart = new Date(2300);
+ Date secondEnd = new Date(2400);
+ Analysis secondAnalysis = new Analysis(projName, secondVersion);
+ secondAnalysis.setStart(secondStart);
+ secondAnalysis.setEnd(secondEnd);
+
+ Analysis priorAnalysis = m_store.getPrior(secondAnalysis);
+ assertNotNull(priorAnalysis);
+ assertEquals(firstAnalysis.getProjectName(), priorAnalysis.getProjectName());
+ assertEquals(firstAnalysis.getBuildNumber(), priorAnalysis.getBuildNumber());
+ assertEquals(firstAnalysis.getStart(), priorAnalysis.getStart());
+ assertEquals(firstAnalysis.getEnd(), priorAnalysis.getEnd());
+ assertEquals(firstAnalysis.getBugCollection(), priorAnalysis.getBugCollection());
+ }
+/*
+ @Test
+ public void testGetBugType() {
+ fail("Not yet implemented");
+ }
+
+ @Test
+ public void testGetCategoryName() {
+ fail("Not yet implemented");
+ }
+
+ @Test
+ public void testGetLoc() {
+ fail("Not yet implemented");
+ }
+
+ @Test
+ public void testGetLocId() {
+ fail("Not yet implemented");
+ }
+
+ @Test
+ public void testFindLocId() {
+ fail("Not yet implemented");
+ }
+
+ @Test
+ public void testStoreLoc() {
+ fail("Not yet implemented");
+ }
+
+ @Test
+ public void testGetVarIdBugInstance() {
+ fail("Not yet implemented");
+ }
+
+ @Test
+ public void testGetVar() {
+ fail("Not yet implemented");
+ }
+
+ @Test
+ public void testGetVarIdLocalVariable() {
+ fail("Not yet implemented");
+ }
+
+ @Test
+ public void testFindVarId() {
+ fail("Not yet implemented");
+ }
+
+ @Test
+ public void testStoreVar() {
+ fail("Not yet implemented");
+ }
+
+ @Test
+ public void testGetPriorId() {
+ fail("Not yet implemented");
+ }
+
+ @Test
+ public void testGetAnalysis() {
+ fail("Not yet implemented");
+ }
+
+ @Test
+ public void testGetBugCollection() {
+ fail("Not yet implemented");
+ }
+*/
+}
--- /dev/null
+package net.jaekl.cfb.util;
+
+import static org.junit.Assert.*;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.Test;
+
+public class UtilTest {
+ @Test
+ public void testListsAreEqual()
+ {
+ Object[][][] equal = {
+ {
+ null,
+ null
+ },
+ {
+ { "one", "two", "three" },
+ { "one", "two", "three" }
+ },
+ {
+ { Integer.valueOf(1), Integer.valueOf(2048), Integer.valueOf(3) },
+ { Integer.valueOf(1), Integer.valueOf(2048), Integer.valueOf(3) }
+ }
+ };
+ Object[][][] unequal = {
+ {
+ null,
+ { "one", "two", "three" }
+ },
+ {
+ { "1", "1", "2" },
+ { "1", "2", "1" }
+ },
+ {
+ { "1", "1", "2" },
+ { "1", "2", "2" }
+ },
+ {
+ { "1", "1", "2" },
+ { "1", "2" }
+ },
+ {
+ { "1", "2", "3" },
+ { Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3) }
+ },
+ {
+ { Integer.valueOf(1), Integer.valueOf(2048), Integer.valueOf(3) },
+ { Integer.valueOf(1), Integer.valueOf(2048), Long.valueOf(3) }
+ }
+ };
+
+ for (Object[][] lists : equal) {
+ List<?> a = (null == lists[0]) ? null : Arrays.asList(lists[0]);
+ List<?> b = (null == lists[1]) ? null : Arrays.asList(lists[1]);
+ boolean result = Util.listsAreEqual(a, b);
+ assertTrue(result);
+ }
+
+ for (Object[][] lists : unequal) {
+ List<?> a = (null == lists[0]) ? null : Arrays.asList(lists[0]);
+ List<?> b = (null == lists[1]) ? null : Arrays.asList(lists[1]);
+ boolean result = Util.listsAreEqual(a, b);
+ assertFalse(result);
+ }
+ }
+}