PageRenderTime 11ms CodeModel.GetById 2ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/com/google/appengine/datanucleus/jdo/JDOBatchTestCase.java

http://datanucleus-appengine.googlecode.com/
Java | 69 lines | 39 code | 12 blank | 18 comment | 1 complexity | 6492e8797b8bf12ac8ec0401db2ce118 MD5 | raw file
 1/**********************************************************************
 2Copyright (c) 2009 Google Inc.
 3
 4Licensed under the Apache License, Version 2.0 (the "License");
 5you may not use this file except in compliance with the License.
 6You may obtain a copy of the License at
 7
 8http://www.apache.org/licenses/LICENSE-2.0
 9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15**********************************************************************/
16package com.google.appengine.datanucleus.jdo;
17
18import com.google.appengine.api.datastore.DatastoreService;
19import com.google.appengine.api.datastore.DatastoreServiceConfig;
20import com.google.appengine.datanucleus.DatastoreServiceFactoryInternal;
21
22import java.lang.reflect.InvocationHandler;
23import java.lang.reflect.InvocationTargetException;
24import java.lang.reflect.Method;
25import java.lang.reflect.Proxy;
26
27/**
28 * @author Max Ross <maxr@google.com>
29 */
30abstract class JDOBatchTestCase extends JDOTestCase {
31
32  BatchRecorder batchRecorder;
33
34  static abstract class BatchRecorder implements InvocationHandler {
35
36    private final DatastoreService delegate;
37    int batchOps = 0;
38
39    public BatchRecorder(DatastoreServiceConfig config) {
40      this.delegate = DatastoreServiceFactoryInternal.getDatastoreService(config);
41    }
42
43    public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
44      method.setAccessible(true);
45      if (isBatchMethod(method)) {
46        batchOps++;
47      }
48      try {
49        return method.invoke(delegate, objects);
50      } catch (InvocationTargetException ite) {
51        throw ite.getTargetException();
52      }
53    }
54
55    abstract boolean isBatchMethod(Method m);
56  }
57
58  @Override
59  protected void setUp() throws Exception {
60    super.setUp();
61    batchRecorder = newBatchRecorder();
62    DatastoreService recordBatchPuts = (DatastoreService)
63        Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] {DatastoreService.class}, batchRecorder);
64    DatastoreServiceFactoryInternal.setDatastoreService(recordBatchPuts);
65  }
66
67  abstract BatchRecorder newBatchRecorder();
68
69}