PageRenderTime 24ms CodeModel.GetById 17ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 0ms

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