PageRenderTime 34ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tests_bugs/com/google/appengine/datanucleus/bugs/jpa/JPABugTestCase.java

http://datanucleus-appengine.googlecode.com/
Java | 211 lines | 154 code | 28 blank | 29 comment | 17 complexity | 007253bb6a39f6cbc57f13342202be95 MD5 | raw file
Possible License(s): Apache-2.0
  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.bugs.jpa;
  14. import com.google.appengine.api.datastore.DatastoreService;
  15. import com.google.appengine.api.datastore.DatastoreServiceFactory;
  16. import com.google.appengine.api.datastore.Query;
  17. import com.google.appengine.datanucleus.DatastoreManager;
  18. import com.google.appengine.datanucleus.bugs.DatastoreTestCase;
  19. import com.google.appengine.datanucleus.EntityUtils;
  20. import org.datanucleus.ClassLoaderResolver;
  21. import org.datanucleus.NucleusContext;
  22. import org.datanucleus.api.jpa.JPAEntityManager;
  23. import org.datanucleus.api.jpa.JPAEntityManagerFactory;
  24. import org.datanucleus.metadata.MetaDataManager;
  25. import org.datanucleus.ExecutionContext;
  26. import org.datanucleus.store.mapped.MappedStoreManager;
  27. import java.util.Map;
  28. import java.util.concurrent.ConcurrentHashMap;
  29. import javax.persistence.EntityManager;
  30. import javax.persistence.EntityManagerFactory;
  31. import javax.persistence.Persistence;
  32. /**
  33. * @author Max Ross <maxr@google.com>
  34. */
  35. public class JPABugTestCase extends DatastoreTestCase {
  36. private static
  37. ThreadLocal<Map<EntityManagerFactoryName, EntityManagerFactory>> emfCache =
  38. new ThreadLocal<Map<EntityManagerFactoryName, EntityManagerFactory>>() {
  39. @Override
  40. protected Map<EntityManagerFactoryName, EntityManagerFactory> initialValue() {
  41. // this shouldn't be necessary but I get concurrent mod exceptions
  42. return new ConcurrentHashMap<EntityManagerFactoryName, EntityManagerFactory>();
  43. }
  44. };
  45. protected EntityManagerFactory emf;
  46. protected EntityManager em;
  47. protected DatastoreService ds;
  48. @Override
  49. protected void setUp() throws Exception {
  50. super.setUp();
  51. ds = DatastoreServiceFactory.getDatastoreService();
  52. emf = emfCache.get().get(getEntityManagerFactoryName());
  53. if (emf == null || !emf.isOpen()) {
  54. emf = Persistence.createEntityManagerFactory(getEntityManagerFactoryName().name());
  55. if (cacheManagers()) {
  56. emfCache.get().put(getEntityManagerFactoryName(), emf);
  57. }
  58. }
  59. em = emf.createEntityManager();
  60. }
  61. public enum EntityManagerFactoryName {
  62. // nonTransactionalRead and nonTransactionalWrite are true
  63. transactional_ds_non_transactional_ops_allowed,
  64. // nonTransactionalRead and nonTransactionalWrite are false
  65. transactional_ds_non_transactional_ops_not_allowed,
  66. // nonTransactionalRead and nonTransactionalWrite are true
  67. nontransactional_ds_non_transactional_ops_allowed,
  68. // nonTransactionalRead and nonTransactionalWrite are false
  69. nontransactional_ds_non_transactional_ops_not_allowed
  70. }
  71. /**
  72. * By default we use a datasource that requires txns.
  73. * Override this if your test needs to use a different instance.
  74. */
  75. protected EntityManagerFactoryName getEntityManagerFactoryName() {
  76. return EntityManagerFactoryName.transactional_ds_non_transactional_ops_not_allowed;
  77. }
  78. @Override
  79. protected void tearDown() throws Exception {
  80. try {
  81. if (em.isOpen()) {
  82. if (em.getTransaction().isActive()) {
  83. em.getTransaction().rollback();
  84. }
  85. em.close();
  86. }
  87. em = null;
  88. // see if anybody closed any of our emfs and if so just remove them from the cache -
  89. // we'll rebuild it the next time it's needed.
  90. for (Map.Entry<EntityManagerFactoryName, EntityManagerFactory> entry : emfCache.get().entrySet()) {
  91. if (!entry.getValue().isOpen()) {
  92. emfCache.get().remove(entry.getKey());
  93. }
  94. }
  95. if (!cacheManagers() && emf.isOpen()) {
  96. emf.close();
  97. }
  98. emf = null;
  99. } finally {
  100. super.tearDown();
  101. }
  102. }
  103. protected void beginTxn() {
  104. em.getTransaction().begin();
  105. }
  106. protected void commitTxn() {
  107. em.getTransaction().commit();
  108. }
  109. protected void rollbackTxn() {
  110. if (em.getTransaction().isActive()) {
  111. em.getTransaction().rollback();
  112. }
  113. }
  114. protected void switchDatasource(EntityManagerFactoryName name) {
  115. switchDatasource(name, null);
  116. }
  117. protected void switchDatasource(EntityManagerFactoryName name, Map<String, String> props) {
  118. em.close();
  119. if (!cacheManagers() && emf.isOpen()) {
  120. emf.close();
  121. }
  122. if (props == null) {
  123. emf = Persistence.createEntityManagerFactory(name.name());
  124. } else {
  125. emf = Persistence.createEntityManagerFactory(name.name(), props);
  126. }
  127. em = emf.createEntityManager();
  128. }
  129. @SuppressWarnings("deprecation")
  130. public int countForClass(Class<?> clazz) {
  131. String kind = kindForClass(clazz);
  132. return ds.prepare(new Query(kind)).countEntities();
  133. }
  134. protected String kindForClass(Class<?> clazz) {
  135. NucleusContext nucContext = ((JPAEntityManagerFactory)emf).getNucleusContext();
  136. MetaDataManager mdm = nucContext.getMetaDataManager();
  137. MappedStoreManager storeMgr = (MappedStoreManager) nucContext.getStoreManager();
  138. ClassLoaderResolver clr = nucContext.getClassLoaderResolver(getClass().getClassLoader());
  139. return EntityUtils.determineKind(
  140. mdm.getMetaDataForClass(
  141. clazz,
  142. nucContext.getClassLoaderResolver(getClass().getClassLoader())),
  143. storeMgr,
  144. clr);
  145. }
  146. protected ExecutionContext getExecutionContext() {
  147. return ((JPAEntityManager)em).getExecutionContext();
  148. }
  149. protected String kindForObject(Object obj) {
  150. return kindForClass(obj.getClass());
  151. }
  152. private boolean cacheManagers() {
  153. return !Boolean.valueOf(System.getProperty("do.not.cache.managers"));
  154. }
  155. interface StartEnd {
  156. void start();
  157. void end();
  158. }
  159. final StartEnd TXN_START_END = new StartEnd() {
  160. public void start() {
  161. beginTxn();
  162. }
  163. public void end() {
  164. commitTxn();
  165. }
  166. };
  167. final StartEnd NEW_EM_START_END = new StartEnd() {
  168. public void start() {
  169. if (!em.isOpen()) {
  170. em = emf.createEntityManager();
  171. }
  172. }
  173. public void end() {
  174. em.close();
  175. }
  176. };
  177. protected DatastoreManager getStoreManager() {
  178. return (DatastoreManager) getExecutionContext().getStoreManager();
  179. }
  180. }