PageRenderTime 33ms CodeModel.GetById 21ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://datanucleus-appengine.googlecode.com/
Java | 103 lines | 73 code | 9 blank | 21 comment | 0 complexity | c1ec5c230f83150b160e837b7f974092 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 */
 16package com.google.appengine.datanucleus.jpa;
 17
 18import com.google.appengine.api.datastore.DatastoreServiceConfig;
 19import com.google.appengine.api.datastore.ReadPolicy;
 20import com.google.appengine.datanucleus.DatastoreManager;
 21import com.google.appengine.datanucleus.Utils;
 22
 23import java.util.Map;
 24
 25import javax.persistence.Persistence;
 26import javax.persistence.PersistenceException;
 27
 28/**
 29 * @author Max Ross <max.ross@gmail.com>
 30 */
 31public class JPADatastoreServiceConfigTest extends JPATestCase {
 32
 33  public void testDefaultStorageVersion() {
 34    DatastoreManager storeMgr = (DatastoreManager) getExecutionContext().getStoreManager();
 35    DatastoreServiceConfig config = storeMgr.getDefaultDatastoreServiceConfigForReads();
 36    DatastoreServiceConfig defaultConfig = DatastoreServiceConfig.Builder.withDefaults();
 37    assertEquals(defaultConfig.getDeadline(), config.getDeadline());
 38    assertEquals(defaultConfig.getReadPolicy(), config.getReadPolicy());
 39  }
 40
 41  public void testNonDefaultValues() {
 42    em.close();
 43    emf.close();
 44    Map<String, String> props = Utils.newHashMap();
 45    props.put("javax.persistence.query.timeout", "334");
 46    props.put("datanucleus.datastoreWriteTimeout", "335");
 47    props.put(DatastoreManager.DATASTORE_READ_CONSISTENCY_PROPERTY, ReadPolicy.Consistency.EVENTUAL.name());
 48    emf = Persistence.createEntityManagerFactory(getEntityManagerFactoryName().name(), props);
 49    em = emf.createEntityManager();
 50    DatastoreManager storeMgr = (DatastoreManager) getExecutionContext().getStoreManager();
 51    DatastoreServiceConfig config = storeMgr.getDefaultDatastoreServiceConfigForReads();
 52    assertEquals(.334d, config.getDeadline());
 53    assertEquals(new ReadPolicy(ReadPolicy.Consistency.EVENTUAL), config.getReadPolicy());
 54    config = storeMgr.getDefaultDatastoreServiceConfigForWrites();
 55    assertEquals(.335d, config.getDeadline());
 56    assertEquals(new ReadPolicy(ReadPolicy.Consistency.EVENTUAL), config.getReadPolicy());
 57  }
 58
 59  public void testNonDefaultValuesInConfigFile() {
 60    em.close();
 61    emf.close();
 62    emf = Persistence.createEntityManagerFactory("datastoreserviceconfigprops");
 63    em = emf.createEntityManager();
 64    DatastoreManager storeMgr = (DatastoreManager) getExecutionContext().getStoreManager();
 65    DatastoreServiceConfig config = storeMgr.getDefaultDatastoreServiceConfigForReads();
 66    assertEquals(5.0d, config.getDeadline());
 67    assertEquals(new ReadPolicy(ReadPolicy.Consistency.EVENTUAL), config.getReadPolicy());
 68    config = storeMgr.getDefaultDatastoreServiceConfigForWrites();
 69    assertEquals(10.0d, config.getDeadline());
 70    assertEquals(new ReadPolicy(ReadPolicy.Consistency.EVENTUAL), config.getReadPolicy());
 71    em.close();
 72    emf.close();
 73  }
 74
 75  // TODO This test is nonsense. They both map to the same internal property, so what is it testing? the order of init?
 76  public void testConflictingReadValues() {
 77    em.close();
 78    emf.close();
 79    Map<String, String> props = Utils.newHashMap();
 80    props.put("datanucleus.datastoreReadTimeout", "333");
 81    props.put("javax.persistence.query.timeout", "334");
 82    emf = Persistence.createEntityManagerFactory(getEntityManagerFactoryName().name(), props);
 83    em = emf.createEntityManager();
 84    DatastoreManager storeMgr = (DatastoreManager) getExecutionContext().getStoreManager();
 85    DatastoreServiceConfig config = storeMgr.getDefaultDatastoreServiceConfigForReads();
 86    assertEquals(.333d, config.getDeadline());
 87  }
 88
 89  public void testUnknownReadPolicy() {
 90    em.close();
 91    emf.close();
 92    Map<String, String> props = Utils.newHashMap();
 93    props.put(DatastoreManager.DATASTORE_READ_CONSISTENCY_PROPERTY, "dne");
 94    try {
 95      emf = Persistence.createEntityManagerFactory(getEntityManagerFactoryName().name(), props);
 96    } catch (Exception e) {
 97      // not all Persistence impls wrap EMF creation exception into PersistenceException
 98      Throwable cause = (e instanceof PersistenceException) ? e.getCause() : e;
 99      // good
100      assertTrue(cause.getMessage().startsWith("Illegal value for"));
101    }
102  }
103}