KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.dbunit.util.FileAsserts;
27
28 import java.io.ByteArrayInputStream JavaDoc;
29 import java.io.File JavaDoc;
30 import java.sql.Types JavaDoc;
31 import java.util.Arrays JavaDoc;
32
33 /**
34  * @author Manuel Laflamme
35  * @version $Revision: 1.13 $
36  */

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