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

http://datanucleus-appengine.googlecode.com/ · Java · 184 lines · 128 code · 16 blank · 40 comment · 0 complexity · 1b741b3ee17ca1dbdfd7113023b646b2 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.jpa;
  14. import com.google.appengine.api.datastore.DatastoreServiceConfig;
  15. import com.google.appengine.api.datastore.Entity;
  16. import com.google.appengine.api.datastore.EntityNotFoundException;
  17. import com.google.appengine.api.datastore.KeyFactory;
  18. import com.google.appengine.api.datastore.KeyRange;
  19. import com.google.appengine.datanucleus.BaseDatastoreServiceDelegate;
  20. import com.google.appengine.datanucleus.DatastoreServiceFactoryInternal;
  21. import com.google.appengine.datanucleus.SequenceTestLock;
  22. import com.google.appengine.datanucleus.Utils;
  23. import com.google.appengine.datanucleus.test.jpa.SequenceExamplesJPA.HasSequence;
  24. import com.google.appengine.datanucleus.test.jpa.SequenceExamplesJPA.HasSequenceOnNonPkFields;
  25. import com.google.appengine.datanucleus.test.jpa.SequenceExamplesJPA.HasSequenceWithNoSequenceName;
  26. import com.google.appengine.datanucleus.test.jpa.SequenceExamplesJPA.HasSequenceWithSequenceGenerator;
  27. import com.google.appengine.datanucleus.test.jpa.SequenceExamplesJPA.HasSequenceWithUnencodedStringPk;
  28. import com.google.appengine.datanucleus.valuegenerator.SequenceGenerator;
  29. import java.util.List;
  30. /**
  31. * @author Max Ross <maxr@google.com>
  32. */
  33. public class JPASequenceTest extends JPATestCase {
  34. private final List<String> sequenceNames = Utils.newArrayList();
  35. private final List<Long> sequenceBatchSizes = Utils.newArrayList();
  36. @Override
  37. protected void setUp() throws Exception {
  38. super.setUp();
  39. DatastoreServiceConfig config = getStoreManager().getDefaultDatastoreServiceConfigForReads();
  40. DatastoreServiceFactoryInternal.setDatastoreService(
  41. new BaseDatastoreServiceDelegate(DatastoreServiceFactoryInternal.getDatastoreService(config)) {
  42. @Override
  43. public KeyRange allocateIds(String kind, long size) {
  44. sequenceNames.add(kind);
  45. sequenceBatchSizes.add(size);
  46. return super.allocateIds(kind, size);
  47. }
  48. });
  49. SequenceTestLock.LOCK.acquire();
  50. SequenceGenerator.setSequencePostfixAppendage("JPA");
  51. }
  52. @Override
  53. protected void tearDown() throws Exception {
  54. SequenceGenerator.clearSequencePostfixAppendage();
  55. SequenceTestLock.LOCK.release();
  56. DatastoreServiceFactoryInternal.setDatastoreService(null);
  57. sequenceNames.clear();
  58. super.tearDown();
  59. }
  60. public void testSimpleInsert() throws EntityNotFoundException {
  61. String kind = getKind(HasSequence.class);
  62. HasSequence pojo = new HasSequence();
  63. pojo.setVal("jpa1");
  64. beginTxn();
  65. em.persist(pojo);
  66. commitTxn();
  67. Entity e = ds.get(KeyFactory.createKey(kind, pojo.getId()));
  68. assertEquals("jpa1", e.getProperty("val"));
  69. HasSequence pojo2 = new HasSequence();
  70. pojo2.setVal("jpa2");
  71. beginTxn();
  72. em.persist(pojo2);
  73. commitTxn();
  74. e = ds.get(KeyFactory.createKey(kind, pojo2.getId()));
  75. assertEquals("jpa2", e.getProperty("val"));
  76. // the local datastore id allocator is a single sequence so if there
  77. // are any other allocations happening we can't assert on exact values.
  78. // uncomment this check and the others below when we bring the local
  79. // allocator in line with the prod allocator
  80. // assertEquals(pojo.getId().longValue(), pojo2.getId() - 1);
  81. assertTrue(pojo.getId().longValue() < pojo2.getId());
  82. assertEquals(Utils.newArrayList(kind + "_SEQUENCE__JPA", kind + "_SEQUENCE__JPA"), sequenceNames);
  83. assertEquals(Utils.newArrayList(1L, 1L), sequenceBatchSizes);
  84. }
  85. public void testInsertWithSequenceGenerator() throws EntityNotFoundException {
  86. String kind = getKind(HasSequenceWithSequenceGenerator.class);
  87. HasSequenceWithSequenceGenerator pojo = new HasSequenceWithSequenceGenerator();
  88. pojo.setVal("jpa1");
  89. beginTxn();
  90. em.persist(pojo);
  91. commitTxn();
  92. Entity e = ds.get(KeyFactory.createKey(kind, pojo.getId()));
  93. assertEquals("jpa1", e.getProperty("val"));
  94. assertEquals(Utils.newArrayList("jpathat"), sequenceNames);
  95. assertEquals(Utils.newArrayList(12L), sequenceBatchSizes);
  96. }
  97. public void testInsertWithSequenceGenerator_NoSequenceName() throws EntityNotFoundException {
  98. String kind = getKind(HasSequenceWithNoSequenceName.class);
  99. KeyRange keyRange = ds.allocateIds(kind, 5);
  100. HasSequenceWithNoSequenceName pojo = new HasSequenceWithNoSequenceName();
  101. beginTxn();
  102. em.persist(pojo);
  103. commitTxn();
  104. ds.get(KeyFactory.createKey(kind, pojo.getId()));
  105. // the local datastore id allocator is a single sequence so if there
  106. // are any other allocations happening we can't assert on exact values.
  107. // uncomment this check and the others below when we bring the local
  108. // allocator in line with the prod allocator
  109. // assertEquals(keyRange.getEnd().getId(), pojo.getId() - 1);
  110. assertTrue(keyRange.getEnd().getId() < pojo.getId());
  111. keyRange = ds.allocateIds(kind, 1);
  112. // assertEquals(pojo.getId() + 12, keyRange.getStart().getId());
  113. assertTrue(pojo.getId() + 12 <= keyRange.getStart().getId());
  114. assertEquals(Utils.newArrayList(kind + "_SEQUENCE__JPA"), sequenceNames);
  115. assertEquals(Utils.newArrayList(12L), sequenceBatchSizes);
  116. }
  117. public void testSequenceWithUnencodedStringPk() throws EntityNotFoundException {
  118. String kind = getKind(HasSequenceWithUnencodedStringPk.class);
  119. HasSequenceWithUnencodedStringPk pojo = new HasSequenceWithUnencodedStringPk();
  120. beginTxn();
  121. em.persist(pojo);
  122. commitTxn();
  123. ds.get(KeyFactory.createKey(kind, pojo.getId()));
  124. HasSequenceWithUnencodedStringPk pojo2 = new HasSequenceWithUnencodedStringPk();
  125. beginTxn();
  126. em.persist(pojo2);
  127. commitTxn();
  128. ds.get(KeyFactory.createKey(kind, pojo2.getId()));
  129. // the local datastore id allocator is a single sequence so if there
  130. // are any other allocations happening we can't assert on exact values.
  131. // uncomment this check and the others below when we bring the local
  132. // allocator in line with the prod allocator
  133. // assertEquals(Long.parseLong(pojo.getId()), Long.parseLong(pojo2.getId()) - 1);
  134. assertTrue(Long.parseLong(pojo.getId()) < Long.parseLong(pojo2.getId()));
  135. assertEquals(Utils.newArrayList(kind + "_SEQUENCE__JPA", kind + "_SEQUENCE__JPA"), sequenceNames);
  136. assertEquals(Utils.newArrayList(1L, 1L), sequenceBatchSizes);
  137. }
  138. public void testSequenceOnNonPkFields() {
  139. String kind = getKind(HasSequenceOnNonPkFields.class);
  140. HasSequenceOnNonPkFields pojo = new HasSequenceOnNonPkFields();
  141. pojo.setId("jpa");
  142. beginTxn();
  143. em.persist(pojo);
  144. commitTxn();
  145. // the local datastore id allocator is a single sequence so if there
  146. // are any other allocations happening we can't assert on exact values.
  147. // uncomment this check and the others below when we bring the local
  148. // allocator in line with the prod allocator
  149. // assertEquals(pojo.getVal(), pojo.getVal2() - 1);
  150. assertTrue(pojo.getVal() < pojo.getVal2());
  151. HasSequenceOnNonPkFields pojo2 = new HasSequenceOnNonPkFields();
  152. pojo2.setId("jpa");
  153. beginTxn();
  154. em.persist(pojo2);
  155. commitTxn();
  156. // assertEquals(pojo.getVal2(), pojo2.getVal() - 1);
  157. assertTrue(pojo.getVal2() < pojo2.getVal());
  158. assertEquals(Utils.newArrayList(kind + "_SEQUENCE__JPA", kind + "_SEQUENCE__JPA",
  159. kind + "_SEQUENCE__JPA", kind + "_SEQUENCE__JPA"), sequenceNames);
  160. assertEquals(Utils.newArrayList(1L, 1L, 1L, 1L), sequenceBatchSizes);
  161. }
  162. private String getKind(Class<?> cls) {
  163. return cls.getName().substring(cls.getName().lastIndexOf(".") + 1);
  164. }
  165. }