KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > NumberAccuracyTest


1 package org.apache.ojb.broker;
2
3 import java.math.BigDecimal JavaDoc;
4
5 import org.apache.commons.lang.builder.ToStringBuilder;
6 import org.apache.commons.lang.builder.ToStringStyle;
7 import org.apache.ojb.junit.PBTestCase;
8
9 /**
10  * Test case to check the accuracy of {@link BigDecimal} values stored in DB.
11  *
12  * @author <a HREF="mailto:arminw@apache.org">Armin Waibel</a>
13  * @version $Id: NumberAccuracyTest.java,v 1.1.2.1 2005/12/22 22:57:37 tomdz Exp $
14  */

15 public class NumberAccuracyTest extends PBTestCase
16 {
17     public static void main(String JavaDoc[] args)
18     {
19         junit.textui.TestRunner.main(new String JavaDoc[] {NumberAccuracyTest.class.getName()});
20     }
21
22     public NumberAccuracyTest()
23     {
24     }
25
26     public NumberAccuracyTest(String JavaDoc name)
27     {
28         super(name);
29     }
30
31     public void testBigDecimal()
32     {
33         BigDecimal JavaDoc scaleTwo = new BigDecimal JavaDoc("17.34554");
34         BigDecimal JavaDoc scaleFour = new BigDecimal JavaDoc("67.345567");
35         // round half up values
36
scaleTwo = scaleTwo.setScale(2, BigDecimal.ROUND_HALF_UP);
37         scaleFour = scaleFour.setScale(4, BigDecimal.ROUND_HALF_UP);
38
39         NumberObject no = new NumberObject();
40         no.setScaleTwo(scaleTwo);
41         no.setScaleFour(scaleFour);
42
43 // System.out.println("# " + no.getScaleTwoDouble());
44
// System.out.println("# " + no.getScaleFourDouble());
45
broker.beginTransaction();
46         broker.store(no);
47         broker.commitTransaction();
48
49         Identity oidNo = new Identity(no, broker);
50         broker.clearCache();
51         NumberObject newNo = (NumberObject) broker.getObjectByIdentity(oidNo);
52 // System.out.println("# " + newNo.getScaleTwoDouble());
53
// System.out.println("# " + newNo.getScaleFourDouble());
54
assertEquals(17.35d ,newNo.getScaleTwoDouble(), 0.001);
55         assertEquals(67.3456 ,newNo.getScaleFourDouble(), 0.00001);
56         assertTrue(0 == scaleTwo.compareTo(newNo.getScaleTwo()));
57         assertTrue(0 == scaleFour.compareTo(newNo.getScaleFour()));
58
59
60         BigDecimal JavaDoc newScaleTwo = newNo.getScaleTwo().multiply(new BigDecimal JavaDoc("10"));
61         newNo.setScaleTwo(newScaleTwo);
62         BigDecimal JavaDoc newScaleFour = newNo.getScaleFour().multiply(new BigDecimal JavaDoc("10"));
63         newNo.setScaleFour(newScaleFour);
64         broker.beginTransaction();
65         broker.store(newNo);
66         broker.commitTransaction();
67
68         broker.clearCache();
69         NumberObject newNo_2 = (NumberObject) broker.getObjectByIdentity(oidNo);
70 // System.out.println("# " + newNo.getScaleTwoDouble());
71
// System.out.println("# " + newNo.getScaleFourDouble());
72
assertEquals(173.5d ,newNo_2.getScaleTwoDouble(), 0.1);
73         assertEquals(673.455 ,newNo_2.getScaleFourDouble(), 0.001);
74         assertTrue(0 == newScaleTwo.compareTo(newNo_2.getScaleTwo()));
75         assertTrue(0 == newScaleFour.compareTo(newNo_2.getScaleFour()));
76     }
77
78     public static class NumberObject
79     {
80         private Integer JavaDoc id;
81         private BigDecimal JavaDoc scaleTwo;
82         private BigDecimal JavaDoc scaleFour;
83
84         public Integer JavaDoc getId()
85         {
86             return id;
87         }
88
89         public void setId(Integer JavaDoc id)
90         {
91             this.id = id;
92         }
93
94         public BigDecimal JavaDoc getScaleTwo()
95         {
96             return scaleTwo;
97         }
98
99         public void setScaleTwo(BigDecimal JavaDoc scaleTwo)
100         {
101             this.scaleTwo = scaleTwo;
102         }
103
104         public double getScaleTwoDouble()
105         {
106             return scaleTwo.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
107         }
108
109         public BigDecimal JavaDoc getScaleFour()
110         {
111             return scaleFour;
112         }
113
114         public void setScaleFour(BigDecimal JavaDoc scalefour)
115         {
116             this.scaleFour = scalefour;
117         }
118
119         public double getScaleFourDouble()
120         {
121             return scaleFour.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
122         }
123
124         public String JavaDoc toString()
125         {
126             return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
127         }
128     }
129 }
130
Popular Tags