KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > xml > FlatXmlProducerTest


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
31 import org.xml.sax.EntityResolver JavaDoc;
32 import org.xml.sax.InputSource JavaDoc;
33 import org.xml.sax.SAXException JavaDoc;
34
35 import java.io.File JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.StringReader JavaDoc;
38
39 /**
40  * @author Manuel Laflamme
41  * @since Apr 28, 2003
42  * @version $Revision: 1.6 $
43  */

44 public class FlatXmlProducerTest extends AbstractProducerTest
45 {
46     private static final File JavaDoc DATASET_FILE =
47             new File JavaDoc("src/xml/flatXmlProducerTest.xml");
48
49     public FlatXmlProducerTest(String JavaDoc s)
50     {
51         super(s);
52     }
53
54     protected IDataSetProducer createProducer() throws Exception JavaDoc
55     {
56         String JavaDoc uri = DATASET_FILE.getAbsoluteFile().toURL().toString();
57         InputSource JavaDoc source = new InputSource JavaDoc(uri);
58
59         return new FlatXmlProducer(source);
60     }
61
62     public void testProduceEmptyDataSet() throws Exception JavaDoc
63     {
64         // Setup consumer
65
MockDataSetConsumer consumer = new MockDataSetConsumer();
66         consumer.addExpectedStartDataSet();
67         consumer.addExpectedEndDataSet();
68
69         // Setup producer
70
String JavaDoc content =
71                 "<?xml version=\"1.0\"?>" +
72                 "<dataset/>";
73         InputSource JavaDoc source = new InputSource JavaDoc(new StringReader JavaDoc(content));
74         IDataSetProducer producer = new FlatXmlProducer(source);
75         producer.setConsumer(consumer);
76
77         // Produce and verify consumer
78
producer.produce();
79         consumer.verify();
80     }
81
82     public void testProduceNoDtd() throws Exception JavaDoc
83     {
84         // Setup consumer
85
String JavaDoc 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         // Setup producer
93
String JavaDoc content =
94                 "<?xml version=\"1.0\"?>" +
95                 "<dataset>" +
96                     "<EMPTY_TABLE/>" +
97                 "</dataset>";
98         InputSource JavaDoc source = new InputSource JavaDoc(new StringReader JavaDoc(content));
99         IDataSetProducer producer = new FlatXmlProducer(source);
100         producer.setConsumer(consumer);
101
102         // Produce and verify consumer
103
producer.produce();
104         consumer.verify();
105     }
106
107     public void testProduceIgnoreDtd() throws Exception JavaDoc
108     {
109         // Setup consumer
110
String JavaDoc 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         // Setup producer
118
String JavaDoc content =
119                 "<?xml version=\"1.0\"?>" +
120                 "<!DOCTYPE dataset SYSTEM \"uri:/dummy.dtd\">" +
121                 "<dataset>" +
122                     "<EMPTY_TABLE/>" +
123                 "</dataset>";
124         InputSource JavaDoc source = new InputSource JavaDoc(new StringReader JavaDoc(content));
125         IDataSetProducer producer = new FlatXmlProducer(source, false);
126         producer.setConsumer(consumer);
127
128         // Produce and verify consumer
129
producer.produce();
130         consumer.verify();
131     }
132
133     public void testProduceMetaDataSet() throws Exception JavaDoc
134     {
135         // Setup consumer
136
String JavaDoc 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         // Setup producer
144
String JavaDoc content =
145                 "<?xml version=\"1.0\"?>" +
146                 "<!DOCTYPE dataset SYSTEM \"urn:/dummy.dtd\">" +
147                 "<dataset>" +
148                     "<EMPTY_TABLE/>" +
149                 "</dataset>";
150         InputSource JavaDoc source = new InputSource JavaDoc(new StringReader JavaDoc(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         // Produce and verify consumer
157
producer.produce();
158         consumer.verify();
159     }
160
161     public void testProduceCustomEntityResolver() throws Exception JavaDoc
162     {
163         // Setup consumer
164
String JavaDoc 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         // Setup producer
172
String JavaDoc 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 JavaDoc dtdSource = new InputSource JavaDoc(new StringReader JavaDoc(dtdContent));
181
182         String JavaDoc xmlContent =
183                 "<?xml version=\"1.0\"?>" +
184                 "<!DOCTYPE dataset SYSTEM \"urn:/dummy.dtd\">" +
185                 "<dataset>" +
186                     "<EMPTY_TABLE/>" +
187                 "</dataset>";
188         InputSource JavaDoc xmlSource = new InputSource JavaDoc(new StringReader JavaDoc(xmlContent));
189         IDataSetProducer producer = new FlatXmlProducer(xmlSource, new EntityResolver JavaDoc(){
190             public InputSource JavaDoc resolveEntity(String JavaDoc s, String JavaDoc s1) throws SAXException JavaDoc, IOException JavaDoc
191             {
192                 return dtdSource;
193             }
194         });
195         producer.setConsumer(consumer);
196
197         // Produce and verify consumer
198
producer.produce();
199         consumer.verify();
200     }
201
202     public void testProduceNotWellFormedXml() throws Exception JavaDoc
203     {
204         // Setup consumer
205
MockDataSetConsumer consumer = new MockDataSetConsumer();
206         consumer.addExpectedStartDataSet();
207
208         // Setup producer
209
String JavaDoc content =
210                 "<?xml version=\"1.0\"?>" +
211                 "<dataset>";
212         InputSource JavaDoc source = new InputSource JavaDoc(new StringReader JavaDoc(content));
213         IDataSetProducer producer = new FlatXmlProducer(source);
214         producer.setConsumer(consumer);
215
216         // Produce and verify consumer
217
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