001: /*
002: * TestBigDecimals.java
003: *
004: * Created on October 9, 2006, 6:07 PM
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: /*
011: * Licensed to the Apache Software Foundation (ASF) under one
012: * or more contributor license agreements. See the NOTICE file
013: * distributed with this work for additional information
014: * regarding copyright ownership. The ASF licenses this file
015: * to you under the Apache License, Version 2.0 (the
016: * "License"); you may not use this file except in compliance
017: * with the License. You may obtain a copy of the License at
018: *
019: * http://www.apache.org/licenses/LICENSE-2.0
020: *
021: * Unless required by applicable law or agreed to in writing,
022: * software distributed under the License is distributed on an
023: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
024: * KIND, either express or implied. See the License for the
025: * specific language governing permissions and limitations
026: * under the License.
027: */
028: package org.apache.openjpa.persistence.kernel;
029:
030: import java.math.BigDecimal;
031:
032: import org.apache.openjpa.persistence.kernel.common.apps.AllFieldTypesTest;
033: import junit.framework.AssertionFailedError;
034:
035: import org.apache.openjpa.persistence.OpenJPAEntityManager;
036:
037: public class TestBigDecimals extends BaseKernelTest {
038:
039: /**
040: * Creates a new instance of TestBigDecimals
041: */
042: public TestBigDecimals() {
043: }
044:
045: public TestBigDecimals(String name) {
046: super (name);
047: }
048:
049: public void testBigDecimalDataIntegrity() throws Exception {
050: try {
051: BigDecimal bd = new BigDecimal(Math.random() * 10000000
052: + "");
053: bd = bd.setScale(100);
054: for (int i = 0; i < 50; i++) {
055: bd = bd.movePointLeft(1);
056: bigDecimalTest(bd);
057: }
058: } catch (AssertionFailedError e) {
059: bug(3, e, "Precision loss for BigDecimals");
060: }
061: }
062:
063: // FixMe aokeke: Passes but takes a long time --commenting for resource sake
064: // public void testBigBigDecimals()
065: // throws Exception {
066: // try {
067: // BigDecimal bd = new BigDecimal("1234567890."
068: // + "12345678901234567890123456789012345678901234567890"
069: // + "12345678901234567890123456789012345678901234567890");
070: //
071: // bigDecimalTest(bd);
072: // } catch (AssertionFailedError e) {
073: // bug(3, e, "Precision loss for BigDecimals");
074: // }
075: // }
076:
077: public void bigDecimalTest(final BigDecimal bd) {
078: OpenJPAEntityManager pm = null, pm2 = null;
079:
080: try {
081: pm = getPM();
082: startTx(pm);
083: AllFieldTypesTest aftt = new AllFieldTypesTest();
084: aftt.setTestBigDecimal(bd);
085: pm.persist(aftt);
086: endTx(pm);
087: Object id = pm.getObjectId(aftt);
088: pm.evict(aftt);
089:
090: pm2 = getPM();
091: startTx(pm);
092: AllFieldTypesTest aftt2 = (AllFieldTypesTest) pm2
093: .getObjectId(id);
094:
095: // why wouldn't they be two different objects?
096: assertTrue("identitcal field values", bd != aftt2
097: .getTestBigDecimal());
098:
099: // this should always succeed
100: assertEquals(bd, aftt2.getTestBigDecimal().setScale(
101: bd.scale()));
102:
103: // this will fail if we are losing scale
104: assertEquals(bd, aftt2.getTestBigDecimal());
105:
106: rollbackTx(pm);
107: } catch (Throwable afe) {
108: bug(3, afe, "floating point precision loss");
109: } finally {
110: if (pm != null) {
111: //if (pm.getTransaction().isActive())
112: //rollbackTx(pm,());
113: endEm(pm);
114: }
115:
116: if (pm2 != null) {
117: //if (pm2.getTransaction().isActive())
118: //rollbackTx(pm2,());
119: //pm2.close();
120: endEm(pm2);
121: }
122: }
123: }
124: }
|