import junit.framework.*; public class TableTest extends TestCase { private String[] columnNames; private int[][] rows; private Table table; public static void main(String[] args) { junit.textui.TestRunner.run(TableTest.class); } public TableTest(String name) { super(name); } public void setUp() { columnNames = new String[] {"col1", "col2", "col3"}; rows = new int[][] {{1,2,3}, {4,5,6}}; table = new Table("table1", columnNames, rows); } public void testTableNoError() { assertTrue("table created", true); } public void testTableError() { String[] columnNames = {"col1"}; int[][] rows = {{1,2}, {3,4}}; try { Table table = new Table("table2", columnNames, rows); fail("column and colum name count mismatch"); } catch(IllegalArgumentException iae) { assertTrue("table error caught", true); } } public void testFindColumn() throws Exception { assertTrue("col1", table.findColumn("col1") == 1); assertTrue("col2", table.findColumn("col2") == 2); assertTrue("col3", table.findColumn("col3") == 3); try { table.findColumn("xxx"); fail("didn't throw exception for bad column name"); } catch(Exception ex) { assertTrue("findColumn error caught", true); } } public void testAbsolute() throws Exception { int[] row = table.absolute(1); assertTrue("row 1, col 1", row[0] == 1); assertTrue("row 1, col 2", row[1] == 2); assertTrue("row 1, col 3", row[2] == 3); row = table.absolute(2); assertTrue("row 2, col 1", row[0] == 4); assertTrue("row 2, col 2", row[1] == 5); assertTrue("row 2, col 3", row[2] == 6); try { table.absolute(3); fail("didn't throw exception for bad row position"); } catch(Exception ex) { assertTrue("absolute error caught", true); } } public void testProject() throws Exception { Table resultTable = table.project(new String[] {"col1", "col3"}); assertTrue("project column name 1", resultTable.findColumn("col1") == 1); assertTrue("project column name 2", resultTable.findColumn("col3") == 2); try { resultTable.findColumn("col2"); fail("didn't throw exception for bad column name"); } catch(Exception ex) { assertTrue("column name error caught", true); } } public void testSelect() throws Exception { Table resultTable = table.select("col1", Op.EQ, 4); int[] row = resultTable.absolute(1); assertTrue("select col1 == 4 -> 4,5,6", (row[0] == 4) && (row[1] == 5) && (row[2] == 6)); resultTable = table.select("col2", Op.GE, 2); row = resultTable.absolute(1); assertTrue("select col2 >= 2 -> 1,2,3; ...", (row[0] == 1) && (row[1] == 2) && (row[2] == 3)); row = resultTable.absolute(2); assertTrue("select col2 >= 2 -> ...; 4,5,6", (row[0] == 4) && (row[1] == 5) && (row[2] == 6)); } public void testJoin() throws Exception { String[] columnNames2 = new String[] {"t2col1", "t2col2"}; int[][] rows2 = new int[][] {{3,7}, {6,8}}; Table table2 = new Table("table2", columnNames2, rows2); Table resultTable = table.join("col3", table2, "t2col1"); int[] row = resultTable.absolute(1); assertTrue("join col3 = table2.t2col1 -> 1,2,3,3,7; ...", (row[0] == 1) && (row[1] == 2) && (row[2] == 3) && (row[3] == 3) && (row[4] == 7)); row = resultTable.absolute(2); assertTrue("join col3 = table2.t2col1 -> ...; 4,5,6,6,8", (row[0] == 4) && (row[1] == 5) && (row[2] == 6) && (row[3] == 6) && (row[4] == 8)); } }