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