Example #1
0
  // No sorting order defined. There are Korean, Arabic and Chinese characters.
  public void testSortWithManyDifferentCharacters() {
    testRealm.beginTransaction();
    testRealm.clear(AllTypes.class);
    AllTypes at1 = testRealm.createObject(AllTypes.class);
    at1.setColumnString("단위");
    AllTypes at2 = testRealm.createObject(AllTypes.class);
    at2.setColumnString("테스트");
    AllTypes at3 = testRealm.createObject(AllTypes.class);
    at3.setColumnString("وحدة");
    AllTypes at4 = testRealm.createObject(AllTypes.class);
    at4.setColumnString("اختبار");
    AllTypes at5 = testRealm.createObject(AllTypes.class);
    at5.setColumnString("单位");
    AllTypes at6 = testRealm.createObject(AllTypes.class);
    at6.setColumnString("试验");
    AllTypes at7 = testRealm.createObject(AllTypes.class);
    at7.setColumnString("單位");
    AllTypes at8 = testRealm.createObject(AllTypes.class);
    at8.setColumnString("測試");
    testRealm.commitTransaction();

    RealmResults<AllTypes> result = testRealm.allObjects(AllTypes.class);
    RealmResults<AllTypes> sortedResult = result.where().findAll();
    sortedResult.sort(FIELD_STRING);

    assertEquals(8, sortedResult.size());

    @SuppressWarnings("UnnecessaryLocalVariable")
    RealmResults<AllTypes> reverseResult = result;
    reverseResult.sort(FIELD_STRING, RealmResults.SORT_ORDER_DESCENDING);
    assertEquals(8, reverseResult.size());
  }
Example #2
0
 public void testSortSingleField() {
   RealmResults<AllTypes> sortedList = testRealm.allObjects(AllTypes.class);
   sortedList.sort(new String[] {FIELD_LONG}, new boolean[] {RealmResults.SORT_ORDER_DESCENDING});
   assertEquals(TEST_DATA_SIZE, sortedList.size());
   assertEquals(TEST_DATA_SIZE - 1, sortedList.first().getColumnLong());
   assertEquals(0, sortedList.last().getColumnLong());
 }
Example #3
0
  public void testSortWithRussianCharacters() {
    testRealm.beginTransaction();
    testRealm.clear(AllTypes.class);
    AllTypes at1 = testRealm.createObject(AllTypes.class);
    at1.setColumnString("Санкт-Петербург");
    AllTypes at2 = testRealm.createObject(AllTypes.class);
    at2.setColumnString("Москва");
    AllTypes at3 = testRealm.createObject(AllTypes.class);
    at3.setColumnString("Новороссийск");
    testRealm.commitTransaction();

    RealmResults<AllTypes> result = testRealm.allObjects(AllTypes.class);
    RealmResults<AllTypes> sortedResult = result.where().findAll();
    sortedResult.sort(FIELD_STRING);

    assertEquals(3, sortedResult.size());
    assertEquals("Москва", sortedResult.first().getColumnString());
    assertEquals("Москва", sortedResult.get(0).getColumnString());
    assertEquals("Новороссийск", sortedResult.get(1).getColumnString());
    assertEquals("Санкт-Петербург", sortedResult.get(2).getColumnString());

    RealmResults<AllTypes> reverseResult = result.where().findAll();
    reverseResult.sort(FIELD_STRING, RealmResults.SORT_ORDER_DESCENDING);
    assertEquals(3, reverseResult.size());
    assertEquals("Москва", reverseResult.last().getColumnString());
    assertEquals("Санкт-Петербург", reverseResult.get(0).getColumnString());
    assertEquals("Новороссийск", reverseResult.get(1).getColumnString());
    assertEquals("Москва", reverseResult.get(2).getColumnString());
  }
Example #4
0
  public void testSortWithGreekCharacters() {
    testRealm.beginTransaction();
    testRealm.clear(AllTypes.class);
    AllTypes at1 = testRealm.createObject(AllTypes.class);
    at1.setColumnString("αύριο");
    AllTypes at2 = testRealm.createObject(AllTypes.class);
    at2.setColumnString("ημέρες");
    AllTypes at3 = testRealm.createObject(AllTypes.class);
    at3.setColumnString("δοκιμές");
    testRealm.commitTransaction();

    RealmResults<AllTypes> result = testRealm.allObjects(AllTypes.class);
    RealmResults<AllTypes> sortedResult = result.where().findAll();
    sortedResult.sort(FIELD_STRING);

    assertEquals(3, sortedResult.size());
    assertEquals("αύριο", sortedResult.first().getColumnString());
    assertEquals("αύριο", sortedResult.get(0).getColumnString());
    assertEquals("δοκιμές", sortedResult.get(1).getColumnString());
    assertEquals("ημέρες", sortedResult.get(2).getColumnString());

    RealmResults<AllTypes> reverseResult = result.where().findAll();
    reverseResult.sort(FIELD_STRING, RealmResults.SORT_ORDER_DESCENDING);
    assertEquals(3, reverseResult.size());
    assertEquals("αύριο", reverseResult.last().getColumnString());
    assertEquals("ημέρες", reverseResult.get(0).getColumnString());
    assertEquals("δοκιμές", reverseResult.get(1).getColumnString());
    assertEquals("αύριο", reverseResult.get(2).getColumnString());
  }
Example #5
0
  public void testSortByLong() {
    RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll();
    RealmResults<AllTypes> sortedList = testRealm.allObjects(AllTypes.class);
    sortedList.sort(FIELD_LONG, RealmResults.SORT_ORDER_DESCENDING);
    assertEquals("Should have same size", resultList.size(), sortedList.size());
    assertEquals(TEST_DATA_SIZE, sortedList.size());
    assertEquals(
        "First excepted to be last",
        resultList.first().getColumnLong(),
        sortedList.last().getColumnLong());

    RealmResults<AllTypes> reverseList = sortedList;
    reverseList.sort(FIELD_LONG, RealmResults.SORT_ORDER_ASCENDING);
    assertEquals(TEST_DATA_SIZE, reverseList.size());
    assertEquals(
        "First excepted to be first",
        resultList.first().getColumnLong(),
        reverseList.first().getColumnLong());
    assertEquals(
        "Last excepted to be last",
        resultList.last().getColumnLong(),
        reverseList.last().getColumnLong());

    RealmResults<AllTypes> reserveSortedList = reverseList;
    reverseList.sort(FIELD_LONG, RealmResults.SORT_ORDER_DESCENDING);
    assertEquals(TEST_DATA_SIZE, reserveSortedList.size());
  }
  public void testSortWithDanishCharacters() {
    testRealm.beginTransaction();
    testRealm.clear(AllTypes.class);
    AllTypes at1 = testRealm.createObject(AllTypes.class);
    at1.setColumnString("Æble");
    AllTypes at2 = testRealm.createObject(AllTypes.class);
    at2.setColumnString("Øl");
    AllTypes at3 = testRealm.createObject(AllTypes.class);
    at3.setColumnString("Århus");
    testRealm.commitTransaction();

    RealmResults<AllTypes> result = testRealm.allObjects(AllTypes.class);
    RealmResults<AllTypes> sortedResult = result.where().findAll();
    sortedResult.sort(FIELD_STRING);

    assertEquals(3, sortedResult.size());
    assertEquals("Æble", sortedResult.first().getColumnString());
    assertEquals("Æble", sortedResult.get(0).getColumnString());
    assertEquals("Øl", sortedResult.get(1).getColumnString());
    assertEquals("Århus", sortedResult.get(2).getColumnString());

    RealmResults<AllTypes> reverseResult = result.where().findAll();
    reverseResult.sort(FIELD_STRING, Sort.DESCENDING);
    assertEquals(3, reverseResult.size());
    assertEquals("Æble", reverseResult.last().getColumnString());
    assertEquals("Århus", reverseResult.get(0).getColumnString());
    assertEquals("Øl", reverseResult.get(1).getColumnString());
    assertEquals("Æble", reverseResult.get(2).getColumnString());
  }
Example #7
0
 public void testIndexOf() {
   try {
     RealmResults<AllTypes> all = testRealm.allObjects(AllTypes.class);
     all.indexOf(all.first());
     fail();
   } catch (NoSuchMethodError ignored) {
   }
 }
Example #8
0
  private void doTestSortOnColumnWithPartialNullValues(String fieldName) {
    RealmResults<NullTypes> resultList = testRealm.where(NullTypes.class).findAll();
    // Ascending
    RealmResults<NullTypes> sortedList = testRealm.allObjects(NullTypes.class);
    sortedList.sort(fieldName, RealmResults.SORT_ORDER_ASCENDING);
    assertEquals("Should have same size", resultList.size(), sortedList.size());
    // Null should always be the first one in the ascending sorted list
    assertEquals(2, sortedList.first().getId());
    assertEquals(1, sortedList.last().getId());

    // Descending
    sortedList = testRealm.allObjects(NullTypes.class);
    sortedList.sort(fieldName, RealmResults.SORT_ORDER_DESCENDING);
    assertEquals("Should have same size", resultList.size(), sortedList.size());
    assertEquals(1, sortedList.first().getId());
    // Null should always be the last one in the descending sorted list
    assertEquals(2, sortedList.last().getId());
  }
  public void testContainsDoesNotContainAnItem() {
    RealmConfiguration realmConfig =
        TestHelper.createConfiguration(getContext(), "contains_test.realm");
    Realm.deleteRealm(realmConfig);
    Realm testRealmTwo = Realm.getInstance(realmConfig);
    try {

      testRealmTwo.beginTransaction();
      testRealmTwo.allObjects(AllTypes.class).clear();
      testRealmTwo.allObjects(NonLatinFieldNames.class).clear();

      for (int i = 0; i < TEST_DATA_SIZE; ++i) {
        AllTypes allTypes = testRealmTwo.createObject(AllTypes.class);
        allTypes.setColumnBoolean((i % 2) == 0);
        allTypes.setColumnBinary(new byte[] {1, 2, 3});
        allTypes.setColumnDate(new Date(YEAR_MILLIS * (i - TEST_DATA_SIZE / 2)));
        allTypes.setColumnDouble(3.1415 + i);
        allTypes.setColumnFloat(1.234567f + i);
        allTypes.setColumnString("test data " + i);
        allTypes.setColumnLong(i);
        Dog d = testRealmTwo.createObject(Dog.class);
        d.setName("Foo " + i);
        allTypes.setColumnRealmObject(d);
        allTypes.getColumnRealmList().add(d);
        NonLatinFieldNames nonLatinFieldNames = testRealmTwo.createObject(NonLatinFieldNames.class);
        nonLatinFieldNames.set델타(i);
        nonLatinFieldNames.setΔέλτα(i);
      }
      testRealmTwo.commitTransaction();

      final AllTypes item = testRealmTwo.where(AllTypes.class).findFirst();

      assertFalse(
          "Should not be able to find one object in another Realm via RealmResults#contains",
          testRealm.where(AllTypes.class).findAll().contains(item));

    } finally {
      if (testRealmTwo != null && !testRealmTwo.isClosed()) {
        testRealmTwo.close();
      }
    }
  }
Example #10
0
 public void testSortWithNullThrows() {
   RealmResults<AllTypes> result = testRealm.allObjects(AllTypes.class);
   try {
     result.sort(null);
     fail("Sorting with a null field name should throw an IllegalArgumentException");
   } catch (IllegalArgumentException ignored) {
   }
   try {
     result.sort(null, null);
     fail();
   } catch (IllegalArgumentException ignored) {
   }
 }
Example #11
0
  // Setting a nullable field to null is not an error
  public void testSetNullField() {
    TestHelper.populateTestRealmForNullTests(testRealm);
    RealmResults<NullTypes> list = testRealm.allObjects(NullTypes.class);

    // 1 String
    testRealm.beginTransaction();
    list.first().setFieldStringNull(null);
    testRealm.commitTransaction();
    assertNull(testRealm.allObjects(NullTypes.class).first().getFieldStringNull());

    // 2 Bytes
    testRealm.beginTransaction();
    list.first().setFieldBytesNull(null);
    testRealm.commitTransaction();
    assertNull(testRealm.allObjects(NullTypes.class).first().getFieldBytesNull());

    // 3 Boolean
    testRealm.beginTransaction();
    list.first().setFieldBooleanNull(null);
    testRealm.commitTransaction();
    assertNull(testRealm.allObjects(NullTypes.class).first().getFieldBooleanNull());

    // 4 Byte
    // 5 Short 6 Integer 7 Long are skipped
    testRealm.beginTransaction();
    list.first().setFieldByteNull(null);
    testRealm.commitTransaction();
    assertNull(testRealm.allObjects(NullTypes.class).first().getFieldByteNull());

    // 8 Float
    testRealm.beginTransaction();
    list.first().setFieldFloatNull(null);
    testRealm.commitTransaction();
    assertNull(testRealm.allObjects(NullTypes.class).first().getFieldFloatNull());

    // 9 Double
    testRealm.beginTransaction();
    list.first().setFieldDoubleNull(null);
    testRealm.commitTransaction();
    assertNull(testRealm.allObjects(NullTypes.class).first().getFieldDoubleNull());

    // 10 Date
    testRealm.beginTransaction();
    list.first().setFieldDateNull(null);
    testRealm.commitTransaction();
    assertNull(testRealm.allObjects(NullTypes.class).first().getFieldDateNull());
  }
Example #12
0
  private void populateTestRealm(int objects) {
    testRealm.beginTransaction();
    testRealm.allObjects(AllTypes.class).clear();
    testRealm.allObjects(NonLatinFieldNames.class).clear();

    for (int i = 0; i < objects; ++i) {
      AllTypes allTypes = testRealm.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(3.1415 + i);
      allTypes.setColumnFloat(1.234567f + i);
      allTypes.setColumnString("test data " + i);
      allTypes.setColumnLong(i);
      Dog d = testRealm.createObject(Dog.class);
      d.setName("Foo " + i);
      allTypes.setColumnRealmObject(d);
      allTypes.getColumnRealmList().add(d);
      NonLatinFieldNames nonLatinFieldNames = testRealm.createObject(NonLatinFieldNames.class);
      nonLatinFieldNames.set델타(i);
      nonLatinFieldNames.setΔέλτα(i);
    }
    testRealm.commitTransaction();
  }
Example #13
0
  public void testSortWithTwoLanguages() {
    testRealm.beginTransaction();
    testRealm.clear(AllTypes.class);
    AllTypes allTypes1 = testRealm.createObject(AllTypes.class);
    allTypes1.setColumnString("test");
    AllTypes allTypes2 = testRealm.createObject(AllTypes.class);
    allTypes2.setColumnString("αύριο");
    AllTypes allTypes3 = testRealm.createObject(AllTypes.class);
    allTypes3.setColumnString("work");
    testRealm.commitTransaction();

    try {
      RealmResults<AllTypes> result = testRealm.allObjects(AllTypes.class);
      result.sort(FIELD_STRING);
    } catch (IllegalArgumentException e) {
      fail("Failed to sort with two kinds of alphabets");
    }
  }
Example #14
0
  // first() and last() will throw an exception when no element exist
  public void testResultListFirstLastThrowIfEmpty() {
    testRealm.beginTransaction();
    testRealm.clear(AllTypes.class);
    testRealm.commitTransaction();

    RealmResults<AllTypes> allTypes = testRealm.allObjects(AllTypes.class);
    assertEquals(0, allTypes.size());
    try {
      allTypes.first();
      fail();
    } catch (ArrayIndexOutOfBoundsException ignored) {
    }

    try {
      allTypes.last();
      fail();
    } catch (ArrayIndexOutOfBoundsException ignored) {
    }
  }
Example #15
0
  // Setting a not-nullable field to null is an error
  public void testNullFieldNotNullableField() {
    TestHelper.populateTestRealmForNullTests(testRealm);
    RealmResults<NullTypes> list = testRealm.allObjects(NullTypes.class);

    // 1 String
    try {
      testRealm.beginTransaction();
      list.first().setFieldStringNotNull(null);
      fail();
    } catch (IllegalArgumentException ignored) {
    } finally {
      testRealm.cancelTransaction();
    }

    // 2 Bytes
    try {
      testRealm.beginTransaction();
      list.first().setFieldBytesNotNull(null);
      fail();
    } catch (IllegalArgumentException ignored) {
    } finally {
      testRealm.cancelTransaction();
    }

    // 3 Boolean
    try {
      testRealm.beginTransaction();
      list.first().setFieldBooleanNotNull(null);
      fail();
    } catch (IllegalArgumentException ignored) {
    } finally {
      testRealm.cancelTransaction();
    }

    // 4 Byte
    try {
      testRealm.beginTransaction();
      list.first().setFieldBytesNotNull(null);
      fail();
    } catch (IllegalArgumentException ignored) {
    } finally {
      testRealm.cancelTransaction();
    }

    // 5 Short 6 Integer 7 Long are skipped for this case, same with Bytes

    // 8 Float
    try {
      testRealm.beginTransaction();
      list.first().setFieldFloatNotNull(null);
      fail();
    } catch (IllegalArgumentException ignored) {
    } finally {
      testRealm.cancelTransaction();
    }

    // 9 Double
    try {
      testRealm.beginTransaction();
      list.first().setFieldDoubleNotNull(null);
      fail();
    } catch (IllegalArgumentException ignored) {
    } finally {
      testRealm.cancelTransaction();
    }

    // 10 Date
    try {
      testRealm.beginTransaction();
      list.first().setFieldDateNotNull(null);
      fail();
    } catch (IllegalArgumentException ignored) {
    } finally {
      testRealm.cancelTransaction();
    }
  }
Example #16
0
 public void testSubList() {
   RealmResults<AllTypes> list = testRealm.allObjects(AllTypes.class);
   list.sort("columnLong");
   List<AllTypes> sublist = list.subList(Math.max(list.size() - 20, 0), list.size());
   assertEquals(TEST_DATA_SIZE - 1, sublist.get(sublist.size() - 1).getColumnLong());
 }