KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.TestCase;
25 import org.dbunit.dataset.Column;
26 import org.dbunit.dataset.DefaultDataSet;
27 import org.dbunit.dataset.DefaultTable;
28 import org.dbunit.dataset.datatype.DataType;
29 import org.dbunit.database.IDatabaseConnection;
30
31 import java.io.StringWriter JavaDoc;
32 import java.io.File JavaDoc;
33 import java.io.FileWriter JavaDoc;
34 import java.io.FileOutputStream JavaDoc;
35
36 /**
37  *
38  * @author Manuel Laflamme
39  * @version $Revision: 1.6 $
40  * @since Sep 8, 2003$
41  */

42 public class FlatXmlWriterTest extends TestCase
43 {
44     public FlatXmlWriterTest(String JavaDoc name)
45     {
46         super(name);
47     }
48
49     public void testWrite() throws Exception JavaDoc
50     {
51         String JavaDoc expectedOutput =
52                 "<dataset>\n" +
53                 " <TABLE1 COL0=\"t1v1\" COL1=\"t1v2\"/>\n" +
54                 " <TABLE2 COL0=\"t2v1\" COL1=\"t2v2\"/>\n" +
55                 "</dataset>\n";
56
57         String JavaDoc col0 = "COL0";
58         String JavaDoc col1 = "COL1";
59         Column[] columns = new Column[]{
60             new Column(col0, DataType.UNKNOWN),
61             new Column(col1, DataType.UNKNOWN)
62         };
63
64         DefaultTable table1 = new DefaultTable("TABLE1", columns);
65         table1.addRow();
66         table1.setValue(0, col0, "t1v1");
67         table1.setValue(0, col1, "t1v2");
68
69         DefaultTable table2 = new DefaultTable("TABLE2", columns);
70         table2.addRow();
71         table2.setValue(0, col0, "t2v1");
72         table2.setValue(0, col1, "t2v2");
73
74         StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
75         FlatXmlWriter xmlWriter = new FlatXmlWriter(stringWriter);
76         xmlWriter.write(new DefaultDataSet(table1, table2));
77
78         String JavaDoc actualOutput = stringWriter.toString();
79         assertEquals("output", expectedOutput, actualOutput);
80     }
81
82     public void testWriteWithDocType() throws Exception JavaDoc
83     {
84         String JavaDoc expectedOutput =
85                 "<!DOCTYPE dataset SYSTEM \"dataset.dtd\">\n" +
86                 "<dataset>\n" +
87                 " <TABLE1 COL0=\"v1\" COL1=\"v2\"/>\n" +
88                 "</dataset>\n";
89
90         String JavaDoc col0 = "COL0";
91         String JavaDoc col1 = "COL1";
92         Column[] columns = new Column[]{
93             new Column(col0, DataType.UNKNOWN),
94             new Column(col1, DataType.UNKNOWN)
95         };
96
97         DefaultTable table1 = new DefaultTable("TABLE1", columns);
98         table1.addRow();
99         table1.setValue(0, col0, "v1");
100         table1.setValue(0, col1, "v2");
101
102         StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
103         FlatXmlWriter xmlWriter = new FlatXmlWriter(stringWriter);
104         xmlWriter.setDocType("dataset.dtd");
105         xmlWriter.write(new DefaultDataSet(table1));
106
107         String JavaDoc actualOutput = stringWriter.toString();
108         assertEquals("output", expectedOutput, actualOutput);
109     }
110
111     public void testWriteExcludeEmptyTable() throws Exception JavaDoc
112     {
113         String JavaDoc expectedOutput =
114                 "<dataset>\n" +
115                 " <TEST_TABLE COL0=\"value\"/>\n" +
116                 "</dataset>\n";
117
118         String JavaDoc col0 = "COL0";
119         Column[] columns = new Column[]{
120             new Column(col0, DataType.UNKNOWN),
121         };
122
123         DefaultTable table1 = new DefaultTable("TEST_TABLE", columns);
124         table1.addRow();
125         table1.setValue(0, col0, "value");
126         DefaultTable table2 = new DefaultTable("EMPTY_TABLE", columns);
127
128         StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
129         FlatXmlWriter datasetWriter = new FlatXmlWriter(stringWriter);
130         datasetWriter.setIncludeEmptyTable(false);
131         datasetWriter.write(new DefaultDataSet(table1, table2));
132
133         String JavaDoc actualOutput = stringWriter.toString();
134         assertEquals("output", expectedOutput, actualOutput);
135     }
136                
137     public void testWriteIncludeEmptyTable() throws Exception JavaDoc
138     {
139         String JavaDoc expectedOutput =
140                 "<dataset>\n" +
141                 " <TEST_TABLE COL0=\"value\"/>\n" +
142                 " <EMPTY_TABLE/>\n" +
143                 "</dataset>\n";
144
145         String JavaDoc col0 = "COL0";
146         Column[] columns = new Column[]{
147             new Column(col0, DataType.UNKNOWN),
148         };
149
150         DefaultTable table1 = new DefaultTable("TEST_TABLE", columns);
151         table1.addRow();
152         table1.setValue(0, col0, "value");
153         DefaultTable table2 = new DefaultTable("EMPTY_TABLE", columns);
154
155         StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
156         FlatXmlWriter datasetWriter = new FlatXmlWriter(stringWriter);
157         datasetWriter.setIncludeEmptyTable(true);
158         datasetWriter.write(new DefaultDataSet(table1, table2));
159
160         String JavaDoc actualOutput = stringWriter.toString();
161         assertEquals("output", expectedOutput, actualOutput);
162     }
163
164     public void testWriteNullValue() throws Exception JavaDoc
165     {
166         String JavaDoc expectedOutput =
167                 "<dataset>\n" +
168                 " <TEST_TABLE COL0=\"c0r0\" COL1=\"c1r0\"/>\n" +
169                 " <TEST_TABLE COL0=\"c0r1\"/>\n" +
170                 "</dataset>\n";
171
172         String JavaDoc col0 = "COL0";
173         String JavaDoc col1 = "COL1";
174         Column[] columns = new Column[]{
175             new Column(col0, DataType.UNKNOWN),
176             new Column(col1, DataType.UNKNOWN)
177         };
178
179         DefaultTable table = new DefaultTable("TEST_TABLE", columns);
180         table.addRow();
181         table.setValue(0, col0, "c0r0");
182         table.setValue(0, col1, "c1r0");
183         table.addRow();
184         table.setValue(1, col0, "c0r1");
185         table.setValue(1, col1, null);
186
187         StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
188         FlatXmlWriter xmlWriter = new FlatXmlWriter(stringWriter);
189         xmlWriter.write(new DefaultDataSet(table));
190
191         String JavaDoc actualOutput = stringWriter.toString();
192         assertEquals("output", expectedOutput, actualOutput);
193     }
194 }
195
Popular Tags