1 21 package org.dbunit.dataset.xml; 22 23 import org.dbunit.dataset.Column; 24 import org.dbunit.dataset.DataSetException; 25 import org.dbunit.dataset.datatype.DataType; 26 import org.dbunit.dataset.stream.AbstractProducerTest; 27 import org.dbunit.dataset.stream.IDataSetProducer; 28 import org.dbunit.dataset.stream.MockDataSetConsumer; 29 import org.xml.sax.InputSource ; 30 31 import java.io.File ; 32 import java.io.StringReader ; 33 34 39 public class XmlProducerTest extends AbstractProducerTest 40 { 41 private static final File DATASET_FILE = 42 new File ("src/xml/xmlProducerTest.xml"); 43 44 public XmlProducerTest(String s) 45 { 46 super(s); 47 } 48 49 protected IDataSetProducer createProducer() throws Exception 50 { 51 String uri = DATASET_FILE.getAbsoluteFile().toURL().toString(); 52 InputSource source = new InputSource (uri); 53 54 XmlProducer producer = new XmlProducer(source); 55 producer.setValidating(true); 56 return producer; 57 } 58 59 protected Column[] createExpectedColumns(Column.Nullable nullable) throws Exception 60 { 61 return super.createExpectedColumns(Column.NULLABLE_UNKNOWN); 62 } 63 64 public void testProduceEmptyDataSet() throws Exception 65 { 66 MockDataSetConsumer consumer = new MockDataSetConsumer(); 68 consumer.addExpectedStartDataSet(); 69 consumer.addExpectedEndDataSet(); 70 71 String content = 73 "<?xml version=\"1.0\"?>" + 74 "<dataset/>"; 75 InputSource source = new InputSource (new StringReader (content)); 76 IDataSetProducer producer = new XmlProducer(source); 77 producer.setConsumer(consumer); 78 79 producer.produce(); 81 consumer.verify(); 82 } 83 84 public void testProduceNullValue() throws Exception 85 { 86 String tableName = "TEST_TABLE"; 87 Column[] expectedColumns = new Column[]{ 88 new Column("c1", DataType.UNKNOWN), 89 new Column("c2", DataType.UNKNOWN), 90 new Column("c3", DataType.UNKNOWN), 91 }; 92 Object [] expectedValues = new Object []{null, "", "value"}; 93 94 MockDataSetConsumer consumer = new MockDataSetConsumer(); 96 consumer.addExpectedStartDataSet(); 97 consumer.addExpectedStartTable(tableName, expectedColumns); 98 consumer.addExpectedRow(tableName, expectedValues); 99 consumer.addExpectedEndTable(tableName); 100 consumer.addExpectedEndDataSet(); 101 102 String content = 104 "<?xml version=\"1.0\"?>" + 105 "<dataset>" + 106 " <table name='TEST_TABLE'>" + 107 " <column>c1</column>" + 108 " <column>c2</column>" + 109 " <column>c3</column>" + 110 " <row>" + 111 " <null/>" + 112 " <value></value>" + 113 " <value>value</value>" + 114 " </row>" + 115 " </table>" + 116 "</dataset>"; 117 InputSource source = new InputSource (new StringReader (content)); 118 IDataSetProducer producer = new XmlProducer(source); 119 producer.setConsumer(consumer); 120 121 producer.produce(); 123 consumer.verify(); 124 } 125 126 public void testProduceMissingColumn() throws Exception 127 { 128 String tableName = "TEST_TABLE"; 129 Column[] expectedColumns = new Column[]{ 130 new Column("c1", DataType.UNKNOWN), 131 new Column("c2", DataType.UNKNOWN), 132 new Column("c3", DataType.UNKNOWN), 133 }; 134 Object [] expectedValues = new Object []{null, "", "value", "extra"}; 135 136 MockDataSetConsumer consumer = new MockDataSetConsumer(); 138 consumer.addExpectedStartDataSet(); 139 consumer.addExpectedStartTable(tableName, expectedColumns); 140 consumer.addExpectedRow(tableName, expectedValues); 141 consumer.addExpectedEndTable(tableName); 142 consumer.addExpectedEndDataSet(); 143 144 String content = 146 "<?xml version=\"1.0\"?>" + 147 "<dataset>" + 148 " <table name='TEST_TABLE'>" + 149 " <column>c1</column>" + 150 " <column>c2</column>" + 151 " <column>c3</column>" + 152 " <row>" + 153 " <null/>" + 154 " <value></value>" + 155 " <value>value</value>" + 156 " <value>extra</value>" + 157 " </row>" + 158 " </table>" + 159 "</dataset>"; 160 InputSource source = new InputSource (new StringReader (content)); 161 IDataSetProducer producer = new XmlProducer(source); 162 producer.setConsumer(consumer); 163 164 producer.produce(); 166 consumer.verify(); 167 } 168 169 public void testProduceNotWellFormedXml() throws Exception 170 { 171 MockDataSetConsumer consumer = new MockDataSetConsumer(); 173 consumer.addExpectedStartDataSet(); 174 175 String content = 177 "<?xml version=\"1.0\"?>" + 178 "<dataset>"; 179 InputSource source = new InputSource (new StringReader (content)); 180 IDataSetProducer producer = new XmlProducer(source); 181 producer.setConsumer(consumer); 182 183 try 185 { 186 producer.produce(); 187 fail("Should not be here!"); 188 } 189 catch (DataSetException e) 190 { 191 } 192 193 consumer.verify(); 194 } 195 196 public void testProduceInvalidXml() throws Exception 197 { 198 MockDataSetConsumer consumer = new MockDataSetConsumer(); 200 201 String content = 203 "<?xml version=\"1.0\"?>" + 204 "<!DOCTYPE dataset SYSTEM \"dataset.dtd\" >" + 205 "<invalid/>"; 206 InputSource source = new InputSource (new StringReader (content)); 207 source.setSystemId("http:/nowhere.to.go"); 208 XmlProducer producer = new XmlProducer(source); 209 producer.setValidating(true); 210 producer.setConsumer(consumer); 211 212 try 214 { 215 producer.produce(); 216 fail("Should not be here!"); 217 } 218 catch (DataSetException e) 219 { 220 } 221 222 consumer.verify(); 223 } 224 225 } 226 | Popular Tags |