1 33 package smallsql.junit; 34 35 import junit.framework.*; 36 37 import java.math.BigDecimal ; 38 import java.sql.*; 39 40 import smallsql.database.Money; 41 42 public class TestMoneyRounding extends TestCase{ 43 44 static final String 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 e){ 60 } 62 } 63 64 public void testMoney1() throws Exception { 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 { 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 obj1 = rs.getObject(1); 83 Object 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 mon1 = (BigDecimal )obj1; 91 BigDecimal mon2 = (BigDecimal )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 { 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 |