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.DefaultDataSet; 26 import org.dbunit.dataset.DefaultTable; 27 import org.dbunit.dataset.stream.AbstractProducerTest; 28 import org.dbunit.dataset.stream.IDataSetProducer; 29 import org.dbunit.dataset.stream.MockDataSetConsumer; 30 31 import org.xml.sax.EntityResolver ; 32 import org.xml.sax.InputSource ; 33 import org.xml.sax.SAXException ; 34 35 import java.io.File ; 36 import java.io.IOException ; 37 import java.io.StringReader ; 38 39 44 public class FlatXmlProducerTest extends AbstractProducerTest 45 { 46 private static final File DATASET_FILE = 47 new File ("src/xml/flatXmlProducerTest.xml"); 48 49 public FlatXmlProducerTest(String s) 50 { 51 super(s); 52 } 53 54 protected IDataSetProducer createProducer() throws Exception 55 { 56 String uri = DATASET_FILE.getAbsoluteFile().toURL().toString(); 57 InputSource source = new InputSource (uri); 58 59 return new FlatXmlProducer(source); 60 } 61 62 public void testProduceEmptyDataSet() throws Exception 63 { 64 MockDataSetConsumer consumer = new MockDataSetConsumer(); 66 consumer.addExpectedStartDataSet(); 67 consumer.addExpectedEndDataSet(); 68 69 String content = 71 "<?xml version=\"1.0\"?>" + 72 "<dataset/>"; 73 InputSource source = new InputSource (new StringReader (content)); 74 IDataSetProducer producer = new FlatXmlProducer(source); 75 producer.setConsumer(consumer); 76 77 producer.produce(); 79 consumer.verify(); 80 } 81 82 public void testProduceNoDtd() throws Exception 83 { 84 String tableName = "EMPTY_TABLE"; 86 MockDataSetConsumer consumer = new MockDataSetConsumer(); 87 consumer.addExpectedStartDataSet(); 88 Column[] expectedColumns = new Column[0]; 89 consumer.addExpectedEmptyTable(tableName, expectedColumns); 90 consumer.addExpectedEndDataSet(); 91 92 String content = 94 "<?xml version=\"1.0\"?>" + 95 "<dataset>" + 96 "<EMPTY_TABLE/>" + 97 "</dataset>"; 98 InputSource source = new InputSource (new StringReader (content)); 99 IDataSetProducer producer = new FlatXmlProducer(source); 100 producer.setConsumer(consumer); 101 102 producer.produce(); 104 consumer.verify(); 105 } 106 107 public void testProduceIgnoreDtd() throws Exception 108 { 109 String tableName = "EMPTY_TABLE"; 111 MockDataSetConsumer consumer = new MockDataSetConsumer(); 112 consumer.addExpectedStartDataSet(); 113 Column[] expectedColumns = new Column[0]; 114 consumer.addExpectedEmptyTable(tableName, expectedColumns); 115 consumer.addExpectedEndDataSet(); 116 117 String content = 119 "<?xml version=\"1.0\"?>" + 120 "<!DOCTYPE dataset SYSTEM \"uri:/dummy.dtd\">" + 121 "<dataset>" + 122 "<EMPTY_TABLE/>" + 123 "</dataset>"; 124 InputSource source = new InputSource (new StringReader (content)); 125 IDataSetProducer producer = new FlatXmlProducer(source, false); 126 producer.setConsumer(consumer); 127 128 producer.produce(); 130 consumer.verify(); 131 } 132 133 public void testProduceMetaDataSet() throws Exception 134 { 135 String tableName = "EMPTY_TABLE"; 137 MockDataSetConsumer consumer = new MockDataSetConsumer(); 138 consumer.addExpectedStartDataSet(); 139 Column[] expectedColumns = createExpectedColumns(Column.NULLABLE); 140 consumer.addExpectedEmptyTable(tableName, expectedColumns); 141 consumer.addExpectedEndDataSet(); 142 143 String content = 145 "<?xml version=\"1.0\"?>" + 146 "<!DOCTYPE dataset SYSTEM \"urn:/dummy.dtd\">" + 147 "<dataset>" + 148 "<EMPTY_TABLE/>" + 149 "</dataset>"; 150 InputSource source = new InputSource (new StringReader (content)); 151 DefaultDataSet metaDataSet = new DefaultDataSet(); 152 metaDataSet.addTable(new DefaultTable(tableName, expectedColumns)); 153 IDataSetProducer producer = new FlatXmlProducer(source, metaDataSet); 154 producer.setConsumer(consumer); 155 156 producer.produce(); 158 consumer.verify(); 159 } 160 161 public void testProduceCustomEntityResolver() throws Exception 162 { 163 String tableName = "EMPTY_TABLE"; 165 MockDataSetConsumer consumer = new MockDataSetConsumer(); 166 consumer.addExpectedStartDataSet(); 167 Column[] expectedColumns = createExpectedColumns(Column.NULLABLE); 168 consumer.addExpectedEmptyTable(tableName, expectedColumns); 169 consumer.addExpectedEndDataSet(); 170 171 String dtdContent = 173 "<!ELEMENT dataset (EMPTY_TABLE)>" + 174 "<!ATTLIST EMPTY_TABLE " + 175 "COLUMN0 CDATA #IMPLIED " + 176 "COLUMN1 CDATA #IMPLIED " + 177 "COLUMN2 CDATA #IMPLIED " + 178 "COLUMN3 CDATA #IMPLIED>" + 179 "<!ELEMENT TEST_TABLE EMPTY>"; 180 final InputSource dtdSource = new InputSource (new StringReader (dtdContent)); 181 182 String xmlContent = 183 "<?xml version=\"1.0\"?>" + 184 "<!DOCTYPE dataset SYSTEM \"urn:/dummy.dtd\">" + 185 "<dataset>" + 186 "<EMPTY_TABLE/>" + 187 "</dataset>"; 188 InputSource xmlSource = new InputSource (new StringReader (xmlContent)); 189 IDataSetProducer producer = new FlatXmlProducer(xmlSource, new EntityResolver (){ 190 public InputSource resolveEntity(String s, String s1) throws SAXException , IOException 191 { 192 return dtdSource; 193 } 194 }); 195 producer.setConsumer(consumer); 196 197 producer.produce(); 199 consumer.verify(); 200 } 201 202 public void testProduceNotWellFormedXml() throws Exception 203 { 204 MockDataSetConsumer consumer = new MockDataSetConsumer(); 206 consumer.addExpectedStartDataSet(); 207 208 String content = 210 "<?xml version=\"1.0\"?>" + 211 "<dataset>"; 212 InputSource source = new InputSource (new StringReader (content)); 213 IDataSetProducer producer = new FlatXmlProducer(source); 214 producer.setConsumer(consumer); 215 216 try 218 { 219 producer.produce(); 220 fail("Should not be here!"); 221 } 222 catch (DataSetException e) 223 { 224 } 225 226 consumer.verify(); 227 } 228 229 } 230 | Popular Tags |