KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > datatype > StringDataTypeTest


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.datatype;
23
24 import org.dbunit.database.ExtendedMockSingleRowResultSet;
25 import org.dbunit.dataset.ITable;
26
27 import java.sql.Types JavaDoc;
28
29 /**
30  * @author Manuel Laflamme
31  * @version $Revision: 1.14 $
32  */

33
34 public class StringDataTypeTest extends AbstractDataTypeTest
35 {
36     private final static DataType[] TYPES = {
37         DataType.CHAR,
38         DataType.VARCHAR,
39         DataType.LONGVARCHAR,
40 // DataType.CLOB,
41
};
42
43     public StringDataTypeTest(String JavaDoc name)
44     {
45         super(name);
46     }
47
48     public void testToString() throws Exception JavaDoc
49     {
50         String JavaDoc[] expected = {
51             "CHAR",
52             "VARCHAR",
53             "LONGVARCHAR",
54 // "CLOB",
55
};
56
57         assertEquals("type count", expected.length, TYPES.length);
58         for (int i = 0; i < TYPES.length; i++)
59         {
60             assertEquals("name", expected[i], TYPES[i].toString());
61         }
62     }
63
64     public void testGetTypeClass() throws Exception JavaDoc
65     {
66         for (int i = 0; i < TYPES.length; i++)
67         {
68             assertEquals("class", String JavaDoc.class, TYPES[i].getTypeClass());
69         }
70     }
71
72     public void testIsNumber() throws Exception JavaDoc
73     {
74         for (int i = 0; i < TYPES.length; i++)
75         {
76             assertEquals("is number", false, TYPES[i].isNumber());
77         }
78     }
79
80     public void testIsDateTime() throws Exception JavaDoc
81     {
82         for (int i = 0; i < TYPES.length; i++)
83         {
84             assertEquals("is date/time", false, TYPES[i].isDateTime());
85         }
86     }
87
88     public void testTypeCast() throws Exception JavaDoc
89     {
90         Object JavaDoc[] values = {
91             null,
92             "bla",
93             new java.sql.Date JavaDoc(1234),
94             new java.sql.Time JavaDoc(1234),
95             new java.sql.Timestamp JavaDoc(1234),
96             Boolean.TRUE,
97             new Integer JavaDoc(1234),
98             new Long JavaDoc(1234),
99             new Double JavaDoc(12.34),
100             new byte[]{'a', 'b', 'c', 'd'},
101         };
102         String JavaDoc[] expected = {
103             null,
104             "bla",
105             new java.sql.Date JavaDoc(1234).toString(),
106             new java.sql.Time JavaDoc(1234).toString(),
107             new java.sql.Timestamp JavaDoc(1234).toString(),
108             "true",
109             "1234",
110             "1234",
111             "12.34",
112             "YWJjZA==",
113         };
114
115         assertEquals("actual vs expected count", values.length, expected.length);
116
117         for (int i = 0; i < TYPES.length; i++)
118         {
119             for (int j = 0; j < values.length; j++)
120             {
121                 assertEquals("typecast " + j, expected[j],
122                         TYPES[i].typeCast(values[j]));
123             }
124         }
125     }
126
127     public void testTypeCastNone() throws Exception JavaDoc
128     {
129         for (int i = 0; i < TYPES.length; i++)
130         {
131             DataType type = TYPES[i];
132             assertEquals("typecast " + type, null, type.typeCast(ITable.NO_VALUE));
133         }
134     }
135
136     public void testTypeCastInvalid() throws Exception JavaDoc
137     {
138         Object JavaDoc[] values = {
139             new Object JavaDoc()
140         };
141
142         for (int i = 0; i < TYPES.length; i++)
143         {
144             for (int j = 0; j < values.length; j++)
145             {
146                 try
147                 {
148                     TYPES[i].typeCast(values[j]);
149                     fail("Should throw TypeCastException");
150                 }
151                 catch (TypeCastException e)
152                 {
153                 }
154             }
155         }
156     }
157
158     public void testCompareEquals() throws Exception JavaDoc
159     {
160         Object JavaDoc[] values1 = {
161             null,
162             "bla",
163             new java.sql.Date JavaDoc(1234),
164             new java.sql.Time JavaDoc(1234),
165             new java.sql.Timestamp JavaDoc(1234),
166             Boolean.TRUE,
167             new Integer JavaDoc(1234),
168             new Long JavaDoc(1234),
169             new Double JavaDoc(12.34),
170             new byte[]{'a', 'b', 'c', 'd'},
171         };
172         String JavaDoc[] values2 = {
173             null,
174             "bla",
175             new java.sql.Date JavaDoc(1234).toString(),
176             new java.sql.Time JavaDoc(1234).toString(),
177             new java.sql.Timestamp JavaDoc(1234).toString(),
178             "true",
179             "1234",
180             "1234",
181             "12.34",
182             "YWJjZA==",
183         };
184
185         assertEquals("values count", values1.length, values2.length);
186
187         for (int i = 0; i < TYPES.length; i++)
188         {
189             for (int j = 0; j < values1.length; j++)
190             {
191                 assertEquals("compare1 " + j, 0, TYPES[i].compare(values1[j], values2[j]));
192                 assertEquals("compare2 " + j, 0, TYPES[i].compare(values2[j], values1[j]));
193             }
194         }
195     }
196
197     public void testCompareInvalid() throws Exception JavaDoc
198     {
199         Object JavaDoc[] values1 = {
200             new Object JavaDoc(),
201         };
202         Object JavaDoc[] values2 = {
203             null,
204         };
205
206         assertEquals("values count", values1.length, values2.length);
207
208         for (int i = 0; i < TYPES.length; i++)
209         {
210             for (int j = 0; j < values1.length; j++)
211             {
212                 try
213                 {
214                     TYPES[i].compare(values1[j], values2[j]);
215                     fail("Should throw TypeCastException");
216                 }
217                 catch (TypeCastException e)
218                 {
219                 }
220
221                 try
222                 {
223                     TYPES[i].compare(values2[j], values1[j]);
224                     fail("Should throw TypeCastException");
225                 }
226                 catch (TypeCastException e)
227                 {
228                 }
229             }
230         }
231     }
232
233     public void testCompareDifferent() throws Exception JavaDoc
234     {
235         Object JavaDoc[] less = {
236             null,
237             "",
238             "abcd",
239             "123",
240         };
241
242         Object JavaDoc[] greater = {
243             "bla",
244             "bla",
245             "efgh",
246             "1234",
247         };
248
249         assertEquals("values count", less.length, greater.length);
250
251         for (int i = 0; i < TYPES.length; i++)
252         {
253             for (int j = 0; j < less.length; j++)
254             {
255                 assertTrue("less " + j, TYPES[i].compare(less[j], greater[j]) < 0);
256                 assertTrue("greater " + j, TYPES[i].compare(greater[j], less[j]) > 0);
257             }
258         }
259     }
260
261     public void testSqlType() throws Exception JavaDoc
262     {
263         int[] sqlTypes = {
264             Types.CHAR,
265             Types.VARCHAR,
266             Types.LONGVARCHAR,
267 // Types.CLOB,
268
};
269
270         assertEquals("count", sqlTypes.length, TYPES.length);
271         for (int i = 0; i < TYPES.length; i++)
272         {
273             assertEquals("forSqlType", TYPES[i], DataType.forSqlType(sqlTypes[i]));
274             assertEquals("forSqlTypeName", TYPES[i], DataType.forSqlTypeName(TYPES[i].toString()));
275             assertEquals("getSqlType", sqlTypes[i], TYPES[i].getSqlType());
276         }
277     }
278
279     public void testForObject() throws Exception JavaDoc
280     {
281         assertEquals(DataType.VARCHAR, DataType.forObject(""));
282     }
283
284     public void testAsString() throws Exception JavaDoc
285     {
286         Object JavaDoc[] values = {
287             new String JavaDoc("1234"),
288         };
289
290         String JavaDoc[] expected = {
291             "1234",
292         };
293
294         assertEquals("actual vs expected count", values.length, expected.length);
295
296         for (int i = 0; i < values.length; i++)
297         {
298             assertEquals("asString " + i, expected[i], DataType.asString(values[i]));
299         }
300     }
301
302     public void testGetSqlValue() throws Exception JavaDoc
303     {
304         String JavaDoc[] expected = {
305             null,
306             "bla",
307         };
308
309         ExtendedMockSingleRowResultSet resultSet = new ExtendedMockSingleRowResultSet();
310         resultSet.addExpectedIndexedValues(expected);
311
312         for (int i = 0; i < expected.length; i++)
313         {
314             Object JavaDoc expectedValue = expected[i];
315
316             for (int j = 0; j < TYPES.length; j++)
317             {
318                 DataType dataType = TYPES[j];
319                 Object JavaDoc actualValue = dataType.getSqlValue(i + 1, resultSet);
320                 assertEquals("value " + j, expectedValue, actualValue);
321             }
322         }
323     }
324
325 }
326
Popular Tags