KickJava   Java API By Example, From Geeks To Geeks.

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


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 JavaDoc;
28
29 /**
30  * @author Manuel Laflamme
31  * @version $Revision: 1.12 $
32  */

33
34 public class BooleanDataTypeTest extends AbstractDataTypeTest
35 {
36     private final static DataType THIS_TYPE = DataType.BOOLEAN;
37
38     public BooleanDataTypeTest(String JavaDoc name)
39     {
40         super(name);
41     }
42
43     /**
44      *
45      */

46     public void testToString() throws Exception JavaDoc
47     {
48         assertEquals("name", "BIT", THIS_TYPE.toString());
49     }
50
51     /**
52      *
53      */

54     public void testGetTypeClass() throws Exception JavaDoc
55     {
56         assertEquals("class", Boolean JavaDoc.class, THIS_TYPE.getTypeClass());
57     }
58
59     /**
60      *
61      */

62     public void testIsNumber() throws Exception JavaDoc
63     {
64         assertEquals("is number", false, THIS_TYPE.isNumber());
65     }
66
67     public void testIsDateTime() throws Exception JavaDoc
68     {
69         assertEquals("is date/time", false, THIS_TYPE.isDateTime());
70     }
71
72     public void testTypeCast() throws Exception JavaDoc
73     {
74         Object JavaDoc[] values = {
75             null,
76             "1",
77             "0",
78             "true",
79             "false",
80             Boolean.TRUE,
81             Boolean.FALSE,
82             new Integer JavaDoc(1),
83             new Integer JavaDoc(0),
84             new Integer JavaDoc(123),
85         };
86         Boolean JavaDoc[] expected = {
87             null,
88             Boolean.TRUE,
89             Boolean.FALSE,
90             Boolean.TRUE,
91             Boolean.FALSE,
92             Boolean.TRUE,
93             Boolean.FALSE,
94             Boolean.TRUE,
95             Boolean.FALSE,
96             Boolean.TRUE,
97         };
98
99         assertEquals("actual vs expected count", values.length, expected.length);
100
101         for (int i = 0; i < values.length; i++)
102         {
103             assertEquals("typecast " + i, expected[i],
104                     THIS_TYPE.typeCast(values[i]));
105         }
106     }
107
108     public void testTypeCastNone() throws Exception JavaDoc
109     {
110         assertEquals("typecast", null, THIS_TYPE.typeCast(ITable.NO_VALUE));
111     }
112
113     public void testTypeCastInvalid() throws Exception JavaDoc
114     {
115         Object JavaDoc[] values = {"bla"};
116
117         for (int i = 0; i < values.length; i++)
118         {
119             try
120             {
121                 THIS_TYPE.typeCast(values[i]);
122                 fail("Should throw TypeCastException");
123             }
124             catch (TypeCastException e)
125             {
126             }
127         }
128     }
129
130     public void testCompareEquals() throws Exception JavaDoc
131     {
132         Object JavaDoc[] values1 = {
133             null,
134             "1",
135             "0",
136             Boolean.TRUE,
137             Boolean.FALSE,
138         };
139         Object JavaDoc[] values2 = {
140             null,
141             Boolean.TRUE,
142             Boolean.FALSE,
143             "true",
144             "false",
145         };
146
147         assertEquals("values count", values1.length, values2.length);
148
149         for (int i = 0; i < values1.length; i++)
150         {
151             assertEquals("compare1 " + i,
152                     0, THIS_TYPE.compare(values1[i], values2[i]));
153             assertEquals("compare2 " + i,
154                     0, THIS_TYPE.compare(values2[i], values1[i]));
155         }
156     }
157
158     public void testCompareInvalid() throws Exception JavaDoc
159     {
160         Object JavaDoc[] values1 = {
161             "bla",
162             Boolean.FALSE,
163         };
164         Object JavaDoc[] values2 = {
165             Boolean.TRUE,
166             "bla",
167         };
168
169         assertEquals("values count", values1.length, values2.length);
170
171         for (int i = 0; i < values1.length; i++)
172         {
173             try
174             {
175                 THIS_TYPE.compare(values1[i], values2[i]);
176                 fail("Should have throw TypeCastException");
177             }
178             catch (TypeCastException e)
179             {
180
181             }
182
183             try
184             {
185                 THIS_TYPE.compare(values2[i], values1[i]);
186                 fail("Should have throw TypeCastException");
187             }
188             catch (TypeCastException e)
189             {
190
191             }
192         }
193     }
194
195     public void testCompareDifferent() throws Exception JavaDoc
196     {
197         Object JavaDoc[] less = {
198             null,
199             null,
200             Boolean.FALSE,
201         };
202         Object JavaDoc[] greater = {
203             Boolean.TRUE,
204             Boolean.FALSE,
205             Boolean.TRUE,
206         };
207
208         assertEquals("values count", less.length, greater.length);
209
210         for (int i = 0; i < less.length; i++)
211         {
212             assertTrue("less " + i, THIS_TYPE.compare(less[i], greater[i]) < 0);
213             assertTrue("greater " + i, THIS_TYPE.compare(greater[i], less[i]) > 0);
214         }
215     }
216
217     public void testSqlType() throws Exception JavaDoc
218     {
219         assertEquals("forSqlType", THIS_TYPE, DataType.forSqlType(Types.BIT));
220         assertEquals("forSqlTypeName", THIS_TYPE, DataType.forSqlTypeName(THIS_TYPE.toString()));
221         assertEquals("getSqlType", Types.BIT, THIS_TYPE.getSqlType());
222     }
223
224     public void testForObject() throws Exception JavaDoc
225     {
226         assertEquals(THIS_TYPE, DataType.forObject(Boolean.TRUE));
227     }
228
229     /**
230      *
231      */

232     public void testAsString() throws Exception JavaDoc
233     {
234         Boolean JavaDoc[] values = {
235             Boolean.TRUE,
236             Boolean.FALSE,
237         };
238
239         String JavaDoc[] expected = {
240             "true",
241             "false",
242         };
243
244         assertEquals("actual vs expected count", values.length, expected.length);
245
246         for (int i = 0; i < values.length; i++)
247         {
248             assertEquals("asString " + i, expected[i], DataType.asString(values[i]));
249         }
250     }
251
252     public void testGetSqlValue() throws Exception JavaDoc
253     {
254         Object JavaDoc[] expected = new Object JavaDoc[] {
255             null,
256             Boolean.TRUE,
257             Boolean.FALSE,
258         };
259
260         ExtendedMockSingleRowResultSet resultSet = new ExtendedMockSingleRowResultSet();
261         resultSet.addExpectedIndexedValues(expected);
262
263         for (int i = 0; i < expected.length; i++)
264         {
265             Object JavaDoc expectedValue = expected[i];
266             Object JavaDoc actualValue = THIS_TYPE.getSqlValue(i + 1, resultSet);
267             assertEquals("value", expectedValue, actualValue);
268         }
269     }
270 }
271
Popular Tags