Skip to content

Commit bf55acd

Browse files
BoxStore: do not warn on (Java)/allow null (Kotlin) DbExceptionListener.
1 parent fde02ef commit bf55acd

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

objectbox-java/src/main/java/io/objectbox/BoxStore.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package io.objectbox;
1818

19-
import io.objectbox.annotation.apihint.Beta;
2019
import org.greenrobot.essentials.collections.LongHashMap;
2120

2221
import java.io.Closeable;
@@ -40,6 +39,7 @@
4039
import javax.annotation.Nullable;
4140
import javax.annotation.concurrent.ThreadSafe;
4241

42+
import io.objectbox.annotation.apihint.Beta;
4343
import io.objectbox.annotation.apihint.Experimental;
4444
import io.objectbox.annotation.apihint.Internal;
4545
import io.objectbox.converter.PropertyConverter;
@@ -165,7 +165,7 @@ static native void nativeRegisterCustomType(long store, int entityId, int proper
165165

166166
static native int nativeCleanStaleReadTransactions(long store);
167167

168-
static native void nativeSetDbExceptionListener(long store, DbExceptionListener dbExceptionListener);
168+
static native void nativeSetDbExceptionListener(long store, @Nullable DbExceptionListener dbExceptionListener);
169169

170170
static native void nativeSetDebugFlags(long store, int debugFlags);
171171

@@ -1027,10 +1027,12 @@ private void verifyObjectBrowserNotRunning() {
10271027
}
10281028

10291029
/**
1030-
* The given listener will be called when an exception is thrown.
1031-
* This for example allows a central error handling, e.g. a special logging for DB related exceptions.
1030+
* Sets a listener that will be called when an exception is thrown. Replaces a previously set listener.
1031+
* Set to {@code null} to remove the listener.
1032+
* <p>
1033+
* This for example allows central error handling or special logging for database-related exceptions.
10321034
*/
1033-
public void setDbExceptionListener(DbExceptionListener dbExceptionListener) {
1035+
public void setDbExceptionListener(@Nullable DbExceptionListener dbExceptionListener) {
10341036
nativeSetDbExceptionListener(handle, dbExceptionListener);
10351037
}
10361038

tests/objectbox-java-test/src/test/java/io/objectbox/exception/ExceptionTest.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,64 @@
1616

1717
package io.objectbox.exception;
1818

19-
import io.objectbox.AbstractObjectBoxTest;
2019
import org.junit.Test;
2120

2221
import java.util.ArrayList;
2322
import java.util.List;
23+
import java.util.concurrent.atomic.AtomicBoolean;
24+
25+
import io.objectbox.AbstractObjectBoxTest;
26+
2427

2528
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;
2632

33+
/**
34+
* Tests related to {@link DbExceptionListener}.
35+
*/
2736
public class ExceptionTest extends AbstractObjectBoxTest {
2837

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+
2977
@Test
3078
public void testThrowExceptions() {
3179
final List<Exception> exs = new ArrayList<>();

0 commit comments

Comments
 (0)