1+ /*
2+ * Copyright 2020 ObjectBox Ltd. 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 io .objectbox .exception ;
18+
19+ import io .objectbox .AbstractObjectBoxTest ;
20+ import org .junit .Test ;
21+
22+ import java .util .ArrayList ;
23+ import java .util .List ;
24+
25+ import static org .junit .Assert .assertEquals ;
26+
27+ public class ExceptionTest extends AbstractObjectBoxTest {
28+
29+ @ Test
30+ public void testThrowExceptions () {
31+ final List <Exception > exs = new ArrayList <>();
32+ DbExceptionListener exceptionListener = e -> {
33+ exs .add (e );
34+ DbExceptionListener .cancelCurrentException ();
35+ };
36+ store .setDbExceptionListener (exceptionListener );
37+ int maxExNo = 10 ;
38+ for (int exNo = 0 ; exNo <= maxExNo ; exNo ++) {
39+ DbExceptionListenerJni .nativeThrowException (store .getNativeStore (), exNo );
40+ }
41+ int expectedSize = maxExNo + 1 ;
42+ assertEquals (expectedSize , exs .size ());
43+ Class <?>[] expectedClasses = {
44+ DbException .class ,
45+ IllegalStateException .class ,
46+ DbException .class , // OpenDb
47+ DbFullException .class ,
48+ DbShutdownException .class ,
49+ DbSchemaException .class ,
50+ ConstraintViolationException .class ,
51+ UniqueViolationException .class ,
52+ FileCorruptException .class ,
53+ PagesCorruptException .class ,
54+ IllegalArgumentException .class ,
55+ };
56+ assertEquals (expectedSize , expectedClasses .length );
57+ for (int i = 0 ; i < expectedSize ; i ++) {
58+ assertEquals (expectedClasses [i ], exs .get (i ).getClass ());
59+ }
60+ }
61+
62+ }
0 commit comments