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.ComplexEmbeddedPC;
024: import org.apache.openjpa.persistence.kernel.common.apps.EmbeddedOwnerPC;
025: import org.apache.openjpa.persistence.kernel.common.apps.EmbeddedPC;
026:
027: public class TestEJBEmbedded extends BaseKernelTest {
028:
029: private Object _oid1 = null;
030: private Object _oid2 = null;
031:
032: EmbeddedOwnerPC.EmbKey id1;
033: EmbeddedOwnerPC.EmbKey id2;
034:
035: public TestEJBEmbedded(String name) {
036: super (name);
037: }
038:
039: @SuppressWarnings("unchecked")
040: public void setUp() {
041: deleteAll(EmbeddedOwnerPC.class);
042: deleteAll(EmbeddedPC.class);
043:
044: EntityManager em = currentEntityManager();
045: startTx(em);
046:
047: EmbeddedOwnerPC pc1 = new EmbeddedOwnerPC(1, 2);
048: pc1.setStringField("string1");
049: EmbeddedPC embed1 = new EmbeddedPC();
050: embed1.setStringField("embedString1");
051: embed1.setIntField(1);
052: pc1.setEmbedded(embed1);
053:
054: EmbeddedPC embed2 = new EmbeddedPC();
055: embed2.setStringField("embedString2");
056: embed2.setIntField(2);
057:
058: EmbeddedOwnerPC pc2 = new EmbeddedOwnerPC(3, 4);
059:
060: em.persist(pc1);
061: em.persist(pc2);
062:
063: endTx(em);
064:
065: id1 = new EmbeddedOwnerPC.EmbKey("1:2");
066:
067: id2 = new EmbeddedOwnerPC.EmbKey();
068: id2.id1 = 3;
069: id2.id2 = 4;
070:
071: endEm(em);
072: }
073:
074: public void testInsert() {
075: EntityManager pm = currentEntityManager();
076: startTx(pm);
077:
078: EmbeddedOwnerPC pc = pm.find(EmbeddedOwnerPC.class, id1);
079: EmbeddedOwnerPC pc2 = pm.find(EmbeddedOwnerPC.class, id2);
080: assertNotNull("pc is null in testInsert", pc);
081: assertNotNull("p2 is null in testInsert", pc2);
082:
083: assertEquals("string1", pc.getStringField());
084: assertNotNull(pc.getEmbedded());
085: assertEquals("embedString1", pc.getEmbedded().getStringField());
086: assertEquals(1, pc.getEmbedded().getIntField());
087:
088: endTx(pm);
089: endEm(pm);
090: }
091:
092: public void testDelete() {
093: EntityManager pm = currentEntityManager();
094: startTx(pm);
095: EmbeddedOwnerPC pc = (EmbeddedOwnerPC) pm.find(
096: EmbeddedOwnerPC.class, id1);
097:
098: assertNotNull("pc is null in testDelete", pc);
099:
100: EmbeddedPC embed = pc.getEmbedded();
101:
102: pc.setEmbedded(null);
103:
104: endTx(pm);
105:
106: endEm(pm);
107:
108: pm = currentEntityManager();
109: startTx(pm);
110: pc = (EmbeddedOwnerPC) pm.find(EmbeddedOwnerPC.class, id1);
111:
112: assertEquals("the expt strng is not string1", "string1", pc
113: .getStringField());
114: assertNull("pc's embedded is null", pc.getEmbedded());
115: endTx(pm);
116: endEm(pm);
117: }
118:
119: public void testUpdateRetain() {
120: EntityManager pm = currentEntityManager();
121: startTx(pm);
122: EmbeddedOwnerPC pc = (EmbeddedOwnerPC) pm.find(
123: EmbeddedOwnerPC.class, id1);
124:
125: assertNotNull("pc is null testUpdateRetain", pc);
126:
127: pc.setStringField("string2");
128: pc.getEmbedded().setStringField("embedString2");
129:
130: endTx(pm);
131:
132: assertEquals("string2", pc.getStringField());
133: assertNotNull(pc.getEmbedded());
134: assertEquals("embedString2", pc.getEmbedded().getStringField());
135: assertEquals(1, pc.getEmbedded().getIntField());
136:
137: endEm(pm);
138: }
139:
140: public void testReplace() {
141: EntityManager pm = currentEntityManager();
142: startTx(pm);
143:
144: EmbeddedOwnerPC pc = pm.find(EmbeddedOwnerPC.class, id1);
145:
146: EmbeddedPC newEmbed = new EmbeddedPC();
147: newEmbed.setStringField("embedString2");
148: pc.setEmbedded(newEmbed);
149:
150: ComplexEmbeddedPC newComplexEmbed = new ComplexEmbeddedPC();
151: newEmbed = new EmbeddedPC();
152: newEmbed.setStringField("embedString3");
153: newComplexEmbed.setStringField("complexEmbedString3");
154: newComplexEmbed.setOwnerField(new EmbeddedOwnerPC(5, 6));
155: pc.setComplexEmbedded(newComplexEmbed);
156:
157: endTx(pm);
158: endEm(pm);
159:
160: pm = currentEntityManager();
161: startTx(pm);
162:
163: pc = pm.find(EmbeddedOwnerPC.class, id1);
164: assertEquals("string1", pc.getStringField());
165: assertNotNull(pc.getEmbedded());
166: assertEquals("the exp strng is not embedString1",
167: "embedString1", pc.getEmbedded().getStringField());
168: assertEquals("intfield is not 1", 1, pc.getEmbedded()
169: .getIntField());
170:
171: endTx(pm);
172: endEm(pm);
173: }
174:
175: public void testShare() {
176: EntityManager pm = currentEntityManager();
177: startTx(pm);
178:
179: EmbeddedOwnerPC pc1 = pm.find(EmbeddedOwnerPC.class, id1);
180: assertNotNull("pc1 is null in testshare", pc1);
181:
182: EmbeddedOwnerPC pc2 = pm.find(EmbeddedOwnerPC.class, id2);
183: assertNotNull("pc2 is null in testshare", pc2);
184:
185: EmbeddedPC embed1 = pc1.getEmbedded();
186:
187: pm.persist(embed1);
188: Integer oid = new Integer(1);
189: endTx(pm);
190:
191: assertEquals("embedString1", embed1.getStringField());
192: assertEquals("embedString1", pc1.getEmbedded().getStringField());
193:
194: endEm(pm);
195:
196: // make sure the changes stick
197: pm = currentEntityManager();
198: startTx(pm);
199: pc1 = pm.find(EmbeddedOwnerPC.class, id1);
200: assertNotNull("pc1 is null in testshare 2nd find", pc1);
201: pc2 = pm.find(EmbeddedOwnerPC.class, id2);
202: assertNotNull("pc2 is null in testshare 2nd find", pc2);
203: embed1 = pm.find(EmbeddedPC.class, oid.intValue());
204:
205: if (embed1 != null) {
206: assertEquals("embedString1", embed1.getStringField());
207: assertEquals("embedString1", pc1.getEmbedded()
208: .getStringField());
209: }
210:
211: endTx(pm);
212: endEm(pm);
213: }
214:
215: public void testOptimisticLocking2() {
216: EntityManager pm1 = getPM(true, false);
217: startTx(pm1);
218:
219: EmbeddedOwnerPC pc1 = pm1.find(EmbeddedOwnerPC.class, id1);
220: assertNotNull("pc1 is null in testoptlock2", pc1);
221:
222: EntityManager pm2 = currentEntityManager();
223:
224: startTx(pm2);
225: EmbeddedOwnerPC pc2 = pm1.find(EmbeddedOwnerPC.class, id1);
226:
227: assertNotNull("pc2 is null in testoptlock2", pc2);
228:
229: EmbeddedPC embed2 = pc2.getEmbedded();
230: embed2.setStringField("xxxx");
231:
232: endTx(pm2);
233: endEm(pm2);
234:
235: EmbeddedPC embed1 = pc1.getEmbedded();
236: embed1.setStringField("yyyy");
237: try {
238: endTx(pm1);
239: fail("Should have thrown an OL exception.");
240: } catch (Exception ove) {
241: } finally {
242: endEm(pm1);
243: }
244: }
245: }
|