/tests/com/google/appengine/datanucleus/jpa/JPADatastoreBridgeTest.java

http://datanucleus-appengine.googlecode.com/ · Java · 105 lines · 73 code · 12 blank · 20 comment · 3 complexity · b63bd476cf25cb2b1aab0b1d074630ef MD5 · raw file

  1. /*
  2. * Copyright (C) 2010 Google Inc
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.google.appengine.datanucleus.jpa;
  17. import com.google.appengine.api.datastore.Entity;
  18. import com.google.appengine.api.datastore.FetchOptions;
  19. import com.google.appengine.api.datastore.Query;
  20. import com.google.appengine.api.datastore.QueryResultIterable;
  21. import com.google.appengine.api.datastore.QueryResultList;
  22. import com.google.appengine.datanucleus.JPADatastoreBridge;
  23. import com.google.appengine.datanucleus.test.jpa.Book;
  24. import java.util.List;
  25. /**
  26. * @author Max Ross <max.ross@gmail.com>
  27. */
  28. public class JPADatastoreBridgeTest extends JPATestCase {
  29. public void testConvertQueryResultList_Empty() {
  30. QueryResultList<Entity> result =
  31. ds.prepare(new Query("blarg")).asQueryResultList(FetchOptions.Builder.withLimit(10));
  32. JPADatastoreBridge bridge = new JPADatastoreBridge();
  33. List<Book> books = bridge.toJPAResult(em, Book.class, result);
  34. assertEquals(0, books.size());
  35. }
  36. public void testConvertQueryResultList() {
  37. for (int i = 0; i < 5; i++) {
  38. Entity e = Book.newBookEntity("harold" + i, "isbn", "the title", 2004);
  39. ds.put(e);
  40. }
  41. QueryResultList<Entity> result =
  42. ds.prepare(new Query("Book")).asQueryResultList(FetchOptions.Builder.withLimit(10));
  43. JPADatastoreBridge bridge = new JPADatastoreBridge();
  44. List<Book> books = bridge.toJPAResult(em, Book.class, result);
  45. assertEquals(5, books.size());
  46. String id = books.get(0).getId();
  47. // make sure these books are connected
  48. beginTxn();
  49. books.get(0).setTitle("different title");
  50. commitTxn();
  51. beginTxn();
  52. Book f = em.find(Book.class, id);
  53. assertEquals("different title", f.getTitle());
  54. commitTxn();
  55. deleteAll();
  56. }
  57. public void testConvertQueryResultIterable() {
  58. for (int i = 0; i < 5; i++) {
  59. Entity e = Book.newBookEntity("harold" + i, "isbn", "the title", 2004);
  60. ds.put(e);
  61. }
  62. QueryResultIterable<Entity> result =
  63. ds.prepare(new Query("Book")).asQueryResultIterable();
  64. JPADatastoreBridge bridge = new JPADatastoreBridge();
  65. List<Book> books = bridge.toJPAResult(em, Book.class, result);
  66. assertEquals(5, books.size());
  67. String id = books.get(0).getId();
  68. // make sure these books are connected
  69. beginTxn();
  70. books.get(0).setTitle("different title");
  71. commitTxn();
  72. beginTxn();
  73. Book f = em.find(Book.class, id);
  74. assertEquals("different title", f.getTitle());
  75. commitTxn();
  76. deleteAll();
  77. }
  78. public void testAccessResultsAfterClose() {
  79. for (int i = 0; i < 3; i++) {
  80. Entity e = Book.newBookEntity("this", "that", "the other");
  81. ds.put(e);
  82. }
  83. QueryResultIterable<Entity> result =
  84. ds.prepare(new Query("Book")).asQueryResultIterable();
  85. beginTxn();
  86. JPADatastoreBridge bridge = new JPADatastoreBridge();
  87. List<Book> books = bridge.toJPAResult(em, Book.class, result);
  88. commitTxn();
  89. em.close();
  90. assertEquals(3, books.size());
  91. deleteAll();
  92. }
  93. }