1 21 package org.dbunit.dataset.excel; 22 23 import org.dbunit.Assertion; 24 import org.dbunit.dataset.AbstractDataSetTest; 25 import org.dbunit.dataset.DataSetUtils; 26 import org.dbunit.dataset.IDataSet; 27 import org.dbunit.dataset.ITable; 28 29 import java.io.*; 30 31 36 public class XlsDataSetTest extends AbstractDataSetTest 37 { 38 public XlsDataSetTest(String s) 39 { 40 super(s); 41 } 42 43 protected IDataSet createDataSet() throws Exception 44 { 45 return new XlsDataSet(new File("src/xml/dataSetTest.xls")); 46 } 47 48 protected IDataSet createDuplicateDataSet() throws Exception 49 { 50 return new XlsDataSet( 51 new File("src/xml/dataSetDuplicateTest.xls")); 52 } 53 54 public void testWrite() throws Exception 55 { 56 IDataSet expectedDataSet = createDataSet(); 57 File tempFile = File.createTempFile("xlsDataSetTest", ".xls"); 58 try 59 { 60 OutputStream out = new FileOutputStream(tempFile); 61 62 try 64 { 65 XlsDataSet.write(expectedDataSet, out); 66 } 67 finally 68 { 69 out.close(); 70 } 71 72 InputStream in = new FileInputStream(tempFile); 74 try 75 { 76 IDataSet actualDataSet = new XlsDataSet(in); 77 78 assertEquals("table count", expectedDataSet.getTableNames().length, 80 actualDataSet.getTableNames().length); 81 82 ITable[] expected = DataSetUtils.getTables(expectedDataSet); 84 ITable[] actual = DataSetUtils.getTables(actualDataSet); 85 assertEquals("table count", expected.length, actual.length); 86 for (int i = 0; i < expected.length; i++) 87 { 88 String expectedName = expected[i].getTableMetaData().getTableName(); 89 String actualName = actual[i].getTableMetaData().getTableName(); 90 assertEquals("table name", expectedName, actualName); 91 92 assertTrue("not same instance", expected[i] != actual[i]); 93 Assertion.assertEquals(expected[i], actual[i]); 94 } 95 } 96 finally 97 { 98 in.close(); 99 } 100 } 101 finally 102 { 103 tempFile.delete(); 104 } 105 } 106 107 public void testDuplicateWrite() throws Exception 108 { 109 IDataSet expectedDataSet = createDuplicateDataSet(); 110 File tempFile = File.createTempFile("xlsDataSetDuplicateTest", ".xls"); 111 try 112 { 113 OutputStream out = new FileOutputStream(tempFile); 114 115 try 117 { 118 XlsDataSet.write(expectedDataSet, out); 119 } 120 finally 121 { 122 out.close(); 123 } 124 125 InputStream in = new FileInputStream(tempFile); 127 try 128 { 129 IDataSet actualDataSet = new XlsDataSet(in); 130 131 assertEquals("table count", expectedDataSet.getTableNames().length, 133 actualDataSet.getTableNames().length); 134 135 ITable[] expected = DataSetUtils.getTables(expectedDataSet); 137 ITable[] actual = DataSetUtils.getTables(actualDataSet); 138 assertEquals("table count", expected.length, actual.length); 139 for (int i = 0; i < expected.length; i++) 140 { 141 String expectedName = expected[i].getTableMetaData().getTableName(); 142 String actualName = actual[i].getTableMetaData().getTableName(); 143 assertEquals("table name", expectedName, actualName); 144 145 assertTrue("not same instance", expected[i] != actual[i]); 146 Assertion.assertEquals(expected[i], actual[i]); 147 } 148 } 149 finally 150 { 151 in.close(); 152 } 153 } 154 finally 155 { 156 tempFile.delete(); 157 } 158 } 159 160 } 161 | Popular Tags |