View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2004, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   */
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  import org.dbunit.testutil.TestUtils;
31  
32  import org.xml.sax.EntityResolver;
33  import org.xml.sax.InputSource;
34  import org.xml.sax.SAXException;
35  
36  import java.io.File;
37  import java.io.IOException;
38  import java.io.StringReader;
39  
40  /**
41   * @author Manuel Laflamme
42   * @since Apr 28, 2003
43   * @version $Revision$
44   */
45  public class FlatXmlProducerTest extends AbstractProducerTest
46  {
47      private static final File DATASET_FILE =
48              TestUtils.getFile("xml/flatXmlProducerTest.xml");
49  
50      public FlatXmlProducerTest(String s)
51      {
52          super(s);
53      }
54  
55      protected IDataSetProducer createProducer() throws Exception
56      {
57          String uri = DATASET_FILE.getAbsoluteFile().toURL().toString();
58          InputSource source = new InputSource(uri);
59  
60          return new FlatXmlProducer(source);
61      }
62  
63      public void testProduceEmptyDataSet() throws Exception
64      {
65          // Setup consumer
66          MockDataSetConsumer consumer = new MockDataSetConsumer();
67          consumer.addExpectedStartDataSet();
68          consumer.addExpectedEndDataSet();
69  
70          // Setup producer
71          String content =
72                  "<?xml version=\"1.0\"?>" +
73                  "<dataset/>";
74          InputSource source = new InputSource(new StringReader(content));
75          IDataSetProducer producer = new FlatXmlProducer(source);
76          producer.setConsumer(consumer);
77  
78          // Produce and verify consumer
79          producer.produce();
80          consumer.verify();
81      }
82  
83      public void testProduceNoDtd() throws Exception
84      {
85          // Setup consumer
86          String tableName = "EMPTY_TABLE";
87          MockDataSetConsumer consumer = new MockDataSetConsumer();
88          consumer.addExpectedStartDataSet();
89          Column[] expectedColumns = new Column[0];
90          consumer.addExpectedEmptyTable(tableName, expectedColumns);
91          consumer.addExpectedEndDataSet();
92  
93          // Setup producer
94          String content =
95                  "<?xml version=\"1.0\"?>" +
96                  "<dataset>" +
97                      "<EMPTY_TABLE/>" +
98                  "</dataset>";
99          InputSource source = new InputSource(new StringReader(content));
100         IDataSetProducer producer = new FlatXmlProducer(source);
101         producer.setConsumer(consumer);
102 
103         // Produce and verify consumer
104         producer.produce();
105         consumer.verify();
106     }
107 
108     public void testProduceIgnoreDtd() throws Exception
109     {
110         // Setup consumer
111         String tableName = "EMPTY_TABLE";
112         MockDataSetConsumer consumer = new MockDataSetConsumer();
113         consumer.addExpectedStartDataSet();
114         Column[] expectedColumns = new Column[0];
115         consumer.addExpectedEmptyTable(tableName, expectedColumns);
116         consumer.addExpectedEndDataSet();
117 
118         // Setup producer
119         String content =
120                 "<?xml version=\"1.0\"?>" +
121                 "<!DOCTYPE dataset SYSTEM \"uri:/dummy.dtd\">" +
122                 "<dataset>" +
123                     "<EMPTY_TABLE/>" +
124                 "</dataset>";
125         InputSource source = new InputSource(new StringReader(content));
126         IDataSetProducer producer = new FlatXmlProducer(source, false);
127         producer.setConsumer(consumer);
128 
129         // Produce and verify consumer
130         producer.produce();
131         consumer.verify();
132     }
133 
134     public void testProduceMetaDataSet() throws Exception
135     {
136         // Setup consumer
137         String tableName = "EMPTY_TABLE";
138         MockDataSetConsumer consumer = new MockDataSetConsumer();
139         consumer.addExpectedStartDataSet();
140         Column[] expectedColumns = createExpectedColumns(Column.NULLABLE);
141         consumer.addExpectedEmptyTable(tableName, expectedColumns);
142         consumer.addExpectedEndDataSet();
143 
144         // Setup producer
145         String content =
146                 "<?xml version=\"1.0\"?>" +
147                 "<!DOCTYPE dataset SYSTEM \"urn:/dummy.dtd\">" +
148                 "<dataset>" +
149                     "<EMPTY_TABLE/>" +
150                 "</dataset>";
151         InputSource source = new InputSource(new StringReader(content));
152         DefaultDataSet metaDataSet = new DefaultDataSet();
153         metaDataSet.addTable(new DefaultTable(tableName, expectedColumns));
154         IDataSetProducer producer = new FlatXmlProducer(source, metaDataSet);
155         producer.setConsumer(consumer);
156 
157         // Produce and verify consumer
158         producer.produce();
159         consumer.verify();
160     }
161 
162     public void testProduceCustomEntityResolver() throws Exception
163     {
164         // Setup consumer
165         String tableName = "EMPTY_TABLE";
166         MockDataSetConsumer consumer = new MockDataSetConsumer();
167         consumer.addExpectedStartDataSet();
168         Column[] expectedColumns = createExpectedColumns(Column.NULLABLE);
169         consumer.addExpectedEmptyTable(tableName, expectedColumns);
170         consumer.addExpectedEndDataSet();
171 
172         // Setup producer
173        String dtdContent =
174                 "<!ELEMENT dataset (EMPTY_TABLE)>" +
175                 "<!ATTLIST EMPTY_TABLE " +
176                     "COLUMN0 CDATA #IMPLIED " +
177                     "COLUMN1 CDATA #IMPLIED " +
178                     "COLUMN2 CDATA #IMPLIED " +
179                     "COLUMN3 CDATA #IMPLIED>" +
180                 "<!ELEMENT TEST_TABLE EMPTY>";
181         final InputSource dtdSource = new InputSource(new StringReader(dtdContent));
182 
183         String xmlContent =
184                 "<?xml version=\"1.0\"?>" +
185                 "<!DOCTYPE dataset SYSTEM \"urn:/dummy.dtd\">" +
186                 "<dataset>" +
187                     "<EMPTY_TABLE/>" +
188                 "</dataset>";
189         InputSource xmlSource = new InputSource(new StringReader(xmlContent));
190         IDataSetProducer producer = new FlatXmlProducer(xmlSource, new EntityResolver(){
191             public InputSource resolveEntity(String s, String s1) throws SAXException, IOException
192             {
193                 return dtdSource;
194             }
195         });
196         producer.setConsumer(consumer);
197 
198         // Produce and verify consumer
199         producer.produce();
200         consumer.verify();
201     }
202 
203     public void testProduceNotWellFormedXml() throws Exception
204     {
205         // Setup consumer
206         MockDataSetConsumer consumer = new MockDataSetConsumer();
207         consumer.addExpectedStartDataSet();
208 
209         // Setup producer
210         String content =
211                 "<?xml version=\"1.0\"?>" +
212                 "<dataset>";
213         InputSource source = new InputSource(new StringReader(content));
214         IDataSetProducer producer = new FlatXmlProducer(source);
215         producer.setConsumer(consumer);
216 
217         // Produce and verify consumer
218         try
219         {
220             producer.produce();
221             fail("Should not be here!");
222         }
223         catch (DataSetException e)
224         {
225         }
226 
227         consumer.verify();
228     }
229 
230 }