/tests_bugs/com/google/appengine/datanucleus/bugs/DatastoreTestCase.java

http://datanucleus-appengine.googlecode.com/ · Java · 147 lines · 87 code · 27 blank · 33 comment · 1 complexity · f7807154d433dc2d3ec547b99f5e83ee 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.bugs;
  17. //import com.google.appengine.testing.cloudcover.util.CloudCoverLocalServiceTestHelper;
  18. import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
  19. import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
  20. import com.google.apphosting.api.ApiProxy;
  21. import junit.framework.TestCase;
  22. import javax.jdo.spi.JDOImplHelper;
  23. import java.lang.reflect.Field;
  24. import java.util.*;
  25. /**
  26. * Base class for all tests that access the datastore.
  27. *
  28. * @author Max Ross <max.ross@gmail.com>
  29. */
  30. public class DatastoreTestCase extends TestCase {
  31. private final LocalServiceTestHelper helper = new LocalServiceTestHelper(
  32. new LocalDatastoreServiceTestConfig());
  33. // TODO(maxr): Put this back once we have a maven project for cloudcover
  34. // private final CloudCoverLocalServiceTestHelper helper = new CloudCoverLocalServiceTestHelper(
  35. // new LocalDatastoreServiceTestConfig());
  36. @Override
  37. protected void setUp() throws Exception {
  38. super.setUp();
  39. synchronized (JDOImplHelper.class) {
  40. Field f = JDOImplHelper.class.getDeclaredField("registeredClasses");
  41. f.setAccessible(true);
  42. Map map = (Map) f.get(null);
  43. if (!(map instanceof ThreadLocalMap)) {
  44. f.set(null, new ThreadLocalMap((Map) f.get(null)));
  45. }
  46. }
  47. helper.setUp();
  48. }
  49. @Override
  50. protected void tearDown() throws Exception {
  51. helper.tearDown();
  52. super.tearDown();
  53. }
  54. protected void setDelegateForThread(ApiProxy.Delegate delegate) {
  55. ApiProxy.setDelegate(delegate);
  56. // CloudCoverLocalServiceTestHelper.setDelegate(delegate);
  57. }
  58. protected ApiProxy.Delegate getDelegateForThread() {
  59. return ApiProxy.getDelegate();
  60. // return CloudCoverLocalServiceTestHelper.getDelegate();
  61. }
  62. /**
  63. * A bizarro custom map implementation that we inject into the jdo
  64. * implementation to get around a concurrent modification bug.
  65. * Methods that are supposed to return views instead return copies. This is
  66. * non-standard but it addresses the concurrency issues. We're only doing
  67. * this for tests so it's not a big deal.
  68. */
  69. private static final class ThreadLocalMap implements Map {
  70. private final Map delegate;
  71. private ThreadLocalMap(Map delegate) {
  72. this.delegate = delegate;
  73. }
  74. public int size() {
  75. return delegate.size();
  76. }
  77. public boolean isEmpty() {
  78. return delegate.isEmpty();
  79. }
  80. public boolean containsKey(Object o) {
  81. return delegate.containsKey(o);
  82. }
  83. public boolean containsValue(Object o) {
  84. return delegate.containsValue(o);
  85. }
  86. public Object get(Object o) {
  87. return delegate.get(o);
  88. }
  89. public synchronized Object put(Object o, Object o1) {
  90. return delegate.put(o, o1);
  91. }
  92. public synchronized Object remove(Object o) {
  93. return delegate.remove(o);
  94. }
  95. public synchronized void putAll(Map map) {
  96. delegate.putAll(map);
  97. }
  98. public synchronized void clear() {
  99. delegate.clear();
  100. }
  101. public synchronized Set keySet() {
  102. Set set = delegate.keySet();
  103. return new HashSet(set);
  104. }
  105. public synchronized Collection values() {
  106. Collection values = delegate.values();
  107. return new ArrayList(values);
  108. }
  109. public Set entrySet() {
  110. Set entries = delegate.entrySet();
  111. return new HashSet(entries);
  112. }
  113. public boolean equals(Object o) {
  114. return delegate.equals(o);
  115. }
  116. public int hashCode() {
  117. return delegate.hashCode();
  118. }
  119. }
  120. }