/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. */
  16. package com.google.appengine.datanucleus.query;
  17. import com.google.appengine.api.datastore.DatastoreFailureException;
  18. import com.google.appengine.api.datastore.DatastoreTimeoutException;
  19. import com.google.appengine.api.datastore.Entity;
  20. import com.google.appengine.datanucleus.DatastoreTestCase;
  21. import org.datanucleus.api.ApiAdapter;
  22. import org.datanucleus.api.jdo.JDOAdapter;
  23. import org.datanucleus.api.jpa.JPAAdapter;
  24. import org.datanucleus.store.query.QueryTimeoutException;
  25. import org.easymock.EasyMock;
  26. import java.util.Arrays;
  27. import java.util.Iterator;
  28. import javax.jdo.JDODataStoreException;
  29. import javax.jdo.JDOFatalUserException;
  30. import javax.persistence.PersistenceException;
  31. /**
  32. * @author Max Ross <max.ross@gmail.com>
  33. */
  34. public class RuntimeExceptionWrappingIteratorTest extends DatastoreTestCase {
  35. private boolean receivedException = false;
  36. private RuntimeExceptionObserver observer = new RuntimeExceptionObserver() {
  37. public void onException() {
  38. receivedException = true;
  39. }
  40. };
  41. private Iterator<Entity> iter;
  42. protected void setUpIterator(RuntimeException rte) {
  43. receivedException = false;
  44. iter = EasyMock.createMock(Iterator.class);
  45. EasyMock.expect(iter.hasNext()).andThrow(rte);
  46. EasyMock.expect(iter.next()).andThrow(rte);
  47. iter.remove();
  48. EasyMock.expectLastCall().andThrow(rte);
  49. EasyMock.replay(iter);
  50. }
  51. public void testNoExceptionsJPA() {
  52. Entity e1 = new Entity("foo");
  53. Entity e2 = new Entity("foo");
  54. Entity e3 = new Entity("foo");
  55. ApiAdapter api = new JPAAdapter();
  56. RuntimeExceptionWrappingIterator rewi =
  57. new RuntimeExceptionWrappingIterator(api, Arrays.asList(e1, e2, e3).iterator(), observer);
  58. int count = 0;
  59. while (rewi.hasNext()) {
  60. rewi.next();
  61. count++;
  62. }
  63. assertEquals(3, count);
  64. }
  65. public void testExceptionsJPA_IllegalArg() {
  66. setUpIterator(new IllegalArgumentException("boom"));
  67. ApiAdapter api = new JPAAdapter();
  68. RuntimeExceptionWrappingIterator rewi =
  69. new RuntimeExceptionWrappingIterator(api, iter, observer);
  70. try {
  71. rewi.hasNext();
  72. fail("expected exception");
  73. } catch (PersistenceException pe) {
  74. // good
  75. assertTrue(pe.getCause() instanceof IllegalArgumentException);
  76. assertEquals(pe.getCause().getMessage(), "boom");
  77. assertTrue(receivedException);
  78. receivedException = false;
  79. }
  80. try {
  81. rewi.next();
  82. fail("expected exception");
  83. } catch (PersistenceException pe) {
  84. // good
  85. assertTrue(pe.getCause() instanceof IllegalArgumentException);
  86. assertEquals(pe.getCause().getMessage(), "boom");
  87. assertTrue(receivedException);
  88. receivedException = false;
  89. }
  90. try {
  91. rewi.remove();
  92. fail("expected exception");
  93. } catch (PersistenceException pe) {
  94. // good
  95. assertTrue(pe.getCause() instanceof IllegalArgumentException);
  96. assertEquals(pe.getCause().getMessage(), "boom");
  97. assertTrue(receivedException);
  98. receivedException = false;
  99. }
  100. }
  101. public void testExceptionsJPA_DatastoreFailure() {
  102. setUpIterator(new DatastoreFailureException("boom"));
  103. ApiAdapter api = new JPAAdapter();
  104. RuntimeExceptionWrappingIterator rewi =
  105. new RuntimeExceptionWrappingIterator(api, iter, observer);
  106. try {
  107. rewi.hasNext();
  108. fail("expected exception");
  109. } catch (PersistenceException pe) {
  110. // good
  111. assertTrue(pe.getCause() instanceof DatastoreFailureException);
  112. assertEquals(pe.getCause().getMessage(), "boom");
  113. assertTrue(receivedException);
  114. receivedException = false;
  115. }
  116. try {
  117. rewi.next();
  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.remove();
  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. }
  137. public void testExceptionsJPA_Timeout() {
  138. setUpIterator(new DatastoreTimeoutException("boom"));
  139. ApiAdapter api = new JPAAdapter();
  140. RuntimeExceptionWrappingIterator rewi =
  141. new RuntimeExceptionWrappingIterator(api, iter, observer);
  142. try {
  143. rewi.hasNext();
  144. fail("expected exception");
  145. } catch (javax.persistence.QueryTimeoutException qte) {
  146. // good
  147. assertTrue(qte.getCause() instanceof QueryTimeoutException);
  148. assertTrue(qte.getCause().getCause() instanceof DatastoreTimeoutException);
  149. assertEquals(qte.getCause().getCause().getMessage(), "boom");
  150. assertTrue(receivedException);
  151. receivedException = false;
  152. }
  153. try {
  154. rewi.next();
  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.remove();
  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. }
  176. public void testNoExceptionsJDO() {
  177. Entity e1 = new Entity("foo");
  178. Entity e2 = new Entity("foo");
  179. Entity e3 = new Entity("foo");
  180. ApiAdapter api = new JDOAdapter();
  181. RuntimeExceptionWrappingIterator rewi =
  182. new RuntimeExceptionWrappingIterator(api, Arrays.asList(e1, e2, e3).iterator(), observer);
  183. int count = 0;
  184. while (rewi.hasNext()) {
  185. rewi.next();
  186. count++;
  187. }
  188. assertEquals(3, count);
  189. }
  190. public void testExceptionsJDO_IllegalArg() {
  191. setUpIterator(new IllegalArgumentException("boom"));
  192. ApiAdapter api = new JDOAdapter();
  193. RuntimeExceptionWrappingIterator rewi =
  194. new RuntimeExceptionWrappingIterator(api, iter, observer);
  195. try {
  196. rewi.hasNext();
  197. fail("expected exception");
  198. } catch (JDOFatalUserException jfue) {
  199. // good
  200. assertTrue(jfue.getCause() instanceof IllegalArgumentException);
  201. assertEquals(jfue.getCause().getMessage(), "boom");
  202. assertTrue(receivedException);
  203. receivedException = false;
  204. }
  205. try {
  206. rewi.next();
  207. fail("expected exception");
  208. } catch (JDOFatalUserException jfue) {
  209. // good
  210. assertTrue(jfue.getCause() instanceof IllegalArgumentException);
  211. assertEquals(jfue.getCause().getMessage(), "boom");
  212. assertTrue(receivedException);
  213. receivedException = false;
  214. }
  215. try {
  216. rewi.remove();
  217. fail("expected exception");
  218. } catch (JDOFatalUserException jfue) {
  219. // good
  220. assertTrue(jfue.getCause() instanceof IllegalArgumentException);
  221. assertEquals(jfue.getCause().getMessage(), "boom");
  222. assertTrue(receivedException);
  223. receivedException = false;
  224. }
  225. }
  226. public void testExceptionsJDO_DatastoreFailure() {
  227. setUpIterator(new DatastoreFailureException("boom"));
  228. ApiAdapter api = new JDOAdapter();
  229. RuntimeExceptionWrappingIterator rewi =
  230. new RuntimeExceptionWrappingIterator(api, iter, observer);
  231. try {
  232. rewi.hasNext();
  233. fail("expected exception");
  234. } catch (JDODataStoreException jdse) {
  235. // good
  236. assertTrue(jdse.getCause() instanceof DatastoreFailureException);
  237. assertEquals(jdse.getCause().getMessage(), "boom");
  238. assertTrue(receivedException);
  239. receivedException = false;
  240. }
  241. try {
  242. rewi.next();
  243. fail("expected exception");
  244. } catch (JDODataStoreException jdse) {
  245. // good
  246. assertTrue(jdse.getCause() instanceof DatastoreFailureException);
  247. assertEquals(jdse.getCause().getMessage(), "boom");
  248. assertTrue(receivedException);
  249. receivedException = false;
  250. }
  251. try {
  252. rewi.remove();
  253. fail("expected exception");
  254. } catch (JDODataStoreException jdse) {
  255. // good
  256. assertTrue(jdse.getCause() instanceof DatastoreFailureException);
  257. assertEquals(jdse.getCause().getMessage(), "boom");
  258. assertTrue(receivedException);
  259. receivedException = false;
  260. }
  261. }
  262. public void testExceptionsJDO_Timeout() {
  263. setUpIterator(new DatastoreTimeoutException("boom"));
  264. ApiAdapter api = new JDOAdapter();
  265. RuntimeExceptionWrappingIterator rewi =
  266. new RuntimeExceptionWrappingIterator(api, iter, observer);
  267. try {
  268. rewi.hasNext();
  269. fail("expected exception");
  270. } catch (JDODataStoreException jqte) {
  271. // good
  272. assertTrue(jqte.getCause() instanceof QueryTimeoutException);
  273. assertTrue(jqte.getCause().getCause() instanceof DatastoreTimeoutException);
  274. assertEquals(jqte.getCause().getCause().getMessage(), "boom");
  275. assertTrue(receivedException);
  276. receivedException = false;
  277. }
  278. try {
  279. rewi.next();
  280. fail("expected exception");
  281. } catch (JDODataStoreException jqte) {
  282. // good
  283. assertTrue(jqte.getCause() instanceof QueryTimeoutException);
  284. assertTrue(jqte.getCause().getCause() instanceof DatastoreTimeoutException);
  285. assertEquals(jqte.getCause().getCause().getMessage(), "boom");
  286. assertTrue(receivedException);
  287. receivedException = false;
  288. }
  289. try {
  290. rewi.remove();
  291. fail("expected exception");
  292. } catch (JDODataStoreException jqte) {
  293. // good
  294. assertTrue(jqte.getCause() instanceof QueryTimeoutException);
  295. assertTrue(jqte.getCause().getCause() instanceof DatastoreTimeoutException);
  296. assertEquals(jqte.getCause().getCause().getMessage(), "boom");
  297. assertTrue(receivedException);
  298. receivedException = false;
  299. }
  300. }
  301. }