001: package jimm.datavision.test;
002:
003: import jimm.datavision.source.*;
004: import junit.framework.TestCase;
005: import junit.framework.TestSuite;
006: import junit.framework.Test;
007: import java.util.*;
008:
009: class TestTable extends Table {
010: TestTable(String name) {
011: super (null, name);
012: columns = new TreeMap();
013: }
014:
015: void add(Column col) {
016: columns.put(col.getName(), col);
017: }
018: }
019:
020: // ----------------------------------------------------------------
021:
022: class TestColumn extends Column {
023: TestColumn(String name) {
024: super (name, name, java.sql.Types.INTEGER);
025: }
026: }
027:
028: // ----------------------------------------------------------------
029:
030: public class ColumnIteratorTest extends TestCase {
031:
032: protected static final int NUM_TABLES = 3;
033: protected static final int NUM_COLUMNS = 9;
034:
035: protected TestTable[] tables;
036: protected TestColumn[] columns;
037: protected ArrayList tableList;
038:
039: public static Test suite() {
040: return new TestSuite(ColumnIteratorTest.class);
041: }
042:
043: public ColumnIteratorTest(String name) {
044: super (name);
045: }
046:
047: public void setUp() {
048: tables = new TestTable[NUM_TABLES];
049: for (int i = 0; i < NUM_TABLES; ++i)
050: tables[i] = new TestTable("table" + i);
051:
052: columns = new TestColumn[NUM_COLUMNS];
053: for (int i = 0; i < NUM_COLUMNS; ++i)
054: columns[i] = new TestColumn("col" + i);
055:
056: tableList = new ArrayList();
057: }
058:
059: public void testEmpty() {
060: for (Iterator iter = new ColumnIterator(tableList.iterator()); iter
061: .hasNext();) {
062: fail("should not return anything");
063: }
064: }
065:
066: public void testAllEmptyTables() {
067: ArrayList tables = new ArrayList();
068:
069: for (Iterator iter = new ColumnIterator(tables.iterator()); iter
070: .hasNext();) {
071: fail("should not return anything");
072: }
073: }
074:
075: public void testOneTable() {
076: for (int i = 0; i < 3; ++i)
077: // One table, three columns
078: tables[0].add(columns[i]);
079:
080: tableList.add(tables[0]);
081:
082: int i = 0;
083: for (Iterator iter = new ColumnIterator(tableList.iterator()); iter
084: .hasNext(); ++i)
085: assertSame(columns[i], (Column) iter.next());
086: assertEquals(3, i);
087: }
088:
089: public void testManyTables() {
090: for (int i = 0; i < NUM_TABLES; ++i) { // Three tables
091: tableList.add(tables[i]);
092: for (int j = 0; j < 3; ++j)
093: // Three columns each
094: tables[i].add(columns[i * 3 + j]);
095: }
096:
097: int i = 0;
098: for (Iterator iter = new ColumnIterator(tableList.iterator()); iter
099: .hasNext(); ++i)
100: assertSame(columns[i], (Column) iter.next());
101: assertEquals(NUM_COLUMNS, i);
102: }
103:
104: protected void skipTableTest(int skip) {
105: int j = 0;
106: for (int i = 0; i < NUM_TABLES; ++i) {
107: tableList.add(tables[i]);
108: if (i != skip) {
109: tables[i].add(columns[j++]);
110: tables[i].add(columns[j++]);
111: tables[i].add(columns[j++]);
112: }
113: }
114:
115: int i = 0;
116: for (Iterator iter = new ColumnIterator(tableList.iterator()); iter
117: .hasNext(); ++i)
118: assertSame(columns[i], (Column) iter.next());
119: assertEquals((NUM_TABLES - 1) * 3, i);
120: }
121:
122: public void testEmptyTable0() {
123: skipTableTest(0);
124: }
125:
126: public void testEmptyTable1() {
127: skipTableTest(1);
128: }
129:
130: public void testEmptyTable2() {
131: skipTableTest(2);
132: }
133:
134: public void testBeyondEnd() {
135: Iterator iter = new ColumnIterator(tableList.iterator());
136: assertTrue("should not have more", !iter.hasNext());
137: try {
138: iter.next();
139: fail("should throw exception");
140: } catch (NoSuchElementException e) {
141: }
142: }
143:
144: public void testRemove() {
145: try {
146: tableList.add(tables[0]);
147: new ColumnIterator(tableList.iterator()).remove();
148: fail("remove not supported");
149: } catch (UnsupportedOperationException e) {
150: }
151: }
152:
153: public static void main(String[] args) {
154: junit.textui.TestRunner.run(suite());
155: System.exit(0);
156: }
157:
158: }
|