Search in sources :

Example 66 with AllTypes

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

the class RealmResultsTests method size_returns_Integer_MAX_VALUE_for_huge_results.

@Test
public void size_returns_Integer_MAX_VALUE_for_huge_results() {
    final OsResults osResults = Mockito.mock(OsResults.class);
    final RealmResults<AllTypes> targetResult = TestHelper.newRealmResults(realm, osResults, AllTypes.class);
    Mockito.when(osResults.isLoaded()).thenReturn(true);
    Mockito.when(osResults.size()).thenReturn(((long) Integer.MAX_VALUE) - 1);
    assertEquals(Integer.MAX_VALUE - 1, targetResult.size());
    Mockito.when(osResults.size()).thenReturn(((long) Integer.MAX_VALUE));
    assertEquals(Integer.MAX_VALUE, targetResult.size());
    Mockito.when(osResults.size()).thenReturn(((long) Integer.MAX_VALUE) + 1);
    assertEquals(Integer.MAX_VALUE, targetResult.size());
}
Also used : AllTypes(io.realm.entities.AllTypes) DictionaryAllTypes(io.realm.entities.DictionaryAllTypes) OsResults(io.realm.internal.OsResults) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 67 with AllTypes

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

the class RealmResultsTests method removeAllChangeListeners.

@Test
@RunTestInLooperThread
public void removeAllChangeListeners() {
    final AtomicInteger listenersTriggered = new AtomicInteger(0);
    final Realm realm = looperThread.getRealm();
    RealmResults<AllTypes> collection = realm.where(AllTypes.class).findAll();
    RealmChangeListener<RealmResults<AllTypes>> listenerA = new RealmChangeListener<RealmResults<AllTypes>>() {

        @Override
        public void onChange(RealmResults<AllTypes> object) {
            listenersTriggered.incrementAndGet();
        }
    };
    RealmChangeListener<RealmResults<AllTypes>> listenerB = new RealmChangeListener<RealmResults<AllTypes>>() {

        @Override
        public void onChange(RealmResults<AllTypes> object) {
            listenersTriggered.incrementAndGet();
        }
    };
    looperThread.keepStrongReference(collection);
    collection.addChangeListener(listenerA);
    collection.addChangeListener(listenerB);
    collection.removeAllChangeListeners();
    realm.beginTransaction();
    realm.createObject(AllTypes.class);
    realm.commitTransaction();
    // The above commit should have put a REALM_CHANGED event on the Looper queue before this runnable.
    looperThread.postRunnable(new Runnable() {

        @Override
        public void run() {
            if (listenersTriggered.get() == 0) {
                looperThread.testComplete();
            } else {
                fail("Listeners wasn't removed");
            }
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AllTypes(io.realm.entities.AllTypes) DictionaryAllTypes(io.realm.entities.DictionaryAllTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 68 with AllTypes

use of io.realm.entities.AllTypes 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 69 with AllTypes

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

the class RealmResultsTests method setValue_specificType_wrongFieldNameThrows.

@Test
public void setValue_specificType_wrongFieldNameThrows() {
    populateAllJavaTypes(5);
    RealmResults<AllTypes> collection = realm.where(AllTypes.class).findAll();
    realm.beginTransaction();
    for (BulkSetMethods type : BulkSetMethods.values()) {
        try {
            switch(type) {
                case STRING:
                    collection.setString("foo", "bar");
                    break;
                case BOOLEAN:
                    collection.setBoolean("foo", true);
                    break;
                case BYTE:
                    collection.setByte("foo", (byte) 1);
                    break;
                case SHORT:
                    collection.setShort("foo", (short) 2);
                    break;
                case INTEGER:
                    collection.setInt("foo", 3);
                    break;
                case LONG:
                    collection.setLong("foo", 4L);
                    break;
                case FLOAT:
                    collection.setFloat("foo", 1.23F);
                    break;
                case DOUBLE:
                    collection.setDouble("foo", 1.234);
                    break;
                case BINARY:
                    collection.setBlob("foo", new byte[] { 1, 2, 3 });
                    break;
                case DATE:
                    collection.setDate("foo", new Date(1000));
                    break;
                case DECIMAL128:
                    collection.setDecimal128("foo", new Decimal128(1000));
                    break;
                case OBJECT_ID:
                    collection.setObjectId("foo", new ObjectId(TestHelper.randomObjectIdHexString()));
                    break;
                case UUID:
                    collection.setUUID("foo", UUID.randomUUID());
                    break;
                case OBJECT:
                    collection.setObject("foo", realm.createObject(AllTypes.class));
                    break;
                case MODEL_LIST:
                    collection.setList("foo", new RealmList<>());
                    break;
                case STRING_VALUE_LIST:
                    collection.setList("foo", new RealmList<>("Foo"));
                    break;
                case BOOLEAN_VALUE_LIST:
                    collection.setList("foo", new RealmList<>(true));
                    break;
                case BYTE_VALUE_LIST:
                    collection.setList("foo", new RealmList<>((byte) 1));
                    break;
                case SHORT_VALUE_LIST:
                    collection.setList("foo", new RealmList<>((short) 1));
                    break;
                case INTEGER_VALUE_LIST:
                    collection.setList("foo", new RealmList<>(1));
                    break;
                case LONG_VALUE_LIST:
                    collection.setList("foo", new RealmList<>(1L));
                    break;
                case FLOAT_VALUE_LIST:
                    collection.setList("foo", new RealmList<>(1.1F));
                    break;
                case DOUBLE_VALUE_LIST:
                    collection.setList("foo", new RealmList<>(1.1D));
                    break;
                case BINARY_VALUE_LIST:
                    collection.setList("foo", new RealmList<>(new byte[] {}));
                    break;
                case DATE_VALUE_LIST:
                    collection.setList("foo", new RealmList<>(new Date()));
                    break;
                case DECIMAL128_VALUE_LIST:
                    collection.setList("foo", new RealmList<>(new Decimal128(1000)));
                    break;
                case OBJECT_ID_VALUE_LIST:
                    collection.setList("foo", new RealmList<>(new ObjectId(TestHelper.randomObjectIdHexString())));
                    break;
                case UUID_VALUE_LIST:
                    collection.setList("foo", new RealmList<>(UUID.randomUUID()));
                    break;
                default:
                    fail("Unknown type: " + type);
            }
            fail(type + " should have thrown an exception");
        } catch (IllegalArgumentException e) {
            assertTrue(e.getMessage().contains("does not exist"));
        }
    }
}
Also used : ObjectId(org.bson.types.ObjectId) AllTypes(io.realm.entities.AllTypes) DictionaryAllTypes(io.realm.entities.DictionaryAllTypes) Decimal128(org.bson.types.Decimal128) Date(java.util.Date) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 70 with AllTypes

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

the class RealmResultsTests method populateTestRealm.

// distinctAsync
private void populateTestRealm(int objects) {
    realm.beginTransaction();
    realm.deleteAll();
    for (int i = 0; i < objects; ++i) {
        AllTypes allTypes = realm.createObject(AllTypes.class);
        allTypes.setColumnBoolean((i % 2) == 0);
        allTypes.setColumnBinary(new byte[] { 1, 2, 3 });
        allTypes.setColumnDate(new Date(YEAR_MILLIS * (i - objects / 2)));
        allTypes.setColumnDouble(Math.PI + i);
        allTypes.setColumnFloat(1.234567f + i);
        allTypes.setColumnString("test data " + i);
        allTypes.setColumnLong(i);
        Dog d = realm.createObject(Dog.class);
        d.setName("Foo " + i);
        allTypes.setColumnRealmObject(d);
        allTypes.getColumnRealmList().add(d);
        NonLatinFieldNames nonLatinFieldNames = realm.createObject(NonLatinFieldNames.class);
        nonLatinFieldNames.set델타(i);
        nonLatinFieldNames.setΔέλτα(i);
    }
    realm.commitTransaction();
}
Also used : NonLatinFieldNames(io.realm.entities.NonLatinFieldNames) AllTypes(io.realm.entities.AllTypes) DictionaryAllTypes(io.realm.entities.DictionaryAllTypes) Dog(io.realm.entities.Dog) Date(java.util.Date)

Aggregations

AllTypes (io.realm.entities.AllTypes)225 Test (org.junit.Test)210 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)70 UiThreadTest (androidx.test.annotation.UiThreadTest)54 Date (java.util.Date)36 DictionaryAllTypes (io.realm.entities.DictionaryAllTypes)32 Dog (io.realm.entities.Dog)27 JSONObject (org.json.JSONObject)26 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)22 RunInLooperThread (io.realm.rule.RunInLooperThread)20 InputStream (java.io.InputStream)19 Decimal128 (org.bson.types.Decimal128)19 ByteArrayInputStream (java.io.ByteArrayInputStream)17 ObjectId (org.bson.types.ObjectId)15 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 UiThreadTest (android.support.test.annotation.UiThreadTest)10 AtomicLong (java.util.concurrent.atomic.AtomicLong)10 OsSharedRealm (io.realm.internal.OsSharedRealm)9 BigDecimal (java.math.BigDecimal)9 NonLatinFieldNames (io.realm.entities.NonLatinFieldNames)8