View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2005-2006, 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.DefaultDataSet;
30  import org.dbunit.dataset.DefaultTable;
31  import org.dbunit.dataset.IDataSet;
32  import org.dbunit.dataset.datatype.DataType;
33  
34  /**
35   *
36   * @author Felipe Leme
37   * @version $Id$
38   * @since Sep 9, 2005$
39   */
40  public class XmlDataSetWriterTest extends TestCase
41  {
42      public XmlDataSetWriterTest(String name)
43      {
44          super(name);
45      }
46      
47      public static DefaultDataSet getDefaultDataSet() throws Exception {
48  
49          String col0 = "COL0";
50          String col1 = "COL1";
51          Column[] columns = new Column[]{
52              new Column(col0, DataType.UNKNOWN),
53              new Column(col1, DataType.UNKNOWN)
54          };
55  
56          DefaultTable table1 = new DefaultTable("TABLE1", columns);
57          table1.addRow();
58          table1.setValue(0, col0, "t1v1");
59          table1.setValue(0, col1, "t1v2");
60  
61          DefaultTable table2 = new DefaultTable("TABLE2", columns);
62          table2.addRow();
63          table2.setValue(0, col0, "t2v1");
64          table2.setValue(0, col1, "t2v2");
65  
66          DefaultDataSet dataSet = new DefaultDataSet(table1, table2);
67          return dataSet;
68       }
69  
70      public static DefaultDataSet getMinimalDataSet() throws Exception {
71  
72          String col0 = "COL0";
73          String col1 = "COL1";
74          Column[] columns = new Column[]{
75              new Column(col0, DataType.UNKNOWN),
76              new Column(col1, DataType.UNKNOWN)
77          };
78  
79          DefaultTable table1 = new DefaultTable("TABLE1", columns);
80          table1.addRow();
81          table1.setValue(0, col0, "t1v1");
82          table1.setValue(0, col1, "t1v2");
83  
84          DefaultDataSet dataSet = new DefaultDataSet(table1);
85          return dataSet;
86       }
87  
88      
89      public void testWrite() throws Exception
90      {
91          String expectedOutput =
92                  "<dataset>\n" +
93                  "  <table name=\"TABLE1\">\n" +
94                  "    <column>COL0</column>\n" +
95                  "    <column>COL1</column>\n" +
96                  "    <row>\n" +
97                  "      <value>t1v1</value>\n" +
98                  "      <value>t1v2</value>\n" +
99                  "    </row>\n" +
100                 "  </table>\n" +
101                 "  <table name=\"TABLE2\">\n" +
102                 "    <column>COL0</column>\n" +
103                 "    <column>COL1</column>\n" +
104                 "    <row>\n" +
105                 "      <value>t2v1</value>\n" +
106                 "      <value>t2v2</value>\n" +
107                 "    </row>\n" +
108                 "  </table>\n" +
109                 "</dataset>\n";
110         
111         StringWriter stringWriter = new StringWriter();
112         XmlDataSetWriter xmlWriter = new XmlDataSetWriter(stringWriter);
113         IDataSet dataSet = getDefaultDataSet();
114         xmlWriter.write( dataSet );
115 
116         String actualOutput = stringWriter.toString();
117         assertEquals("output", expectedOutput, actualOutput);
118     }
119     
120     public void testWriteWithComments() throws Exception
121     {
122         String expectedOutput =
123                 "<dataset>\n" +
124                 "  <table name=\"TABLE1\">\n" +
125                 "    <column>COL0</column>\n" +
126                 "    <column>COL1</column>\n" +
127                 "    <row>\n" +
128                 "      <value>t1v1</value>\n" +
129                 "      <!-- COL0 -->\n" +
130                 "      <value>t1v2</value>\n" +
131                 "      <!-- COL1 -->\n" +
132                 "    </row>\n" +
133                 "  </table>\n" +
134                 "  <table name=\"TABLE2\">\n" +
135                 "    <column>COL0</column>\n" +
136                 "    <column>COL1</column>\n" +
137                 "    <row>\n" +
138                 "      <value>t2v1</value>\n" +
139                 "      <!-- COL0 -->\n" +
140                 "      <value>t2v2</value>\n" +
141                 "      <!-- COL1 -->\n" +
142                 "    </row>\n" +
143                 "  </table>\n" +
144                 "</dataset>\n";
145         StringWriter stringWriter = new StringWriter();
146         XmlDataSetWriter xmlWriter = new XmlDataSetWriter(stringWriter);
147         xmlWriter.setIncludeColumnComments(true);
148         IDataSet dataSet = getDefaultDataSet();
149         xmlWriter.write( dataSet );
150 
151         String actualOutput = stringWriter.toString();
152         assertEquals("output", expectedOutput, actualOutput);
153     }
154     
155     public void testWriteWithCData() throws Exception
156     {
157         // Setup
158         Column[] columns = new Column[]{
159                 new Column("COL1", DataType.UNKNOWN)
160         };
161         DefaultTable table = new DefaultTable("TABLE1", columns);
162         table.addRow(new Object[]{"<myXmlData><![CDATA[Data that itself is in a CDATA section]]></myXmlData>"});
163         DefaultDataSet dataSet = new DefaultDataSet(table);
164         
165         // Write the XML
166         StringWriter stringWriter = new StringWriter();
167         XmlDataSetWriter xmlWriter = new XmlDataSetWriter(stringWriter);
168         xmlWriter.write(dataSet);
169         
170         String actualOutput = stringWriter.toString();
171         String expectedOutput = "<dataset>\n"+
172                                     "  <table name=\"TABLE1\">\n" +
173                                     "    <column>COL1</column>\n"+
174                                     "    <row>\n"+
175                                     "      <value><![CDATA[<myXmlData><![CDATA[Data that itself is in a CDATA section]]]]><![CDATA[></myXmlData>]]></value>\n"+
176                                     "    </row>\n"+
177                                     "  </table>\n"+
178                                 "</dataset>\n";
179         assertEquals("output", expectedOutput, actualOutput);
180         
181     }
182     
183     public void testWritePrettyPrintDisabled() throws Exception
184     {
185         String expectedOutput =
186                 "<dataset>" +
187                 "<table name=\"TABLE1\">" +
188                 "<column>COL0</column>" +
189                 "<column>COL1</column>" +
190                 "<row>" +
191                 "<value>t1v1</value>" +
192                 "<value>t1v2</value>" +
193                 "</row>" +
194                 "</table>" +
195                 "</dataset>";
196         
197         StringWriter stringWriter = new StringWriter();
198         XmlDataSetWriter xmlWriter = new XmlDataSetWriter(stringWriter);
199         xmlWriter.setPrettyPrint(false);
200         IDataSet dataSet = getMinimalDataSet();
201         xmlWriter.write( dataSet );
202 
203         String actualOutput = stringWriter.toString();
204         assertEquals("output", expectedOutput, actualOutput);
205     }
206 
207 }