Search in sources :

Example 76 with AllJavaTypes

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

the class DynamicRealmObjectTests method untypedSetter_illegalImplicitConversionThrows.

@Test
public void untypedSetter_illegalImplicitConversionThrows() {
    realm.beginTransaction();
    AllJavaTypes obj = realm.createObject(AllJavaTypes.class, 0);
    DynamicRealmObject dObj = new DynamicRealmObject(obj);
    try {
        for (SupportedType type : SupportedType.values()) {
            try {
                switch(type) {
                    case SHORT:
                        dObj.set(AllJavaTypes.FIELD_SHORT, "foo");
                        break;
                    case INT:
                        dObj.set(AllJavaTypes.FIELD_INT, "foo");
                        break;
                    case LONG:
                        dObj.set(AllJavaTypes.FIELD_ID, "foo");
                        break;
                    case FLOAT:
                        dObj.set(AllJavaTypes.FIELD_FLOAT, "foo");
                        break;
                    case DOUBLE:
                        dObj.set(AllJavaTypes.FIELD_DOUBLE, "foo");
                        break;
                    case DATE:
                        dObj.set(AllJavaTypes.FIELD_DATE, "foo");
                        break;
                    // Boolean is special as it returns false for all strings != "true"
                    case BOOLEAN:
                    case BYTE:
                    case OBJECT:
                    case LIST:
                    case STRING:
                    case BINARY:
                        continue;
                    default:
                        fail("Unknown type: " + type);
                }
                fail(type + " failed");
            } catch (IllegalArgumentException ignored) {
            } catch (RealmException e) {
                if (!(e.getCause() instanceof ParseException)) {
                    // Providing "foo" to the date parser will blow up with a RealmException
                    // and the cause will be a ParseException.
                    fail(type + " failed");
                }
            }
        }
    } finally {
        realm.cancelTransaction();
    }
}
Also used : ParseException(java.text.ParseException) AllJavaTypes(io.realm.entities.AllJavaTypes) RealmException(io.realm.exceptions.RealmException) Test(org.junit.Test)

Example 77 with AllJavaTypes

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

the class DynamicRealmObjectTests method equals_standardAndDynamicObjectsNotEqual.

@Test
public void equals_standardAndDynamicObjectsNotEqual() {
    AllJavaTypes standardObj = realm.where(AllJavaTypes.class).findFirst();
    //noinspection EqualsBetweenInconvertibleTypes
    assertFalse(dObjTyped.equals(standardObj));
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) Test(org.junit.Test)

Example 78 with AllJavaTypes

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

the class DynamicRealmObjectTests method equals.

@Test
public void equals() {
    AllJavaTypes obj1 = realm.where(AllJavaTypes.class).findFirst();
    AllJavaTypes obj2 = realm.where(AllJavaTypes.class).findFirst();
    DynamicRealmObject dObj1 = new DynamicRealmObject(obj1);
    DynamicRealmObject dObj2 = new DynamicRealmObject(obj2);
    assertTrue(dObj1.equals(dObj2));
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) Test(org.junit.Test)

Example 79 with AllJavaTypes

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

the class DynamicRealmObjectTests method typedGettersAndSetters.

// Tests all typed setters/setters.
@Test
public void typedGettersAndSetters() {
    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.setBoolean(AllJavaTypes.FIELD_BOOLEAN, true);
                    assertTrue(dObj.getBoolean(AllJavaTypes.FIELD_BOOLEAN));
                    break;
                case SHORT:
                    dObj.setShort(AllJavaTypes.FIELD_SHORT, (short) 42);
                    assertEquals(42, dObj.getShort(AllJavaTypes.FIELD_SHORT));
                    break;
                case INT:
                    dObj.setInt(AllJavaTypes.FIELD_INT, 42);
                    assertEquals(42, dObj.getInt(AllJavaTypes.FIELD_INT));
                    break;
                case LONG:
                    dObj.setLong(AllJavaTypes.FIELD_LONG, 42L);
                    assertEquals(42, dObj.getLong(AllJavaTypes.FIELD_LONG));
                    break;
                case BYTE:
                    dObj.setByte(AllJavaTypes.FIELD_BYTE, (byte) 4);
                    assertEquals(4, dObj.getByte(AllJavaTypes.FIELD_BYTE));
                    break;
                case FLOAT:
                    dObj.setFloat(AllJavaTypes.FIELD_FLOAT, 1.23f);
                    assertEquals(1.23f, dObj.getFloat(AllJavaTypes.FIELD_FLOAT), 0f);
                    break;
                case DOUBLE:
                    dObj.setDouble(AllJavaTypes.FIELD_DOUBLE, 1.234d);
                    assertEquals(1.234d, dObj.getDouble(AllJavaTypes.FIELD_DOUBLE), 0d);
                    break;
                case STRING:
                    dObj.setString(AllJavaTypes.FIELD_STRING, "str");
                    assertEquals("str", dObj.getString(AllJavaTypes.FIELD_STRING));
                    break;
                case BINARY:
                    dObj.setBlob(AllJavaTypes.FIELD_BINARY, new byte[] { 1, 2, 3 });
                    assertArrayEquals(new byte[] { 1, 2, 3 }, dObj.getBlob(AllJavaTypes.FIELD_BINARY));
                    break;
                case DATE:
                    dObj.setDate(AllJavaTypes.FIELD_DATE, new Date(1000));
                    assertEquals(new Date(1000), dObj.getDate(AllJavaTypes.FIELD_DATE));
                    break;
                case OBJECT:
                    dObj.setObject(AllJavaTypes.FIELD_OBJECT, dObj);
                    assertEquals(dObj, dObj.getObject(AllJavaTypes.FIELD_OBJECT));
                    break;
                case LIST:
                    // Ignores. See testGetList/testSetList.
                    break;
                default:
                    fail();
            }
        }
    } finally {
        realm.cancelTransaction();
    }
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) Date(java.util.Date) Test(org.junit.Test)

Example 80 with AllJavaTypes

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

the class LinkingObjectsManagedTests method basic_singleBacklinkList.

// Setting a linked list field creates the correct backlink
@Test
public void basic_singleBacklinkList() {
    realm.beginTransaction();
    AllJavaTypes child = realm.createObject(AllJavaTypes.class, 1);
    AllJavaTypes parent = realm.createObject(AllJavaTypes.class, 2);
    parent.getFieldList().add(child);
    realm.commitTransaction();
    assertEquals(1, child.getListParents().size());
    assertTrue(child.getListParents().contains(parent));
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) 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