01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19: package org.apache.openjpa.persistence.kernel;
20:
21: import java.util.List;
22: import javax.persistence.EntityManager;
23:
24: import org.apache.openjpa.persistence.kernel.common.apps.RuntimeTest1;
25: import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
26:
27: public class TestEJBRetainValues extends AbstractTestCase {
28:
29: public TestEJBRetainValues(String testName) {
30: super (testName, "kernelcactusapp");
31: }
32:
33: public void setUp() {
34: deleteAll(RuntimeTest1.class);
35: }
36:
37: /**
38: * tests the behavior of the same object being viewed from
39: * different PersistenceManagers with RetainValues set to true
40: */
41: public void testRetainValues() {
42: // persist the object with field == "x"
43: RuntimeTest1 test = new RuntimeTest1();
44: test.setStringField("x");
45: EntityManager pm = currentEntityManager();
46: startTx(pm);
47: pm.persist(test);
48: endTx(pm);
49:
50: // find the object in a different PM
51: EntityManager pm2 = currentEntityManager();
52: List l = (buildSelectQuery(pm2, "x"));
53: assertNotNull("l is null in testRetainValues", l);
54: RuntimeTest1 sameThing = (RuntimeTest1) l.iterator().next();
55:
56: assertEquals("x", sameThing.getStringField());
57: assertEquals("x", test.getStringField());
58:
59: // set the second object field to "y"
60: startTx(pm2);
61: sameThing.setStringField("y");
62: endTx(pm2);
63: assertEquals("y", sameThing.getStringField());
64: assertEquals("x", test.getStringField());
65:
66: // do some searching in the first PM
67: List l2 = buildSelectQuery(pm, "x");
68: assertEquals(0, l2.size()); //should be zero if retainvalue is set to true but that is kodo specific.
69:
70: List l3 = buildSelectQuery(pm, "y");
71: assertEquals(1, l3.size());
72: assertEquals(test, l3.iterator().next());
73: assertEquals("x", test.getStringField());
74:
75: endEm(pm2);
76: endEm(pm);
77: }
78:
79: public static List buildSelectQuery(EntityManager em, String param) {
80: return em
81: .createQuery(
82: "SELECT c FROM RuntimeTest1 c where c.stringField = :username")
83: .setParameter("username", param).getResultList();
84: }
85: }
|