KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc;
30
31 import java.io.File JavaDoc;
32 import java.io.StringReader JavaDoc;
33
34 /**
35  * @author Manuel Laflamme
36  * @since Apr 30, 2003
37  * @version $Revision: 1.5 $
38  */

39 public class XmlProducerTest extends AbstractProducerTest
40 {
41     private static final File JavaDoc DATASET_FILE =
42             new File JavaDoc("src/xml/xmlProducerTest.xml");
43
44     public XmlProducerTest(String JavaDoc s)
45     {
46         super(s);
47     }
48
49     protected IDataSetProducer createProducer() throws Exception JavaDoc
50     {
51         String JavaDoc uri = DATASET_FILE.getAbsoluteFile().toURL().toString();
52         InputSource JavaDoc source = new InputSource JavaDoc(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 JavaDoc
60     {
61         return super.createExpectedColumns(Column.NULLABLE_UNKNOWN);
62     }
63
64     public void testProduceEmptyDataSet() throws Exception JavaDoc
65     {
66         // Setup consumer
67
MockDataSetConsumer consumer = new MockDataSetConsumer();
68         consumer.addExpectedStartDataSet();
69         consumer.addExpectedEndDataSet();
70
71         // Setup producer
72
String JavaDoc content =
73                 "<?xml version=\"1.0\"?>" +
74                 "<dataset/>";
75         InputSource JavaDoc source = new InputSource JavaDoc(new StringReader JavaDoc(content));
76         IDataSetProducer producer = new XmlProducer(source);
77         producer.setConsumer(consumer);
78
79         // Produce and verify consumer
80
producer.produce();
81         consumer.verify();
82     }
83
84     public void testProduceNullValue() throws Exception JavaDoc
85     {
86         String JavaDoc 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 JavaDoc[] expectedValues = new Object JavaDoc[]{null, "", "value"};
93
94         // Setup consumer
95
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         // Setup producer
103
String JavaDoc 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 JavaDoc source = new InputSource JavaDoc(new StringReader JavaDoc(content));
118         IDataSetProducer producer = new XmlProducer(source);
119         producer.setConsumer(consumer);
120
121         // Produce and verify consumer
122
producer.produce();
123         consumer.verify();
124     }
125
126     public void testProduceMissingColumn() throws Exception JavaDoc
127     {
128         String JavaDoc 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 JavaDoc[] expectedValues = new Object JavaDoc[]{null, "", "value", "extra"};
135
136         // Setup consumer
137
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         // Setup producer
145
String JavaDoc 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 JavaDoc source = new InputSource JavaDoc(new StringReader JavaDoc(content));
161         IDataSetProducer producer = new XmlProducer(source);
162         producer.setConsumer(consumer);
163
164         // Produce and verify consumer
165
producer.produce();
166         consumer.verify();
167     }
168
169     public void testProduceNotWellFormedXml() throws Exception JavaDoc
170     {
171         // Setup consumer
172
MockDataSetConsumer consumer = new MockDataSetConsumer();
173         consumer.addExpectedStartDataSet();
174
175         // Setup producer
176
String JavaDoc content =
177                 "<?xml version=\"1.0\"?>" +
178                 "<dataset>";
179         InputSource JavaDoc source = new InputSource JavaDoc(new StringReader JavaDoc(content));
180         IDataSetProducer producer = new XmlProducer(source);
181         producer.setConsumer(consumer);
182
183         // Produce and verify consumer
184
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 JavaDoc
197     {
198         // Setup consumer
199
MockDataSetConsumer consumer = new MockDataSetConsumer();
200
201         // Setup producer
202
String JavaDoc content =
203                 "<?xml version=\"1.0\"?>" +
204                 "<!DOCTYPE dataset SYSTEM \"dataset.dtd\" >" +
205                 "<invalid/>";
206         InputSource JavaDoc source = new InputSource JavaDoc(new StringReader JavaDoc(content));
207         source.setSystemId("http:/nowhere.to.go");
208         XmlProducer producer = new XmlProducer(source);
209         producer.setValidating(true);
210         producer.setConsumer(consumer);
211
212         // Produce and verify consumer
213
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