001: /*
002: * TestQueries.java
003: *
004: * Created on October 13, 2006, 4:27 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.Collection;
031: import java.util.Iterator;
032:
033: import org.apache.openjpa.persistence.kernel.common.apps.RuntimeTest1;
034: import org.apache.openjpa.persistence.kernel.common.apps.RuntimeTest2;
035:
036: import org.apache.openjpa.persistence.Extent;
037: import org.apache.openjpa.persistence.OpenJPAEntityManager;
038: import org.apache.openjpa.persistence.OpenJPAQuery;
039:
040: public class TestQueries extends BaseKernelTest {
041:
042: private static final int CHILD_COUNT = 3;
043: private int id = 10000;
044:
045: public TestQueries(String name) {
046: super (name);
047: }
048:
049: public TestQueries() {
050: }
051:
052: public void setUp() throws Exception {
053: super .setUp();
054: deleteAll(RuntimeTest1.class);
055: }
056:
057: public void testSimpleQuery() {
058: OpenJPAEntityManager pm = getPM();
059:
060: persist(newRuntimeTest1("SimpleQueryA", 50), pm);
061: persist(newRuntimeTest2("SimpleQueryB", 50), pm);
062:
063: Collection results = runQuery(RuntimeTest1.class, false,
064: "stringField = \'SimpleQueryA\'", pm);
065: assertEquals(1, results.size());
066:
067: results = runQuery(RuntimeTest1.class, true, "intField1 = 50",
068: pm);
069: assertEquals(2, results.size());
070:
071: results = runQuery(RuntimeTest2.class, true, "intField1 = 50",
072: pm);
073: assertEquals(1, results.size());
074:
075: results = runQuery(RuntimeTest2.class, false, "intField1 = 50",
076: pm);
077: assertEquals(1, results.size());
078:
079: endEm(pm);
080: }
081:
082: public void testAndQuery() {
083: String query = "intField1 > 10 AND o.intField1 < 50";
084:
085: for (int currentCount = 1; currentCount < 15; currentCount++) {
086: OpenJPAEntityManager pm = getPM();
087:
088: // make sure that none exist
089: deleteByQuery(RuntimeTest1.class, false, query, pm);
090:
091: startTx(pm);
092: for (int i = 1; i <= currentCount; i++)
093: pm.persist(newRuntimeTest1("AndQueryTest", 30 + i));
094: endTx(pm);
095:
096: // verify that the query works.
097: Collection results = runQuery(RuntimeTest1.class, false,
098: query, pm);
099: assertEquals(currentCount, results.size());
100: endEm(pm);
101: }
102: }
103:
104: public void testRelationQuery() {
105: relationQuery(10, "JOE", 20);
106: relationQuery(99, "BOB", 2);
107: relationQuery(3, "MARTHA", 1);
108: relationQuery(5, "brenda", 43);
109: relationQuery(43, "SarA", 55);
110: }
111:
112: public void relationQuery(int intField, String stringField,
113: int count) {
114: OpenJPAEntityManager pm = getPM();
115:
116: String query = "selfOneOne.intField1 = " + intField
117: + " AND o.selfOneOne.stringField = '" + stringField
118: + "'";
119: deleteByQuery(RuntimeTest1.class, true, query, pm);
120:
121: // we go up the to max count, adding to the people, and validate
122: // with a query each time.
123: for (int currentcount = 0; currentcount <= count; currentcount++) {
124: if (currentcount != 0)
125: persist(newRuntimeTest2(stringField, intField), pm);
126:
127: Collection results = runQuery(RuntimeTest1.class, true,
128: query, pm);
129: assertEquals("query (" + query + ") failed to yield "
130: + currentcount + " instances", currentcount
131: * CHILD_COUNT, results.size());
132: }
133:
134: endEm(pm);
135: }
136:
137: public void testQueryExecuteThrowsExceptionWhenNoNTR() {
138: OpenJPAEntityManager pm = getPM();
139: startTx(pm);
140: pm.persist(newRuntimeTest1("Query", 10));
141: endTx(pm);
142:
143: pm.setNontransactionalRead(false);
144: // OpenJPAQuery q = pm.createNativeQuery("",RuntimeTest1.class);
145: OpenJPAQuery q = pm.createQuery("SELECT o FROM RuntimeTest1 o");
146: try {
147: Collection c = (Collection) q.getResultList();
148: fail("Query.execute() should have thrown a JDOException when "
149: + "PM is outside a Transaction and NTR==false");
150: } catch (Exception jdoe) {
151: // good
152: startTx(pm);
153: q.getResultList();
154: rollbackTx(pm);
155: }
156: }
157:
158: /**
159: * Delete the results of a query, so we can reinsert and test.
160: */
161: private void deleteByQuery(Class type, boolean subs, String filter,
162: OpenJPAEntityManager pm) {
163: startTx(pm);
164: Extent extent = pm.createExtent(type, subs);
165: //FIXME jthomas
166: // OpenJPAQuery query = pm.newQuery(extent, filter);
167: // Collection items = (Collection) query.execute();
168: String cstrng = type.getSimpleName();
169:
170: OpenJPAQuery query = pm.createQuery("SELECT o FROM " + cstrng
171: + " o WHERE o." + filter);
172: query.setSubclasses(subs);
173: Collection items = (Collection) query.getResultList();
174: for (Iterator i = items.iterator(); i.hasNext();)
175: pm.remove(i.next());
176:
177: //FIXME jthomas
178: //query = pm.newQuery(extent, filter);
179:
180: query = pm.createQuery("SELECT o FROM " + cstrng
181: + " o WHERE o." + filter);
182: query.setSubclasses(subs);
183: items = query.getResultList();
184: endTx(pm);
185: assertEquals("after deleting from query (" + query
186: + "), there should have been zero items", 0, items
187: .size());
188: }
189:
190: private Collection runQuery(Class type, boolean subs,
191: String filter, OpenJPAEntityManager pm) {
192: Extent extent = pm.createExtent(type, subs);
193: //FIXME jthomas
194: //Query query = pm.newQuery(extent, filter);
195: String cstrng = type.getName();
196: OpenJPAQuery query = pm.createQuery("SELECT o FROM " + cstrng
197: + " o WHERE o." + filter);
198: query.setSubclasses(subs);
199: Collection results = (Collection) query.getResultList();
200: return results;
201: }
202:
203: private void persist(RuntimeTest1 parent, OpenJPAEntityManager pm) {
204: RuntimeTest1 child;
205: for (int i = 0; i < CHILD_COUNT; i++) {
206: child = newRuntimeTest1("CHILD" + i, i * 10);
207: child.setSelfOneOne(parent);
208: parent.getSelfOneMany().add(child);
209: }
210:
211: startTx(pm);
212: pm.persist(parent);
213: endTx(pm);
214: }
215:
216: private RuntimeTest1 newRuntimeTest1(String stringField,
217: int intField) {
218: RuntimeTest1 pc = new RuntimeTest1(stringField, intField);
219: pc.setIntField(id++);
220: return pc;
221: }
222:
223: private RuntimeTest2 newRuntimeTest2(String stringField,
224: int intField) {
225: RuntimeTest2 pc = new RuntimeTest2(stringField, intField);
226: pc.setIntField(id++);
227: return pc;
228: }
229: }
|