PageRenderTime 26ms CodeModel.GetById 22ms RepoModel.GetById 0ms 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
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.jdo;
  14. import com.google.appengine.api.datastore.DatastoreService;
  15. import com.google.appengine.api.datastore.DatastoreServiceConfig;
  16. import com.google.appengine.datanucleus.DatastoreServiceFactoryInternal;
  17. import java.lang.reflect.InvocationHandler;
  18. import java.lang.reflect.InvocationTargetException;
  19. import java.lang.reflect.Method;
  20. import java.lang.reflect.Proxy;
  21. /**
  22. * @author Max Ross <maxr@google.com>
  23. */
  24. abstract class JDOBatchTestCase extends JDOTestCase {
  25. BatchRecorder batchRecorder;
  26. static abstract class BatchRecorder implements InvocationHandler {
  27. private final DatastoreService delegate;
  28. int batchOps = 0;
  29. public BatchRecorder(DatastoreServiceConfig config) {
  30. this.delegate = DatastoreServiceFactoryInternal.getDatastoreService(config);
  31. }
  32. public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
  33. method.setAccessible(true);
  34. if (isBatchMethod(method)) {
  35. batchOps++;
  36. }
  37. try {
  38. return method.invoke(delegate, objects);
  39. } catch (InvocationTargetException ite) {
  40. throw ite.getTargetException();
  41. }
  42. }
  43. abstract boolean isBatchMethod(Method m);
  44. }
  45. @Override
  46. protected void setUp() throws Exception {
  47. super.setUp();
  48. batchRecorder = newBatchRecorder();
  49. DatastoreService recordBatchPuts = (DatastoreService)
  50. Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] {DatastoreService.class}, batchRecorder);
  51. DatastoreServiceFactoryInternal.setDatastoreService(recordBatchPuts);
  52. }
  53. abstract BatchRecorder newBatchRecorder();
  54. }