/tests/com/google/appengine/datanucleus/DatastoreServiceRecordingImpl.java
Java | 187 lines | 136 code | 29 blank | 22 comment | 8 complexity | 1c25416f4a9f219503a4e16377c1dfd4 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 com.google.appengine.api.datastore.DatastoreAttributes; 19import com.google.appengine.api.datastore.DatastoreService; 20import com.google.appengine.api.datastore.Entity; 21import com.google.appengine.api.datastore.EntityNotFoundException; 22import com.google.appengine.api.datastore.Index; 23import com.google.appengine.api.datastore.Key; 24import com.google.appengine.api.datastore.KeyRange; 25import com.google.appengine.api.datastore.PreparedQuery; 26import com.google.appengine.api.datastore.Query; 27import com.google.appengine.api.datastore.Transaction; 28import com.google.appengine.api.datastore.TransactionOptions; 29 30import java.util.Collection; 31import java.util.List; 32import java.util.Map; 33 34/** 35 * This testing helper class allows a test to perform an integration test while 36 * simultaneously recording calls. This is useful when used in conjunction with 37 * a mocking utility such as EasyMock. 38 * 39 * @author Erick Armbrust <earmbrust@google.com> 40 */ 41public class DatastoreServiceRecordingImpl implements DatastoreService { 42 43 private final DatastoreService recorder; 44 private final DatastoreService delegate; 45 private final Transaction txnRecorder; 46 private final TxnIdAnswer txnIdAnswer; 47 48 public DatastoreServiceRecordingImpl(DatastoreService recorder, DatastoreService delegate, Transaction txnRecorder, 49 TxnIdAnswer txnIdAnswer) { 50 this.recorder = recorder; 51 this.delegate = delegate; 52 this.txnRecorder = txnRecorder; 53 this.txnIdAnswer = txnIdAnswer; 54 } 55 56 public Entity get(Key key) throws EntityNotFoundException { 57 recorder.get(key); 58 return delegate.get(key); 59 } 60 61 public Entity get(Transaction txn, Key key) throws EntityNotFoundException { 62 recorder.get(txn, key); 63 return delegate.get(txn, key); 64 } 65 66 public Map<Key, Entity> get(Iterable<Key> keys) { 67 recorder.get(keys); 68 return delegate.get(keys); 69 } 70 71 public Map<Key, Entity> get(Transaction txn, Iterable<Key> keys) { 72 recorder.get(txn, keys); 73 return delegate.get(txn, keys); 74 } 75 76 public Key put(Entity entity) { 77 recorder.put(entity); 78 return delegate.put(entity); 79 } 80 81 public Key put(Transaction txn, Entity entity) { 82 recorder.put(txn, entity); 83 return delegate.put(txn, entity); 84 } 85 86 public List<Key> put(Iterable<Entity> entities) { 87 recorder.put(entities); 88 return delegate.put(entities); 89 } 90 91 public List<Key> put(Transaction txn, Iterable<Entity> entities) { 92 recorder.put(txn, entities); 93 return delegate.put(txn, entities); 94 } 95 96 public void delete(Key... keys) { 97 recorder.delete(keys); 98 delegate.delete(keys); 99 } 100 101 public void delete(Transaction txn, Key... keys) { 102 recorder.delete(txn, keys); 103 delegate.delete(txn, keys); 104 } 105 106 public void delete(Iterable<Key> keys) { 107 recorder.delete(keys); 108 delegate.delete(keys); 109 } 110 111 public void delete(Transaction txn, Iterable<Key> keys) { 112 recorder.delete(txn, keys); 113 delegate.delete(txn, keys); 114 } 115 116 public Transaction beginTransaction() { 117 Transaction txn = delegate.beginTransaction(); 118 txnIdAnswer.setExpectedTxnId(txn.getId()); 119 recorder.beginTransaction(); 120 return new TransactionRecordingImpl(txn, txnRecorder); 121 } 122 123 public Transaction beginTransaction(TransactionOptions transactionOptions) { 124 Transaction txn = delegate.beginTransaction(transactionOptions); 125 txnIdAnswer.setExpectedTxnId(txn.getId()); 126 recorder.beginTransaction(transactionOptions); 127 return new TransactionRecordingImpl(txn, txnRecorder); 128 } 129 130 public Transaction getCurrentTransaction() { 131 Transaction t1 = recorder.getCurrentTransaction(); 132 Transaction t2 = delegate.getCurrentTransaction(); 133 if (t1 == null || t2 == null) { 134 return null; 135 } 136 return new TransactionRecordingImpl(t1, t2); 137 } 138 139 public Transaction getCurrentTransaction(Transaction txn) { 140 Transaction t1 = recorder.getCurrentTransaction(txn); 141 Transaction t2 = delegate.getCurrentTransaction(txn); 142 if (t1 == null || t2 == null) { 143 return null; 144 } 145 return new TransactionRecordingImpl(t1, t2); 146 } 147 148 public Collection<Transaction> getActiveTransactions() { 149 recorder.getActiveTransactions(); 150 return delegate.getActiveTransactions(); 151 } 152 153 public PreparedQuery prepare(Query query) { 154 recorder.prepare(query); 155 return delegate.prepare(query); 156 } 157 158 public PreparedQuery prepare(Transaction transaction, Query query) { 159 recorder.prepare(transaction, query); 160 return delegate.prepare(transaction, query); 161 } 162 163 public KeyRange allocateIds(String kind, long num) { 164 recorder.allocateIds(kind, num); 165 return delegate.allocateIds(kind, num); 166 } 167 168 public KeyRange allocateIds(Key parent, String kind, long num) { 169 recorder.allocateIds(parent, kind, num); 170 return delegate.allocateIds(parent, kind, num); 171 } 172 173 public KeyRangeState allocateIdRange(KeyRange keyRange) { 174 recorder.allocateIdRange(keyRange); 175 return delegate.allocateIdRange(keyRange); 176 } 177 178 public DatastoreAttributes getDatastoreAttributes() { 179 recorder.getDatastoreAttributes(); 180 return delegate.getDatastoreAttributes(); 181 } 182 183 public Map<Index, Index.IndexState> getIndexes() { 184 recorder.getIndexes(); 185 return delegate.getIndexes(); 186 } 187}