1 package org.apache.ojb.broker; 2 3 import java.math.BigDecimal ; 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 15 public class NumberAccuracyTest extends PBTestCase 16 { 17 public static void main(String [] args) 18 { 19 junit.textui.TestRunner.main(new String [] {NumberAccuracyTest.class.getName()}); 20 } 21 22 public NumberAccuracyTest() 23 { 24 } 25 26 public NumberAccuracyTest(String name) 27 { 28 super(name); 29 } 30 31 public void testBigDecimal() 32 { 33 BigDecimal scaleTwo = new BigDecimal ("17.34554"); 34 BigDecimal scaleFour = new BigDecimal ("67.345567"); 35 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 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 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 newScaleTwo = newNo.getScaleTwo().multiply(new BigDecimal ("10")); 61 newNo.setScaleTwo(newScaleTwo); 62 BigDecimal newScaleFour = newNo.getScaleFour().multiply(new BigDecimal ("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 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 id; 81 private BigDecimal scaleTwo; 82 private BigDecimal scaleFour; 83 84 public Integer getId() 85 { 86 return id; 87 } 88 89 public void setId(Integer id) 90 { 91 this.id = id; 92 } 93 94 public BigDecimal getScaleTwo() 95 { 96 return scaleTwo; 97 } 98 99 public void setScaleTwo(BigDecimal 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 getScaleFour() 110 { 111 return scaleFour; 112 } 113 114 public void setScaleFour(BigDecimal 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 toString() 125 { 126 return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); 127 } 128 } 129 } 130 | Popular Tags |