001: /*
002: * TestExtents2.java
003: *
004: * Created on October 10, 2006, 5:30 PM
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: /*
011: * Licensed to the Apache Software Foundation (ASF) under one
012: * or more contributor license agreements. See the NOTICE file
013: * distributed with this work for additional information
014: * regarding copyright ownership. The ASF licenses this file
015: * to you under the Apache License, Version 2.0 (the
016: * "License"); you may not use this file except in compliance
017: * with the License. You may obtain a copy of the License at
018: *
019: * http://www.apache.org/licenses/LICENSE-2.0
020: *
021: * Unless required by applicable law or agreed to in writing,
022: * software distributed under the License is distributed on an
023: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
024: * KIND, either express or implied. See the License for the
025: * specific language governing permissions and limitations
026: * under the License.
027: */
028: package org.apache.openjpa.persistence.kernel;
029:
030: import java.util.Iterator;
031:
032: import org.apache.openjpa.persistence.kernel.common.apps.RuntimeTest1;
033: import org.apache.openjpa.persistence.kernel.common.apps.RuntimeTest2;
034: import org.apache.openjpa.persistence.kernel.common.apps.RuntimeTest3;
035:
036: import org.apache.openjpa.persistence.Extent;
037: import org.apache.openjpa.persistence.OpenJPAEntityManager;
038:
039: public class TestExtents2 extends BaseKernelTest {
040:
041: private Object _oid1 = null;
042: private Object _oid2 = null;
043: private Object _oid3 = null;
044:
045: public TestExtents2(String name) {
046: super (name);
047: }
048:
049: /**
050: * Creates a new instance of TestExtents2
051: */
052: public TestExtents2() {
053: }
054:
055: public void setUp() {
056: deleteAll(RuntimeTest1.class);
057:
058: RuntimeTest1 test1 = new RuntimeTest1();
059: test1.setIntField(1);
060: RuntimeTest2 test2 = new RuntimeTest2();
061: test2.setIntField(2);
062: test2.setIntField2(2);
063: RuntimeTest3 test3 = new RuntimeTest3();
064: test3.setIntField(3);
065: test3.setIntField3(3);
066:
067: OpenJPAEntityManager pm = getPM();
068: startTx(pm);
069: pm.persist(test1);
070: pm.persist(test2);
071: pm.persist(test3);
072: _oid1 = pm.getObjectId(test1);
073: _oid2 = pm.getObjectId(test2);
074: _oid3 = pm.getObjectId(test3);
075: endTx(pm);
076: endEm(pm);
077: }
078:
079: public void testProperties() {
080: OpenJPAEntityManager pm = getPM();
081: Extent ext = pm.createExtent(RuntimeTest2.class, false);
082: assertEquals(pm, ext.getEntityManager());
083: assertEquals(RuntimeTest2.class, ext.getElementClass());
084: assertTrue(!ext.hasSubclasses());
085:
086: ext = pm.createExtent(RuntimeTest1.class, true);
087: assertEquals(pm, ext.getEntityManager());
088: assertEquals(RuntimeTest1.class, ext.getElementClass());
089: assertTrue(ext.hasSubclasses());
090: }
091:
092: public void testNoSubclasses() {
093: OpenJPAEntityManager pm = getPM();
094: Extent ext = pm.createExtent(RuntimeTest1.class, false);
095: Iterator itr = ext.iterator();
096: assertTrue(itr.hasNext());
097: assertEquals(_oid1, pm.getObjectId(itr.next()));
098: assertTrue(!itr.hasNext());
099: ext.closeAll();
100: }
101:
102: public void testSubclasses() {
103: OpenJPAEntityManager pm = getPM();
104: Extent ext = pm.createExtent(RuntimeTest1.class, true);
105:
106: int test1Count = 0;
107: int test2Count = 0;
108: int test3Count = 0;
109: Object next;
110: for (Iterator itr = ext.iterator(); itr.hasNext();) {
111: next = pm.getObjectId(itr.next());
112: if (_oid1.equals(next))
113: test1Count++;
114: else if (_oid2.equals(next))
115: test2Count++;
116: else if (_oid3.equals(next))
117: test3Count++;
118: }
119: ext.closeAll();
120: assertEquals(1, test1Count);
121: assertEquals(1, test2Count);
122: assertEquals(1, test3Count);
123: }
124:
125: public void testContainsNewlyPersisted() {
126: RuntimeTest1 test1 = new RuntimeTest1();
127: RuntimeTest1 test2 = new RuntimeTest2();
128:
129: OpenJPAEntityManager pm = getPM();
130:
131: // pcl: 14 Oct 2003: default in 3.0 is now true, but this test
132: // assumes false somewhere.
133:
134: pm.setIgnoreChanges(false);
135: startTx(pm);
136: try {
137: pm.persist(test1);
138: pm.persist(test2);
139: Object newOid = pm.getObjectId(test1);
140:
141: Extent ext = pm.createExtent(RuntimeTest1.class, false);
142: boolean foundOid1 = false;
143: boolean foundNew = false;
144: Object next;
145: for (Iterator itr = ext.iterator(); itr.hasNext();) {
146: next = pm.getObjectId(itr.next());
147: if (_oid1.equals(next))
148: foundOid1 = true;
149: else if (newOid.equals(next))
150: foundNew = true;
151: else
152: fail("Bad object in extent.");
153: }
154: ext.closeAll();
155: assertTrue(foundOid1);
156: assertTrue(foundNew);
157: } finally {
158: rollbackTx(pm);
159: }
160: }
161:
162: public void testNotContainsNewlyDeleted() {
163: OpenJPAEntityManager pm = getPM();
164:
165: // pcl: 14 Oct 2003: default in 3.0 is now true, but this test
166: // assumes false somewhere.
167: pm.setIgnoreChanges(false);
168: startTx(pm);
169: try {
170: RuntimeTest2 test2 = (RuntimeTest2) pm.find(
171: RuntimeTest2.class, _oid2);
172: pm.remove(test2);
173: RuntimeTest1 test1 = new RuntimeTest1();
174: pm.persist(test1);
175: pm.remove(test1);
176:
177: Extent ext = pm.createExtent(RuntimeTest1.class, true);
178: boolean foundOid1 = false;
179: boolean foundOid3 = false;
180: Object next;
181: for (Iterator itr = ext.iterator(); itr.hasNext();) {
182: next = pm.getObjectId(itr.next());
183: if (_oid1.equals(next))
184: foundOid1 = true;
185: else if (_oid3.equals(next))
186: foundOid3 = true;
187: else
188: fail("Bad object in extent.");
189: }
190: ext.closeAll();
191: assertTrue(foundOid1);
192: assertTrue(foundOid3);
193: } finally {
194: rollbackTx(pm);
195: }
196: }
197: }
|