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