|
| 1 | +/* |
| 2 | + * Copyright 2017 Google Inc. All Rights Reserved. |
| 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 | + |
| 17 | +package com.google.cloud.datastore; |
| 18 | + |
| 19 | +import static junit.framework.TestCase.assertFalse; |
| 20 | +import static junit.framework.TestCase.assertTrue; |
| 21 | + |
| 22 | +import com.google.cloud.BaseServiceException; |
| 23 | +import com.google.cloud.ExceptionHandler; |
| 24 | +import org.junit.Rule; |
| 25 | +import org.junit.Test; |
| 26 | +import org.junit.rules.ExpectedException; |
| 27 | + |
| 28 | +/** Tests for {@link TransactionExceptionHandler}. */ |
| 29 | +public class TransactionExceptionHandlerTest { |
| 30 | + |
| 31 | + @Rule public ExpectedException thrown = ExpectedException.none(); |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testShouldTry() { |
| 35 | + ExceptionHandler handler = |
| 36 | + ExceptionHandler.newBuilder() |
| 37 | + .abortOn(RuntimeException.class) |
| 38 | + .addInterceptors(DatastoreImpl.EXCEPTION_HANDLER_INTERCEPTOR) |
| 39 | + .build(); |
| 40 | + ExceptionHandler transactionHandler = TransactionExceptionHandler.build(); |
| 41 | + |
| 42 | + assertFalse(handler.accept(new DatastoreException(10, "", "ABORTED", false, null))); |
| 43 | + assertFalse(handler.accept(new DatastoreException(10, "", "", false, null))); |
| 44 | + assertFalse(handler.accept(new DatastoreException(0, "", "", false, null))); |
| 45 | + |
| 46 | + assertTrue(transactionHandler.accept(new DatastoreException(10, "", "ABORTED", false, null))); |
| 47 | + assertTrue(transactionHandler.accept(new DatastoreException(10, "", "", false, null))); |
| 48 | + assertFalse(transactionHandler.accept(new DatastoreException(0, "", "", false, null))); |
| 49 | + |
| 50 | + DatastoreException nestedDatastoreException = |
| 51 | + new DatastoreException( |
| 52 | + BaseServiceException.UNKNOWN_CODE, |
| 53 | + "", |
| 54 | + null, |
| 55 | + new DatastoreException(10, "", "ABORTED", false, null)); |
| 56 | + |
| 57 | + assertTrue(transactionHandler.accept(nestedDatastoreException)); |
| 58 | + assertFalse(handler.accept(nestedDatastoreException)); |
| 59 | + } |
| 60 | +} |
0 commit comments