Search in sources :

Example 6 with AllTypesRealmModel

use of io.realm.entities.pojo.AllTypesRealmModel in project realm-java by realm.

the class RealmChangeListenerTests method returnedRealmResultsOfModelIsNotNull.

@Test
@RunTestInLooperThread
public void returnedRealmResultsOfModelIsNotNull() {
    Realm realm = looperThread.realm;
    RealmResults<AllTypesRealmModel> alltypes = realm.where(AllTypesRealmModel.class).findAll();
    looperThread.keepStrongReference.add(alltypes);
    alltypes.addChangeListener(new RealmChangeListener<RealmResults<AllTypesRealmModel>>() {

        @Override
        public void onChange(RealmResults<AllTypesRealmModel> result) {
            assertEquals("data 1", result.first().columnString);
            looperThread.testComplete();
        }
    });
    realm.beginTransaction();
    AllTypesRealmModel model = realm.createObject(AllTypesRealmModel.class, 0);
    model.columnString = "data 1";
    realm.commitTransaction();
}
Also used : AllTypesRealmModel(io.realm.entities.pojo.AllTypesRealmModel) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 7 with AllTypesRealmModel

use of io.realm.entities.pojo.AllTypesRealmModel in project realm-java by realm.

the class RealmModelTests method populateTestRealm.

private void populateTestRealm(Realm realm, int objects) {
    realm.beginTransaction();
    realm.delete(AllTypesRealmModel.class);
    for (int i = 0; i < objects; ++i) {
        AllTypesRealmModel allTypes = new AllTypesRealmModel();
        allTypes.columnLong = i;
        allTypes.columnBoolean = (i % 3) == 0;
        allTypes.columnBinary = new byte[] { 1, 2, 3 };
        allTypes.columnDate = new Date();
        allTypes.columnDouble = 3.1415 + i;
        allTypes.columnFloat = 1.234567f;
        allTypes.columnString = "test data ";
        allTypes.columnByte = 0x2A;
        realm.copyToRealm(allTypes);
    }
    realm.commitTransaction();
}
Also used : AllTypesRealmModel(io.realm.entities.pojo.AllTypesRealmModel) Date(java.util.Date)

Example 8 with AllTypesRealmModel

use of io.realm.entities.pojo.AllTypesRealmModel in project realm-java by realm.

the class RealmModelTests method copyFromRealm.

@Test
public void copyFromRealm() {
    populateTestRealm(realm, TEST_DATA_SIZE);
    AllTypesRealmModel realmObject = realm.where(AllTypesRealmModel.class).findAllSorted(AllTypesRealmModel.FIELD_LONG).first();
    AllTypesRealmModel unmanagedObject = realm.copyFromRealm(realmObject);
    assertArrayEquals(realmObject.columnBinary, unmanagedObject.columnBinary);
    assertEquals(realmObject.columnString, unmanagedObject.columnString);
    assertEquals(realmObject.columnLong, unmanagedObject.columnLong);
    assertEquals(realmObject.columnFloat, unmanagedObject.columnFloat, 0.00000000001);
    assertEquals(realmObject.columnDouble, unmanagedObject.columnDouble, 0.00000000001);
    assertEquals(realmObject.columnBoolean, unmanagedObject.columnBoolean);
    assertEquals(realmObject.columnDate, unmanagedObject.columnDate);
    assertEquals(realmObject.hashCode(), unmanagedObject.hashCode());
}
Also used : AllTypesRealmModel(io.realm.entities.pojo.AllTypesRealmModel) Test(org.junit.Test)

Example 9 with AllTypesRealmModel

use of io.realm.entities.pojo.AllTypesRealmModel in project realm-java by realm.

the class RealmModelTests method dynamicObject.

@Test
public void dynamicObject() {
    populateTestRealm(realm, TEST_DATA_SIZE);
    AllTypesRealmModel typedObj = realm.where(AllTypesRealmModel.class).findFirst();
    assertNotNull(typedObj);
    DynamicRealmObject dObj = new DynamicRealmObject(typedObj);
    realm.beginTransaction();
    dObj.setByte(AllTypesRealmModel.FIELD_BYTE, (byte) 42);
    assertEquals(42, dObj.getLong(AllTypesRealmModel.FIELD_BYTE));
    assertEquals(42, typedObj.columnByte);
    dObj.setBlob(AllTypesRealmModel.FIELD_BINARY, new byte[] { 1, 2, 3 });
    Assert.assertArrayEquals(new byte[] { 1, 2, 3 }, dObj.getBlob(AllTypesRealmModel.FIELD_BINARY));
    Assert.assertArrayEquals(new byte[] { 1, 2, 3 }, typedObj.columnBinary);
    realm.cancelTransaction();
}
Also used : AllTypesRealmModel(io.realm.entities.pojo.AllTypesRealmModel) Test(org.junit.Test)

Example 10 with AllTypesRealmModel

use of io.realm.entities.pojo.AllTypesRealmModel in project realm-java by realm.

the class RealmModelTests method realmModelWithRealmModelField.

// Tests the behaviour of a RealmModel, containing a RealmModel field.
@Test
public void realmModelWithRealmModelField() {
    RealmModelWithRealmModelField realmModelWithRealmModelField = new RealmModelWithRealmModelField();
    AllTypesRealmModel allTypePojo = new AllTypesRealmModel();
    allTypePojo.columnLong = 42;
    realmModelWithRealmModelField.setAllTypesRealmModel(allTypePojo);
    realm.beginTransaction();
    realm.copyToRealm(realmModelWithRealmModelField);
    realm.commitTransaction();
    RealmResults<RealmModelWithRealmModelField> all = realm.where(RealmModelWithRealmModelField.class).findAll();
    assertEquals(1, all.size());
    assertEquals(42, all.first().getAllTypesRealmModel().columnLong);
}
Also used : AllTypesRealmModel(io.realm.entities.pojo.AllTypesRealmModel) RealmModelWithRealmModelField(io.realm.entities.pojo.RealmModelWithRealmModelField) Test(org.junit.Test)

Aggregations

AllTypesRealmModel (io.realm.entities.pojo.AllTypesRealmModel)12 Test (org.junit.Test)11 Date (java.util.Date)3 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)2 RealmModelWithRealmListOfRealmModel (io.realm.entities.pojo.RealmModelWithRealmListOfRealmModel)1 RealmModelWithRealmModelField (io.realm.entities.pojo.RealmModelWithRealmModelField)1 RealmObjectWithRealmListOfRealmModel (io.realm.entities.pojo.RealmObjectWithRealmListOfRealmModel)1 RealmObjectWithRealmModelField (io.realm.entities.pojo.RealmObjectWithRealmModelField)1