PageRenderTime 62ms CodeModel.GetById 42ms app.highlight 17ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/com/google/appengine/datanucleus/query/RuntimeExceptionWrappingIteratorTest.java

http://datanucleus-appengine.googlecode.com/
Java | 315 lines | 265 code | 14 blank | 36 comment | 2 complexity | 70961305654a31b0898fc14af045f5e1 MD5 | raw file
  1/*
  2 * Copyright (C) 2010 Google Inc
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16package com.google.appengine.datanucleus.query;
 17
 18import com.google.appengine.api.datastore.DatastoreFailureException;
 19import com.google.appengine.api.datastore.DatastoreTimeoutException;
 20import com.google.appengine.api.datastore.Entity;
 21import com.google.appengine.datanucleus.DatastoreTestCase;
 22
 23import org.datanucleus.api.ApiAdapter;
 24import org.datanucleus.api.jdo.JDOAdapter;
 25import org.datanucleus.api.jpa.JPAAdapter;
 26import org.datanucleus.store.query.QueryTimeoutException;
 27import org.easymock.EasyMock;
 28
 29import java.util.Arrays;
 30import java.util.Iterator;
 31
 32import javax.jdo.JDODataStoreException;
 33import javax.jdo.JDOFatalUserException;
 34import javax.persistence.PersistenceException;
 35
 36/**
 37 * @author Max Ross <max.ross@gmail.com>
 38 */
 39public class RuntimeExceptionWrappingIteratorTest extends DatastoreTestCase {
 40
 41  private boolean receivedException = false;
 42  private RuntimeExceptionObserver observer = new RuntimeExceptionObserver() {
 43    public void onException() {
 44      receivedException = true;
 45    }
 46  };
 47  private Iterator<Entity> iter;
 48
 49  protected void setUpIterator(RuntimeException rte) {
 50    receivedException = false;
 51    iter = EasyMock.createMock(Iterator.class);
 52    EasyMock.expect(iter.hasNext()).andThrow(rte);
 53    EasyMock.expect(iter.next()).andThrow(rte);
 54    iter.remove();
 55    EasyMock.expectLastCall().andThrow(rte);
 56    EasyMock.replay(iter);
 57  }
 58
 59  public void testNoExceptionsJPA() {
 60    Entity e1 = new Entity("foo");
 61    Entity e2 = new Entity("foo");
 62    Entity e3 = new Entity("foo");
 63    ApiAdapter api = new JPAAdapter();
 64    RuntimeExceptionWrappingIterator rewi =
 65        new RuntimeExceptionWrappingIterator(api, Arrays.asList(e1, e2, e3).iterator(), observer);
 66    int count = 0;
 67    while (rewi.hasNext()) {
 68      rewi.next();
 69      count++;
 70    }
 71    assertEquals(3, count);
 72  }
 73
 74  public void testExceptionsJPA_IllegalArg() {
 75    setUpIterator(new IllegalArgumentException("boom"));
 76    ApiAdapter api = new JPAAdapter();
 77    RuntimeExceptionWrappingIterator rewi =
 78        new RuntimeExceptionWrappingIterator(api, iter, observer);
 79    try {
 80      rewi.hasNext();
 81      fail("expected exception");
 82    } catch (PersistenceException pe) {
 83      // good
 84      assertTrue(pe.getCause() instanceof IllegalArgumentException);
 85      assertEquals(pe.getCause().getMessage(), "boom");
 86      assertTrue(receivedException);
 87      receivedException = false;
 88    }
 89    try {
 90      rewi.next();
 91      fail("expected exception");
 92    } catch (PersistenceException pe) {
 93      // good
 94      assertTrue(pe.getCause() instanceof IllegalArgumentException);
 95      assertEquals(pe.getCause().getMessage(), "boom");
 96      assertTrue(receivedException);
 97      receivedException = false;
 98    }
 99    try {
100      rewi.remove();
101      fail("expected exception");
102    } catch (PersistenceException pe) {
103      // good
104      assertTrue(pe.getCause() instanceof IllegalArgumentException);
105      assertEquals(pe.getCause().getMessage(), "boom");
106      assertTrue(receivedException);
107      receivedException = false;
108    }
109  }
110
111  public void testExceptionsJPA_DatastoreFailure() {
112    setUpIterator(new DatastoreFailureException("boom"));
113    ApiAdapter api = new JPAAdapter();
114    RuntimeExceptionWrappingIterator rewi =
115        new RuntimeExceptionWrappingIterator(api, iter, observer);
116    try {
117      rewi.hasNext();
118      fail("expected exception");
119    } catch (PersistenceException pe) {
120      // good
121      assertTrue(pe.getCause() instanceof DatastoreFailureException);
122      assertEquals(pe.getCause().getMessage(), "boom");
123      assertTrue(receivedException);
124      receivedException = false;
125    }
126    try {
127      rewi.next();
128      fail("expected exception");
129    } catch (PersistenceException pe) {
130      // good
131      assertTrue(pe.getCause() instanceof DatastoreFailureException);
132      assertEquals(pe.getCause().getMessage(), "boom");
133      assertTrue(receivedException);
134      receivedException = false;
135    }
136    try {
137      rewi.remove();
138      fail("expected exception");
139    } catch (PersistenceException pe) {
140      // good
141      assertTrue(pe.getCause() instanceof DatastoreFailureException);
142      assertEquals(pe.getCause().getMessage(), "boom");
143      assertTrue(receivedException);
144      receivedException = false;
145    }
146  }
147
148  public void testExceptionsJPA_Timeout() {
149    setUpIterator(new DatastoreTimeoutException("boom"));
150    ApiAdapter api = new JPAAdapter();
151    RuntimeExceptionWrappingIterator rewi =
152        new RuntimeExceptionWrappingIterator(api, iter, observer);
153    try {
154      rewi.hasNext();
155      fail("expected exception");
156    } catch (javax.persistence.QueryTimeoutException qte) {
157      // good
158      assertTrue(qte.getCause() instanceof QueryTimeoutException);
159      assertTrue(qte.getCause().getCause() instanceof DatastoreTimeoutException);
160      assertEquals(qte.getCause().getCause().getMessage(), "boom");
161      assertTrue(receivedException);
162      receivedException = false;
163    }
164    try {
165      rewi.next();
166      fail("expected exception");
167    } catch (javax.persistence.QueryTimeoutException qte) {
168      // good
169      assertTrue(qte.getCause() instanceof QueryTimeoutException);
170      assertTrue(qte.getCause().getCause() instanceof DatastoreTimeoutException);
171      assertEquals(qte.getCause().getCause().getMessage(), "boom");
172      assertTrue(receivedException);
173      receivedException = false;
174    }
175    try {
176      rewi.remove();
177      fail("expected exception");
178    } catch (javax.persistence.QueryTimeoutException qte) {
179      // good
180      assertTrue(qte.getCause() instanceof QueryTimeoutException);
181      assertTrue(qte.getCause().getCause() instanceof DatastoreTimeoutException);
182      assertEquals(qte.getCause().getCause().getMessage(), "boom");
183      assertTrue(receivedException);
184      receivedException = false;
185    }
186  }
187  public void testNoExceptionsJDO() {
188    Entity e1 = new Entity("foo");
189    Entity e2 = new Entity("foo");
190    Entity e3 = new Entity("foo");
191    ApiAdapter api = new JDOAdapter();
192    RuntimeExceptionWrappingIterator rewi =
193        new RuntimeExceptionWrappingIterator(api, Arrays.asList(e1, e2, e3).iterator(), observer);
194    int count = 0;
195    while (rewi.hasNext()) {
196      rewi.next();
197      count++;
198    }
199    assertEquals(3, count);
200  }
201
202  public void testExceptionsJDO_IllegalArg() {
203    setUpIterator(new IllegalArgumentException("boom"));
204    ApiAdapter api = new JDOAdapter();
205    RuntimeExceptionWrappingIterator rewi =
206        new RuntimeExceptionWrappingIterator(api, iter, observer);
207    try {
208      rewi.hasNext();
209      fail("expected exception");
210    } catch (JDOFatalUserException jfue) {
211      // good
212      assertTrue(jfue.getCause() instanceof IllegalArgumentException);
213      assertEquals(jfue.getCause().getMessage(), "boom");
214      assertTrue(receivedException);
215      receivedException = false;
216    }
217    try {
218      rewi.next();
219      fail("expected exception");
220    } catch (JDOFatalUserException jfue) {
221      // good
222      assertTrue(jfue.getCause() instanceof IllegalArgumentException);
223      assertEquals(jfue.getCause().getMessage(), "boom");
224      assertTrue(receivedException);
225      receivedException = false;
226    }
227    try {
228      rewi.remove();
229      fail("expected exception");
230    } catch (JDOFatalUserException jfue) {
231      // good
232      assertTrue(jfue.getCause() instanceof IllegalArgumentException);
233      assertEquals(jfue.getCause().getMessage(), "boom");
234      assertTrue(receivedException);
235      receivedException = false;
236    }
237  }
238
239  public void testExceptionsJDO_DatastoreFailure() {
240    setUpIterator(new DatastoreFailureException("boom"));
241    ApiAdapter api = new JDOAdapter();
242    RuntimeExceptionWrappingIterator rewi =
243        new RuntimeExceptionWrappingIterator(api, iter, observer);
244    try {
245      rewi.hasNext();
246      fail("expected exception");
247    } catch (JDODataStoreException jdse) {
248      // good
249      assertTrue(jdse.getCause() instanceof DatastoreFailureException);
250      assertEquals(jdse.getCause().getMessage(), "boom");
251      assertTrue(receivedException);
252      receivedException = false;
253    }
254    try {
255      rewi.next();
256      fail("expected exception");
257    } catch (JDODataStoreException jdse) {
258      // good
259      assertTrue(jdse.getCause() instanceof DatastoreFailureException);
260      assertEquals(jdse.getCause().getMessage(), "boom");
261      assertTrue(receivedException);
262      receivedException = false;
263    }
264    try {
265      rewi.remove();
266      fail("expected exception");
267    } catch (JDODataStoreException jdse) {
268      // good
269      assertTrue(jdse.getCause() instanceof DatastoreFailureException);
270      assertEquals(jdse.getCause().getMessage(), "boom");
271      assertTrue(receivedException);
272      receivedException = false;
273    }
274  }
275
276  public void testExceptionsJDO_Timeout() {
277    setUpIterator(new DatastoreTimeoutException("boom"));
278    ApiAdapter api = new JDOAdapter();
279    RuntimeExceptionWrappingIterator rewi =
280        new RuntimeExceptionWrappingIterator(api, iter, observer);
281    try {
282      rewi.hasNext();
283      fail("expected exception");
284    } catch (JDODataStoreException jqte) {
285      // good
286      assertTrue(jqte.getCause() instanceof QueryTimeoutException);
287      assertTrue(jqte.getCause().getCause() instanceof DatastoreTimeoutException);
288      assertEquals(jqte.getCause().getCause().getMessage(), "boom");
289      assertTrue(receivedException);
290      receivedException = false;
291    }
292    try {
293      rewi.next();
294      fail("expected exception");
295    } catch (JDODataStoreException jqte) {
296      // good
297      assertTrue(jqte.getCause() instanceof QueryTimeoutException);
298      assertTrue(jqte.getCause().getCause() instanceof DatastoreTimeoutException);
299      assertEquals(jqte.getCause().getCause().getMessage(), "boom");
300      assertTrue(receivedException);
301      receivedException = false;
302    }
303    try {
304      rewi.remove();
305      fail("expected exception");
306    } catch (JDODataStoreException jqte) {
307      // good
308      assertTrue(jqte.getCause() instanceof QueryTimeoutException);
309      assertTrue(jqte.getCause().getCause() instanceof DatastoreTimeoutException);
310      assertEquals(jqte.getCause().getCause().getMessage(), "boom");
311      assertTrue(receivedException);
312      receivedException = false;
313    }
314  }
315}