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.kernel;
020:
021: import javax.persistence.EntityManager;
022:
023: import org.apache.openjpa.persistence.kernel.common.apps.Entity1;
024: import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
025:
026: public class Test2EJBConcurrency extends AbstractTestCase {
027:
028: private Object _id = null;
029:
030: public Test2EJBConcurrency(String name) {
031: super (name, "kernelcactusapp");
032: }
033:
034: public void setUp() throws Exception {
035: deleteAll(Entity1.class);
036:
037: EntityManager em = currentEntityManager();
038: startTx(em);
039:
040: Entity1 b = new Entity1(3, "STRING", 10);
041: em.persist(b);
042: em.flush();
043:
044: endTx(em);
045: endEm(em);
046: }
047:
048: /**
049: * Test optimistic concurrency.
050: */
051: public void testOptConcurrency1() throws Exception {
052: EntityManager em1 = currentEntityManager();
053: startTx(em1);
054:
055: EntityManager em2 = currentEntityManager();
056: startTx(em2);
057:
058: Entity1 b1 = (Entity1) em1.find(Entity1.class, 3);
059: b1.setStringField("STRING2");
060: endTx(em1);
061: assertEquals("b1.getstringField is not STRING2 as exp.",
062: "STRING2", b1.getStringField());
063:
064: Entity1 b2 = (Entity1) em2.find(Entity1.class, 3);
065: assertEquals("b2.getstringField is not STRING2 as exp.",
066: "STRING2", b2.getStringField());
067: b2.setStringField("STRING3");
068: endTx(em2);
069: assertEquals("b2.getstringField is not STRING3 as exp.",
070: "STRING3", b2.getStringField());
071:
072: startTx(em1);
073: b1 = (Entity1) em1.find(Entity1.class, 3);
074: em1.refresh(b1);
075: assertEquals("b1.getstringField is not STRING3 as exp.",
076: "STRING2", b1.getStringField());
077: b1.setStringField("STRING4");
078: endTx(em1);
079:
080: b2 = (Entity1) em2.find(Entity1.class, 3);
081: assertEquals("b2.getstringField is not STRING3 as exp.",
082: "STRING3", b2.getStringField());
083:
084: endEm(em1);
085: endEm(em2);
086: }
087:
088: /**
089: * Test optimistic concurrency.
090: */
091: // public void testOptConcurrency2 ()
092: // throws Exception
093: // {
094: // EntityManager em1 = currentEntityManager();
095: // startTx(em1);
096: //
097: // EntityManager em2 = currentEntityManager();
098: // startTx(em2);
099: //
100: // Entity1 b1 = (Entity1) em1.find (Entity1.class, 3);
101: // Entity1 b2 = (Entity1) em2.find (Entity1.class, 3);
102: //
103: // assertEquals ("b1.getstringField is not STRING as exp.","STRING", b1.getStringField ());
104: // assertEquals ("b2.getstringField is not STRING as exp.","STRING", b2.getStringField ());
105: //
106: // b1.setStringField ("STRING2");
107: // endTx(em1);
108: // assertEquals ("b1.getstringField is not STRING as exp.","STRING2", b1.getStringField ());
109: //
110: // assertEquals ("b2.getstringField is not STRING as exp.","STRING", b2.getStringField ());
111: // b2.setStringField ("STRING3");
112: //
113: // try
114: // {
115: // endTx(em2);
116: // fail ("OL Violation");
117: // }
118: // catch (Exception ole)
119: // {
120: // // expected
121: // }
122: //
123: // rollbackTx(em2);
124: //
125: //
126: // b2 = (Entity1) em2.find (Entity1.class, 3);
127: // assertEquals ("b2.getstringField is not STRING2 as exp.","STRING2", b2.getStringField ());
128: //
129: // endEm(em1);
130: // endEm(em2);
131: // }
132: }
|