PageRenderTime 32ms CodeModel.GetById 11ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/com/google/appengine/datanucleus/query/JPQLQueryOwnedJoinTest.java

http://datanucleus-appengine.googlecode.com/
Java | 249 lines | 204 code | 17 blank | 28 comment | 10 complexity | 8e5d6a3a2613f3cef4aab3936e9c99c8 MD5 | raw file
  1/**********************************************************************
  2Copyright (c) 2009 Google Inc.
  3
  4Licensed under the Apache License, Version 2.0 (the "License");
  5you may not use this file except in compliance with the License.
  6You may obtain a copy of the License at
  7
  8http://www.apache.org/licenses/LICENSE-2.0
  9
 10Unless required by applicable law or agreed to in writing, software
 11distributed under the License is distributed on an "AS IS" BASIS,
 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13See the License for the specific language governing permissions and
 14limitations under the License.
 15**********************************************************************/
 16package com.google.appengine.datanucleus.query;
 17
 18import static com.google.appengine.datanucleus.test.jpa.OwnedJoinsJPA.Course;
 19import static com.google.appengine.datanucleus.test.jpa.OwnedJoinsJPA.Major;
 20import static com.google.appengine.datanucleus.test.jpa.OwnedJoinsJPA.newCourse;
 21import static com.google.appengine.datanucleus.test.jpa.OwnedJoinsJPA.newMajor;
 22import static com.google.appengine.datanucleus.test.jpa.OwnedJoinsJPA.newStudent;
 23
 24import com.google.appengine.datanucleus.jpa.JPATestCase;
 25import com.google.appengine.datanucleus.test.jpa.OwnedJoinsJPA.Student;
 26
 27import java.util.Collections;
 28
 29import javax.persistence.PersistenceException;
 30import javax.persistence.Query;
 31
 32/**
 33 * @author Max Ross <maxr@google.com>
 34 */
 35public class JPQLQueryOwnedJoinTest extends JPATestCase {
 36
 37  public void testJoinOnOneToMany_Simple() {
 38    Course course1 = newCourse("Biology");
 39    Course course2 = newCourse("Not Biology");
 40    Student student = newStudent(10, course1, course2);
 41    beginTxn();
 42    em.persist(student);
 43    commitTxn();
 44    beginTxn();
 45    Query q = em.createQuery(
 46        "select from " + Student.class.getName() + " s JOIN s.courses c where "
 47        + "c.department = 'Biology' and "
 48        + "s.grade = 10");
 49    
 50    assertEquals(student.getId(), ((Student) q.getSingleResult()).getId());
 51    commitTxn();
 52  }
 53
 54  public void testJoinOnOneToMany_LegalOrderBy() {
 55    Course course1 = newCourse("Biology");
 56    Course course2 = newCourse("Not Biology");
 57    Student student = newStudent(10, course1, course2);
 58    beginTxn();
 59    em.persist(student);
 60    commitTxn();
 61    beginTxn();
 62    Query q = em.createQuery(
 63        "select from " + Student.class.getName() + " s JOIN s.courses c where "
 64        + "c.department = 'Biology' and "
 65        + "s.grade = 10 order by s.courses asc");
 66    assertEquals(student.getId(), ((Student) q.getSingleResult()).getId());
 67    commitTxn();
 68  }
 69
 70  public void testJoinOnOneToMany_Offset() {
 71    Course course1 = newCourse("Biology");
 72    Course course2 = newCourse("Not Biology");
 73    Course course3 = newCourse("Biology");
 74    Course course4 = newCourse("Not Biology");
 75    Course course5 = newCourse("Biology");
 76    Course course6 = newCourse("Not Biology");
 77    Student student = newStudent(10, course1, course2);
 78    Student student2 = newStudent(11, course3, course4);
 79    Student student3 = newStudent(10, course5, course6);
 80    beginTxn();
 81    em.persist(student);
 82    commitTxn();
 83    beginTxn();
 84    em.persist(student2);
 85    commitTxn();
 86    beginTxn();
 87    em.persist(student3);
 88    commitTxn();
 89    beginTxn();
 90    Query q = em.createQuery(
 91        "select from " + Student.class.getName() + " s JOIN s.courses c where "
 92        + "c.department = 'Biology' and "
 93        + "s.grade = 10");
 94    q.setFirstResult(1);
 95    assertEquals(student3.getId(), ((Student) q.getSingleResult()).getId());
 96    q = em.createQuery(
 97        "select from " + Student.class.getName() + " s JOIN s.courses c where "
 98        + "c.department = 'Biology' and "
 99        + "s.grade = 10");
100    q.setFirstResult(2);
101    assertEquals(Collections.emptyList(), q.getResultList());
102    commitTxn();
103  }
104
105  public void testJoinOnOneToMany_Limit() {
106    Course course1 = newCourse("Biology");
107    Course course2 = newCourse("Not Biology");
108    Course course3 = newCourse("Biology");
109    Course course4 = newCourse("Not Biology");
110    Course course5 = newCourse("Biology");
111    Course course6 = newCourse("Not Biology");
112    Student student = newStudent(10, course1, course2);
113    Student student2 = newStudent(11, course3, course4);
114    Student student3 = newStudent(10, course5, course6);
115    beginTxn();
116    em.persist(student);
117    commitTxn();
118    beginTxn();
119    em.persist(student2);
120    commitTxn();
121    beginTxn();
122    em.persist(student3);
123    commitTxn();
124    beginTxn();
125    Query q = em.createQuery(
126        "select from " + Student.class.getName() + " s JOIN s.courses c where "
127        + "c.department = 'Biology' and "
128        + "s.grade = 10");
129    q.setMaxResults(1);
130    assertEquals(student.getId(), ((Student) q.getSingleResult()).getId());
131    q = em.createQuery(
132        "select from " + Student.class.getName() + " s JOIN s.courses c where "
133        + "c.department = 'Biology' and "
134        + "s.grade = 10");
135    q.setMaxResults(0);
136    assertEquals(Collections.emptyList(), q.getResultList());
137    commitTxn();
138  }
139
140  public void testJoinOnOneToOne_Simple() {
141    Major major1 = newMajor("Liberal Arts");
142    Major major2 = newMajor("Engineering");
143    Student student1 = newStudent(10, major1);
144    Student student2 = newStudent(10, major2);
145    beginTxn();
146    em.persist(student1);
147    commitTxn();
148    beginTxn();
149    em.persist(student2);
150    commitTxn();
151    beginTxn();
152    Query q = em.createQuery(
153        "select from " + Student.class.getName() + " s JOIN s.major m where "
154        + "m.school = 'Liberal Arts' and "
155        + "s.grade = 10");
156    assertEquals(student1.getId(), ((Student) q.getSingleResult()).getId());
157    commitTxn();
158  }
159
160  public void testJoinOnOneToMany_Illegal() {
161    beginTxn();
162    // all filters on parent must be equality filters
163    Query q = em.createQuery(
164        "select from " + Student.class.getName() + " s JOIN s.courses c where "
165        + "c.department = 'Biology' and "
166        + "s.grade > 10");
167    try {
168      q.getResultList();
169      fail("expected exception");
170    } catch (PersistenceException pe) {
171        if (pe.getCause() instanceof DatastoreQuery.UnsupportedDatastoreFeatureException) {
172          // good
173        }
174        else {
175          throw pe;
176        }
177    }
178
179    // all filters on child must be equality filters
180    q = em.createQuery(
181        "select from " + Student.class.getName() + " s JOIN s.courses c where "
182        + "c.department > 'Biology' and "
183        + "s.grade = 10");
184    try {
185      q.getResultList();
186      fail("expected exception");
187    } catch (PersistenceException pe) {
188        if (pe.getCause() instanceof DatastoreQuery.UnsupportedDatastoreFeatureException) {
189          // good
190        }
191        else {
192          throw pe;
193        }
194    }
195
196    // sort on parent can only be by join column in asc order
197    q = em.createQuery(
198        "select from " + Student.class.getName() + " s JOIN s.courses c where "
199        + "c.department = 'Biology' and "
200        + "s.grade = 10 order by s.grade");
201    try {
202      q.getResultList();
203      fail("expected exception");
204    } catch (PersistenceException pe) {
205        if (pe.getCause() instanceof DatastoreQuery.UnsupportedDatastoreFeatureException) {
206          // good
207        }
208        else {
209          throw pe;
210        }
211    }
212
213    // sort is by the join column but in the wrong order
214    q = em.createQuery(
215        "select from " + Student.class.getName() + " s JOIN s.courses c where "
216        + "c.department = 'Biology' and "
217        + "s.grade = 10 order by s.courses desc");
218    try {
219      q.getResultList();
220      fail("expected exception");
221    } catch (PersistenceException pe) {
222        if (pe.getCause() instanceof DatastoreQuery.UnsupportedDatastoreFeatureException) {
223          // good
224        }
225        else {
226          throw pe;
227        }
228    }
229
230    // can't sort by child property
231    q = em.createQuery(
232        "select from " + Student.class.getName() + " s JOIN s.courses c where "
233        + "c.department = 'Biology' and "
234        + "s.grade = 10 order by c.department");
235    try {
236      q.getResultList();
237      fail("expected exception");
238    } catch (PersistenceException pe) {
239        if (pe.getCause() instanceof DatastoreQuery.UnsupportedDatastoreFeatureException) {
240          // good
241        }
242        else {
243          throw pe;
244        }
245    }
246    commitTxn();
247  }
248
249}