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  
22  package org.dbunit.dataset.xml;
23  
24  import java.io.StringWriter;
25  
26  import junit.framework.TestCase;
27  
28  import org.dbunit.dataset.Column;
29  import org.dbunit.dataset.DataSetException;
30  import org.dbunit.dataset.DefaultDataSet;
31  import org.dbunit.dataset.DefaultTable;
32  import org.dbunit.dataset.IDataSet;
33  import org.dbunit.dataset.datatype.DataType;
34  
35  /**
36   *
37   * @author Manuel Laflamme
38   * @version $Revision$
39   * @since Sep 8, 2003$
40   */
41  public class FlatXmlWriterTest extends TestCase
42  {
43      public FlatXmlWriterTest(String name)
44      {
45          super(name);
46      }
47  
48      public void testWrite() throws Exception
49      {
50          String expectedOutput =
51                  "<dataset>\n" +
52                  "  <TABLE1 COL0=\"t1v1\" COL1=\"t1v2\"/>\n" +
53                  "  <TABLE2 COL0=\"t2v1\" COL1=\"t2v2\"/>\n" +
54                  "</dataset>\n";
55  
56          IDataSet dataSet = XmlDataSetWriterTest.getDefaultDataSet();
57          
58          StringWriter stringWriter = new StringWriter();
59          FlatXmlWriter xmlWriter = new FlatXmlWriter(stringWriter);
60          xmlWriter.write(dataSet);
61  
62          String actualOutput = stringWriter.toString();
63          assertEquals("output", expectedOutput, actualOutput);
64      }
65  
66      public void testWriteWithDocType() throws Exception
67      {
68          String expectedOutput =
69                  "<!DOCTYPE dataset SYSTEM \"dataset.dtd\">\n" +
70                  "<dataset>\n" +
71                  "  <TABLE1 COL0=\"t1v1\" COL1=\"t1v2\"/>\n" +
72                  "</dataset>\n";
73  
74          IDataSet dataSet = XmlDataSetWriterTest.getMinimalDataSet();
75  
76          StringWriter stringWriter = new StringWriter();
77          FlatXmlWriter xmlWriter = new FlatXmlWriter(stringWriter);
78          xmlWriter.setDocType("dataset.dtd");
79          xmlWriter.write(dataSet);
80  
81          String actualOutput = stringWriter.toString();
82          assertEquals("output", expectedOutput, actualOutput);
83      }
84  
85      public void testWriteExcludeEmptyTable() throws Exception
86      {
87          String expectedOutput =
88                  "<dataset>\n" +
89                  "  <TEST_TABLE COL0=\"value\"/>\n" +
90                  "</dataset>\n";
91  
92          IDataSet dataSet = getEmptyTableDataSet();
93          
94          StringWriter stringWriter = new StringWriter();
95          FlatXmlWriter datasetWriter = new FlatXmlWriter(stringWriter);
96          datasetWriter.setIncludeEmptyTable(false);
97          datasetWriter.write(dataSet);
98  
99          String actualOutput = stringWriter.toString();
100         assertEquals("output", expectedOutput, actualOutput);
101     }
102                
103     public void testWriteIncludeEmptyTable() throws Exception
104     {
105         String expectedOutput =
106                 "<dataset>\n" +
107                 "  <TEST_TABLE COL0=\"value\"/>\n" +
108                 "  <EMPTY_TABLE/>\n" +
109                 "</dataset>\n";
110 
111         IDataSet dataSet = getEmptyTableDataSet();
112 
113         StringWriter stringWriter = new StringWriter();
114         FlatXmlWriter datasetWriter = new FlatXmlWriter(stringWriter);
115         datasetWriter.setIncludeEmptyTable(true);
116         datasetWriter.write(dataSet);
117 
118         String actualOutput = stringWriter.toString();
119         assertEquals("output", expectedOutput, actualOutput);
120     }
121 
122     public void testWriteNullValue() throws Exception
123     {
124         String expectedOutput =
125                 "<dataset>\n" +
126                 "  <TEST_TABLE COL0=\"c0r0\" COL1=\"c1r0\"/>\n" +
127                 "  <TEST_TABLE COL0=\"c0r1\"/>\n" +
128                 "</dataset>\n";
129 
130         String col0 = "COL0";
131         String col1 = "COL1";
132         Column[] columns = new Column[]{
133             new Column(col0, DataType.UNKNOWN),
134             new Column(col1, DataType.UNKNOWN)
135         };
136 
137         DefaultTable table = new DefaultTable("TEST_TABLE", columns);
138         table.addRow();
139         table.setValue(0, col0, "c0r0");
140         table.setValue(0, col1, "c1r0");
141         table.addRow();
142         table.setValue(1, col0, "c0r1");
143         table.setValue(1, col1, null);
144 
145         StringWriter stringWriter = new StringWriter();
146         FlatXmlWriter xmlWriter = new FlatXmlWriter(stringWriter);
147         xmlWriter.write(new DefaultDataSet(table));
148 
149         String actualOutput = stringWriter.toString();
150         assertEquals("output", expectedOutput, actualOutput);
151     }
152     
153     public void testWritePrettyPrintDisabled() throws Exception
154     {
155         String expectedOutput =
156                 "<dataset>" +
157                 "<TABLE1 COL0=\"t1v1\" COL1=\"t1v2\"/>" +
158                 "</dataset>";
159 
160         IDataSet dataSet = XmlDataSetWriterTest.getMinimalDataSet();
161 
162         StringWriter stringWriter = new StringWriter();
163         FlatXmlWriter xmlWriter = new FlatXmlWriter(stringWriter);
164         xmlWriter.setPrettyPrint(false);
165         xmlWriter.write(dataSet);
166 
167         String actualOutput = stringWriter.toString();
168         assertEquals("output", expectedOutput, actualOutput);
169     }
170 
171     
172     public static IDataSet getEmptyTableDataSet() throws DataSetException 
173     {
174         String col0 = "COL0";
175         Column[] columns = new Column[]{
176             new Column(col0, DataType.UNKNOWN),
177         };
178 
179         DefaultTable table1 = new DefaultTable("TEST_TABLE", columns);
180         table1.addRow();
181         table1.setValue(0, col0, "value");
182         DefaultTable table2 = new DefaultTable("EMPTY_TABLE", columns);
183         IDataSet dataSet = new DefaultDataSet(table1, table2);
184         return dataSet;
185     }
186 
187 }