PageRenderTime 30ms CodeModel.GetById 13ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

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