PageRenderTime 51ms CodeModel.GetById 1ms app.highlight 44ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://datanucleus-appengine.googlecode.com/
Java | 254 lines | 207 code | 19 blank | 28 comment | 11 complexity | 22d555b20e6375b3eeb662aeac62df14 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 com.google.appengine.api.datastore.Entity;
 19import com.google.appengine.api.datastore.EntityNotFoundException;
 20import com.google.appengine.api.datastore.KeyFactory;
 21import com.google.appengine.datanucleus.jpa.JPATestCase;
 22import com.google.appengine.datanucleus.test.jpa.UnownedJoinsJPA.Course;
 23import com.google.appengine.datanucleus.test.jpa.UnownedJoinsJPA.Major;
 24import com.google.appengine.datanucleus.test.jpa.UnownedJoinsJPA.Student;
 25
 26import static com.google.appengine.datanucleus.test.jpa.UnownedJoinsJPA.newCourse;
 27import static com.google.appengine.datanucleus.test.jpa.UnownedJoinsJPA.newMajor;
 28import static com.google.appengine.datanucleus.test.jpa.UnownedJoinsJPA.newStudent;
 29
 30import java.util.Collections;
 31
 32import javax.persistence.PersistenceException;
 33import javax.persistence.Query;
 34
 35/**
 36 * @author Max Ross <maxr@google.com>
 37 */
 38public class JPQLQueryUnownedJoinTest extends JPATestCase {
 39
 40  public void testTransientMeansTransient() throws EntityNotFoundException {
 41    Student student = new Student();
 42    beginTxn();
 43    em.persist(student);
 44    commitTxn();
 45    Entity studentEntity = ds.get(KeyFactory.createKey(kindForClass(Student.class), student.getId()));
 46    assertEquals(3, studentEntity.getProperties().size());
 47    assertTrue(studentEntity.hasProperty("courses"));
 48    assertTrue(studentEntity.hasProperty("grade"));
 49    assertTrue(studentEntity.hasProperty("major"));
 50  }
 51
 52  public void testJoinOnOneToMany_Simple() {
 53    Course course1 = newCourse("Biology");
 54    Course course2 = newCourse("Not Biology");
 55    persistInTxn(course1, course2);
 56    Student student = newStudent(10, course1, course2);
 57    beginTxn();
 58    em.persist(student);
 59    commitTxn();
 60    beginTxn();
 61    Query q = em.createQuery(
 62        "select from " + Student.class.getName() + " s JOIN s.coursesAlias c where "
 63        + "c.department = 'Biology' and "
 64        + "s.grade = 10");
 65    
 66    assertEquals(student.getId(), ((Student) q.getSingleResult()).getId());
 67    commitTxn();
 68  }
 69
 70  public void testJoinOnOneToMany_LegalOrderBy() {
 71    Course course1 = newCourse("Biology");
 72    Course course2 = newCourse("Not Biology");
 73    persistInTxn(course1, course2);
 74    Student student = newStudent(10, course1, course2);
 75    persistInTxn(student);
 76    beginTxn();
 77    Query q = em.createQuery(
 78        "select from " + Student.class.getName() + " s JOIN s.coursesAlias c where "
 79        + "c.department = 'Biology' and "
 80        + "s.grade = 10 order by s.courses asc");
 81    assertEquals(student.getId(), ((Student) q.getSingleResult()).getId());
 82    commitTxn();
 83  }
 84
 85  private void persistInTxn(Object... objects) {
 86    for (Object o : objects) {
 87      beginTxn();
 88      em.persist(o);
 89      commitTxn();
 90    }
 91  }
 92
 93  public void testJoinOnOneToMany_Offset() {
 94    Course course1 = newCourse("Biology");
 95    Course course2 = newCourse("Not Biology");
 96    Course course3 = newCourse("Biology");
 97    Course course4 = newCourse("Not Biology");
 98    Course course5 = newCourse("Biology");
 99    Course course6 = newCourse("Not Biology");
100    persistInTxn(course1, course2, course3, course4, course5, course6);
101    Student student = newStudent(10, course1, course2);
102    Student student2 = newStudent(11, course3, course4);
103    Student student3 = newStudent(10, course5, course6);
104    persistInTxn(student, student2, student3);
105    beginTxn();
106    Query q = em.createQuery(
107        "select from " + Student.class.getName() + " s JOIN s.coursesAlias c where "
108        + "c.department = 'Biology' and "
109        + "s.grade = 10");
110    q.setFirstResult(1);
111    assertEquals(student3.getId(), ((Student) q.getSingleResult()).getId());
112    q = em.createQuery(
113        "select from " + Student.class.getName() + " s JOIN s.coursesAlias c where "
114        + "c.department = 'Biology' and "
115        + "s.grade = 10");
116    q.setFirstResult(2);
117    assertEquals(Collections.emptyList(), q.getResultList());
118    commitTxn();
119  }
120
121  public void testJoinOnOneToMany_Limit() {
122    Course course1 = newCourse("Biology");
123    Course course2 = newCourse("Not Biology");
124    Course course3 = newCourse("Biology");
125    Course course4 = newCourse("Not Biology");
126    Course course5 = newCourse("Biology");
127    Course course6 = newCourse("Not Biology");
128    persistInTxn(course1, course2, course3, course4, course5, course6);
129    Student student = newStudent(10, course1, course2);
130    Student student2 = newStudent(11, course3, course4);
131    Student student3 = newStudent(10, course5, course6);
132    persistInTxn(student, student2, student3);
133    beginTxn();
134    Query q = em.createQuery(
135        "select from " + Student.class.getName() + " s JOIN s.coursesAlias c where "
136        + "c.department = 'Biology' and "
137        + "s.grade = 10");
138    q.setMaxResults(1);
139    assertEquals(student.getId(), ((Student) q.getSingleResult()).getId());
140    q = em.createQuery(
141        "select from " + Student.class.getName() + " s JOIN s.coursesAlias c where "
142        + "c.department = 'Biology' and "
143        + "s.grade = 10");
144    q.setMaxResults(0);
145    assertEquals(Collections.emptyList(), q.getResultList());
146    commitTxn();
147  }
148
149  public void testJoinOnOneToOne_Simple() {
150    Major major1 = newMajor("Liberal Arts");
151    Major major2 = newMajor("Engineering");
152    persistInTxn(major1, major2);
153    Student student1 = newStudent(10, major1);
154    Student student2 = newStudent(10, major2);
155    persistInTxn(student1, student2);
156    beginTxn();
157    Query q = em.createQuery(
158        "select from " + Student.class.getName() + " s JOIN s.majorAlias m where "
159        + "m.school = 'Liberal Arts' and "
160        + "s.grade = 10");
161    assertEquals(student1.getId(), ((Student) q.getSingleResult()).getId());
162    commitTxn();
163  }
164
165  public void testJoinOnOneToMany_Illegal() {
166    beginTxn();
167    // all filters on parent must be equality filters
168    Query q = em.createQuery(
169        "select from " + Student.class.getName() + " s JOIN s.coursesAlias c where "
170        + "c.department = 'Biology' and "
171        + "s.grade > 10");
172    try {
173      q.getResultList();
174      fail("expected exception");
175    } catch (PersistenceException pe) {
176        if (pe.getCause() instanceof DatastoreQuery.UnsupportedDatastoreFeatureException) {
177            // good
178        }
179        else {
180          throw pe;
181        }
182    }
183
184    // all filters on child must be equality filters
185    q = em.createQuery(
186        "select from " + Student.class.getName() + " s JOIN s.coursesAlias c where "
187        + "c.department > 'Biology' and "
188        + "s.grade = 10");
189    try {
190      q.getResultList();
191      fail("expected exception");
192    } catch (PersistenceException pe) {
193        if (pe.getCause() instanceof DatastoreQuery.UnsupportedDatastoreFeatureException) {
194            // good
195        }
196        else {
197          throw pe;
198        }
199    }
200
201    // sort on parent can only be by join column in asc order
202    q = em.createQuery(
203        "select from " + Student.class.getName() + " s JOIN s.coursesAlias c where "
204        + "c.department = 'Biology' and "
205        + "s.grade = 10 order by s.grade");
206    try {
207      q.getResultList();
208      fail("expected exception");
209    } catch (PersistenceException pe) {
210        if (pe.getCause() instanceof DatastoreQuery.UnsupportedDatastoreFeatureException) {
211            // good
212        }
213        else {
214          throw pe;
215        }
216    }
217
218    // sort is by the join column but in the wrong order
219    q = em.createQuery(
220        "select from " + Student.class.getName() + " s JOIN s.coursesAlias c where "
221        + "c.department = 'Biology' and "
222        + "s.grade = 10 order by s.coursesAlias desc");
223    try {
224      q.getResultList();
225      fail("expected exception");
226    } catch (PersistenceException pe) {
227        if (pe.getCause() instanceof DatastoreQuery.UnsupportedDatastoreFeatureException) {
228            // good
229        }
230        else {
231          throw pe;
232        }
233    }
234
235    // can't sort by child property
236    q = em.createQuery(
237        "select from " + Student.class.getName() + " s JOIN s.coursesAlias c where "
238        + "c.department = 'Biology' and "
239        + "s.grade = 10 order by c.department");
240    try {
241      q.getResultList();
242      fail("expected exception");
243    } catch (PersistenceException pe) {
244        if (pe.getCause() instanceof DatastoreQuery.UnsupportedDatastoreFeatureException) {
245            // good
246        }
247        else {
248          throw pe;
249        }
250    }
251    commitTxn();
252  }
253
254}