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