View Javadoc
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$
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             "[text]This is text with UTF-8 (the default) characters >>àéç<<",
101             "[text UTF-8]This is text with UTF-8 (the default) characters >>àéç<<",
102             "[text]c27ccbf5-6ca1-4bdd-8cb0-bacfea6a5a8b",
103             "[base64]VGhpcyBpcyBhIHRlc3QgZm9yIGJhc2U2NC4K=="
104         };
105 
106         byte[][] expected = {
107             null,
108             new byte[0],
109             new byte[]{'a', 'b', 'c', 'd'},
110             new byte[]{0, 1, 2, 3, 4, 5},
111             values[4].toString().replaceAll("\\[.*?\\]", "").getBytes("UTF-8"),
112             values[5].toString().replaceAll("\\[.*?\\]", "").getBytes("UTF-8"),
113             values[6].toString().replaceAll("\\[.*?\\]", "").getBytes("UTF-8"),
114             "This is a test for base64.\n".getBytes(),
115         };
116 
117         assertEquals("actual vs expected count", values.length, expected.length);
118 
119         for (int i = 0; i < TYPES.length; i++)
120         {
121             for (int j = 0; j < values.length; j++)
122             {
123                 byte[] actual = (byte[])TYPES[i].typeCast(values[j]);
124                 assertTrue("typecast " + j, Arrays.equals(expected[j], actual));
125             }
126         }
127     }
128 
129     public void testTypeCastFileName() throws Exception
130     {
131         File file = new File("LICENSE.txt");
132 
133         Object[] values = {
134             "[file]" + file.toString(),
135             file.toString(),
136             file.getAbsolutePath(),
137             file.toURI().toURL().toString(),
138             file,
139             file.toURI().toURL(),
140             "[url]" + file.toURI().toURL(),
141         };
142 
143         assertEquals("exists", true, file.exists());
144 
145         for (int i = 0; i < TYPES.length; i++)
146         {
147             for (int j = 0; j < values.length; j++)
148             {
149                 byte[] actual = (byte[])TYPES[i].typeCast(values[j]);
150                 FileAsserts.assertEquals(new ByteArrayInputStream(actual), file);
151             }
152         }
153     }
154 
155     public void testTypeCastNone() throws Exception
156     {
157         for (int i = 0; i < TYPES.length; i++)
158         {
159             DataType type = TYPES[i];
160             assertEquals("typecast " + type, null, type.typeCast(ITable.NO_VALUE));
161         }
162     }
163 
164     public void testTypeCastInvalid() throws Exception
165     {
166         Object[] values = {
167             new Object(),
168             new Integer(1234),
169         };
170 
171         for (int i = 0; i < TYPES.length; i++)
172         {
173             for (int j = 0; j < values.length; j++)
174             {
175                 try
176                 {
177                     TYPES[i].typeCast(values[j]);
178                     fail("Should throw TypeCastException: " + values[j]);
179                 }
180                 catch (TypeCastException e)
181                 {
182                 }
183             }
184         }
185     }
186 
187     public void testCompareEquals() throws Exception
188     {
189         Object[] values1 = {
190             null,
191             "",
192             "YWJjZA==",
193             new byte[]{0, 1, 2, 3, 4, 5},
194         };
195 
196         byte[][] values2 = {
197             null,
198             new byte[0],
199             new byte[]{'a', 'b', 'c', 'd'},
200             new byte[]{0, 1, 2, 3, 4, 5},
201         };
202 
203         assertEquals("values count", values1.length, values2.length);
204 
205         for (int i = 0; i < TYPES.length; i++)
206         {
207             for (int j = 0; j < values1.length; j++)
208             {
209                 assertEquals("compare1 " + j, 0, TYPES[i].compare(values1[j], values2[j]));
210                 assertEquals("compare2 " + j, 0, TYPES[i].compare(values2[j], values1[j]));
211             }
212         }
213     }
214 
215     public void testCompareInvalid() throws Exception
216     {
217         Object[] values1 = {
218             new Object(),
219             new java.util.Date()
220         };
221         Object[] values2 = {
222             null,
223             null
224         };
225 
226         assertEquals("values count", values1.length, values2.length);
227 
228         for (int i = 0; i < TYPES.length; i++)
229         {
230             for (int j = 0; j < values1.length; j++)
231             {
232                 try
233                 {
234                     TYPES[i].compare(values1[j], values2[j]);
235                     fail("Should throw TypeCastException");
236                 }
237                 catch (TypeCastException e)
238                 {
239                 }
240 
241                 try
242                 {
243                     TYPES[i].compare(values2[j], values1[j]);
244                     fail("Should throw TypeCastException");
245                 }
246                 catch (TypeCastException e)
247                 {
248                 }
249             }
250         }
251     }
252 
253     public void testCompareDifferent() throws Exception
254     {
255         Object[] less = {
256             null,
257             new byte[]{'a', 'a', 'c', 'd'},
258             new byte[]{0, 1, 2, 3, 4, 5},
259         };
260         Object[] greater = {
261             new byte[0],
262             new byte[]{'a', 'b', 'c', 'd'},
263             new byte[]{0, 1, 2, 3, 4, 5, 6},
264         };
265 
266         assertEquals("values count", less.length, greater.length);
267 
268         for (int i = 0; i < TYPES.length; i++)
269         {
270             for (int j = 0; j < less.length; j++)
271             {
272                 assertTrue("less " + j, TYPES[i].compare(less[j], greater[j]) < 0);
273                 assertTrue("greater " + j, TYPES[i].compare(greater[j], less[j]) > 0);
274             }
275         }
276     }
277 
278     public void testSqlType() throws Exception
279     {
280         int[] sqlTypes = {
281             Types.BINARY,
282             Types.VARBINARY,
283             Types.LONGVARBINARY,
284 //            Types.BLOB,
285         };
286 
287         assertEquals("count", sqlTypes.length, TYPES.length);
288         for (int i = 0; i < TYPES.length; i++)
289         {
290             assertEquals("forSqlType", TYPES[i], DataType.forSqlType(sqlTypes[i]));
291             assertEquals("forSqlTypeName", TYPES[i], DataType.forSqlTypeName(TYPES[i].toString()));
292             assertEquals("getSqlType", sqlTypes[i], TYPES[i].getSqlType());
293         }
294     }
295 
296     public void testForObject() throws Exception
297     {
298         assertEquals(DataType.VARBINARY, DataType.forObject(new byte[0]));
299     }
300 
301     public void testAsString() throws Exception
302     {
303         byte[][] values = {
304             new byte[0],
305             new byte[]{'a', 'b', 'c', 'd'},
306         };
307 
308         String[] expected = {
309             "",
310             "YWJjZA==",
311         };
312 
313         assertEquals("actual vs expected count", values.length, expected.length);
314 
315         for (int i = 0; i < values.length; i++)
316         {
317             assertEquals("asString " + i, expected[i], DataType.asString(values[i]));
318         }
319     }
320 
321     public void testGetSqlValue() throws Exception
322     {
323         byte[][] expected = {
324             null,
325             new byte[0],
326             new byte[]{'a', 'b', 'c', 'd'},
327             new byte[]{0, 1, 2, 3, 4, 5},
328         };
329 
330         ExtendedMockSingleRowResultSet resultSet = new ExtendedMockSingleRowResultSet();
331         resultSet.addExpectedIndexedValues(expected);
332 
333         for (int i = 0; i < expected.length; i++)
334         {
335             Object expectedValue = expected[i];
336 
337             for (int j = 0; j < TYPES.length; j++)
338             {
339                 DataType dataType = TYPES[j];
340                 Object actualValue = dataType.getSqlValue(i + 1, resultSet);
341                 assertEquals("value " + j, expectedValue, actualValue);
342             }
343         }
344     }
345 
346     public void testSetSqlValue() throws Exception
347     {
348     	MockPreparedStatement preparedStatement = new MockPreparedStatement();
349 
350         Object[] expected = {
351                 null,
352                 new byte[0],
353                 new byte[]{'a', 'b', 'c', 'd'},
354         };
355 
356         int[] expectedSqlTypesForDataType = {
357         		Types.BINARY,
358                 Types.VARBINARY,
359                 Types.LONGVARBINARY
360         };
361 
362         for (int i = 0; i < expected.length; i++)
363         {
364             Object expectedValue = expected[i];
365 
366             for (int j = 0; j < TYPES.length; j++)
367             {
368                 DataType dataType = TYPES[j];
369                 int expectedSqlType = expectedSqlTypesForDataType[j];
370 
371                 dataType.setSqlValue(expectedValue, 1, preparedStatement);
372                 // Check the results immediately
373                 assertEquals("Loop " + i + " Type " + dataType, 1, preparedStatement.getLastSetObjectParamIndex());
374                 assertEquals("Loop " + i + " Type " + dataType, expectedSqlType, preparedStatement.getLastSetObjectTargetSqlType());
375                 Object actualValue = preparedStatement.getLastSetObjectParamValue();
376                 assertEquals("Loop " + i + " Type " + dataType, expectedValue, actualValue);
377             }
378         }
379     }
380 }