Search in sources :

Example 11 with Owner

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

the class RealmListTests method clear.

@Test
public void clear() {
    Owner owner = realm.where(Owner.class).findFirst();
    realm.beginTransaction();
    assertEquals(TEST_SIZE, owner.getDogs().size());
    owner.getDogs().clear();
    assertEquals(0, owner.getDogs().size());
    realm.commitTransaction();
}
Also used : Owner(io.realm.entities.Owner) Test(org.junit.Test)

Example 12 with Owner

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

the class RealmListTests method getObjects.

@Test
public void getObjects() {
    Owner owner = realm.where(Owner.class).findFirst();
    RealmList<Dog> dogs = owner.getDogs();
    assertNotNull(dogs);
    assertEquals("Dog 1", dogs.get(1).getName());
}
Also used : Owner(io.realm.entities.Owner) Dog(io.realm.entities.Dog) Test(org.junit.Test)

Example 13 with Owner

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

the class RealmResultsTests method asJSON.

@Test
public void asJSON() throws JSONException {
    Date date = Calendar.getInstance().getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    // Core return dates in UTC time
    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    String now = sdf.format(date);
    realm.beginTransaction();
    AllTypes allTypes = realm.createObject(AllTypes.class);
    Dog dog1 = realm.createObject(Dog.class);
    Dog dog2 = realm.createObject(Dog.class);
    Dog dog3 = realm.createObject(Dog.class);
    dog1.setName("dog1");
    dog1.setAge(1);
    dog1.setBirthday(date);
    dog1.setHasTail(true);
    dog1.setHeight(1.1f);
    dog1.setWeight(10.1f);
    dog2.setName("dog2");
    dog2.setAge(2);
    dog2.setBirthday(date);
    dog2.setHasTail(false);
    dog2.setHeight(2.1f);
    dog2.setWeight(20.1f);
    dog3.setName("dog3");
    dog3.setAge(3);
    dog3.setBirthday(date);
    dog3.setHasTail(true);
    dog3.setHeight(3.1f);
    dog3.setWeight(30.1f);
    Owner owner = realm.createObject(Owner.class);
    owner.setName("Dog owner 1");
    dog3.setOwner(owner);
    allTypes.setColumnString("alltypes1");
    allTypes.setColumnLong(1337L);
    allTypes.setColumnFloat(3.14f);
    allTypes.setColumnDouble(0.89123);
    allTypes.setColumnBoolean(false);
    allTypes.setColumnDate(date);
    allTypes.setColumnDecimal128(new Decimal128(new BigDecimal("0.123456789")));
    allTypes.setColumnObjectId(new ObjectId(TestHelper.generateObjectIdHexString(7)));
    allTypes.setColumnUUID(UUID.fromString(uuid1));
    allTypes.setColumnBinary(new byte[] { 1, 2, 3 });
    allTypes.setColumnMutableRealmInteger(0);
    allTypes.setColumnRealmObject(dog1);
    allTypes.getColumnRealmList().add(dog2);
    allTypes.getColumnRealmList().add(dog3);
    allTypes.getColumnStringList().add("Foo");
    allTypes.getColumnStringList().add("Bar");
    allTypes.getColumnBooleanList().add(false);
    allTypes.getColumnBooleanList().add(true);
    allTypes.getColumnLongList().add(1000L);
    allTypes.getColumnLongList().add(2000L);
    allTypes.getColumnDoubleList().add(1.123);
    allTypes.getColumnDoubleList().add(5.321);
    allTypes.getColumnFloatList().add(0.12f);
    allTypes.getColumnFloatList().add(0.13f);
    allTypes.getColumnDateList().add(date);
    allTypes.getColumnDateList().add(date);
    allTypes.getColumnDecimal128List().add(new Decimal128(-42));
    allTypes.getColumnDecimal128List().add(Decimal128.NaN);
    allTypes.getColumnDecimal128List().add(Decimal128.NEGATIVE_ZERO);
    allTypes.getColumnDecimal128List().add(Decimal128.POSITIVE_ZERO);
    allTypes.getColumnDecimal128List().add(Decimal128.NEGATIVE_INFINITY);
    allTypes.getColumnDecimal128List().add(Decimal128.POSITIVE_INFINITY);
    allTypes.getColumnObjectIdList().add(new ObjectId(TestHelper.generateObjectIdHexString(1)));
    allTypes.getColumnObjectIdList().add(new ObjectId(TestHelper.generateObjectIdHexString(2)));
    allTypes.getColumnUUIDList().add(UUID.fromString(uuid1));
    allTypes.getColumnUUIDList().add(UUID.fromString(uuid2));
    AllTypes allTypes2 = realm.createObject(AllTypes.class);
    allTypes2.setColumnString("alltypes2");
    realm.commitTransaction();
    RealmResults<AllTypes> all = realm.where(AllTypes.class).equalTo("columnString", "alltypes1").findAll();
    assertEquals(1, all.size());
    String json = all.asJSON();
    final String expectedJSON = "[\n" + "   {\n" + "      \"_key\":100,\n" + "      \"columnString\":\"alltypes1\",\n" + "      \"columnLong\":1337,\n" + "      \"columnFloat\":3.1400001e+00,\n" + "      \"columnDouble\":8.9122999999999997e-01,\n" + "      \"columnBoolean\":false,\n" + "      \"columnDate\": \"" + now + "\",\n" + "      \"columnBinary\":\"AQID\",\n" + "      \"columnDecimal128\":\"1.23456789E-1\",\n" + "      \"columnObjectId\":\"789abcdef0123456789abcde\",\n" + "      \"columnUUID\":\"" + uuid1 + "\",\n" + "      \"columnMutableRealmInteger\":0,\n" + "      \"columnRealmObject\":{\n" + "         \"_key\":100,\n" + "         \"name\":\"dog1\",\n" + "         \"age\":1,\n" + "         \"height\":1.1000000e+00,\n" + "         \"weight\":1.0100000381469727e+01,\n" + "         \"hasTail\":true,\n" + "         \"birthday\": \"" + now + "\",\n" + "         \"owner\":null\n" + "      },\n" + "      \"columnRealmList\":[\n" + "         {\n" + "            \"_key\":101,\n" + "            \"name\":\"dog2\",\n" + "            \"age\":2,\n" + "            \"height\":2.0999999e+00,\n" + "            \"weight\":2.0100000381469727e+01,\n" + "            \"hasTail\":false,\n" + "            \"birthday\": \"" + now + "\",\n" + "            \"owner\":null\n" + "         },\n" + "         {\n" + "            \"_key\":102,\n" + "            \"name\":\"dog3\",\n" + "            \"age\":3,\n" + "            \"height\":3.0999999e+00,\n" + "            \"weight\":3.0100000381469727e+01,\n" + "            \"hasTail\":true,\n" + "            \"birthday\": \"" + now + "\",\n" + "            \"owner\":{\n" + "               \"_key\":0,\n" + "               \"name\":\"Dog owner 1\",\n" + "               \"dogs\":[\n" + "\n" + "               ],\n" + "               \"cat\":null\n" + "            }\n" + "         }\n" + "      ],\n" + "      \"columnStringList\":[\n" + "         \"Foo\",\n" + "         \"Bar\"\n" + "      ],\n" + "      \"columnBinaryList\":[\n" + "\n" + "      ],\n" + "      \"columnBooleanList\":[\n" + "         false,\n" + "         true\n" + "      ],\n" + "      \"columnLongList\":[\n" + "         1000,\n" + "         2000\n" + "      ],\n" + "      \"columnDoubleList\":[\n" + "         1.1230000000000000e+00,\n" + "         5.3209999999999997e+00\n" + "      ],\n" + "      \"columnFloatList\":[\n" + "         1.2000000e-01,\n" + "         1.3000000e-01\n" + "      ],\n" + "      \"columnDateList\":[\n" + "            \"" + now + "\",\n" + "            \"" + now + "\"\n" + "      ],\n" + "      \"columnDecimal128List\":[\n" + "         \"-42\",\n" + "         \"NaN\",\n" + "         \"-0\",\n" + "         \"0\",\n" + "         \"-Inf\",\n" + "         \"Inf\"\n" + "      ],\n" + "      \"columnObjectIdList\":[\n" + "         \"123456789abcdef012345678\",\n" + "         \"23456789abcdef0123456789\"\n" + "      ],\n" + "      \"columnUUIDList\":[\n" + "         \"" + uuid1 + "\",\n" + "         \"" + uuid2 + "\"\n" + "      ]\n" + "   }\n" + "]";
    JSONAssert.assertEquals(expectedJSON, json, false);
}
Also used : Owner(io.realm.entities.Owner) ObjectId(org.bson.types.ObjectId) AllTypes(io.realm.entities.AllTypes) DictionaryAllTypes(io.realm.entities.DictionaryAllTypes) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) Decimal128(org.bson.types.Decimal128) SimpleDateFormat(java.text.SimpleDateFormat) Dog(io.realm.entities.Dog) Date(java.util.Date) BigDecimal(java.math.BigDecimal) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 14 with Owner

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

the class RealmQueryTests method queryLink.

@Test
public void queryLink() {
    realm.beginTransaction();
    Owner owner = realm.createObject(Owner.class);
    Dog dog1 = realm.createObject(Dog.class);
    dog1.setName("Dog 1");
    dog1.setWeight(1);
    Dog dog2 = realm.createObject(Dog.class);
    dog2.setName("Dog 2");
    dog2.setWeight(2);
    owner.getDogs().add(dog1);
    owner.getDogs().add(dog2);
    realm.commitTransaction();
    // Dog.weight has index 4 which is more than the total number of columns in Owner
    // This tests exposes a subtle error where the Owner table spec is used instead of Dog table spec.
    RealmResults<Dog> dogs = realm.where(Owner.class).findFirst().getDogs().where().sort("name", Sort.ASCENDING).findAll();
    Dog dog = dogs.where().equalTo("weight", 1d).findFirst();
    assertEquals(dog1, dog);
}
Also used : CatOwner(io.realm.entities.CatOwner) Owner(io.realm.entities.Owner) Dog(io.realm.entities.Dog) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 15 with Owner

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

the class RealmQueryTests method findFirst.

@Test
public void findFirst() {
    realm.beginTransaction();
    Owner owner1 = realm.createObject(Owner.class);
    owner1.setName("Owner 1");
    Dog dog1 = realm.createObject(Dog.class);
    dog1.setName("Dog 1");
    dog1.setWeight(1);
    Dog dog2 = realm.createObject(Dog.class);
    dog2.setName("Dog 2");
    dog2.setWeight(2);
    owner1.getDogs().add(dog1);
    owner1.getDogs().add(dog2);
    Owner owner2 = realm.createObject(Owner.class);
    owner2.setName("Owner 2");
    Dog dog3 = realm.createObject(Dog.class);
    dog3.setName("Dog 3");
    dog3.setWeight(1);
    Dog dog4 = realm.createObject(Dog.class);
    dog4.setName("Dog 4");
    dog4.setWeight(2);
    owner2.getDogs().add(dog3);
    owner2.getDogs().add(dog4);
    realm.commitTransaction();
    RealmList<Dog> dogs = realm.where(Owner.class).equalTo("name", "Owner 2").findFirst().getDogs();
    Dog dog = dogs.where().equalTo("name", "Dog 4").findFirst();
    assertEquals(dog4, dog);
}
Also used : CatOwner(io.realm.entities.CatOwner) Owner(io.realm.entities.Owner) Dog(io.realm.entities.Dog) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Aggregations

Owner (io.realm.entities.Owner)36 Dog (io.realm.entities.Dog)30 Test (org.junit.Test)27 UiThreadTest (androidx.test.annotation.UiThreadTest)3 CatOwner (io.realm.entities.CatOwner)3 Date (java.util.Date)3 Cat (io.realm.entities.Cat)2 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)2 AllTypes (io.realm.entities.AllTypes)1 DictionaryAllTypes (io.realm.entities.DictionaryAllTypes)1 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)1 RealmBackgroundTask (io.realm.util.RealmBackgroundTask)1 BigDecimal (java.math.BigDecimal)1 SimpleDateFormat (java.text.SimpleDateFormat)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Decimal128 (org.bson.types.Decimal128)1 ObjectId (org.bson.types.ObjectId)1 Before (org.junit.Before)1