|
36 | 36 | import java.util.Arrays;
|
37 | 37 | import java.util.Date;
|
38 | 38 | import java.util.List;
|
| 39 | +import java.util.concurrent.RejectedExecutionException; |
39 | 40 |
|
40 | 41 | import static io.objectbox.TestEntity_.simpleBoolean;
|
41 | 42 | import static io.objectbox.TestEntity_.simpleByteArray;
|
|
45 | 46 | import static io.objectbox.TestEntity_.simpleShort;
|
46 | 47 | import static io.objectbox.TestEntity_.simpleString;
|
47 | 48 | import static io.objectbox.TestEntity_.simpleStringArray;
|
| 49 | +import static io.objectbox.TestEntity_.stringObjectMap; |
48 | 50 | import static org.junit.Assert.assertArrayEquals;
|
49 | 51 | import static org.junit.Assert.assertEquals;
|
50 | 52 | import static org.junit.Assert.assertFalse;
|
@@ -137,6 +139,55 @@ private void assertThrowsQueryIsClosed(ThrowingRunnable runnable) {
|
137 | 139 | assertEquals("This query is closed. Build and use a new one.", ex.getMessage());
|
138 | 140 | }
|
139 | 141 |
|
| 142 | + @Test |
| 143 | + public void useAfterStoreClose_failsIfUsingStore() { |
| 144 | + Query<TestEntity> query = box.query( |
| 145 | + simpleString.equal("") |
| 146 | + .and(stringObjectMap.containsKeyValue("", "")) |
| 147 | + .and(simpleInt.equal(0)) |
| 148 | + .and(simpleInt.oneOf(new int[]{0}).alias("oneOf4")) |
| 149 | + .and(simpleLong.oneOf(new long[]{0}).alias("oneOf8")) |
| 150 | + .and(simpleInt.between(0, 0).alias("between")) |
| 151 | + .and(simpleString.oneOf(new String[]{""}).alias("oneOfS")) |
| 152 | + .and(simpleByteArray.equal(new byte[]{0})) |
| 153 | + ).build(); |
| 154 | + store.close(); |
| 155 | + |
| 156 | + assertThrowsStoreIsClosed(query::count); |
| 157 | + assertThrowsStoreIsClosed(query::find); |
| 158 | + assertThrowsStoreIsClosed(() -> query.find(0, 1)); |
| 159 | + assertThrowsStoreIsClosed(query::findFirst); |
| 160 | + assertThrowsStoreIsClosed(query::findIds); |
| 161 | + assertThrowsStoreIsClosed(() -> query.findIds(0, 1)); |
| 162 | + assertThrowsStoreIsClosed(query::findLazy); |
| 163 | + assertThrowsStoreIsClosed(query::findLazyCached); |
| 164 | + assertThrowsStoreIsClosed(query::findUnique); |
| 165 | + assertThrowsStoreIsClosed(query::remove); |
| 166 | + |
| 167 | + // describe and setParameter continue to work as store is not accessed. |
| 168 | + assertFalse(query.describe().isEmpty()); |
| 169 | + assertFalse(query.describeParameters().isEmpty()); |
| 170 | + query.setParameter(simpleString, "value"); |
| 171 | + query.setParameters(stringObjectMap, "a", "b"); |
| 172 | + query.setParameter(simpleInt, 1); |
| 173 | + query.setParameters("oneOf4", new int[]{1, 2}); |
| 174 | + query.setParameters("oneOf8", new long[]{1, 2}); |
| 175 | + query.setParameters("between", 1, 2); |
| 176 | + query.setParameter(simpleInt, 1.0); |
| 177 | + query.setParameters("between", 1.0, 2.0); |
| 178 | + query.setParameters("oneOfS", new String[]{"a", "b"}); |
| 179 | + query.setParameter(simpleByteArray, new byte[]{1, 2}); |
| 180 | + |
| 181 | + // Internal thread pool is shut down as part of closing store, should no longer accept new work. |
| 182 | + assertThrows(RejectedExecutionException.class, () -> query.subscribe().observer(data -> { |
| 183 | + })); |
| 184 | + } |
| 185 | + |
| 186 | + private void assertThrowsStoreIsClosed(ThrowingRunnable runnable) { |
| 187 | + IllegalStateException ex = assertThrows(IllegalStateException.class, runnable); |
| 188 | + assertEquals("Store is closed", ex.getMessage()); |
| 189 | + } |
| 190 | + |
140 | 191 | @Test
|
141 | 192 | public void testNullNotNull() {
|
142 | 193 | List<TestEntity> scalars = putTestEntitiesScalars();
|
|
0 commit comments