1 21 22 package org.dbunit.dataset.xml; 23 24 import org.dbunit.Assertion; 25 import org.dbunit.dataset.*; 26 27 import java.io.File ; 28 import java.io.FileReader ; 29 import java.io.FileWriter ; 30 import java.io.Writer ; 31 32 37 public class FlatXmlDataSetTest extends AbstractDataSetTest 38 { 39 protected static final File DATASET_FILE = 40 new File ("src/xml/flatXmlDataSetTest.xml"); 41 protected static final File DUPLICATE_DATASET_FILE = 42 new File ("src/xml/flatXmlDataSetDuplicateTest.xml"); 43 44 public FlatXmlDataSetTest(String s) 45 { 46 super(s); 47 } 48 49 protected IDataSet createDataSet() throws Exception 50 { 51 return new FlatXmlDataSet(DATASET_FILE); 52 } 53 54 protected IDataSet createDuplicateDataSet() throws Exception 55 { 56 return new FlatXmlDataSet( 57 DUPLICATE_DATASET_FILE); 58 } 59 60 public void testMissingColumnAndEnableDtdMetadata() throws Exception 61 { 62 IDataSet dataSet = new FlatXmlDataSet( 63 new File ("src/xml/flatXmlTableTest.xml"), true); 64 65 ITable table = dataSet.getTable("MISSING_VALUES"); 66 67 Column[] columns = table.getTableMetaData().getColumns(); 68 assertEquals("column count", 3, columns.length); 69 } 70 71 public void testMissingColumnAndDisableDtdMetadata() throws Exception 72 { 73 IDataSet dataSet = new FlatXmlDataSet( 74 new File ("src/xml/flatXmlTableTest.xml"), false); 75 76 ITable table = dataSet.getTable("MISSING_VALUES"); 77 78 Column[] columns = table.getTableMetaData().getColumns(); 79 assertEquals("column count", 2, columns.length); 80 } 81 82 public void testWrite() throws Exception 83 { 84 IDataSet expectedDataSet = createDataSet(); 85 File tempFile = File.createTempFile("flatXmlDataSetTest", ".xml"); 86 try 87 { 88 Writer out = new FileWriter (tempFile); 89 90 try 92 { 93 FlatXmlDataSet.write(expectedDataSet, out); 94 } 95 finally 96 { 97 out.close(); 98 } 99 100 FileReader in = new FileReader (tempFile); 102 try 103 { 104 IDataSet actualDataSet = new FlatXmlDataSet(in); 105 106 assertEquals("table count", expectedDataSet.getTableNames().length, 108 actualDataSet.getTableNames().length); 109 110 ITable[] expected = DataSetUtils.getTables(expectedDataSet); 112 ITable[] actual = DataSetUtils.getTables(actualDataSet); 113 assertEquals("table count", expected.length, actual.length); 114 for (int i = 0; i < expected.length; i++) 115 { 116 String expectedName = expected[i].getTableMetaData().getTableName(); 117 String actualName = actual[i].getTableMetaData().getTableName(); 118 assertEquals("table name", expectedName, actualName); 119 120 assertTrue("not same instance", expected[i] != actual[i]); 121 Assertion.assertEquals(expected[i], actual[i]); 122 } 123 } 124 finally 125 { 126 in.close(); 127 } 128 } 129 finally 130 { 131 tempFile.delete(); 132 } 133 } 134 135 public void testDuplicateWrite() throws Exception 136 { 137 IDataSet expectedDataSet = createDuplicateDataSet(); 138 File tempFile = File.createTempFile("flatXmlDataSetDuplicateTest", ".xml"); 139 try 140 { 141 Writer out = new FileWriter (tempFile); 142 143 try 145 { 146 FlatXmlDataSet.write(expectedDataSet, out); 147 } 148 finally 149 { 150 out.close(); 151 } 152 153 FileReader in = new FileReader (tempFile); 155 try 156 { 157 IDataSet actualDataSet = new FlatXmlDataSet(in); 158 159 assertEquals("table count", expectedDataSet.getTableNames().length, 161 actualDataSet.getTableNames().length); 162 163 ITable[] expected = DataSetUtils.getTables(expectedDataSet); 165 ITable[] actual = DataSetUtils.getTables(actualDataSet); 166 assertEquals("table count", expected.length, actual.length); 167 for (int i = 0; i < expected.length; i++) 168 { 169 String expectedName = expected[i].getTableMetaData().getTableName(); 170 String actualName = actual[i].getTableMetaData().getTableName(); 171 assertEquals("table name", expectedName, actualName); 172 173 assertTrue("not same instance", expected[i] != actual[i]); 174 Assertion.assertEquals(expected[i], actual[i]); 175 } 176 } 177 finally 178 { 179 in.close(); 180 } 181 } 182 finally 183 { 184 tempFile.delete(); 185 } 186 } 187 188 } 189 190 191 192 193 194 195 196 | Popular Tags |