|
16 | 16 |
|
17 | 17 | package io.objectbox.exception;
|
18 | 18 |
|
19 |
| -import io.objectbox.AbstractObjectBoxTest; |
20 | 19 | import org.junit.Test;
|
21 | 20 |
|
22 | 21 | import java.util.ArrayList;
|
23 | 22 | import java.util.List;
|
| 23 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 24 | + |
| 25 | +import io.objectbox.AbstractObjectBoxTest; |
| 26 | + |
24 | 27 |
|
25 | 28 | import static org.junit.Assert.assertEquals;
|
| 29 | +import static org.junit.Assert.assertFalse; |
| 30 | +import static org.junit.Assert.assertThrows; |
| 31 | +import static org.junit.Assert.assertTrue; |
26 | 32 |
|
| 33 | +/** |
| 34 | + * Tests related to {@link DbExceptionListener}. |
| 35 | + */ |
27 | 36 | public class ExceptionTest extends AbstractObjectBoxTest {
|
28 | 37 |
|
| 38 | + @Test |
| 39 | + public void exceptionListener_null_works() { |
| 40 | + store.setDbExceptionListener(null); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void exceptionListener_removing_works() { |
| 45 | + AtomicBoolean replacedListenerCalled = new AtomicBoolean(false); |
| 46 | + DbExceptionListener listenerRemoved = e -> replacedListenerCalled.set(true); |
| 47 | + |
| 48 | + store.setDbExceptionListener(listenerRemoved); |
| 49 | + store.setDbExceptionListener(null); |
| 50 | + |
| 51 | + assertThrows( |
| 52 | + DbException.class, |
| 53 | + () -> DbExceptionListenerJni.nativeThrowException(store.getNativeStore(), 0) |
| 54 | + ); |
| 55 | + assertFalse("Replaced DbExceptionListener was called.", replacedListenerCalled.get()); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void exceptionListener_replacing_works() { |
| 60 | + AtomicBoolean replacedListenerCalled = new AtomicBoolean(false); |
| 61 | + DbExceptionListener listenerReplaced = e -> replacedListenerCalled.set(true); |
| 62 | + |
| 63 | + AtomicBoolean newListenerCalled = new AtomicBoolean(false); |
| 64 | + DbExceptionListener listenerNew = e -> newListenerCalled.set(true); |
| 65 | + |
| 66 | + store.setDbExceptionListener(listenerReplaced); |
| 67 | + store.setDbExceptionListener(listenerNew); |
| 68 | + |
| 69 | + assertThrows( |
| 70 | + DbException.class, |
| 71 | + () -> DbExceptionListenerJni.nativeThrowException(store.getNativeStore(), 0) |
| 72 | + ); |
| 73 | + assertFalse("Replaced DbExceptionListener was called.", replacedListenerCalled.get()); |
| 74 | + assertTrue("New DbExceptionListener was NOT called.", newListenerCalled.get()); |
| 75 | + } |
| 76 | + |
29 | 77 | @Test
|
30 | 78 | public void testThrowExceptions() {
|
31 | 79 | final List<Exception> exs = new ArrayList<>();
|
|
0 commit comments