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.jdbc.annotations;
020:
021: import javax.persistence.EntityManager;
022:
023: import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
024: import org.apache.openjpa.jdbc.meta.ClassMapping;
025: import org.apache.openjpa.jdbc.meta.strats.NoneVersionStrategy;
026: import org.apache.openjpa.persistence.test.SingleEMFTestCase;
027:
028: /**
029: * Test for opt-lock
030: *
031: * @author Steve Kim
032: */
033: public class TestVersion extends SingleEMFTestCase {
034:
035: public void setUp() {
036: setUp(AnnoTest1.class, AnnoTest2.class, AnnoTest3.class,
037: Flat1.class, EmbedOwner.class, EmbedValue.class,
038: CLEAR_TABLES);
039:
040: EntityManager em = emf.createEntityManager();
041: em.getTransaction().begin();
042: AnnoTest1 test1 = new AnnoTest1();
043: test1.setPk(new Long(5));
044: test1.setBasic(50);
045: test1.setTransient(500);
046: em.persist(test1);
047:
048: AnnoTest2 test2 = new AnnoTest2();
049: test2.setPk1(5);
050: test2.setPk2("bar");
051: test2.setBasic("50");
052: em.persist(test2);
053:
054: AnnoTest3 test3 = new AnnoTest3();
055: test3.setPk(new Long(3));
056: test3.setBasic2(50);
057: em.persist(test3);
058: em.getTransaction().commit();
059: em.close();
060: }
061:
062: public void testVersionNumeric() {
063: EntityManager em1 = emf.createEntityManager();
064: em1.getTransaction().begin();
065: EntityManager em2 = emf.createEntityManager();
066: em2.getTransaction().begin();
067:
068: AnnoTest1 pc1 = em1.find(AnnoTest1.class, new Long(5));
069: AnnoTest1 pc2 = em2.find(AnnoTest1.class, new Long(5));
070: assertEquals(1, pc1.getVersion());
071: assertEquals(1, pc2.getVersion());
072: assertEquals(0, pc1.getTransient());
073: pc1.setBasic(75);
074: pc2.setBasic(75);
075: em1.getTransaction().commit();
076: em1.close();
077:
078: em1 = emf.createEntityManager();
079: pc1 = em1.find(AnnoTest1.class, new Long(5));
080: assertEquals(2, pc1.getVersion());
081: em1.close();
082: try {
083: em2.getTransaction().commit();
084: fail("Optimistic fail");
085: } catch (Exception e) {
086: } finally {
087: em2.close();
088: }
089: }
090:
091: public void testVersionTimestamp() {
092: // ensure that some time has passed
093: // since the records were created
094: try {
095: Thread.sleep(50);
096: } catch (InterruptedException e) {
097: // do nothing
098: }
099:
100: EntityManager em1 = emf.createEntityManager();
101: em1.getTransaction().begin();
102: EntityManager em2 = emf.createEntityManager();
103: em2.getTransaction().begin();
104:
105: AnnoTest2 pc1 = em1.find(AnnoTest2.class, new AnnoTest2.Oid(5,
106: "bar"));
107: AnnoTest2 pc2 = em2.find(AnnoTest2.class, new AnnoTest2.Oid(5,
108: "bar"));
109: assertNotNull(pc1.getVersion());
110: assertEquals(pc1.getVersion(), pc2.getVersion());
111: pc1.setBasic("75");
112: pc2.setBasic("75");
113: em1.getTransaction().commit();
114: em1.close();
115:
116: em1 = emf.createEntityManager();
117: pc1 = em1.find(AnnoTest2.class, new AnnoTest2.Oid(5, "bar"));
118: java.util.Date pc1Version = pc1.getVersion();
119: java.util.Date pc2Version = pc2.getVersion();
120: assertTrue(pc1Version.compareTo(pc2Version) > 0);
121: em1.close();
122: try {
123: em2.getTransaction().commit();
124: fail("Optimistic fail");
125: } catch (Exception e) {
126: } finally {
127: em2.close();
128: }
129: }
130:
131: public void testVersionSubclass() {
132: EntityManager em1 = emf.createEntityManager();
133: em1.getTransaction().begin();
134: EntityManager em2 = emf.createEntityManager();
135: em2.getTransaction().begin();
136:
137: AnnoTest3 pc1 = em1.find(AnnoTest3.class, new Long(3));
138: AnnoTest3 pc2 = em2.find(AnnoTest3.class, new Long(3));
139: assertEquals(1, pc1.getVersion());
140: assertEquals(1, pc2.getVersion());
141: pc1.setBasic2(75);
142: pc2.setBasic2(75);
143: em1.getTransaction().commit();
144: em1.close();
145:
146: em1 = emf.createEntityManager();
147: pc1 = em1.find(AnnoTest3.class, new Long(3));
148: assertEquals(2, pc1.getVersion());
149: em1.close();
150: try {
151: em2.getTransaction().commit();
152: fail("Optimistic fail");
153: } catch (Exception e) {
154: } finally {
155: em2.close();
156: }
157: }
158:
159: public void testVersionNoChange() {
160: EntityManager em = emf.createEntityManager();
161: em.getTransaction().begin();
162:
163: AnnoTest1 pc = em.find(AnnoTest1.class, new Long(5));
164: assertEquals(1, pc.getVersion());
165: assertEquals(0, pc.getTransient());
166: pc.setTransient(750);
167: em.getTransaction().commit();
168: em.close();
169:
170: em = emf.createEntityManager();
171: pc = em.find(AnnoTest1.class, new Long(5));
172: assertEquals(1, pc.getVersion());
173: assertEquals(0, pc.getTransient());
174: em.close();
175: }
176:
177: public void testNoDefaultVersionWithoutFieldOrColumn() {
178: ClassMapping cls = ((JDBCConfiguration) emf.getConfiguration())
179: .getMappingRepositoryInstance().getMapping(
180: EmbedOwner.class, null, true);
181: assertEquals(NoneVersionStrategy.getInstance(), cls
182: .getVersion().getStrategy());
183: assertEquals(0, cls.getVersion().getColumns().length);
184: }
185:
186: public void testVersionWithField() {
187: ClassMapping cls = ((JDBCConfiguration) emf.getConfiguration())
188: .getMappingRepositoryInstance().getMapping(
189: AnnoTest1.class, null, true);
190: assertTrue(NoneVersionStrategy.getInstance() != cls
191: .getVersion().getStrategy());
192: assertEquals(1, cls.getVersion().getColumns().length);
193: }
194: }
|