PageRenderTime 111ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/com/google/appengine/datanucleus/BatchManagerTest.java

http://datanucleus-appengine.googlecode.com/
Java | 105 lines | 75 code | 11 blank | 19 comment | 0 complexity | 5719a99d1786ca3228eb72e1da9f96e7 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;
  14. import org.datanucleus.api.jdo.JDOPersistenceManager;
  15. import org.datanucleus.state.ObjectProvider;
  16. import org.easymock.EasyMock;
  17. import com.google.appengine.datanucleus.jdo.JDOTestCase;
  18. import java.util.HashMap;
  19. import java.util.List;
  20. /**
  21. * @author Max Ross <maxr@google.com>
  22. */
  23. public class BatchManagerTest extends JDOTestCase {
  24. private static class MyBatchManager extends BatchManager<Object> {
  25. String getOperation() {
  26. return "test";
  27. }
  28. void processBatchState(DatastorePersistenceHandler handler, List<Object> batchStateList) {
  29. }
  30. }
  31. public void testLegalWorkflow_NoOps() {
  32. BatchManager bm = new MyBatchManager();
  33. assertFalse(bm.batchOperationInProgress());
  34. bm.start();
  35. assertTrue(bm.batchOperationInProgress());
  36. bm.finish(null);
  37. assertFalse(bm.batchOperationInProgress());
  38. }
  39. private ObjectProvider newStateManagerMock() {
  40. return EasyMock.createNiceMock(ObjectProvider.class);
  41. }
  42. public void testLegalWorkflow() throws IllegalAccessException, NoSuchFieldException {
  43. final List<?> providedBatchStateList = Utils.newArrayList();
  44. BatchManager<Object> bm = new MyBatchManager() {
  45. @Override
  46. void processBatchState(DatastorePersistenceHandler handler, List batchStateList) {
  47. providedBatchStateList.addAll(batchStateList);
  48. }
  49. };
  50. assertFalse(bm.batchOperationInProgress());
  51. bm.start();
  52. assertTrue(bm.batchOperationInProgress());
  53. final ObjectProvider sm1 = newStateManagerMock();
  54. final ObjectProvider sm2 = newStateManagerMock();
  55. bm.add(sm1);
  56. bm.add(sm2);
  57. JDOPersistenceManager jpm = (JDOPersistenceManager) pm;
  58. DatastoreManager dm = new DatastoreManager(
  59. jpm.getExecutionContext().getClassLoaderResolver(), jpm.getExecutionContext().getNucleusContext(),
  60. new HashMap<String, Object>());
  61. bm.finish(new DatastorePersistenceHandler(dm));
  62. assertEquals(Utils.newArrayList(sm1, sm2), providedBatchStateList);
  63. assertFalse(bm.batchOperationInProgress());
  64. }
  65. public void testLegalWorkflowWithException() throws IllegalAccessException, NoSuchFieldException {
  66. BatchManager<Object> bm = new MyBatchManager() {
  67. @Override
  68. void processBatchState(DatastorePersistenceHandler handler, List batchStateList) {
  69. throw new RuntimeException("yar");
  70. }
  71. };
  72. assertFalse(bm.batchOperationInProgress());
  73. bm.start();
  74. assertTrue(bm.batchOperationInProgress());
  75. final ObjectProvider sm1 = newStateManagerMock();
  76. final ObjectProvider sm2 = newStateManagerMock();
  77. bm.add(sm1);
  78. bm.add(sm2);
  79. JDOPersistenceManager jpm = (JDOPersistenceManager) pm;
  80. DatastoreManager dm = new DatastoreManager(
  81. jpm.getExecutionContext().getClassLoaderResolver(), jpm.getExecutionContext().getNucleusContext(),
  82. new HashMap<String, Object>());
  83. try {
  84. bm.finish(new DatastorePersistenceHandler(dm));
  85. fail("expected rte");
  86. } catch (RuntimeException rte) {
  87. // good
  88. }
  89. assertFalse(bm.batchOperationInProgress());
  90. }
  91. }