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.stream.AbstractProducerTest;
26  import org.dbunit.dataset.stream.IDataSetProducer;
27  import org.dbunit.dataset.stream.MockDataSetConsumer;
28  import org.dbunit.testutil.TestUtils;
29  import org.xml.sax.InputSource;
30  
31  import java.io.File;
32  import java.io.FileInputStream;
33  import java.io.StringReader;
34  
35  import junitx.framework.StringAssert;
36  
37  /**
38   * @author Manuel Laflamme
39   * @since Apr 29, 2003
40   * @version $Revision$
41   */
42  public class FlatDtdProducerTest extends AbstractProducerTest
43  {
44      private static final File DTD_FILE =
45              TestUtils.getFile("dtd/flatDtdProducerTest.dtd");
46  
47      public FlatDtdProducerTest(String s)
48      {
49          super(s);
50      }
51  
52      protected IDataSetProducer createProducer() throws Exception
53      {
54          InputSource source = new InputSource(new FileInputStream(DTD_FILE));
55          return new FlatDtdProducer(source);
56      }
57  
58      protected int[] getExpectedRowCount() throws Exception
59      {
60          return new int[] {0, 0, 0, 0, 0, 0};
61      }
62  
63      public void testSequenceModel() throws Exception
64      {
65          // Setup consumer
66          MockDataSetConsumer consumer = new MockDataSetConsumer();
67          consumer.addExpectedStartDataSet();
68          consumer.addExpectedEmptyTableIgnoreColumns("DUPLICATE_TABLE");
69          consumer.addExpectedEmptyTableIgnoreColumns("TEST_TABLE");
70          consumer.addExpectedEmptyTableIgnoreColumns("DUPLICATE_TABLE");
71          consumer.addExpectedEndDataSet();
72  
73          // Setup producer
74          String content =
75                  "<!ELEMENT dataset (DUPLICATE_TABLE*,TEST_TABLE+,DUPLICATE_TABLE?)>" +
76                  "<!ELEMENT TEST_TABLE EMPTY>" +
77                  "<!ELEMENT DUPLICATE_TABLE EMPTY>";
78          InputSource source = new InputSource(new StringReader(content));
79          FlatDtdProducer producer = new FlatDtdProducer(source);
80          producer.setConsumer(consumer);
81  
82          // Produce and verify consumer
83          producer.produce();
84          consumer.verify();
85      }
86  
87      public void testChoicesModel() throws Exception
88      {
89          // Setup consumer
90          MockDataSetConsumer consumer = new MockDataSetConsumer();
91          consumer.addExpectedStartDataSet();
92          consumer.addExpectedEmptyTableIgnoreColumns("TEST_TABLE");
93          consumer.addExpectedEmptyTableIgnoreColumns("SECOND_TABLE");
94          consumer.addExpectedEndDataSet();
95  
96          // Setup producer
97          String content =
98                  "<!ELEMENT dataset (TEST_TABLE|SECOND_TABLE)>" +
99                  "<!ELEMENT TEST_TABLE EMPTY>" +
100                 "<!ELEMENT SECOND_TABLE EMPTY>";
101         InputSource source = new InputSource(new StringReader(content));
102         FlatDtdProducer producer = new FlatDtdProducer(source);
103         producer.setConsumer(consumer);
104 
105         // Produce and verify consumer
106         producer.produce();
107         consumer.verify();
108     }
109 
110     public void testChoicesModel_ElementDeclarationForTableMissing() throws Exception
111     {
112         // Setup consumer
113         MockDataSetConsumer consumer = new MockDataSetConsumer();
114 //        consumer.addExpectedStartDataSet();
115 //        consumer.addExpectedEmptyTableIgnoreColumns("TEST_TABLE");
116 //        consumer.addExpectedEmptyTableIgnoreColumns("SECOND_TABLE");
117 //        consumer.addExpectedEndDataSet();
118 
119         // Setup producer
120         String dtdChoice = "<!ELEMENT dataset ( (TEST_TABLE|SECOND_TABLE)* )><!ELEMENT TEST_TABLE EMPTY>";
121         InputSource source = new InputSource(new StringReader(dtdChoice));
122         FlatDtdProducer producer = new FlatDtdProducer(source);
123         producer.setConsumer(consumer);
124 
125         // Produce and verify consumer
126         try
127         {
128             producer.produce();
129             fail("Should not be able to produce the dataset from an incomplete DTD");
130         }
131         catch(DataSetException expected)
132         {
133             String expectedStartsWith = "ELEMENT/ATTRIBUTE declaration for '" + "SECOND_TABLE" + "' is missing. ";
134             StringAssert.assertStartsWith(expectedStartsWith, expected.getMessage());
135         }
136 //        consumer.verify();
137     }
138 
139     
140     public void testAttrListBeforeParentElement() throws Exception
141     {
142         // Setup consumer
143         MockDataSetConsumer consumer = new MockDataSetConsumer();
144         consumer.addExpectedStartDataSet();
145         Column[] expectedColumns = createExpectedColumns(Column.NULLABLE);
146         consumer.addExpectedEmptyTable("TEST_TABLE", expectedColumns);
147         consumer.addExpectedEndDataSet();
148 
149         // Setup producer
150         String content =
151                 "<!ELEMENT dataset (TEST_TABLE)>" +
152                 "<!ATTLIST TEST_TABLE " +
153                     "COLUMN0 CDATA #IMPLIED " +
154                     "COLUMN1 CDATA #IMPLIED " +
155                     "COLUMN2 CDATA #IMPLIED " +
156                     "COLUMN3 CDATA #IMPLIED>" +
157                 "<!ELEMENT TEST_TABLE EMPTY>";
158 
159         InputSource source = new InputSource(new StringReader(content));
160         FlatDtdProducer producer = new FlatDtdProducer(source);
161         producer.setConsumer(consumer);
162 
163         // Produce and verify consumer
164         producer.produce();
165         consumer.verify();
166     }
167     
168     public void testCleanupTableName() throws Exception {
169         // Setup consumer
170         MockDataSetConsumer consumer = new MockDataSetConsumer();
171         consumer.addExpectedStartDataSet();
172         consumer.addExpectedEmptyTableIgnoreColumns("TABLE_1");
173         consumer.addExpectedEmptyTableIgnoreColumns("TABLE_2");
174         consumer.addExpectedEmptyTableIgnoreColumns("TABLE_3");
175         consumer.addExpectedEndDataSet();
176         
177         // Setup producer
178         String content =
179                 "<!ELEMENT dataset (TABLE_1,(TABLE_2,TABLE_3+)?)+>" +
180                 "<!ELEMENT TABLE_1 EMPTY>" +
181                 "<!ELEMENT TABLE_2 EMPTY>" +
182                 "<!ELEMENT TABLE_3 EMPTY>";
183 
184         InputSource source = new InputSource(new StringReader(content));
185         FlatDtdProducer producer = new FlatDtdProducer(source);
186         producer.setConsumer(consumer);
187 
188         // Produce and verify consumer
189         producer.produce();
190         consumer.verify();
191     }
192     
193     public void testANYModel() throws Exception
194     {
195         // Setup consumer
196         FlatDtdDataSet consumer = new FlatDtdDataSet();
197 
198         // Setup producer
199         String content =
200             "<!ELEMENT dataset ANY>" +
201             "<!ELEMENT TEST_TABLE EMPTY>" +
202             "<!ELEMENT SECOND_TABLE EMPTY>";
203         InputSource source = new InputSource(new StringReader(content));
204         FlatDtdProducer producer = new FlatDtdProducer(source);
205         producer.setConsumer(consumer);
206 
207         // Produce and verify consumer
208         producer.produce();
209         assertEquals(2, consumer.getTables().length);
210         assertNotNull(consumer.getTable("TEST_TABLE"));
211         assertNotNull(consumer.getTable("SECOND_TABLE"));
212     } 
213     
214 }