KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > smallsql > junit > TestMoneyRounding


1 /* =============================================================
2  * SmallSQL : a free Java DBMS library for the Java(tm) platform
3  * =============================================================
4  *
5  * (C) Copyright 2004-2006, by Volker Berlin.
6  *
7  * Project Info: http://www.smallsql.de/
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ---------------
28  * TestMoneyRounding.java
29  * ---------------
30  * Author: Volker Berlin
31  *
32  */

33 package smallsql.junit;
34
35 import junit.framework.*;
36
37 import java.math.BigDecimal JavaDoc;
38 import java.sql.*;
39
40 import smallsql.database.Money;
41
42 public class TestMoneyRounding extends TestCase{
43
44     static final String JavaDoc table = "TestMoneyRounding";
45
46     public void setUp() throws SQLException{
47         tearDown();
48         Connection con = AllTests.getConnection();
49         Statement st = con.createStatement();
50         st.execute("create table " + table + "(a money, b smallmoney)");
51     }
52
53     public void tearDown(){
54         try{
55             Connection con = AllTests.getConnection();
56             Statement st = con.createStatement();
57             st.execute("drop table " + table);
58             st.close();
59         }catch(Throwable JavaDoc e){
60             //e.printStackTrace();
61
}
62     }
63
64     public void testMoney1() throws Exception JavaDoc{
65             Connection con = AllTests.getConnection();
66             Statement st = con.createStatement();
67             int firstValue = -10000;
68             for(int i=firstValue; i<10000; i++){
69                 st.execute("Insert into " + table + "(a,b) values(" + (i/10000.0) + "," +(i/10000.0) +")");
70             }
71             st.close();
72             verify(firstValue);
73     }
74     
75     
76     private void verify(int firstValue) throws Exception JavaDoc{
77         Connection con = AllTests.getConnection();
78         Statement st = con.createStatement();
79         ResultSet rs = st.executeQuery("Select * FROM " + table);
80         long i = firstValue;
81         while(rs.next()){
82             Object JavaDoc obj1 = rs.getObject(1);
83             Object JavaDoc obj2 = rs.getObject(2);
84             if(obj1 instanceof Money){
85                 Money mon1 = (Money)obj1;
86                 Money mon2 = (Money)obj2;
87                 assertEquals("Roundungsfehler money:", i, mon1.unscaledValue());
88                 assertEquals("Roundungsfehler smallmoney:", i, mon2.unscaledValue());
89             }else{
90                 BigDecimal JavaDoc mon1 = (BigDecimal JavaDoc)obj1;
91                 BigDecimal JavaDoc mon2 = (BigDecimal JavaDoc)obj2;
92                 assertEquals("Roundungsfehler money:", i, mon1.unscaledValue().longValue());
93                 assertEquals("Roundungsfehler smallmoney:", i, mon2.unscaledValue().longValue());
94             }
95             i++;
96         }
97         st.close();
98     }
99     
100     
101     public void testMoney2() throws Exception JavaDoc{
102             Connection con = AllTests.getConnection();
103             Statement st = con.createStatement();
104             int firstValue = -10000;
105             for(int i=firstValue; i<10000; i++){
106                 st.execute("Insert into " + table + "(a,b) values( (" + i + "/10000.0), (" + i + "/10000.0) )");
107             }
108             st.close();
109             verify(firstValue);
110     }
111     
112 }
Popular Tags