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