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

/tests/com/google/appengine/datanucleus/jdo/JDOSequenceTest.java

http://datanucleus-appengine.googlecode.com/
Java | 213 lines | 150 code | 19 blank | 44 comment | 0 complexity | dec8f4491cfecacd366ae07e32c4c21f MD5 | raw file
  1/**********************************************************************
  2 Copyright (c) 2009 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 **********************************************************************/
 16package com.google.appengine.datanucleus.jdo;
 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.jdo.SequenceExamplesJDO.HasSequence;
 28import com.google.appengine.datanucleus.test.jdo.SequenceExamplesJDO.HasSequenceOnNonPkFields;
 29import com.google.appengine.datanucleus.test.jdo.SequenceExamplesJDO.HasSequenceWithNoSequenceName;
 30import com.google.appengine.datanucleus.test.jdo.SequenceExamplesJDO.HasSequenceWithSequenceGenerator;
 31import com.google.appengine.datanucleus.test.jdo.SequenceExamplesJDO.HasSequenceWithSequenceGenerator2;
 32import com.google.appengine.datanucleus.test.jdo.SequenceExamplesJDO.HasSequenceWithUnencodedStringPk;
 33import com.google.appengine.datanucleus.valuegenerator.SequenceGenerator;
 34
 35import java.util.List;
 36
 37import javax.jdo.datastore.Sequence;
 38
 39/**
 40 * @author Max Ross <maxr@google.com>
 41 */
 42public class JDOSequenceTest extends JDOTestCase {
 43
 44  private final List<String> sequenceNames = Utils.newArrayList();
 45  private final List<Long> sequenceBatchSizes = Utils.newArrayList();
 46
 47  @Override
 48  protected void setUp() throws Exception {
 49    super.setUp();
 50    DatastoreServiceConfig config = getStoreManager().getDefaultDatastoreServiceConfigForReads();
 51    DatastoreServiceFactoryInternal.setDatastoreService(
 52        new BaseDatastoreServiceDelegate(DatastoreServiceFactoryInternal.getDatastoreService(config)) {
 53      @Override
 54      public KeyRange allocateIds(String kind, long size) {
 55        sequenceNames.add(kind);
 56        sequenceBatchSizes.add(size);
 57        return super.allocateIds(kind, size);
 58      }
 59    });
 60    SequenceTestLock.LOCK.acquire();
 61    SequenceGenerator.setSequencePostfixAppendage("JDO");
 62  }
 63
 64  @Override
 65  protected void tearDown() throws Exception {
 66    SequenceGenerator.clearSequencePostfixAppendage();
 67    SequenceTestLock.LOCK.release();
 68    DatastoreServiceFactoryInternal.setDatastoreService(null);
 69    sequenceNames.clear();
 70    sequenceBatchSizes.clear();
 71    super.tearDown();
 72  }
 73
 74
 75  public void testSimpleInsert() throws EntityNotFoundException {
 76    String kind = getKind(HasSequence.class);
 77    HasSequence pojo = new HasSequence();
 78    pojo.setVal("jdo1");
 79    beginTxn();
 80    pm.makePersistent(pojo);
 81    commitTxn();
 82    Entity e = ds.get(KeyFactory.createKey(kind, pojo.getId()));
 83    assertEquals("jdo1", e.getProperty("val"));
 84
 85    HasSequence pojo2 = new HasSequence();
 86    pojo2.setVal("jdo2");
 87    beginTxn();
 88    pm.makePersistent(pojo2);
 89    commitTxn();
 90    e = ds.get(KeyFactory.createKey(kind, pojo2.getId()));
 91    assertEquals("jdo2", e.getProperty("val"));
 92    // the local datastore id allocator is a single sequence so if there
 93    // are any other allocations happening we can't assert on exact values.
 94    // uncomment this check and the others below when we bring the local
 95    // allocator in line with the prod allocator
 96    assertTrue(pojo.getId() < pojo2.getId());
 97    assertEquals(Utils.newArrayList(kind + "_SEQUENCE__JDO", kind + "_SEQUENCE__JDO"), sequenceNames);
 98    assertEquals(Utils.newArrayList(1L, 1L), sequenceBatchSizes);
 99  }
100
101  public void testInsertWithSequenceGenerator() throws EntityNotFoundException {
102    String kind = getKind(HasSequenceWithSequenceGenerator.class);
103    HasSequenceWithSequenceGenerator pojo = new HasSequenceWithSequenceGenerator();
104    pojo.setVal("jdo1");
105    beginTxn();
106    pm.makePersistent(pojo);
107    commitTxn();
108    Entity e = ds.get(KeyFactory.createKey(kind, pojo.getId()));
109    assertEquals("jdo1", e.getProperty("val"));
110    assertEquals(Utils.newArrayList("jdothat"), sequenceNames);
111    assertEquals(Utils.newArrayList(12L), sequenceBatchSizes);
112  }
113
114  public void testInsertWithSequenceGenerator_NoSequenceName() throws EntityNotFoundException {
115    String kind = getKind(HasSequenceWithNoSequenceName.class);
116    HasSequenceWithNoSequenceName pojo = new HasSequenceWithNoSequenceName();
117    beginTxn();
118    pm.makePersistent(pojo);
119    commitTxn();
120    ds.get(KeyFactory.createKey(kind, pojo.getId()));
121    assertEquals(Utils.newArrayList(kind + "_SEQUENCE__JDO"), sequenceNames);
122    assertEquals(Utils.newArrayList(12L), sequenceBatchSizes);
123  }
124  
125  public void testDirectSequenceAccess() throws Exception {
126    // in order to make sure we get a fresh block of ids when we persist
127    // we need to make sure we don't reuse the pmf
128    pmf.close();
129    tearDown();
130    setUp();
131    KeyRange range = ds.allocateIds("jdothat2", 1);
132    HasSequenceWithSequenceGenerator2 pojo = new HasSequenceWithSequenceGenerator2();
133    beginTxn();
134    pm.makePersistent(pojo);
135    commitTxn();
136    // the local datastore id allocator is a single sequence so if there
137    // are any other allocations happening we can't assert on exact values.
138    // uncomment this check and the others below when we bring the local
139    // allocator in line with the prod allocator
140    // assertEquals(range.getEnd().getId(), pojo.getId() - 1);
141    assertTrue(range.getEnd().getId() < pojo.getId());
142    Sequence seq = pm.getSequence("jdo1b");
143//    assertEquals(pojo.getId() + 12, seq.nextValue());
144    assertTrue(pojo.getId() + 12 <= seq.nextValue());
145//    assertEquals(pojo.getId() + 13, seq.nextValue());
146    assertTrue(pojo.getId() + 13 <= seq.nextValue());
147    assertEquals(Utils.newArrayList("jdothat2", "jdothat2"), sequenceNames);
148    assertEquals(Utils.newArrayList(12L, 12L), sequenceBatchSizes);
149    sequenceNames.clear();
150    sequenceBatchSizes.clear();
151    // getting a sequence always gets you a fresh batch
152    seq = pm.getSequence("jdo1b");
153//    assertEquals(pojo.getId() + 24, seq.nextValue());
154    assertTrue(pojo.getId() + 24 <= seq.nextValue());
155//    assertEquals(pojo.getId() + 25, seq.nextValue());
156    assertTrue(pojo.getId() + 25 <= seq.nextValue());
157    assertEquals(Utils.newArrayList("jdothat2"), sequenceNames);
158    assertEquals(Utils.newArrayList(12L), sequenceBatchSizes);
159  }
160
161  public void testSequenceWithUnencodedStringPk() throws EntityNotFoundException {
162    String kind = getKind(HasSequenceWithUnencodedStringPk.class);
163    HasSequenceWithUnencodedStringPk pojo = new HasSequenceWithUnencodedStringPk();
164    beginTxn();
165    pm.makePersistent(pojo);
166    commitTxn();
167    ds.get(KeyFactory.createKey(kind, pojo.getId()));
168
169    HasSequenceWithUnencodedStringPk pojo2 = new HasSequenceWithUnencodedStringPk();
170    beginTxn();
171    pm.makePersistent(pojo2);
172    commitTxn();
173    ds.get(KeyFactory.createKey(kind, pojo2.getId()));
174    // the local datastore id allocator is a single sequence so if there
175    // are any other allocations happening we can't assert on exact values.
176    // uncomment this check and the others below when we bring the local
177    // allocator in line with the prod allocator
178//    assertEquals(Long.parseLong(pojo.getId()), Long.parseLong(pojo2.getId()) - 1);
179    assertTrue(Long.parseLong(pojo.getId()) < Long.parseLong(pojo2.getId()));
180    assertEquals(Utils.newArrayList(kind + "_SEQUENCE__JDO", kind + "_SEQUENCE__JDO"), sequenceNames);
181    assertEquals(Utils.newArrayList(1L, 1L), sequenceBatchSizes);
182  }
183
184  public void testSequenceOnNonPkFields() throws EntityNotFoundException {
185    String kind = getKind(HasSequenceOnNonPkFields.class);
186    HasSequenceOnNonPkFields pojo = new HasSequenceOnNonPkFields();
187    pojo.setId("jdo");
188    beginTxn();
189    pm.makePersistent(pojo);
190    // the local datastore id allocator is a single sequence so if there
191    // are any other allocations happening we can't assert on exact values.
192    // uncomment this check and the others below when we bring the local
193    // allocator in line with the prod allocator
194    assertTrue(pojo.getVal1() < pojo.getVal2());
195    commitTxn();
196
197    HasSequenceOnNonPkFields pojo2 = new HasSequenceOnNonPkFields();
198    pojo2.setId("jdo");
199    beginTxn();
200    pm.makePersistent(pojo2);
201//    assertEquals(pojo.getVal2(), pojo2.getVal1() - 1);
202    assertTrue(pojo.getVal2() < pojo2.getVal1());
203    commitTxn();
204    assertEquals(Utils.newArrayList(kind + "_SEQUENCE__JDO", kind + "_SEQUENCE__JDO",
205                                    kind + "_SEQUENCE__JDO", kind + "_SEQUENCE__JDO"), sequenceNames);
206    assertEquals(Utils.newArrayList(1L, 1L, 1L, 1L), sequenceBatchSizes);
207  }
208
209  private String getKind(Class<?> cls) {
210    return cls.getName().substring(cls.getName().lastIndexOf(".") + 1);
211  }
212
213}