Search in sources :

Example 31 with AllJavaTypes

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

the class CollectionTests method createStringCollection.

// Creates a list of AllJavaTypes with its `fieldString` field set to a given value.
protected OrderedRealmCollection<AllJavaTypes> createStringCollection(Realm realm, ManagedCollection collectionClass, String... args) {
    realm.beginTransaction();
    realm.deleteAll();
    OrderedRealmCollection<AllJavaTypes> orderedCollection;
    switch(collectionClass) {
        case REALMRESULTS_SNAPSHOT_RESULTS_BASE:
        case REALMRESULTS:
            int id = 0;
            for (String arg : args) {
                AllJavaTypes obj = realm.createObject(AllJavaTypes.class, id++);
                obj.setFieldString(arg);
            }
            realm.commitTransaction();
            orderedCollection = realm.where(AllJavaTypes.class).findAllSorted(AllJavaTypes.FIELD_STRING);
            break;
        case REALMRESULTS_SNAPSHOT_LIST_BASE:
        case MANAGED_REALMLIST:
            AllJavaTypes first = realm.createObject(AllJavaTypes.class, 0);
            first.setFieldString(args[0]);
            first.getFieldList().add(first);
            for (int i = 1; i < args.length; i++) {
                AllJavaTypes obj = realm.createObject(AllJavaTypes.class, i);
                obj.setFieldString(args[i]);
                first.getFieldList().add(obj);
            }
            realm.commitTransaction();
            orderedCollection = first.getFieldList();
            break;
        default:
            throw new AssertionError("Unknown collection: " + collectionClass);
    }
    if (isSnapshot(collectionClass)) {
        orderedCollection = orderedCollection.createSnapshot();
    }
    return orderedCollection;
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes)

Example 32 with AllJavaTypes

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

the class CollectionTests method populateInMemoryList.

protected RealmList<AllJavaTypes> populateInMemoryList(int objects) {
    RealmList<AllJavaTypes> list = new RealmList<AllJavaTypes>();
    for (int i = 0; i < objects; i++) {
        AllJavaTypes obj = new AllJavaTypes();
        fillObject(i, objects, obj);
        list.add(obj);
    }
    return list;
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes)

Example 33 with AllJavaTypes

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

the class DynamicRealmObjectTests method untypedGetterSetter.

@Test
public void untypedGetterSetter() {
    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((Boolean) dObj.get(AllJavaTypes.FIELD_BOOLEAN));
                    break;
                case SHORT:
                    dObj.set(AllJavaTypes.FIELD_SHORT, (short) 42);
                    assertEquals(Long.parseLong("42"), dObj.get(AllJavaTypes.FIELD_SHORT));
                    break;
                case INT:
                    dObj.set(AllJavaTypes.FIELD_INT, 42);
                    assertEquals(Long.parseLong("42"), dObj.get(AllJavaTypes.FIELD_INT));
                    break;
                case LONG:
                    dObj.set(AllJavaTypes.FIELD_LONG, 42L);
                    assertEquals(Long.parseLong("42"), dObj.get(AllJavaTypes.FIELD_LONG));
                    break;
                case BYTE:
                    dObj.set(AllJavaTypes.FIELD_BYTE, (byte) 4);
                    assertEquals(Long.parseLong("4"), dObj.get(AllJavaTypes.FIELD_BYTE));
                    break;
                case FLOAT:
                    dObj.set(AllJavaTypes.FIELD_FLOAT, 1.23f);
                    assertEquals(Float.parseFloat("1.23"), dObj.get(AllJavaTypes.FIELD_FLOAT));
                    break;
                case DOUBLE:
                    dObj.set(AllJavaTypes.FIELD_DOUBLE, 1.234d);
                    assertEquals(Double.parseDouble("1.234"), dObj.get(AllJavaTypes.FIELD_DOUBLE));
                    break;
                case STRING:
                    dObj.set(AllJavaTypes.FIELD_STRING, "str");
                    assertEquals("str", dObj.get(AllJavaTypes.FIELD_STRING));
                    break;
                case BINARY:
                    dObj.set(AllJavaTypes.FIELD_BINARY, new byte[] { 1, 2, 3 });
                    assertArrayEquals(new byte[] { 1, 2, 3 }, (byte[]) dObj.get(AllJavaTypes.FIELD_BINARY));
                    break;
                case DATE:
                    dObj.set(AllJavaTypes.FIELD_DATE, new Date(1000));
                    assertEquals(new Date(1000), dObj.get(AllJavaTypes.FIELD_DATE));
                    break;
                case OBJECT:
                    dObj.set(AllJavaTypes.FIELD_OBJECT, dObj);
                    assertEquals(dObj, dObj.get(AllJavaTypes.FIELD_OBJECT));
                    break;
                case LIST:
                    RealmList<DynamicRealmObject> newList = new RealmList<DynamicRealmObject>();
                    newList.add(dObj);
                    dObj.set(AllJavaTypes.FIELD_LIST, newList);
                    RealmList<DynamicRealmObject> list = dObj.getList(AllJavaTypes.FIELD_LIST);
                    assertEquals(1, list.size());
                    assertEquals(dObj, list.get(0));
                    break;
                default:
                    fail();
            }
        }
    } finally {
        realm.cancelTransaction();
    }
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) Date(java.util.Date) Test(org.junit.Test)

Example 34 with AllJavaTypes

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

the class LinkingObjectsManagedTests method query_backlinkInMiddle.

// Query on a field descriptor that has a backlink in the middle
// The test objects are:
//             gen1
//             / \
//         gen2A gen2B
//           \\   //
//            gen3
//  /  = object ref
//  // = list ref
@Test
@Ignore
public void query_backlinkInMiddle() {
    realm.beginTransaction();
    AllJavaTypes gen1 = realm.createObject(AllJavaTypes.class, 10);
    AllJavaTypes gen2A = realm.createObject(AllJavaTypes.class, 1);
    gen2A.setFieldObject(gen1);
    AllJavaTypes gen2B = realm.createObject(AllJavaTypes.class, 2);
    gen2B.setFieldObject(gen1);
    AllJavaTypes gen3 = realm.createObject(AllJavaTypes.class, 3);
    RealmList<AllJavaTypes> parents = gen3.getFieldList();
    parents.add(gen2A);
    parents.add(gen2B);
    realm.commitTransaction();
    RealmResults<AllJavaTypes> result = realm.where(AllJavaTypes.class).lessThan("objectParents.listParents.fieldId", 4).findAll();
    assertEquals(2, result.size());
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 35 with AllJavaTypes

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

the class LinkingObjectsManagedTests method notification_notSentAfterUnregisterListenerRealmResults.

// A listener registered on the backlinked field should not be called after the listener is removed
@Test
@RunTestInLooperThread
public void notification_notSentAfterUnregisterListenerRealmResults() {
    final Realm looperThreadRealm = looperThread.realm;
    looperThreadRealm.beginTransaction();
    AllJavaTypes child = looperThreadRealm.createObject(AllJavaTypes.class, 10);
    looperThreadRealm.commitTransaction();
    RealmChangeListener<RealmResults<AllJavaTypes>> listener = new RealmChangeListener<RealmResults<AllJavaTypes>>() {

        @Override
        public void onChange(RealmResults<AllJavaTypes> object) {
            fail("Not expecting notification after unregister");
        }
    };
    RealmResults<AllJavaTypes> objParents = child.getObjectParents();
    objParents.addChangeListener(listener);
    objParents.removeChangeListener(listener);
    looperThreadRealm.beginTransaction();
    AllJavaTypes parent = looperThreadRealm.createObject(AllJavaTypes.class, 1);
    parent.setFieldObject(child);
    looperThreadRealm.commitTransaction();
    verifyPostConditions(looperThreadRealm, new PostConditions() {

        public void run(Realm realm) {
            assertEquals(2, looperThreadRealm.where(AllJavaTypes.class).findAll().size());
        }
    }, child, parent);
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) 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