001: /*
002: * TestRestoreValues.java
003: *
004: * Created on October 13, 2006, 4:48 PM
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009: /*
010: * Licensed to the Apache Software Foundation (ASF) under one
011: * or more contributor license agreements. See the NOTICE file
012: * distributed with this work for additional information
013: * regarding copyright ownership. The ASF licenses this file
014: * to you under the Apache License, Version 2.0 (the
015: * "License"); you may not use this file except in compliance
016: * with the License. You may obtain a copy of the License at
017: *
018: * http://www.apache.org/licenses/LICENSE-2.0
019: *
020: * Unless required by applicable law or agreed to in writing,
021: * software distributed under the License is distributed on an
022: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
023: * KIND, either express or implied. See the License for the
024: * specific language governing permissions and limitations
025: * under the License.
026: */
027: package org.apache.openjpa.persistence.kernel;
028:
029: import java.math.BigInteger;
030:
031: import org.apache.openjpa.persistence.kernel.common.apps.FetchGroupTestObject;
032:
033: import org.apache.openjpa.persistence.OpenJPAEntityManager;
034: import org.apache.openjpa.persistence.RestoreStateType;
035:
036: public class TestRestoreValues extends BaseKernelTest {
037:
038: private Object _oid = null;
039:
040: /**
041: * Creates a new instance of TestRestoreValues
042: */
043: public TestRestoreValues(String name) {
044: super (name);
045: }
046:
047: public void setUp() {
048: deleteAll(FetchGroupTestObject.class);
049:
050: FetchGroupTestObject pc = new FetchGroupTestObject();
051: pc.setA(1);
052: pc.setB("2");
053: pc.setC(new BigInteger("100"));
054:
055: OpenJPAEntityManager pm = getPM();
056: //pm.getTransaction().begin();
057: startTx(pm);
058: pm.persist(pc);
059: //pm.getTransaction().commit();
060: endTx(pm);
061: _oid = pm.getObjectId(pc);
062: endEm(pm);
063: }
064:
065: public OpenJPAEntityManager getPM() {
066: OpenJPAEntityManager pm = super .getPM();
067: pm.setOptimistic(true);
068: //pm.setRestoreValues(true);
069: //pm.setRetainValues(true);
070: pm.setRestoreState(RestoreStateType.ALL);
071: pm.setRetainState(true);
072:
073: return pm;
074: }
075:
076: public void testUnloadedFieldDirtiedBeforeLoadedField() {
077: OpenJPAEntityManager pm = getPM();
078: FetchGroupTestObject pc = (FetchGroupTestObject) pm.find(
079: FetchGroupTestObject.class, _oid);
080:
081: assertNotNull("fetch object is null", pc);
082:
083: startTx(pm);
084: pc.setB("3");
085: pc.setA(2);
086: rollbackTx(pm);
087:
088: assertEquals(1, pc.getA());
089: assertEquals("2", pc.getB());
090: assertEquals(new BigInteger("100"), pc.getC());
091: endEm(pm);
092: }
093:
094: public void testUnloadedFieldDirtiedAfterLoadedField() {
095: OpenJPAEntityManager pm = getPM();
096: FetchGroupTestObject pc = (FetchGroupTestObject) pm.find(
097: FetchGroupTestObject.class, _oid);
098:
099: startTx(pm);
100: pc.setA(2);
101: pc.setB("3");
102: rollbackTx(pm);
103:
104: assertEquals(1, pc.getA());
105: assertEquals("2", pc.getB());
106: assertEquals(new BigInteger("100"), pc.getC());
107: endEm(pm);
108: }
109:
110: public void testLoadedFieldDirtiedAfterLoadedField() {
111: OpenJPAEntityManager pm = getPM();
112: FetchGroupTestObject pc = (FetchGroupTestObject) pm.find(
113: FetchGroupTestObject.class, _oid);
114:
115: startTx(pm);
116: pc.setA(2);
117: pc.getB();
118: pc.setB("3");
119: rollbackTx(pm);
120:
121: assertEquals(1, pc.getA());
122: assertEquals("2", pc.getB());
123: assertEquals(new BigInteger("100"), pc.getC());
124: endEm(pm);
125: }
126:
127: public void testNewInstanceUnmodifiedInTransaction() {
128: FetchGroupTestObject pc = new FetchGroupTestObject();
129: pc.setA(2);
130: pc.setB("3");
131:
132: OpenJPAEntityManager pm = getPM();
133: startTx(pm);
134: pm.persist(pc);
135: rollbackTx(pm);
136: assertEquals(2, pc.getA());
137: assertEquals("3", pc.getB());
138: endEm(pm);
139: }
140: }
|