001: package org.apache.ojb.broker;
002:
003: import java.math.BigDecimal;
004:
005: import org.apache.commons.lang.builder.ToStringBuilder;
006: import org.apache.commons.lang.builder.ToStringStyle;
007: import org.apache.ojb.junit.PBTestCase;
008:
009: /**
010: * Test case to check the accuracy of {@link BigDecimal} values stored in DB.
011: *
012: * @author <a href="mailto:arminw@apache.org">Armin Waibel</a>
013: * @version $Id: NumberAccuracyTest.java,v 1.1.2.1 2005/12/22 22:57:37 tomdz Exp $
014: */
015: public class NumberAccuracyTest extends PBTestCase {
016: public static void main(String[] args) {
017: junit.textui.TestRunner
018: .main(new String[] { NumberAccuracyTest.class.getName() });
019: }
020:
021: public NumberAccuracyTest() {
022: }
023:
024: public NumberAccuracyTest(String name) {
025: super (name);
026: }
027:
028: public void testBigDecimal() {
029: BigDecimal scaleTwo = new BigDecimal("17.34554");
030: BigDecimal scaleFour = new BigDecimal("67.345567");
031: // round half up values
032: scaleTwo = scaleTwo.setScale(2, BigDecimal.ROUND_HALF_UP);
033: scaleFour = scaleFour.setScale(4, BigDecimal.ROUND_HALF_UP);
034:
035: NumberObject no = new NumberObject();
036: no.setScaleTwo(scaleTwo);
037: no.setScaleFour(scaleFour);
038:
039: // System.out.println("# " + no.getScaleTwoDouble());
040: // System.out.println("# " + no.getScaleFourDouble());
041: broker.beginTransaction();
042: broker.store(no);
043: broker.commitTransaction();
044:
045: Identity oidNo = new Identity(no, broker);
046: broker.clearCache();
047: NumberObject newNo = (NumberObject) broker
048: .getObjectByIdentity(oidNo);
049: // System.out.println("# " + newNo.getScaleTwoDouble());
050: // System.out.println("# " + newNo.getScaleFourDouble());
051: assertEquals(17.35d, newNo.getScaleTwoDouble(), 0.001);
052: assertEquals(67.3456, newNo.getScaleFourDouble(), 0.00001);
053: assertTrue(0 == scaleTwo.compareTo(newNo.getScaleTwo()));
054: assertTrue(0 == scaleFour.compareTo(newNo.getScaleFour()));
055:
056: BigDecimal newScaleTwo = newNo.getScaleTwo().multiply(
057: new BigDecimal("10"));
058: newNo.setScaleTwo(newScaleTwo);
059: BigDecimal newScaleFour = newNo.getScaleFour().multiply(
060: new BigDecimal("10"));
061: newNo.setScaleFour(newScaleFour);
062: broker.beginTransaction();
063: broker.store(newNo);
064: broker.commitTransaction();
065:
066: broker.clearCache();
067: NumberObject newNo_2 = (NumberObject) broker
068: .getObjectByIdentity(oidNo);
069: // System.out.println("# " + newNo.getScaleTwoDouble());
070: // System.out.println("# " + newNo.getScaleFourDouble());
071: assertEquals(173.5d, newNo_2.getScaleTwoDouble(), 0.1);
072: assertEquals(673.455, newNo_2.getScaleFourDouble(), 0.001);
073: assertTrue(0 == newScaleTwo.compareTo(newNo_2.getScaleTwo()));
074: assertTrue(0 == newScaleFour.compareTo(newNo_2.getScaleFour()));
075: }
076:
077: public static class NumberObject {
078: private Integer id;
079: private BigDecimal scaleTwo;
080: private BigDecimal scaleFour;
081:
082: public Integer getId() {
083: return id;
084: }
085:
086: public void setId(Integer id) {
087: this .id = id;
088: }
089:
090: public BigDecimal getScaleTwo() {
091: return scaleTwo;
092: }
093:
094: public void setScaleTwo(BigDecimal scaleTwo) {
095: this .scaleTwo = scaleTwo;
096: }
097:
098: public double getScaleTwoDouble() {
099: return scaleTwo.setScale(2, BigDecimal.ROUND_HALF_UP)
100: .doubleValue();
101: }
102:
103: public BigDecimal getScaleFour() {
104: return scaleFour;
105: }
106:
107: public void setScaleFour(BigDecimal scalefour) {
108: this .scaleFour = scalefour;
109: }
110:
111: public double getScaleFourDouble() {
112: return scaleFour.setScale(4, BigDecimal.ROUND_HALF_UP)
113: .doubleValue();
114: }
115:
116: public String toString() {
117: return ToStringBuilder.reflectionToString(this,
118: ToStringStyle.MULTI_LINE_STYLE);
119: }
120: }
121: }
|