1 21 22 package org.dbunit.dataset.xml; 23 24 import org.dbunit.Assertion; 25 import org.dbunit.dataset.AbstractDataSetTest; 26 import org.dbunit.dataset.DataSetUtils; 27 import org.dbunit.dataset.IDataSet; 28 import org.dbunit.dataset.ITable; 29 30 import java.io.*; 31 import java.util.ArrayList ; 32 import java.util.List ; 33 34 39 public class XmlDataSetTest extends AbstractDataSetTest 40 { 41 public XmlDataSetTest(String s) 42 { 43 super(s); 44 } 45 46 protected IDataSet createDataSet() throws Exception 47 { 48 Reader in = new FileReader( 49 new File("src/xml/dataSetTest.xml")); 50 return new XmlDataSet(in); 51 } 52 53 protected IDataSet createDuplicateDataSet() throws Exception 54 { 55 InputStream in = new FileInputStream( 56 new File("src/xml/xmlDataSetDuplicateTest.xml")); 57 return new XmlDataSet(in); 58 } 59 60 public void testWrite() throws Exception 61 { 62 List tableList = new ArrayList (); 63 64 IDataSet expectedDataSet = (XmlDataSet)createDataSet(); 65 File tempFile = File.createTempFile("dataSetTest", ".xml"); 66 try 67 { 68 OutputStream out = new FileOutputStream(tempFile); 69 70 try 71 { 72 XmlDataSet.write(expectedDataSet, out); 74 75 IDataSet actualDataSet = new XmlDataSet(new FileReader(tempFile)); 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 out.close(); 99 } 100 } 101 finally 102 { 103 tempFile.delete(); 104 } 105 } 106 107 public void testDuplicateWrite() throws Exception 108 { 109 List tableList = new ArrayList (); 110 111 IDataSet expectedDataSet = (XmlDataSet)createDuplicateDataSet(); 112 File tempFile = File.createTempFile("xmlDataSetDuplicateTest", ".xml"); 113 try 114 { 115 OutputStream out = new FileOutputStream(tempFile); 116 117 try 118 { 119 XmlDataSet.write(expectedDataSet, out); 121 122 IDataSet actualDataSet = new XmlDataSet(new FileReader(tempFile)); 124 125 assertEquals("table count", expectedDataSet.getTableNames().length, 127 actualDataSet.getTableNames().length); 128 129 ITable[] expected = DataSetUtils.getTables(expectedDataSet); 131 ITable[] actual = DataSetUtils.getTables(actualDataSet); 132 assertEquals("table count", expected.length, actual.length); 133 for (int i = 0; i < expected.length; i++) 134 { 135 String expectedName = expected[i].getTableMetaData().getTableName(); 136 String actualName = actual[i].getTableMetaData().getTableName(); 137 assertEquals("table name", expectedName, actualName); 138 139 assertTrue("not same instance", expected[i] != actual[i]); 140 Assertion.assertEquals(expected[i], actual[i]); 141 } 142 } 143 finally 144 { 145 out.close(); 146 } 147 } 148 finally 149 { 150 tempFile.delete(); 151 } 152 } 153 154 } 155 156 157 158 159 | Popular Tags |