001: /*
002: * TestRetainValuesInOptimistic.java
003: *
004: * Created on October 16, 2006, 10:18 AM
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.util.HashMap;
031: import java.util.Locale;
032: import java.util.Map;
033:
034: import org.apache.openjpa.persistence.kernel.common.apps.RuntimeTest1;
035:
036: import org.apache.openjpa.persistence.OpenJPAEntityManager;
037: import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
038:
039: public class TestRetainValuesInOptimistic extends BaseKernelTest {
040:
041: private Object _oid = null;
042:
043: /**
044: * Creates a new instance of TestRetainValuesInOptimistic
045: */
046: public TestRetainValuesInOptimistic() {
047: }
048:
049: public TestRetainValuesInOptimistic(String test) {
050: super (test);
051: }
052:
053: public void setUp() throws Exception {
054: super .setUp();
055:
056: deleteAll(RuntimeTest1.class);
057:
058: OpenJPAEntityManager pm = getPM();
059: startTx(pm);
060: RuntimeTest1 pc = new RuntimeTest1("str1", 1);
061: pm.persist(pc);
062: endTx(pm);
063: _oid = pm.getObjectId(pc);
064: endEm(pm);
065: }
066:
067: @Override
068: protected void addProperties(Map map) {
069: super .addProperties(map);
070: map.put("openjpa.Log", "SQL=TRACE, Runtime=TRACE");
071: }
072:
073: public void testRetain() {
074: clearTest(true);
075: optLockTest(true);
076: }
077:
078: public void testNotRetain() {
079: clearTest(false);
080: optLockTest(false);
081: }
082:
083: private void clearTest(boolean retain) {
084: OpenJPAEntityManager pm = getPM(retain);
085: OpenJPAEntityManagerFactory pmf = pm.getEntityManagerFactory();
086: RuntimeTest1 pc = (RuntimeTest1) pm.find(RuntimeTest1.class,
087: _oid);
088:
089: OpenJPAEntityManager pm2 = getPM();
090: RuntimeTest1 pc2 = (RuntimeTest1) pm2.find(RuntimeTest1.class,
091: _oid);
092: startTx(pm2);
093: pc2.setStringField("str2");
094: pc2.setIntField1(2);
095: endTx(pm2);
096: endEm(pm2);
097:
098: startTx(pm);
099: // tickle the object so that it enters the transaction
100: pc.setLocaleField(Locale.CHINA);
101: assertEquals((retain) ? "str1" : "str2", pc.getStringField());
102: assertEquals((retain) ? 1 : 2, pc.getIntField1());
103: try {
104: endTx(pm);
105: if (retain)
106: fail("Should have caused OL violation");
107: } catch (RuntimeException re) {
108: if (!retain)
109: throw re;
110: } catch (Exception e) {
111: //
112: }
113:
114: endEm(pm);
115:
116: // make sure everything stuck
117: if (!retain) {
118: pm = getPM();
119: pc = (RuntimeTest1) pm.find(RuntimeTest1.class, _oid);
120: assertEquals("str2", pc.getStringField());
121: assertEquals(2, pc.getIntField1());
122: endEm(pm);
123: try {
124: pmf.close();
125: } catch (Exception e) {
126: // consumme exceptions ... other PMs might be open and
127: // active on this PMF.
128: }
129: }
130: }
131:
132: private void optLockTest(boolean retain) {
133: OpenJPAEntityManager pm1 = getPM(retain);
134: OpenJPAEntityManagerFactory pmf = pm1.getEntityManagerFactory();
135:
136: startTx(pm1);
137: RuntimeTest1 pc1 = (RuntimeTest1) pm1.find(RuntimeTest1.class,
138: _oid);
139: endTx(pm1);
140:
141: OpenJPAEntityManager pm2 = getPM(retain);
142: startTx(pm2);
143: RuntimeTest1 pc2 = (RuntimeTest1) pm2.find(RuntimeTest1.class,
144: _oid);
145: pc2.setStringField("str3");
146: pc2.setIntField1(3);
147: endTx(pm2);
148:
149: startTx(pm1);
150: pc1.setStringField("str4");
151: pc1.setIntField1(4);
152: try {
153: endTx(pm1);
154: if (retain)
155: fail("Expected opt lock error.");
156: } catch (Exception jove) {
157: if (!retain)
158: fail("Caught opt lock error.");
159: }
160:
161: pm1.close();
162: endEm(pm2);
163: if (retain) {
164: try {
165: pmf.close();
166: } catch (Exception e) {
167: // maybe other PMs are open...
168: }
169: }
170: }
171:
172: private OpenJPAEntityManager getPM(boolean retain) {
173: OpenJPAEntityManager pm;
174: Map props = new HashMap();
175: props.put("openjpa.AutoClear", "all");
176:
177: if (retain)
178: pm = getPM(true, true);
179: else {
180: OpenJPAEntityManagerFactory pmf = getEmf(props);
181:
182: pm = (OpenJPAEntityManager) pmf.createEntityManager();
183: pm.setOptimistic(true);
184: pm.setRetainState(true);
185: }
186: return pm;
187: }
188: }
|