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

http://datanucleus-appengine.googlecode.com/ · Java · 255 lines · 208 code · 17 blank · 30 comment · 12 complexity · 081e5facdc36683edae446d816b5b61e MD5 · raw file

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