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.math.BigDecimal;
28  import java.sql.Types;
29  
30  /**
31   * @author Manuel Laflamme
32   * @author Last changed by: $Author$
33   * @version $Revision$ $Date$
34   */
35  public class BooleanDataTypeTest extends AbstractDataTypeTest
36  {
37      private final static DataType THIS_TYPE = DataType.BOOLEAN;
38  
39      public BooleanDataTypeTest(String name)
40      {
41          super(name);
42      }
43  
44      /**
45       *
46       */
47      public void testToString() throws Exception
48      {
49          assertEquals("name", "BOOLEAN", THIS_TYPE.toString());
50      }
51  
52      /**
53       *
54       */
55      public void testGetTypeClass() throws Exception
56      {
57          assertEquals("class", Boolean.class, THIS_TYPE.getTypeClass());
58      }
59  
60      /**
61       *
62       */
63      public void testIsNumber() throws Exception
64      {
65          assertEquals("is number", false, THIS_TYPE.isNumber());
66      }
67  
68      public void testIsDateTime() throws Exception
69      {
70          assertEquals("is date/time", false, THIS_TYPE.isDateTime());
71      }
72  
73      public void testTypeCast() throws Exception
74      {
75          Object[] values = {
76              null,
77              "1", // Strings
78              "0",
79              "true",
80              "false",
81              "4894358", //TODO should it be possible to cast this into a Boolean?
82              Boolean.TRUE, // Booleans
83              Boolean.FALSE,
84              new Integer(1), // Numbers
85              new Integer(0),
86              new Integer(123), //TODO should it be possible to cast this into a Boolean?
87              new BigDecimal("20.53"), //TODO should it be possible to cast this into a Boolean?
88          };
89          Boolean[] expected = {
90              null,
91              Boolean.TRUE, // Strings
92              Boolean.FALSE,
93              Boolean.TRUE,
94              Boolean.FALSE,
95              Boolean.TRUE,
96              Boolean.TRUE, // Booleans
97              Boolean.FALSE,
98              Boolean.TRUE, // Numbers
99              Boolean.FALSE,
100             Boolean.TRUE,
101             Boolean.TRUE,
102         };
103 
104         assertEquals("actual vs expected count", values.length, expected.length);
105 
106         for (int i = 0; i < values.length; i++)
107         {
108             assertEquals("typecast " + i, expected[i],
109                     THIS_TYPE.typeCast(values[i]));
110         }
111     }
112 
113     public void testTypeCastNone() throws Exception
114     {
115         assertEquals("typecast", null, THIS_TYPE.typeCast(ITable.NO_VALUE));
116     }
117 
118     public void testTypeCastInvalid() throws Exception
119     {
120         Object[] values = {"bla"};
121 
122         for (int i = 0; i < values.length; i++)
123         {
124             try
125             {
126                 THIS_TYPE.typeCast(values[i]);
127                 fail("Should throw TypeCastException");
128             }
129             catch (TypeCastException e)
130             {
131             }
132         }
133     }
134 
135     public void testCompareEquals() throws Exception
136     {
137         Object[] values1 = {
138             null,
139             "1",
140             "0",
141             Boolean.TRUE,
142             Boolean.FALSE,
143         };
144         Object[] values2 = {
145             null,
146             Boolean.TRUE,
147             Boolean.FALSE,
148             "true",
149             "false",
150         };
151 
152         assertEquals("values count", values1.length, values2.length);
153 
154         for (int i = 0; i < values1.length; i++)
155         {
156             assertEquals("compare1 " + i,
157                     0, THIS_TYPE.compare(values1[i], values2[i]));
158             assertEquals("compare2 " + i,
159                     0, THIS_TYPE.compare(values2[i], values1[i]));
160         }
161     }
162 
163     public void testCompareInvalid() throws Exception
164     {
165         Object[] values1 = {
166             "bla",
167             Boolean.FALSE,
168         };
169         Object[] values2 = {
170             Boolean.TRUE,
171             "bla",
172         };
173 
174         assertEquals("values count", values1.length, values2.length);
175 
176         for (int i = 0; i < values1.length; i++)
177         {
178             try
179             {
180                 THIS_TYPE.compare(values1[i], values2[i]);
181                 fail("Should have throw TypeCastException");
182             }
183             catch (TypeCastException e)
184             {
185 
186             }
187 
188             try
189             {
190                 THIS_TYPE.compare(values2[i], values1[i]);
191                 fail("Should have throw TypeCastException");
192             }
193             catch (TypeCastException e)
194             {
195 
196             }
197         }
198     }
199 
200     public void testCompareDifferent() throws Exception
201     {
202         Object[] less = {
203             null,
204             null,
205             Boolean.FALSE,
206         };
207         Object[] greater = {
208             Boolean.TRUE,
209             Boolean.FALSE,
210             Boolean.TRUE,
211         };
212 
213         assertEquals("values count", less.length, greater.length);
214 
215         for (int i = 0; i < less.length; i++)
216         {
217             assertTrue("less " + i, THIS_TYPE.compare(less[i], greater[i]) < 0);
218             assertTrue("greater " + i, THIS_TYPE.compare(greater[i], less[i]) > 0);
219         }
220     }
221 
222     public void testSqlType() throws Exception
223     {
224         assertEquals("forSqlType", THIS_TYPE, DataType.forSqlType(Types.BOOLEAN));
225         assertEquals("forSqlTypeName", THIS_TYPE, DataType.forSqlTypeName(THIS_TYPE.toString()));
226         assertEquals("getSqlType", Types.BOOLEAN, THIS_TYPE.getSqlType());
227     }
228 
229     public void testForObject() throws Exception
230     {
231         assertEquals(THIS_TYPE, DataType.forObject(Boolean.TRUE));
232     }
233 
234     /**
235      *
236      */
237     public void testAsString() throws Exception
238     {
239         Boolean[] values = {
240             Boolean.TRUE,
241             Boolean.FALSE,
242         };
243 
244         String[] expected = {
245             "true",
246             "false",
247         };
248 
249         assertEquals("actual vs expected count", values.length, expected.length);
250 
251         for (int i = 0; i < values.length; i++)
252         {
253             assertEquals("asString " + i, expected[i], DataType.asString(values[i]));
254         }
255     }
256 
257     public void testGetSqlValue() throws Exception
258     {
259         Object[] expected = new Object[] {
260             null,
261             Boolean.TRUE,
262             Boolean.FALSE,
263         };
264 
265         ExtendedMockSingleRowResultSet resultSet = new ExtendedMockSingleRowResultSet();
266         resultSet.addExpectedIndexedValues(expected);
267 
268         for (int i = 0; i < expected.length; i++)
269         {
270             Object expectedValue = expected[i];
271             Object actualValue = THIS_TYPE.getSqlValue(i + 1, resultSet);
272             assertEquals("value", expectedValue, actualValue);
273         }
274     }
275 }