1 21 22 package org.dbunit.dataset; 23 24 import org.dbunit.database.AmbiguousTableNameException; 25 26 import java.util.ArrayList ; 27 import java.util.Arrays ; 28 import java.util.List ; 29 30 39 public abstract class AbstractDataSet implements IDataSet 40 { 41 protected ITable[] cloneTables(ITable[] tables) 42 { 43 ITable[] clones = new ITable[tables.length]; 44 for (int i = 0; i < tables.length; i++) 45 { 46 clones[i] = tables[i]; 47 } 48 return clones; 49 } 50 51 protected abstract ITableIterator createIterator(boolean reversed) 52 throws DataSetException; 53 54 57 public String [] getTableNames() throws DataSetException 58 { 59 List tableNameList = new ArrayList (); 60 ITableIterator iterator = createIterator(false); 61 while (iterator.next()) 62 { 63 tableNameList.add(iterator.getTableMetaData().getTableName()); 64 } 65 return (String [])tableNameList.toArray(new String [0]); 66 } 67 68 public ITableMetaData getTableMetaData(String tableName) throws DataSetException 69 { 70 return getTable(tableName).getTableMetaData(); 71 } 72 73 public ITable getTable(String tableName) throws DataSetException 74 { 75 ITable found = null; 76 ITableIterator iterator = createIterator(false); 77 while (iterator.next()) 78 { 79 ITable table = iterator.getTable(); 80 if (tableName.equalsIgnoreCase(table.getTableMetaData().getTableName())) 81 { 82 if (found != null) 83 { 84 throw new AmbiguousTableNameException(tableName); 85 } 86 87 found = table; 88 } 89 } 90 91 if (found != null) 92 { 93 return found; 94 } 95 96 throw new NoSuchTableException(tableName); 97 } 98 99 public ITable[] getTables() throws DataSetException 100 { 101 List tableList = new ArrayList (); 102 ITableIterator iterator = createIterator(false); 103 while (iterator.next()) 104 { 105 tableList.add(iterator.getTable()); 106 } 107 return (ITable[])tableList.toArray(new ITable[0]); 108 } 109 110 public ITableIterator iterator() throws DataSetException 111 { 112 return createIterator(false); 113 } 114 115 public ITableIterator reverseIterator() throws DataSetException 116 { 117 return createIterator(true); 118 } 119 120 123 public String toString() 124 { 125 try 126 { 127 return Arrays.asList(getTableNames()).toString(); 128 } 129 catch (DataSetException e) 130 { 131 return super.toString(); 132 } 133 } 134 } 135 136 137 138 139 140 141 | Popular Tags |