Skip to content

Commit c54e6cd

Browse files
committed
testing DbExceptionListener with tx and query
1 parent e506160 commit c54e6cd

3 files changed

Lines changed: 45 additions & 9 deletions

File tree

objectbox-java/src/main/java/io/objectbox/exception/DbExceptionListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
public interface DbExceptionListener {
2424
/**
2525
* Called when an exception is thrown during a database operation.
26-
* Do NOT throw exceptions in this method: behavior is undefined, e.g. all thrown exceptions may be ignored.
26+
* Do NOT throw exceptions in this method: throw exceptions are ignored (but logged to stderr).
2727
*
2828
* @param e the exception occurred during a database operation
2929
*/

tests/objectbox-java-test/src/main/java/io/objectbox/TransactionTest.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,10 @@
2828
import javax.annotation.Nullable;
2929

3030
import io.objectbox.exception.DbException;
31+
import io.objectbox.exception.DbExceptionListener;
3132
import io.objectbox.exception.DbMaxReadersExceededException;
3233

33-
34-
import static org.junit.Assert.assertArrayEquals;
35-
import static org.junit.Assert.assertEquals;
36-
import static org.junit.Assert.assertFalse;
37-
import static org.junit.Assert.assertNotNull;
38-
import static org.junit.Assert.assertNotSame;
39-
import static org.junit.Assert.assertTrue;
40-
import static org.junit.Assert.fail;
34+
import static org.junit.Assert.*;
4135

4236
public class TransactionTest extends AbstractObjectBoxTest {
4337

@@ -183,6 +177,26 @@ public void testCommitReadTxException() {
183177
}
184178
}
185179

180+
@Test
181+
public void testCommitReadTxException_exceptionListener() {
182+
final Exception[] exs = {null};
183+
DbExceptionListener exceptionListener = new DbExceptionListener() {
184+
@Override
185+
public void onDbException(Exception e) {
186+
exs[0] = e;
187+
}
188+
};
189+
Transaction tx = store.beginReadTx();
190+
store.setDbExceptionListener(exceptionListener);
191+
try {
192+
tx.commit();
193+
fail("Should have thrown");
194+
} catch (IllegalStateException e) {
195+
tx.abort();
196+
assertSame(e, exs[0]);
197+
}
198+
}
199+
186200
/*
187201
@Test
188202
public void testTransactionUsingAfterStoreClosed() {

tests/objectbox-java-test/src/main/java/io/objectbox/query/QueryTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import io.objectbox.TestEntity;
3434
import io.objectbox.TestEntity_;
3535
import io.objectbox.TxCallback;
36+
import io.objectbox.exception.DbException;
37+
import io.objectbox.exception.DbExceptionListener;
3638
import io.objectbox.query.QueryBuilder.StringOrder;
3739
import io.objectbox.relation.MyObjectBox;
3840
import io.objectbox.relation.Order;
@@ -521,6 +523,26 @@ public void testDateParam() {
521523
query.setParameter(Order_.date, now);
522524
}
523525

526+
@Test
527+
public void testFailedUnique_exceptionListener() {
528+
final Exception[] exs = {null};
529+
DbExceptionListener exceptionListener = new DbExceptionListener() {
530+
@Override
531+
public void onDbException(Exception e) {
532+
exs[0] = e;
533+
}
534+
};
535+
putTestEntitiesStrings();
536+
Query<TestEntity> query = box.query().build();
537+
store.setDbExceptionListener(exceptionListener);
538+
try {
539+
query.findUnique();
540+
fail("Should have thrown");
541+
} catch (DbException e) {
542+
assertSame(e, exs[0]);
543+
}
544+
}
545+
524546
private QueryFilter<TestEntity> createTestFilter() {
525547
return new QueryFilter<TestEntity>() {
526548
@Override

0 commit comments

Comments
 (0)