Search in sources :

Example 91 with AllTypes

use of io.realm.entities.AllTypes in project realm-java by realm.

the class RealmTests method copyToRealm_fromOtherRealm.

@Test
public void copyToRealm_fromOtherRealm() {
    realm.beginTransaction();
    AllTypes allTypes = realm.createObject(AllTypes.class);
    allTypes.setColumnString("Test");
    allTypes.setColumnDecimal128(new Decimal128(new BigDecimal("12345")));
    allTypes.setColumnObjectId(new ObjectId(TestHelper.randomObjectIdHexString()));
    allTypes.setColumnUUID(UUID.randomUUID());
    allTypes.setColumnRealmAny(RealmAny.valueOf(UUID.randomUUID()));
    realm.commitTransaction();
    RealmConfiguration realmConfig = configFactory.createConfiguration("other-realm");
    Realm otherRealm = Realm.getInstance(realmConfig);
    otherRealm.beginTransaction();
    AllTypes copiedAllTypes = otherRealm.copyToRealm(allTypes);
    otherRealm.commitTransaction();
    // Same object in different Realms is not the same.
    assertNotSame(allTypes, copiedAllTypes);
    // But data is still the same.
    assertEquals(allTypes.getColumnString(), copiedAllTypes.getColumnString());
    otherRealm.close();
}
Also used : ObjectId(org.bson.types.ObjectId) AllTypes(io.realm.entities.AllTypes) Decimal128(org.bson.types.Decimal128) OsSharedRealm(io.realm.internal.OsSharedRealm) BigDecimal(java.math.BigDecimal) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 92 with AllTypes

use of io.realm.entities.AllTypes in project realm-java by realm.

the class RealmObjectTests method isValid_managedObject.

@Test
public void isValid_managedObject() {
    realm.beginTransaction();
    AllTypes allTypes = realm.createObject(AllTypes.class);
    assertTrue(allTypes.isValid());
    realm.commitTransaction();
    assertTrue(allTypes.isValid());
}
Also used : AllTypes(io.realm.entities.AllTypes) Test(org.junit.Test)

Example 93 with AllTypes

use of io.realm.entities.AllTypes in project realm-java by realm.

the class RealmObjectTests method getter_afterDeleteFromOtherThreadThrows.

@Test
public void getter_afterDeleteFromOtherThreadThrows() {
    final CountDownLatch bgRealmDone = new CountDownLatch(1);
    realm.beginTransaction();
    final AllTypes obj = realm.createObject(AllTypes.class);
    realm.commitTransaction();
    new Thread(new Runnable() {

        @Override
        public void run() {
            Realm bgRealm = Realm.getInstance(realm.getConfiguration());
            bgRealm.beginTransaction();
            bgRealm.delete(AllTypes.class);
            bgRealm.commitTransaction();
            bgRealm.close();
            bgRealmDone.countDown();
        }
    }).start();
    TestHelper.awaitOrFail(bgRealmDone);
    realm.refresh();
    // Object should no longer be available.
    assertFalse(obj.isValid());
    try {
        obj.getColumnLong();
        fail();
    } catch (IllegalStateException ignored) {
    }
}
Also used : AllTypes(io.realm.entities.AllTypes) CountDownLatch(java.util.concurrent.CountDownLatch) RunInLooperThread(io.realm.rule.RunInLooperThread) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 94 with AllTypes

use of io.realm.entities.AllTypes in project realm-java by realm.

the class RealmObjectTests method invalidSurrogates.

// Invalid surrogate pairs:
// Both high and low should lead to an IllegalArgumentException.
@Test
public void invalidSurrogates() {
    String high = "Invalid high surrogate \uD83C\uD83C\uDF51";
    String low = "Invalid low surrogate \uD83C\uDF51\uDF51";
    realm.beginTransaction();
    realm.delete(AllTypes.class);
    realm.commitTransaction();
    realm.beginTransaction();
    try {
        AllTypes highSurrogate = realm.createObject(AllTypes.class);
        highSurrogate.setColumnString(high);
        fail();
    } catch (IllegalArgumentException ignored) {
    }
    realm.cancelTransaction();
    realm.beginTransaction();
    try {
        AllTypes lowSurrogate = realm.createObject(AllTypes.class);
        lowSurrogate.setColumnString(low);
        fail();
    } catch (IllegalArgumentException ignored) {
    }
    realm.cancelTransaction();
}
Also used : AllTypes(io.realm.entities.AllTypes) Test(org.junit.Test)

Example 95 with AllTypes

use of io.realm.entities.AllTypes in project realm-java by realm.

the class RealmObjectTests method runMethodOnWrongThread.

private boolean runMethodOnWrongThread(final Method method) throws ExecutionException, InterruptedException {
    final AllTypes allTypes = realm.where(AllTypes.class).findFirst();
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    Future<Boolean> future = executorService.submit(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            try {
                switch(method) {
                    case METHOD_GETTER:
                        allTypes.getColumnFloat();
                        break;
                    case METHOD_SETTER:
                        allTypes.setColumnFloat(1.0f);
                        break;
                    case METHOD_DELETE_FROM_REALM:
                        allTypes.deleteFromRealm();
                        break;
                }
                return false;
            } catch (IllegalStateException ignored) {
                return true;
            }
        }
    });
    return future.get();
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) AllTypes(io.realm.entities.AllTypes) FileNotFoundException(java.io.FileNotFoundException) RealmException(io.realm.exceptions.RealmException) ExpectedException(org.junit.rules.ExpectedException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

AllTypes (io.realm.entities.AllTypes)225 Test (org.junit.Test)210 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)70 UiThreadTest (androidx.test.annotation.UiThreadTest)54 Date (java.util.Date)36 DictionaryAllTypes (io.realm.entities.DictionaryAllTypes)32 Dog (io.realm.entities.Dog)27 JSONObject (org.json.JSONObject)26 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)22 RunInLooperThread (io.realm.rule.RunInLooperThread)20 InputStream (java.io.InputStream)19 Decimal128 (org.bson.types.Decimal128)19 ByteArrayInputStream (java.io.ByteArrayInputStream)17 ObjectId (org.bson.types.ObjectId)15 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 UiThreadTest (android.support.test.annotation.UiThreadTest)10 AtomicLong (java.util.concurrent.atomic.AtomicLong)10 OsSharedRealm (io.realm.internal.OsSharedRealm)9 BigDecimal (java.math.BigDecimal)9 NonLatinFieldNames (io.realm.entities.NonLatinFieldNames)8