1 package jimm.datavision.test; 2 import jimm.datavision.source.*; 3 import junit.framework.TestCase; 4 import junit.framework.TestSuite; 5 import junit.framework.Test; 6 import java.util.*; 7 8 class TestTable extends Table { 9 TestTable(String name) { 10 super(null, name); 11 columns = new TreeMap(); 12 } 13 void add(Column col) { columns.put(col.getName(), col); } 14 } 15 16 18 class TestColumn extends Column { 19 TestColumn(String name) { 20 super(name, name, java.sql.Types.INTEGER); 21 } 22 } 23 24 26 public class ColumnIteratorTest extends TestCase { 27 28 protected static final int NUM_TABLES = 3; 29 protected static final int NUM_COLUMNS = 9; 30 31 protected TestTable[] tables; 32 protected TestColumn[] columns; 33 protected ArrayList tableList; 34 35 public static Test suite() { 36 return new TestSuite(ColumnIteratorTest.class); 37 } 38 39 public ColumnIteratorTest(String name) { 40 super(name); 41 } 42 43 public void setUp() { 44 tables = new TestTable[NUM_TABLES]; 45 for (int i = 0; i < NUM_TABLES; ++i) 46 tables[i] = new TestTable("table" + i); 47 48 columns = new TestColumn[NUM_COLUMNS]; 49 for (int i = 0; i < NUM_COLUMNS; ++i) 50 columns[i] = new TestColumn("col" + i); 51 52 tableList = new ArrayList(); 53 } 54 55 public void testEmpty() { 56 for (Iterator iter = new ColumnIterator(tableList.iterator()); 57 iter.hasNext(); ) 58 { 59 fail("should not return anything"); 60 } 61 } 62 63 public void testAllEmptyTables() { 64 ArrayList tables = new ArrayList(); 65 66 for (Iterator iter = new ColumnIterator(tables.iterator()); 67 iter.hasNext(); ) 68 { 69 fail("should not return anything"); 70 } 71 } 72 73 public void testOneTable() { 74 for (int i = 0; i < 3; ++i) tables[0].add(columns[i]); 76 77 tableList.add(tables[0]); 78 79 int i = 0; 80 for (Iterator iter = new ColumnIterator(tableList.iterator()); 81 iter.hasNext(); ++i) 82 assertSame(columns[i], (Column)iter.next()); 83 assertEquals(3, i); 84 } 85 86 public void testManyTables() { 87 for (int i = 0; i < NUM_TABLES; ++i) { tableList.add(tables[i]); 89 for (int j = 0; j < 3; ++j) tables[i].add(columns[i * 3 + j]); 91 } 92 93 int i = 0; 94 for (Iterator iter = new ColumnIterator(tableList.iterator()); 95 iter.hasNext(); ++i) 96 assertSame(columns[i], (Column)iter.next()); 97 assertEquals(NUM_COLUMNS, i); 98 } 99 100 protected void skipTableTest(int skip) { 101 int j = 0; 102 for (int i = 0; i < NUM_TABLES; ++i) { 103 tableList.add(tables[i]); 104 if (i != skip) { 105 tables[i].add(columns[j++]); 106 tables[i].add(columns[j++]); 107 tables[i].add(columns[j++]); 108 } 109 } 110 111 int i = 0; 112 for (Iterator iter = new ColumnIterator(tableList.iterator()); 113 iter.hasNext(); ++i) 114 assertSame(columns[i], (Column)iter.next()); 115 assertEquals((NUM_TABLES - 1) * 3, i); 116 } 117 118 public void testEmptyTable0() { 119 skipTableTest(0); 120 } 121 122 public void testEmptyTable1() { 123 skipTableTest(1); 124 } 125 126 public void testEmptyTable2() { 127 skipTableTest(2); 128 } 129 130 public void testBeyondEnd() { 131 Iterator iter = new ColumnIterator(tableList.iterator()); 132 assertTrue("should not have more", !iter.hasNext()); 133 try { 134 iter.next(); 135 fail("should throw exception"); 136 } 137 catch (NoSuchElementException e) {} 138 } 139 140 public void testRemove() { 141 try { 142 tableList.add(tables[0]); 143 new ColumnIterator(tableList.iterator()).remove(); 144 fail("remove not supported"); 145 } 146 catch (UnsupportedOperationException e) {} 147 } 148 149 public static void main(String [] args) { 150 junit.textui.TestRunner.run(suite()); 151 System.exit(0); 152 } 153 154 } 155 | Popular Tags |