001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.persistence.annotations;
020:
021: import javax.persistence.*;
022:
023: import org.apache.openjpa.jdbc.conf.*;
024: import org.apache.openjpa.jdbc.meta.*;
025: import org.apache.openjpa.jdbc.meta.strats.*;
026:
027: import org.apache.openjpa.persistence.annotations.common.apps.annotApp.annotype.*;
028:
029: import org.apache.openjpa.persistence.OpenJPAEntityManager;
030: import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
031: import org.apache.openjpa.persistence.OpenJPAPersistence;
032:
033: /*
034: Test for opt-lock
035:
036: @author Steve Kim
037: */
038: public class TestVersion extends AnnotationTestCase {
039: private Object oid;
040:
041: private Object oid1;
042:
043: private Object oid2;
044:
045: public TestVersion(String name) {
046: super (name, "annotationcactusapp");
047: }
048:
049: public void setUp() {
050: new AnnoTest1();
051: new AnnoTest2();
052: new AnnoTest3();
053:
054: deleteAll(AnnoTest1.class);
055: deleteAll(AnnoTest2.class);
056:
057: OpenJPAEntityManager em = currentEntityManager();
058: startTx(em);
059: AnnoTest1 test1 = new AnnoTest1();
060: test1.setPk(new Long(5));
061: test1.setBasic(50);
062: test1.setTransient(500);
063: em.persist(test1);
064:
065: AnnoTest2 test2 = new AnnoTest2();
066: test2.setPk1(5);
067: test2.setPk2("bar");
068: test2.setBasic("50");
069: em.persist(test2);
070:
071: AnnoTest3 test3 = new AnnoTest3();
072: test3.setPk(new Long(3));
073: test3.setBasic2(50);
074: em.persist(test3);
075: oid = em.getObjectId(test1);
076: oid1 = em.getObjectId(test2);
077: oid2 = em.getObjectId(test3);
078:
079: endTx(em);
080: endEm(em);
081: }
082:
083: /*
084: * Fix Me aokeke -- Testcases causes deadlock during runtime CR307216 is used to track this issue.
085: */
086: public void testVersionNumeric() {
087: OpenJPAEntityManager em1 = currentEntityManager();
088: startTx(em1);
089: EntityManager em2 = getEmf().createEntityManager();
090:
091: AnnoTest1 pc1 = em1.find(AnnoTest1.class, oid);
092: AnnoTest1 pc2 = em2.find(AnnoTest1.class, oid);
093: assertEquals(1, pc1.getVersion());
094: assertEquals(1, pc2.getVersion());
095: assertEquals(0, pc1.getTransient());
096: pc1.setBasic(75);
097:
098: endTx(em1);
099: endEm(em1);
100:
101: em2.getTransaction().begin();
102: pc2.setBasic(75);
103: em1 = (OpenJPAEntityManager) currentEntityManager();
104: pc1 = em1.find(AnnoTest1.class, oid);
105: assertEquals(2, pc1.getVersion());
106: endEm(em1);
107: try {
108: em2.getTransaction().commit();
109: fail("Optimistic fail");
110: } catch (RuntimeException re) {
111: } catch (Exception e) {
112: } finally {
113: em2.close();
114: }
115: }
116:
117: public void testVersionTimestamp() {
118: OpenJPAEntityManager em1 = currentEntityManager();
119: startTx(em1);
120: OpenJPAEntityManager em2 = getEmf().createEntityManager();
121:
122: AnnoTest2 pc1 = em1.find(AnnoTest2.class, oid1);
123: AnnoTest2 pc2 = em2.find(AnnoTest2.class, oid1);
124: assertNotNull(pc1.getVersion());
125: assertEquals(pc1.getVersion(), pc2.getVersion());
126: pc1.setBasic("75");
127:
128: endTx(em1);
129: endEm(em1);
130:
131: em2.getTransaction().begin();
132: pc2.setBasic("75");
133:
134: em1 = (OpenJPAEntityManager) currentEntityManager();
135: pc1 = em1.find(AnnoTest2.class, oid1);
136: assertTrue(pc1.getVersion().compareTo(pc2.getVersion()) > 0);
137: endEm(em1);
138: try {
139: em2.getTransaction().commit();
140: fail("Optimistic fail");
141: } catch (RuntimeException re) {
142: } catch (Exception e) {
143: } finally {
144: em2.close();
145: }
146: }
147:
148: public void testVersionSubclass() {
149: OpenJPAEntityManager em1 = currentEntityManager();
150: startTx(em1);
151: OpenJPAEntityManager em2 = getEmf().createEntityManager();
152:
153: AnnoTest3 pc1 = em1.find(AnnoTest3.class, oid2);
154: AnnoTest3 pc2 = em2.find(AnnoTest3.class, oid2);
155: assertEquals(1, pc1.getVersion());
156: assertEquals(1, pc2.getVersion());
157: pc1.setBasic2(75);
158:
159: endTx(em1);
160: endEm(em1);
161:
162: em2.getTransaction().begin();
163: pc2.setBasic2(75);
164:
165: em1 = (OpenJPAEntityManager) currentEntityManager();
166: pc1 = em1.find(AnnoTest3.class, oid2);
167: assertEquals(2, pc1.getVersion());
168: endEm(em1);
169: try {
170: em2.getTransaction().commit();
171: fail("Optimistic fail");
172: } catch (RuntimeException re) {
173: } catch (Exception e) {
174: } finally {
175: em2.close();
176: }
177: }
178:
179: public void testVersionNoChange() {
180: OpenJPAEntityManager em = currentEntityManager();
181: startTx(em);
182:
183: AnnoTest1 pc = em.find(AnnoTest1.class, oid);
184: assertEquals(1, pc.getVersion());
185: assertEquals(0, pc.getTransient());
186: pc.setTransient(750);
187: endTx(em);
188: endEm(em);
189:
190: em = (OpenJPAEntityManager) currentEntityManager();
191: pc = em.find(AnnoTest1.class, oid);
192: assertEquals(1, pc.getVersion());
193: assertEquals(0, pc.getTransient());
194: endEm(em);
195: }
196:
197: public void testNoDefaultVersionWithoutFieldOrColumn() {
198: OpenJPAEntityManager pm = (OpenJPAEntityManager) currentEntityManager();
199: ClassMapping cls = ((JDBCConfigurationImpl) ((OpenJPAEntityManagerSPI) OpenJPAPersistence
200: .cast(pm)).getConfiguration())
201: .getMappingRepositoryInstance().getMapping(
202: EmbedOwner.class, null, true);
203: assertEquals(NoneVersionStrategy.getInstance(), cls
204: .getVersion().getStrategy());
205: assertEquals(0, cls.getVersion().getColumns().length);
206: endEm(pm);
207: }
208:
209: public void testVersionWithField() {
210: OpenJPAEntityManager pm = (OpenJPAEntityManager) currentEntityManager();
211: ClassMapping cls = ((JDBCConfigurationImpl) ((OpenJPAEntityManagerSPI) OpenJPAPersistence
212: .cast(pm)).getConfiguration())
213: .getMappingRepositoryInstance().getMapping(
214: AnnoTest1.class, null, true);
215: assertTrue(NoneVersionStrategy.getInstance() != cls
216: .getVersion().getStrategy());
217: assertEquals(1, cls.getVersion().getColumns().length);
218: endEm(pm);
219: }
220: }
|