View Javadoc
1   package org.dbunit.dataset.datatype;
2   
3   import java.math.BigDecimal;
4   import java.math.BigInteger;
5   import java.sql.Types;
6   
7   import org.dbunit.database.ExtendedMockSingleRowResultSet;
8   import org.dbunit.dataset.ITable;
9   
10  /**
11   * Unit test for the number tolerant data type which is quite similar to the NumberDataTypeTest.
12   * @author gommma
13   */
14  public class NumberTolerantDataTypeTest extends AbstractDataTypeTest
15  {
16  
17  	private NumberTolerantDataType THIS_TYPE = new NumberTolerantDataType("NUMERIC", Types.NUMERIC,
18  	        new ToleratedDeltaMap.Precision(new BigDecimal("1E-5")) );
19      private NumberTolerantDataType THIS_TYPE_PERCENTAGE = new NumberTolerantDataType("NUMERIC", Types.NUMERIC,
20              new ToleratedDeltaMap.Precision(new BigDecimal("1.0"), true) );
21  
22  	
23      public NumberTolerantDataTypeTest(String name)
24      {
25          super(name);
26      }
27      
28      public void testCreateWithNegativeDelta() throws Exception
29      {
30      	try 
31      	{
32      		new NumberTolerantDataType("NUMERIC", Types.NUMERIC, new ToleratedDeltaMap.Precision(new BigDecimal("-0.1")) );
33      		fail("Should not be able to created datatype with negative delta");
34      	}
35      	catch(IllegalArgumentException expected)
36      	{
37      		String expectedMsg = "The given delta '-0.1' must be >= 0";
38      		assertEquals(expectedMsg, expected.getMessage());
39      	}
40      }
41  
42      public void testCompareToWithDelta_DiffWithinToleratedDelta() throws Exception
43      {
44      	int result = THIS_TYPE.compare(new BigDecimal(0.12345678D), new BigDecimal(0.123456789D));
45      	assertEquals(0, result);
46      }
47  
48      public void testCompareToWithDelta_DiffOutsideOfToleratedDelta() throws Exception
49      {
50      	int result = THIS_TYPE.compare(new BigDecimal(0.1234), new BigDecimal(0.1235D));
51      	assertEquals(-1, result);
52      }
53  
54      public void testCompareToWithDeltaPercentage_DiffWithinToleratedDelta() throws Exception
55      {
56          int result = THIS_TYPE_PERCENTAGE.compare(new BigDecimal("1000.0"), new BigDecimal("1010.0"));
57          assertEquals(0, result);
58      }
59  
60      public void testCompareToWithDeltaPercentage_DiffOutsideOfToleratedDelta() throws Exception
61      {
62          int result = THIS_TYPE_PERCENTAGE.compare(new BigDecimal("1000.0"), new BigDecimal("1010.1"));
63          assertEquals(-1, result);
64      }
65  
66      /**
67       *
68       */
69      public void testToString() throws Exception
70      {
71          assertEquals("name", "NUMERIC", THIS_TYPE.toString());
72      }
73  
74      /**
75       *
76       */
77      public void testGetTypeClass() throws Exception
78      {
79          assertEquals("class", java.math.BigDecimal.class, THIS_TYPE.getTypeClass());
80      }
81  
82      /**
83       *
84       */
85      public void testIsNumber() throws Exception
86      {
87          assertEquals("is number", true, THIS_TYPE.isNumber());
88      }
89  
90      public void testIsDateTime() throws Exception
91      {
92          assertEquals("is date/time", false, THIS_TYPE.isDateTime());
93      }
94  
95      public void testTypeCast() throws Exception
96      {
97          Object[] values = {
98                  null,
99                  new BigDecimal((double)1234),
100                 "1234",
101                 "12.34",
102                 Boolean.TRUE,
103                 Boolean.FALSE,
104             };
105         BigDecimal[] expected = {
106             null,
107             new BigDecimal((double)1234),
108             new BigDecimal((double)1234),
109             new BigDecimal("12.34"),
110             new BigDecimal("1"),
111             new BigDecimal("0"),
112         };
113 
114         assertEquals("actual vs expected count", values.length, expected.length);
115 
116         for (int j = 0; j < values.length; j++)
117         {
118             assertEquals("typecast " + j, expected[j],
119                     THIS_TYPE.typeCast(values[j]));
120         }
121     }
122 
123     public void testTypeCastNone() throws Exception
124     {
125         assertEquals("typecast", null, THIS_TYPE.typeCast(ITable.NO_VALUE));
126     }
127 
128     public void testTypeCastInvalid() throws Exception
129     {
130         Object[] values = {
131             new Object(),
132             "bla",
133         };
134 
135         for (int i = 0; i < values.length; i++)
136         {
137             try
138             {
139                 THIS_TYPE.typeCast(values[i]);
140                 fail("Should throw TypeCastException - " + i);
141             }
142             catch (TypeCastException e)
143             {
144             }
145         }
146     }
147     
148     
149     public void testCompareEquals() throws Exception
150     {
151         Object[] values1 = {
152                 null,
153                 new BigDecimal((double)1234),
154                 "1234",
155                 "12.34",
156                 Boolean.TRUE,
157                 Boolean.FALSE,
158                 new BigDecimal(123.4),
159                 "123",
160             };
161         Object[] values2 = {
162             null,
163             new BigDecimal((double)1234),
164             new BigDecimal(1234),
165             new BigDecimal("12.34"),
166             new BigDecimal("1"),
167             new BigDecimal("0"),
168             new BigDecimal(123.4000),
169             new BigDecimal("123.0"),
170         };
171 
172         assertEquals("values count", values1.length, values2.length);
173 
174         for (int i = 0; i < values1.length; i++)
175         {
176             assertEquals("compare1 " + i, 0, THIS_TYPE.compare(values1[i], values2[i]));
177             assertEquals("compare2 " + i, 0, THIS_TYPE.compare(values2[i], values1[i]));
178         }
179     }
180 
181     public void testCompareInvalid() throws Exception
182     {
183         Object[] values1 = {
184             new Object(),
185             "bla",
186         };
187         Object[] values2 = {
188             null,
189             null,
190         };
191 
192         assertEquals("values count", values1.length, values2.length);
193 
194         for (int i = 0; i < values1.length; i++)
195         {
196             try
197             {
198                 THIS_TYPE.compare(values1[i], values2[i]);
199                 fail("Should throw TypeCastException - " + i);
200             }
201             catch (TypeCastException e)
202             {
203             }
204 
205             try
206             {
207                 THIS_TYPE.compare(values1[i], values2[i]);
208                 fail("Should throw TypeCastException - " + i);
209             }
210             catch (TypeCastException e)
211             {
212             }
213         }
214     }
215 
216     public void testCompareDifferent() throws Exception
217     {
218         Object[] less = {
219                 null,
220                 "-7500",
221                 new BigDecimal("-0.01"),
222                 new BigInteger("1234"),
223             };
224 
225         Object[] greater = {
226             "0",
227             "5.555",
228             new BigDecimal("0.01"),
229             new BigDecimal("1234.5"),
230         };
231 
232         assertEquals("values count", less.length, greater.length);
233 
234         for (int j = 0; j < less.length; j++)
235         {
236             assertTrue("less " + j, THIS_TYPE.compare(less[j], greater[j]) < 0);
237             assertTrue("greater " + j, THIS_TYPE.compare(greater[j], less[j]) > 0);
238         }
239     }
240 
241     public void testSqlType() throws Exception
242     {
243     }
244 
245     public void testForObject() throws Exception
246     {
247     }
248 
249     public void testAsString() throws Exception
250     {
251         BigDecimal[] values = {
252                 new BigDecimal("1234"),
253             };
254 
255         String[] expected = {
256             "1234",
257         };
258 
259         assertEquals("actual vs expected count", values.length, expected.length);
260 
261         for (int i = 0; i < values.length; i++)
262         {
263             assertEquals("asString " + i, expected[i], DataType.asString(values[i]));
264         }
265     }
266 
267     public void testGetSqlValue() throws Exception
268     {
269         BigDecimal[] expected = {
270                 null,
271                 new BigDecimal("12.34"),
272             };
273 
274         ExtendedMockSingleRowResultSet resultSet = new ExtendedMockSingleRowResultSet();
275         resultSet.addExpectedIndexedValues(expected);
276 
277         for (int i = 0; i < expected.length; i++)
278         {
279             Object expectedValue = expected[i];
280 
281             DataType dataType = THIS_TYPE;
282             Object actualValue = dataType.getSqlValue(i + 1, resultSet);
283             assertEquals("value", expectedValue, actualValue);
284         }
285     }
286 
287 }