|
18 | 18 |
|
19 | 19 | import android.support.test.runner.AndroidJUnit4; |
20 | 20 |
|
| 21 | +import org.hamcrest.Matchers; |
21 | 22 | import org.junit.After; |
22 | 23 | import org.junit.Before; |
23 | 24 | import org.junit.Rule; |
|
56 | 57 | import static org.junit.Assert.assertFalse; |
57 | 58 | import static org.junit.Assert.assertNotNull; |
58 | 59 | import static org.junit.Assert.assertNull; |
| 60 | +import static org.junit.Assert.assertSame; |
| 61 | +import static org.junit.Assert.assertThat; |
59 | 62 | import static org.junit.Assert.assertTrue; |
60 | 63 | import static org.junit.Assert.fail; |
61 | 64 |
|
@@ -1158,7 +1161,11 @@ public void getFieldNames() { |
1158 | 1161 | AllJavaTypes.FIELD_DOUBLE, AllJavaTypes.FIELD_BOOLEAN, AllJavaTypes.FIELD_DATE, |
1159 | 1162 | AllJavaTypes.FIELD_BINARY, AllJavaTypes.FIELD_OBJECT, AllJavaTypes.FIELD_LIST}; |
1160 | 1163 | String[] keys = dObjTyped.getFieldNames(); |
1161 | | - assertArrayEquals(expectedKeys, keys); |
| 1164 | + // After the stable ID support, primary key field will be inserted first before others. So even FIELD_STRING is |
| 1165 | + // the first defined field in the class, it will be inserted after FIELD_ID. |
| 1166 | + // See ObjectStore::add_initial_columns #if REALM_HAVE_SYNC_STABLE_IDS branch. |
| 1167 | + assertEquals(expectedKeys.length, keys.length); |
| 1168 | + assertThat(Arrays.asList(expectedKeys), Matchers.hasItems(keys)); |
1162 | 1169 | } |
1163 | 1170 |
|
1164 | 1171 | @Test |
@@ -1259,4 +1266,102 @@ public void testExceptionMessage() { |
1259 | 1266 | assertEquals("Illegal Argument: Field not found: nonExisting", e.getMessage()); |
1260 | 1267 | } |
1261 | 1268 | } |
| 1269 | + |
| 1270 | + @Test |
| 1271 | + public void getDynamicRealm() { |
| 1272 | + realm.beginTransaction(); |
| 1273 | + realm.createObject(AllTypes.class); |
| 1274 | + realm.commitTransaction(); |
| 1275 | + |
| 1276 | + dynamicRealm.refresh(); |
| 1277 | + final DynamicRealmObject object = dynamicRealm.where(AllTypes.CLASS_NAME).findFirst(); |
| 1278 | + |
| 1279 | + assertSame(dynamicRealm, object.getDynamicRealm()); |
| 1280 | + } |
| 1281 | + |
| 1282 | + @Test |
| 1283 | + public void getRealm() { |
| 1284 | + realm.beginTransaction(); |
| 1285 | + realm.createObject(AllTypes.class); |
| 1286 | + realm.commitTransaction(); |
| 1287 | + |
| 1288 | + dynamicRealm.refresh(); |
| 1289 | + final DynamicRealmObject object = dynamicRealm.where(AllTypes.CLASS_NAME).findFirst(); |
| 1290 | + |
| 1291 | + thrown.expect(IllegalStateException.class); |
| 1292 | + object.getRealm(); |
| 1293 | + } |
| 1294 | + |
| 1295 | + @Test |
| 1296 | + public void getRealm_closedObjectThrows() { |
| 1297 | + realm.beginTransaction(); |
| 1298 | + realm.createObject(AllTypes.class); |
| 1299 | + realm.commitTransaction(); |
| 1300 | + |
| 1301 | + dynamicRealm.refresh(); |
| 1302 | + final DynamicRealmObject object = dynamicRealm.where(AllTypes.CLASS_NAME).findFirst(); |
| 1303 | + dynamicRealm.close(); |
| 1304 | + dynamicRealm = null; |
| 1305 | + |
| 1306 | + try { |
| 1307 | + object.getDynamicRealm(); |
| 1308 | + fail(); |
| 1309 | + } catch (IllegalStateException e) { |
| 1310 | + assertEquals(BaseRealm.CLOSED_REALM_MESSAGE, e.getMessage()); |
| 1311 | + } |
| 1312 | + } |
| 1313 | + |
| 1314 | + @Test |
| 1315 | + public void getRealmConfiguration_deletedObjectThrows() { |
| 1316 | + realm.beginTransaction(); |
| 1317 | + realm.createObject(AllTypes.class); |
| 1318 | + realm.commitTransaction(); |
| 1319 | + |
| 1320 | + dynamicRealm.refresh(); |
| 1321 | + final DynamicRealmObject object = dynamicRealm.where(AllTypes.CLASS_NAME).findFirst(); |
| 1322 | + dynamicRealm.beginTransaction(); |
| 1323 | + object.deleteFromRealm(); |
| 1324 | + dynamicRealm.commitTransaction(); |
| 1325 | + |
| 1326 | + try { |
| 1327 | + object.getDynamicRealm(); |
| 1328 | + fail(); |
| 1329 | + } catch (IllegalStateException e) { |
| 1330 | + assertEquals(RealmObject.MSG_DELETED_OBJECT, e.getMessage()); |
| 1331 | + } |
| 1332 | + } |
| 1333 | + |
| 1334 | + @Test |
| 1335 | + public void getRealm_illegalThreadThrows() throws Throwable { |
| 1336 | + realm.beginTransaction(); |
| 1337 | + realm.createObject(AllTypes.class); |
| 1338 | + realm.commitTransaction(); |
| 1339 | + |
| 1340 | + dynamicRealm.refresh(); |
| 1341 | + final DynamicRealmObject object = dynamicRealm.where(AllTypes.CLASS_NAME).findFirst(); |
| 1342 | + |
| 1343 | + final CountDownLatch threadFinished = new CountDownLatch(1); |
| 1344 | + final AtomicReference<Throwable> throwable = new AtomicReference<>(); |
| 1345 | + final Thread thread = new Thread(new Runnable() { |
| 1346 | + @Override |
| 1347 | + public void run() { |
| 1348 | + try { |
| 1349 | + object.getDynamicRealm(); |
| 1350 | + fail(); |
| 1351 | + } catch (Throwable t) { |
| 1352 | + throwable.set(t); |
| 1353 | + } finally { |
| 1354 | + threadFinished.countDown(); |
| 1355 | + } |
| 1356 | + } |
| 1357 | + }); |
| 1358 | + thread.start(); |
| 1359 | + TestHelper.awaitOrFail(threadFinished); |
| 1360 | + |
| 1361 | + final Throwable thrownInTheThread = throwable.get(); |
| 1362 | + if (!(thrownInTheThread instanceof IllegalStateException)) { |
| 1363 | + throw thrownInTheThread; |
| 1364 | + } |
| 1365 | + assertEquals(BaseRealm.INCORRECT_THREAD_MESSAGE, thrownInTheThread.getMessage()); |
| 1366 | + } |
1262 | 1367 | } |
0 commit comments