Search in sources :

Example 1 with AllTypesRealmModel

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

the class BulkInsertTests method insert_realmModel.

@Test
public void insert_realmModel() {
    AllTypesRealmModel allTypes = new AllTypesRealmModel();
    allTypes.columnLong = 10;
    allTypes.columnBoolean = false;
    allTypes.columnBinary = new byte[] { 1, 2, 3 };
    allTypes.columnDate = new Date();
    allTypes.columnDouble = Math.PI;
    allTypes.columnFloat = 1.234567f;
    allTypes.columnString = "test data";
    allTypes.columnByte = 0x2A;
    allTypes.columnDecimal128 = new Decimal128(BigDecimal.TEN);
    allTypes.columnObjectId = new ObjectId(TestHelper.generateObjectIdHexString(7));
    allTypes.columnUUID = UUID.fromString(TestHelper.generateUUIDString(7));
    realm.beginTransaction();
    realm.insert(allTypes);
    realm.commitTransaction();
    AllTypesRealmModel first = realm.where(AllTypesRealmModel.class).findFirst();
    assertNotNull(first);
    assertEquals(allTypes.columnString, first.columnString);
    assertEquals(allTypes.columnLong, first.columnLong);
    assertEquals(allTypes.columnBoolean, first.columnBoolean);
    assertArrayEquals(allTypes.columnBinary, first.columnBinary);
    assertEquals(allTypes.columnDate, first.columnDate);
    assertEquals(allTypes.columnDouble, first.columnDouble, 0.0000001);
    assertEquals(allTypes.columnFloat, first.columnFloat, 0.0000001);
    assertEquals(allTypes.columnByte, first.columnByte);
    assertEquals(allTypes.columnDecimal128, first.columnDecimal128);
    assertEquals(allTypes.columnObjectId, first.columnObjectId);
    assertEquals(allTypes.columnUUID, first.columnUUID);
}
Also used : AllTypesRealmModel(io.realm.entities.pojo.AllTypesRealmModel) ObjectId(org.bson.types.ObjectId) Decimal128(org.bson.types.Decimal128) Date(java.util.Date) Test(org.junit.Test)

Example 2 with AllTypesRealmModel

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

the class RealmChangeListenerTests method returnedRealmModelIsNotNull.

@Test
@RunTestInLooperThread
public void returnedRealmModelIsNotNull() {
    Realm realm = looperThread.getRealm();
    realm.beginTransaction();
    AllTypesRealmModel model = realm.createObject(AllTypesRealmModel.class, 0);
    realm.commitTransaction();
    looperThread.keepStrongReference(model);
    RealmObject.addChangeListener(model, new RealmChangeListener<AllTypesRealmModel>() {

        @Override
        public void onChange(AllTypesRealmModel object) {
            assertEquals("model1", object.columnString);
            looperThread.testComplete();
        }
    });
    realm.beginTransaction();
    model.columnString = "model1";
    realm.commitTransaction();
}
Also used : AllTypesRealmModel(io.realm.entities.pojo.AllTypesRealmModel) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 3 with AllTypesRealmModel

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

the class RealmModelTests method copyToRealmOrUpdate.

@Test
public void copyToRealmOrUpdate() {
    realm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            AllTypesRealmModel obj = new AllTypesRealmModel();
            obj.columnLong = 1;
            realm.copyToRealm(obj);
            AllTypesRealmModel obj2 = new AllTypesRealmModel();
            obj2.columnLong = 1;
            obj2.columnString = "Foo";
            realm.copyToRealmOrUpdate(obj2);
        }
    });
    assertEquals(1, realm.where(AllTypesRealmModel.class).count());
    AllTypesRealmModel obj = realm.where(AllTypesRealmModel.class).findFirst();
    assertNotNull(obj);
    assertEquals("Foo", obj.columnString);
}
Also used : AllTypesRealmModel(io.realm.entities.pojo.AllTypesRealmModel) Test(org.junit.Test)

Example 4 with AllTypesRealmModel

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

the class RealmModelTests method realmModelWithRealmListOfRealmModel.

// Tests the behaviour of a RealmModel, containing a RealmList
// of other RealmModel, in managed and unmanaged mode.
@Test
public void realmModelWithRealmListOfRealmModel() {
    RealmList<AllTypesRealmModel> allTypesRealmModels = new RealmList<AllTypesRealmModel>();
    AllTypesRealmModel allTypePojo;
    for (int i = 0; i < 10; i++) {
        allTypePojo = new AllTypesRealmModel();
        allTypePojo.columnLong = i;
        allTypesRealmModels.add(allTypePojo);
    }
    AllTypesRealmModel pojo1 = allTypesRealmModels.get(1);
    assertEquals(1, pojo1.columnLong);
    allTypesRealmModels.move(1, 0);
    assertEquals(0, allTypesRealmModels.indexOf(pojo1));
    RealmModelWithRealmListOfRealmModel model = new RealmModelWithRealmListOfRealmModel();
    model.setColumnRealmList(allTypesRealmModels);
    realm.beginTransaction();
    realm.copyToRealm(model);
    realm.commitTransaction();
    RealmResults<RealmModelWithRealmListOfRealmModel> all = realm.where(RealmModelWithRealmListOfRealmModel.class).findAll();
    assertEquals(1, all.size());
    assertEquals(10, all.first().getColumnRealmList().size());
    assertEquals(1, all.first().getColumnRealmList().first().columnLong);
}
Also used : AllTypesRealmModel(io.realm.entities.pojo.AllTypesRealmModel) RealmModelWithRealmListOfRealmModel(io.realm.entities.pojo.RealmModelWithRealmListOfRealmModel) Test(org.junit.Test)

Example 5 with AllTypesRealmModel

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

the class RealmModelTests method createOrUpdateAllFromJson.

@Test
public void createOrUpdateAllFromJson() throws IOException {
    assumeThat(Build.VERSION.SDK_INT, greaterThanOrEqualTo(Build.VERSION_CODES.HONEYCOMB));
    realm.beginTransaction();
    realm.createOrUpdateAllFromJson(AllTypesRealmModel.class, TestHelper.loadJsonFromAssets(context, "list_alltypes_primarykey.json"));
    realm.commitTransaction();
    assertEquals(1, realm.where(AllTypesRealmModel.class).count());
    AllTypesRealmModel obj = realm.where(AllTypesRealmModel.class).findFirst();
    assertNotNull(obj);
    assertEquals("Bar", obj.columnString);
    assertEquals(2.23F, obj.columnFloat, 0.000000001);
    assertEquals(2.234D, obj.columnDouble, 0.000000001);
    assertEquals(true, obj.columnBoolean);
    assertArrayEquals(new byte[] { 1, 2, 3 }, obj.columnBinary);
    assertEquals(new Date(2000), obj.columnDate);
    assertEquals("Dog4", obj.columnRealmObject.getName());
    assertEquals(2, obj.columnRealmList.size());
    assertEquals("Dog5", obj.columnRealmList.get(0).getName());
}
Also used : AllTypesRealmModel(io.realm.entities.pojo.AllTypesRealmModel) Date(java.util.Date) Test(org.junit.Test)

Aggregations

AllTypesRealmModel (io.realm.entities.pojo.AllTypesRealmModel)13 Test (org.junit.Test)12 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 Decimal128 (org.bson.types.Decimal128)1 ObjectId (org.bson.types.ObjectId)1