Search in sources :

Example 26 with AllJavaTypes

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

the class OrderedRealmCollectionTests method createCollectionWithMultipleCopies.

private Pair<AllJavaTypes, OrderedRealmCollection<AllJavaTypes>> createCollectionWithMultipleCopies(Realm realm, CollectionClass collectionClass) {
    OrderedRealmCollection<AllJavaTypes> orderedCollection;
    AllJavaTypes obj;
    switch(collectionClass) {
        case REALMRESULTS_SNAPSHOT_LIST_BASE:
        case MANAGED_REALMLIST:
            obj = realm.where(AllJavaTypes.class).equalTo(AllJavaTypes.FIELD_LONG, 1).findFirst();
            RealmList<AllJavaTypes> list = obj.getFieldList();
            realm.beginTransaction();
            list.add(obj);
            realm.commitTransaction();
            orderedCollection = list;
            break;
        case UNMANAGED_REALMLIST:
            obj = new AllJavaTypes(1);
            return new Pair<AllJavaTypes, OrderedRealmCollection<AllJavaTypes>>(obj, new RealmList<AllJavaTypes>(obj, obj));
        case REALMRESULTS_SNAPSHOT_RESULTS_BASE:
        case REALMRESULTS:
            RealmResults<AllJavaTypes> result = realm.where(AllJavaTypes.class).equalTo(AllJavaTypes.FIELD_LONG, 1).findAll();
            obj = result.first();
            orderedCollection = result;
            break;
        default:
            throw new AssertionError("Unsupported class: " + collectionClass);
    }
    if (isSnapshot(collectionClass)) {
        orderedCollection = orderedCollection.createSnapshot();
    }
    return new Pair<AllJavaTypes, OrderedRealmCollection<AllJavaTypes>>(obj, orderedCollection);
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) Pair(android.util.Pair)

Example 27 with AllJavaTypes

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

the class OrderedRealmCollectionTests method first_withDefault.

@Test
public void first_withDefault() {
    AllJavaTypes defaultObject = new AllJavaTypes();
    collection = createEmptyCollection(realm, collectionClass);
    assertEquals(defaultObject, collection.first(defaultObject));
    // Null is an acceptable default
    assertEquals(null, collection.first(null));
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) Test(org.junit.Test)

Example 28 with AllJavaTypes

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

the class RealmObjectTests method setter_changePrimaryKeyThrows.

@Test
public void setter_changePrimaryKeyThrows() {
    realm.beginTransaction();
    AllJavaTypes allJavaTypes = realm.createObject(AllJavaTypes.class, 42);
    thrown.expect(RealmException.class);
    allJavaTypes.setFieldId(111);
    realm.cancelTransaction();
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) UiThreadTest(android.support.test.annotation.UiThreadTest) Test(org.junit.Test)

Example 29 with AllJavaTypes

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

the class DynamicRealmObjectTests method untypedSetter_usingStringConversion.

@Test
public void untypedSetter_usingStringConversion() {
    realm.beginTransaction();
    AllJavaTypes obj = realm.createObject(AllJavaTypes.class, 0);
    DynamicRealmObject dObj = new DynamicRealmObject(obj);
    try {
        for (SupportedType type : SupportedType.values()) {
            switch(type) {
                case BOOLEAN:
                    dObj.set(AllJavaTypes.FIELD_BOOLEAN, "true");
                    assertTrue(dObj.getBoolean(AllJavaTypes.FIELD_BOOLEAN));
                    break;
                case SHORT:
                    dObj.set(AllJavaTypes.FIELD_SHORT, "42");
                    assertEquals((short) 42, dObj.getShort(AllJavaTypes.FIELD_SHORT));
                    break;
                case INT:
                    dObj.set(AllJavaTypes.FIELD_INT, "42");
                    assertEquals(42, dObj.getInt(AllJavaTypes.FIELD_INT));
                    break;
                case LONG:
                    dObj.set(AllJavaTypes.FIELD_LONG, "42");
                    assertEquals((long) 42, dObj.getLong(AllJavaTypes.FIELD_LONG));
                    break;
                case FLOAT:
                    dObj.set(AllJavaTypes.FIELD_FLOAT, "1.23");
                    assertEquals(1.23f, dObj.getFloat(AllJavaTypes.FIELD_FLOAT), 0f);
                    break;
                case DOUBLE:
                    dObj.set(AllJavaTypes.FIELD_DOUBLE, "1.234");
                    assertEquals(1.234d, dObj.getDouble(AllJavaTypes.FIELD_DOUBLE), 0f);
                    break;
                case DATE:
                    dObj.set(AllJavaTypes.FIELD_DATE, "1000");
                    assertEquals(new Date(1000), dObj.getDate(AllJavaTypes.FIELD_DATE));
                    break;
                // These types don't have a string representation that can be parsed.
                case OBJECT:
                case LIST:
                case STRING:
                case BINARY:
                case BYTE:
                    break;
                default:
                    fail("Unknown type: " + type);
                    break;
            }
        }
    } finally {
        realm.cancelTransaction();
    }
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) Date(java.util.Date) Test(org.junit.Test)

Example 30 with AllJavaTypes

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

the class LinkingObjectsManagedTests method json_jsonUpdateCausesNotification.

// A JSON update should generate a notifcation
@Test
@RunTestInLooperThread
public void json_jsonUpdateCausesNotification() {
    final Realm looperThreadRealm = looperThread.realm;
    looperThreadRealm.beginTransaction();
    AllJavaTypes child = looperThreadRealm.createObject(AllJavaTypes.class, 1);
    AllJavaTypes parent = looperThreadRealm.createObject(AllJavaTypes.class, 2);
    parent.setFieldObject(child);
    looperThreadRealm.commitTransaction();
    RealmResults<AllJavaTypes> results = looperThreadRealm.where(AllJavaTypes.class).equalTo("fieldId", 1).findAll();
    assertNotNull(results);
    assertEquals(results.size(), 1);
    child = results.first();
    RealmResults<AllJavaTypes> parents = child.getObjectParents();
    assertNotNull(parents);
    assertEquals(1, parents.size());
    final AtomicInteger counter = new AtomicInteger(0);
    RealmChangeListener<AllJavaTypes> listener = new RealmChangeListener<AllJavaTypes>() {

        @Override
        public void onChange(AllJavaTypes object) {
            counter.incrementAndGet();
        }
    };
    child.addChangeListener(listener);
    looperThreadRealm.beginTransaction();
    try {
        looperThreadRealm.createOrUpdateAllFromJson(AllJavaTypes.class, "[{ \"fieldId\" : 2, \"fieldObject\" : null }]");
    } catch (RealmException e) {
        fail("Failed loading JSON" + e);
    }
    looperThreadRealm.commitTransaction();
    verifyPostConditions(looperThreadRealm, new PostConditions() {

        public void run(Realm realm) {
            RealmResults<AllJavaTypes> results = looperThreadRealm.where(AllJavaTypes.class).equalTo("fieldId", 1).findAll();
            assertNotNull(results);
            assertEquals(results.size(), 1);
            AllJavaTypes child = results.first();
            RealmResults<AllJavaTypes> parents = child.getObjectParents();
            assertNotNull(parents);
            assertEquals(0, parents.size());
            assertEquals(1, counter.get());
        }
    }, child, parent);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AllJavaTypes(io.realm.entities.AllJavaTypes) RealmException(io.realm.exceptions.RealmException) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Aggregations

AllJavaTypes (io.realm.entities.AllJavaTypes)90 Test (org.junit.Test)78 UiThreadTest (android.support.test.annotation.UiThreadTest)24 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Date (java.util.Date)5 RealmException (io.realm.exceptions.RealmException)4 Ignore (org.junit.Ignore)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutionException (java.util.concurrent.ExecutionException)2 ExecutorService (java.util.concurrent.ExecutorService)2 ExpectedException (org.junit.rules.ExpectedException)2 Pair (android.util.Pair)1 CyclicType (io.realm.entities.CyclicType)1 Dog (io.realm.entities.Dog)1 NonLatinFieldNames (io.realm.entities.NonLatinFieldNames)1 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)1 RealmPrimaryKeyConstraintException (io.realm.exceptions.RealmPrimaryKeyConstraintException)1 RunInLooperThread (io.realm.rule.RunInLooperThread)1 Field (java.lang.reflect.Field)1