KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > DataSetUtilsTest


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;
23
24 import junit.framework.TestCase;
25 import org.dbunit.dataset.datatype.DataType;
26
27 import java.sql.Time JavaDoc;
28 import java.sql.Timestamp JavaDoc;
29
30 /**
31  * @author Manuel Laflamme
32  * @version $Revision: 1.17 $
33  * @since Feb 19, 2002
34  */

35 public class DataSetUtilsTest extends TestCase
36 {
37     public DataSetUtilsTest(String JavaDoc s)
38     {
39         super(s);
40     }
41
42
43     public void testGetQualifiedName() throws Exception JavaDoc
44     {
45         assertEquals("prefix + name", "prefix.name",
46                 DataSetUtils.getQualifiedName("prefix", "name"));
47
48         assertEquals("null prefix", "name",
49                 DataSetUtils.getQualifiedName(null, "name"));
50
51         assertEquals("empty prefix", "name",
52                 DataSetUtils.getQualifiedName("", "name"));
53
54         assertEquals("existing prefix", "prefix.name",
55                 DataSetUtils.getQualifiedName("wrongPrefix", "prefix.name"));
56
57         assertEquals("escaped prefix + name", "prefix.name",
58                 DataSetUtils.getQualifiedName("prefix", "name"));
59
60         assertEquals("escaped prefix + name", "[prefix].[name]",
61                 DataSetUtils.getQualifiedName("prefix", "name", "[?]"));
62
63     }
64
65     public void testGetEscapedName() throws Exception JavaDoc
66     {
67         assertEquals("'name'", DataSetUtils.getEscapedName("name", "'?'"));
68
69         assertEquals("[name]", DataSetUtils.getEscapedName("name", "[?]"));
70
71         assertEquals(null, DataSetUtils.getEscapedName(null, "[?]"));
72
73         assertEquals("name", DataSetUtils.getEscapedName("name", null));
74
75         assertEquals("name", DataSetUtils.getEscapedName("name", "invalid pattern!"));
76     }
77
78     public void testGetColumn() throws Exception JavaDoc
79     {
80         Column[] columns = new Column[]{
81             new Column("c0", DataType.UNKNOWN),
82             new Column("c1", DataType.UNKNOWN),
83             new Column("c2", DataType.UNKNOWN),
84             new Column("c3", DataType.UNKNOWN),
85             new Column("c4", DataType.UNKNOWN),
86         };
87
88         for (int i = 0; i < columns.length; i++)
89         {
90             assertEquals("find column same", columns[i],
91                     DataSetUtils.getColumn("c" + i, columns));
92         }
93     }
94
95     public void testGetColumnCaseInsensitive() throws Exception JavaDoc
96     {
97         Column[] columns = new Column[]{
98             new Column("c0", DataType.UNKNOWN),
99             new Column("C1", DataType.UNKNOWN),
100             new Column("c2", DataType.UNKNOWN),
101             new Column("C3", DataType.UNKNOWN),
102             new Column("c4", DataType.UNKNOWN),
103         };
104
105         for (int i = 0; i < columns.length; i++)
106         {
107             assertEquals("find column same", columns[i],
108                     DataSetUtils.getColumn("c" + i, columns));
109         }
110     }
111
112     public void testGetTables() throws Exception JavaDoc
113     {
114         String JavaDoc[] expected = {"t0", "t1", "t2", "t3"};
115         ITable[] testTables = new ITable[]{
116             new DefaultTable("t0"),
117             new DefaultTable("t1"),
118             new DefaultTable("t2"),
119             new DefaultTable("t3"),
120         };
121
122         ITable[] tables = DataSetUtils.getTables(new DefaultDataSet(testTables));
123         assertEquals("table count", expected.length, tables.length);
124         for (int i = 0; i < tables.length; i++)
125         {
126             String JavaDoc name = tables[i].getTableMetaData().getTableName();
127             assertEquals("table name", expected[i], name);
128         }
129     }
130
131     public void testGetTablesByNames() throws Exception JavaDoc
132     {
133         String JavaDoc[] expected = {"t0", "t2"};
134         ITable[] testTables = new ITable[]{
135             new DefaultTable("t0"),
136             new DefaultTable("t1"),
137             new DefaultTable("t2"),
138             new DefaultTable("t3"),
139         };
140
141         ITable[] tables = DataSetUtils.getTables(expected,
142                 new DefaultDataSet(testTables));
143         assertEquals("table count", expected.length, tables.length);
144         for (int i = 0; i < tables.length; i++)
145         {
146             String JavaDoc name = tables[i].getTableMetaData().getTableName();
147             assertEquals("table name", expected[i], name);
148         }
149     }
150
151     public void testGetReserseNames() throws Exception JavaDoc
152     {
153         String JavaDoc[] expected = {"t3", "t2", "t1", "t0"};
154         ITable[] testTables = new ITable[]{
155             new DefaultTable("t0"),
156             new DefaultTable("t1"),
157             new DefaultTable("t2"),
158             new DefaultTable("t3"),
159         };
160
161         String JavaDoc[] names = DataSetUtils.getReverseTableNames(new DefaultDataSet(testTables));
162         assertEquals("table count", expected.length, names.length);
163         for (int i = 0; i < names.length; i++)
164         {
165             assertEquals("table name", expected[i], names[i]);
166         }
167     }
168
169     public void testGetSqlValueString() throws Exception JavaDoc
170     {
171         ValueStringData[] values = new ValueStringData[]{
172             new ValueStringData(null, DataType.REAL, "NULL"),
173             new ValueStringData("1234", DataType.NUMERIC, "1234"),
174             new ValueStringData("1234", DataType.VARCHAR, "'1234'"),
175             new ValueStringData(new Float JavaDoc(1234.45), DataType.REAL, "1234.45"),
176             new ValueStringData(new java.sql.Date JavaDoc(0L), DataType.DATE,
177                     "{d '" + new java.sql.Date JavaDoc(0L).toString() + "'}"),
178             new ValueStringData(new Time JavaDoc(0L), DataType.TIME,
179                     "{t '" + new Time JavaDoc(0L).toString() + "'}"),
180             new ValueStringData(new Timestamp JavaDoc(0L), DataType.TIMESTAMP,
181                     "{ts '" + new Timestamp JavaDoc(0L).toString() + "'}"),
182             new ValueStringData("12'34", DataType.VARCHAR, "'12''34'"),
183             new ValueStringData("'1234", DataType.VARCHAR, "'''1234'"),
184             new ValueStringData("1234'", DataType.VARCHAR, "'1234'''"),
185             new ValueStringData("'12'34'", DataType.VARCHAR, "'''12''34'''"),
186         };
187
188         for (int i = 0; i < values.length; i++)
189         {
190             ValueStringData data = values[i];
191             String JavaDoc valueString = DataSetUtils.getSqlValueString(
192                     data.getValue(), data.getDataType());
193             assertEquals("data " + i, data.getExpected(), valueString);
194         }
195     }
196
197     private class ValueStringData
198     {
199         private final Object JavaDoc _value;
200         private final DataType _dataType;
201         private final String JavaDoc _expected;
202
203         public ValueStringData(Object JavaDoc value, DataType dataType, String JavaDoc expected)
204         {
205             _value = value;
206             _dataType = dataType;
207             _expected = expected;
208         }
209
210         public Object JavaDoc getValue()
211         {
212             return _value;
213         }
214
215         public DataType getDataType()
216         {
217             return _dataType;
218         }
219
220         public String JavaDoc getExpected()
221         {
222             return _expected;
223         }
224     }
225
226 }
227
228
229
230
231
232
233
234
235
Popular Tags