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.slice;
020:
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import javax.persistence.EntityManager;
025:
026: /**
027: * Tests basic create, read, update and delete operations.
028: *
029: * @author Pinaki Poddar
030: *
031: */
032: public class TestBasic extends SliceTestCase {
033:
034: protected String getPersistenceUnitName() {
035: return "slice";
036: }
037:
038: public void setUp() throws Exception {
039: super .setUp(PObject.class, Person.class, Address.class,
040: CLEAR_TABLES);
041: }
042:
043: /**
044: * Persist N objects.
045: */
046: List<PObject> create(int N) {
047: List<PObject> pcs = new ArrayList<PObject>();
048: EntityManager em = emf.createEntityManager();
049: em.getTransaction().begin();
050: for (int i = 0; i < N; i++) {
051: PObject pc = new PObject();
052: pcs.add(pc);
053: em.persist(pc);
054: pc.setValue(i);
055: }
056: em.getTransaction().commit();
057: em.clear();
058: return pcs;
059: }
060:
061: /**
062: * Create a single object.
063: */
064: PObject create() {
065: return create(1).get(0);
066: }
067:
068: /**
069: * Delete a single object by EntityManager.remove()
070: */
071: public void testDelete() {
072: int N = 10;
073: create(N);
074: EntityManager em = emf.createEntityManager();
075: em.getTransaction().begin();
076: int before = count(PObject.class);
077: List all = em.createQuery("SELECT p FROM PObject p")
078: .getResultList();
079: assertFalse(all.isEmpty());
080: em.remove(all.get(0));
081: em.getTransaction().commit();
082:
083: int after = count(PObject.class);
084: assertEquals(before - 1, after);
085: }
086:
087: /**
088: * Delete in bulk by query.
089: */
090: public void testBulkDelete() {
091: EntityManager em = emf.createEntityManager();
092: em.getTransaction().begin();
093: int c = count(PObject.class);
094: int d = em.createQuery("DELETE FROM PObject p").executeUpdate();
095: assertEquals(c, d);
096: em.getTransaction().commit();
097: c = count(PObject.class);
098: assertEquals(0, c);
099:
100: }
101:
102: /**
103: * Store and find the same object.
104: */
105: public void testFind() {
106: PObject pc = create();
107: int value = pc.getValue();
108:
109: EntityManager em = emf.createEntityManager();
110: em.getTransaction().begin();
111: PObject pc2 = em.find(PObject.class, pc.getId());
112: assertNotNull(pc2);
113: assertNotEquals(pc, pc2);
114: assertEquals(pc.getId(), pc2.getId());
115: assertEquals(value, pc2.getValue());
116: }
117:
118: public void testPersistIndependentObjects() {
119: int before = count(PObject.class);
120: EntityManager em = emf.createEntityManager();
121: int N = 2;
122: em.getTransaction().begin();
123: for (int i = 0; i < N; i++)
124: em.persist(new PObject());
125: em.getTransaction().commit();
126: em.clear();
127: int after = count(PObject.class);
128: assertEquals(before + N, after);
129: }
130:
131: public void testPersistConnectedObjectGraph() {
132: Person p1 = new Person("A");
133: Person p2 = new Person("B");
134: Person p3 = new Person("C");
135: Address a1 = new Address("Rome", 12345);
136: Address a2 = new Address("San Francisco", 23456);
137: Address a3 = new Address("New York", 34567);
138: p1.setAddress(a1);
139: p2.setAddress(a2);
140: p3.setAddress(a3);
141:
142: EntityManager em = emf.createEntityManager();
143: em.getTransaction().begin();
144: em.persist(p1);
145: em.persist(p2);
146: em.persist(p3);
147: em.getTransaction().commit();
148:
149: em.clear();
150:
151: em = emf.createEntityManager();
152: em.getTransaction().begin();
153: List<Person> persons = em.createQuery(
154: "SELECT p FROM Person p WHERE p.name=?1").setParameter(
155: 1, "A").getResultList();
156: List<Address> addresses = em.createQuery(
157: "SELECT a FROM Address a").getResultList();
158: for (Address pc : addresses) {
159: assertNotNull(pc.getCity());
160: assertNotNull(pc.getOwner().getName());
161: }
162: for (Person pc : persons) {
163: assertNotNull(pc.getName());
164: assertNotNull(pc.getAddress().getCity());
165: }
166: em.getTransaction().rollback();
167: }
168:
169: /**
170: * Merge only works if the distribution policy assigns the correct slice
171: * from which the instance was fetched.
172: */
173: public void testMerge() {
174: PObject pc = create(1).get(0);
175: int value = pc.getValue();
176: pc.setValue(value + 1);
177: assertNotNull(pc);
178: EntityManager em = emf.createEntityManager();
179: em.getTransaction().begin();
180: PObject pc2 = em.merge(pc);
181: em.getTransaction().commit();
182: em.clear();
183:
184: assertNotNull(pc2);
185: assertNotEquals(pc, pc2);
186: assertEquals(pc.getId(), pc2.getId());
187: assertEquals(value + 1, pc2.getValue());
188: }
189:
190: }
|