KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.dbunit.DatabaseEnvironment;
25 import org.dbunit.database.IDatabaseConnection;
26 import org.dbunit.dataset.*;
27 import org.dbunit.util.FileAsserts;
28
29 import java.io.*;
30
31 /**
32  * @author Manuel Laflamme
33  * @version $Revision: 1.12 $
34  * @since Apr 4, 2002
35  */

36 public class FlatDtdDataSetTest extends AbstractDataSetTest
37 {
38     private static final File DTD_FILE =
39             new File("src/dtd/flatDtdDataSetTest.dtd");
40     private static final File DUPLICATE_FILE =
41             new File("src/dtd/flatDtdDataSetDuplicateTest.dtd");
42
43     public FlatDtdDataSetTest(String JavaDoc s)
44     {
45         super(s);
46     }
47
48     ////////////////////////////////////////////////////////////////////////////
49
// AbstractDataSetTest class
50

51     protected IDataSet createDataSet() throws Exception JavaDoc
52     {
53         return new FlatDtdDataSet(new FileReader(DTD_FILE));
54     }
55
56     protected IDataSet createDuplicateDataSet() throws Exception JavaDoc
57     {
58         return new FlatDtdDataSet(new FileReader(DUPLICATE_FILE));
59     }
60
61     protected int[] getExpectedDuplicateRows()
62     {
63         return new int[] {0, 0, 0};
64     }
65
66     ////////////////////////////////////////////////////////////////////////////
67
// Test methods
68

69     public void testGetDuplicateTable() throws Exception JavaDoc
70     {
71         String JavaDoc expectedTableName = getDuplicateTableName();
72
73         IDataSet dataSet = createDuplicateDataSet();
74         ITable table = dataSet.getTable(expectedTableName);
75         String JavaDoc actualTableName = table.getTableMetaData().getTableName();
76         assertEquals("table name", expectedTableName, actualTableName);
77     }
78
79     public void testGetDuplicateTableMetaData() throws Exception JavaDoc
80     {
81         String JavaDoc expectedTableName = getDuplicateTableName();
82
83         IDataSet dataSet = createDuplicateDataSet();
84         ITableMetaData metaData = dataSet.getTableMetaData(expectedTableName);
85         String JavaDoc actualTableName = metaData.getTableName();
86         assertEquals("table name", expectedTableName, actualTableName);
87     }
88
89     public void testWriteFromDtd() throws Exception JavaDoc
90     {
91         IDataSet dataSet = new FlatDtdDataSet(new FileReader(DTD_FILE));
92
93         File tempFile = File.createTempFile("flatXmlDocType", ".dtd");
94
95         try
96         {
97             Writer out = new FileWriter(tempFile);
98
99             try
100             {
101                 // write DTD in temp file
102
FlatDtdDataSet.write(dataSet, out);
103             }
104             finally
105             {
106                 out.close();
107             }
108
109             FileAsserts.assertEquals(
110                     new BufferedReader(new FileReader(DTD_FILE)),
111                     new BufferedReader(new FileReader(tempFile)));
112         }
113         finally
114         {
115             tempFile.delete();
116         }
117
118     }
119
120     public void testWriteFromDatabase() throws Exception JavaDoc
121     {
122         IDatabaseConnection connection =
123                 DatabaseEnvironment.getInstance().getConnection();
124         IDataSet dataSet = connection.createDataSet();
125
126         File tempFile = File.createTempFile("flatXmlDocType", ".dtd");
127
128         try
129         {
130             Writer out = new FileWriter(tempFile);
131
132             try
133             {
134                 // write DTD in temp file
135
String JavaDoc[] tableNames = getExpectedNames();
136                 FlatDtdDataSet.write(new FilteredDataSet(
137                         tableNames, dataSet), out);
138             }
139             finally
140             {
141                 out.close();
142             }
143
144             FileAsserts.assertEquals(
145                     new BufferedReader(new FileReader(DTD_FILE)),
146                     new BufferedReader(new FileReader(tempFile)));
147         }
148         finally
149         {
150             tempFile.delete();
151         }
152     }
153
154
155 }
156
157
158
159
160
161
Popular Tags