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