Exemplo n.º 1
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());
  }
Exemplo n.º 2
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());
  }
Exemplo n.º 3
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());
  }
Exemplo n.º 4
0
  private void populatePartialNullRowsForNumericTesting() {
    NullTypes nullTypes1 = new NullTypes();
    nullTypes1.setId(1);
    nullTypes1.setFieldIntegerNull(1);
    nullTypes1.setFieldFloatNull(2F);
    nullTypes1.setFieldDoubleNull(3D);
    nullTypes1.setFieldBooleanNull(true);
    nullTypes1.setFieldStringNull("4");
    nullTypes1.setFieldDateNull(new Date(12345));

    NullTypes nullTypes2 = new NullTypes();
    nullTypes2.setId(2);

    NullTypes nullTypes3 = new NullTypes();
    nullTypes3.setId(3);
    nullTypes3.setFieldIntegerNull(0);
    nullTypes3.setFieldFloatNull(0F);
    nullTypes3.setFieldDoubleNull(0D);
    nullTypes3.setFieldBooleanNull(false);
    nullTypes3.setFieldStringNull("0");
    nullTypes3.setFieldDateNull(new Date(0));

    testRealm.beginTransaction();
    testRealm.copyToRealm(nullTypes1);
    testRealm.copyToRealm(nullTypes2);
    testRealm.copyToRealm(nullTypes3);
    testRealm.commitTransaction();
  }
Exemplo n.º 5
0
  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());
  }
Exemplo n.º 6
0
  // Triggered an ARM bug
  public void testComparisons() {
    testRealm.beginTransaction();
    testRealm.clear(AllTypes.class);
    long id = -1;
    for (int i = 0; i < 10; i++) {
      AllTypes allTypes = testRealm.createObject(AllTypes.class);
      allTypes.setColumnLong(id--);
    }
    testRealm.commitTransaction();

    assertEquals(
        10, testRealm.where(AllTypes.class).between(AllTypes.FIELD_LONG, -10, -1).findAll().size());
    assertEquals(
        10, testRealm.where(AllTypes.class).greaterThan(AllTypes.FIELD_LONG, -11).findAll().size());
    assertEquals(
        10,
        testRealm
            .where(AllTypes.class)
            .greaterThanOrEqualTo(AllTypes.FIELD_LONG, -10)
            .findAll()
            .size());
    assertEquals(
        10, testRealm.where(AllTypes.class).lessThan(AllTypes.FIELD_LONG, 128).findAll().size());
    assertEquals(
        10, testRealm.where(AllTypes.class).lessThan(AllTypes.FIELD_LONG, 127).findAll().size());
    assertEquals(
        10,
        testRealm
            .where(AllTypes.class)
            .lessThanOrEqualTo(AllTypes.FIELD_LONG, -1)
            .findAll()
            .size());
    assertEquals(
        10, testRealm.where(AllTypes.class).lessThan(AllTypes.FIELD_LONG, 0).findAll().size());
  }
Exemplo n.º 7
0
 public void testShouldNotContainRemovedItem() {
   RealmQuery<AllTypes> query = testRealm.where(AllTypes.class).findAll().where();
   AllTypes item = testRealm.where(AllTypes.class).findFirst();
   testRealm.beginTransaction();
   item.removeFromRealm();
   testRealm.commitTransaction();
   assertFalse("Should not contain a removed item.", query.findAll().contains(item));
 }
Exemplo n.º 8
0
  // void clear(Class<?> classSpec)
  public void testClearEmptiesTable() {
    RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll();
    assertEquals(TEST_DATA_SIZE, resultList.size());

    testRealm.beginTransaction();
    resultList.clear();
    testRealm.commitTransaction();

    assertEquals(0, resultList.size());
  }
Exemplo n.º 9
0
 public void testWithEmptyRealmObjects() {
   testRealm.beginTransaction();
   testRealm.clear(AllTypes.class);
   testRealm.commitTransaction();
   try {
     testRealm.where(AllTypes.class).findAll().sort(FIELD_STRING);
   } catch (IllegalArgumentException e) {
     fail("Failed to sort an empty RealmResults");
   }
 }
Exemplo n.º 10
0
  public void testRemoveLastEmptyList() {
    RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll();
    testRealm.beginTransaction();
    resultList.clear();
    assertEquals(0, resultList.size());
    resultList.removeLast();
    testRealm.commitTransaction();

    assertEquals(0, resultList.size());
  }
Exemplo n.º 11
0
  public void testClearFromAdapter() {
    RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll();
    RealmAdapter realmAdapter = new RealmAdapter(getContext(), resultList, automaticUpdate);

    testRealm.beginTransaction();
    realmAdapter.getRealmResults().clear();
    testRealm.commitTransaction();

    assertEquals(0, realmAdapter.getCount());
    assertEquals(0, resultList.size());
  }
Exemplo n.º 12
0
  public void testRemove() {
    RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll();
    testRealm.beginTransaction();
    resultList.remove(0);
    testRealm.commitTransaction();

    assertEquals(TEST_DATA_SIZE - 1, resultList.size());

    AllTypes allTypes = resultList.get(0);
    assertEquals(1, allTypes.getColumnLong());
  }
Exemplo n.º 13
0
  public void testRemoveFromAdapter() {
    RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll();
    RealmAdapter realmAdapter = new RealmAdapter(getContext(), resultList, automaticUpdate);

    testRealm.beginTransaction();
    realmAdapter.getRealmResults().remove(0);
    testRealm.commitTransaction();
    assertEquals(TEST_DATA_SIZE - 1, realmAdapter.getCount());

    resultList = testRealm.where(AllTypes.class).equalTo(FIELD_STRING, "test data 0").findAll();
    assertEquals(0, resultList.size());
  }
Exemplo n.º 14
0
  protected void setUp() throws Exception {
    super.setUp();
    Realm.deleteRealmFile(getContext());
    testRealm = Realm.getInstance(getContext());

    testRealm.beginTransaction();
    for (int i = 0; i < TEST_DATA_SIZE; ++i) {
      AllTypes allTypes = testRealm.createObject(AllTypes.class);
      allTypes.setColumnString("test data " + i);
    }
    testRealm.commitTransaction();
  }
Exemplo n.º 15
0
 public boolean methodWrongThread(final Method method)
     throws ExecutionException, InterruptedException {
   final RealmResults<AllTypes> allTypeses = testRealm.where(AllTypes.class).findAll();
   testRealm.beginTransaction();
   ExecutorService executorService = Executors.newSingleThreadExecutor();
   Future<Boolean> future =
       executorService.submit(
           new Callable<Boolean>() {
             @Override
             public Boolean call() throws Exception {
               try {
                 switch (method) {
                   case METHOD_MIN:
                     allTypeses.min(FIELD_FLOAT);
                     break;
                   case METHOD_MAX:
                     allTypeses.max(FIELD_FLOAT);
                     break;
                   case METHOD_SUM:
                     allTypeses.sum(FIELD_FLOAT);
                     break;
                   case METHOD_AVG:
                     allTypeses.average(FIELD_FLOAT);
                     break;
                   case METHOD_SORT:
                     allTypeses.sort(FIELD_FLOAT);
                     break;
                   case METHOD_WHERE:
                     allTypeses.where();
                     break;
                   case METHOD_REMOVE:
                     allTypeses.remove(0);
                     break;
                   case METHOD_REMOVE_LAST:
                     allTypeses.removeLast();
                     break;
                   case METHOD_CLEAR:
                     allTypeses.clear();
                     break;
                 }
                 return false;
               } catch (IllegalStateException ignored) {
                 return true;
               }
             }
           });
   Boolean result = future.get();
   testRealm.cancelTransaction();
   return result;
 }
Exemplo n.º 16
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());
  }
Exemplo n.º 17
0
  public void testSortByChildObject() {
    testRealm.beginTransaction();
    Owner owner = testRealm.createObject(Owner.class);
    owner.setName("owner");
    Cat cat = testRealm.createObject(Cat.class);
    cat.setName("cat");
    owner.setCat(cat);
    testRealm.commitTransaction();

    RealmQuery<Owner> query = testRealm.where(Owner.class);
    RealmResults<Owner> owners = query.findAll();

    try {
      owners.sort("cat.name");
      fail("Sorting by child object properties should result in a IllegalArgumentException");
    } catch (IllegalArgumentException ignore) {
    }
  }
Exemplo n.º 18
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");
    }
  }
Exemplo n.º 19
0
  public void testRemoveLast() {
    RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll();
    testRealm.beginTransaction();
    resultList.removeLast();
    testRealm.commitTransaction();

    assertEquals(
        "ResultList.removeLast did not remove record", TEST_DATA_SIZE - 1, resultList.size());

    AllTypes allTypes = resultList.get(resultList.size() - 1);
    assertEquals(
        "ResultList.removeLast unexpected last record",
        TEST_DATA_SIZE - 2,
        allTypes.getColumnLong());

    RealmResults<AllTypes> resultListCheck = testRealm.where(AllTypes.class).findAll();
    assertEquals("ResultList.removeLast not committed", TEST_DATA_SIZE - 1, resultListCheck.size());
  }
Exemplo n.º 20
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) {
    }
  }
Exemplo n.º 21
0
  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();
      }
    }
  }
Exemplo n.º 22
0
  public void testUpdateRealmResultInAdapter() {
    RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).findAll();
    resultList.sort(FIELD_STRING);
    RealmAdapter realmAdapter = new RealmAdapter(getContext(), resultList, false);
    assertEquals(
        resultList.first().getColumnString(),
        realmAdapter.getRealmResults().first().getColumnString());
    assertEquals(resultList.size(), realmAdapter.getRealmResults().size());

    testRealm.beginTransaction();
    AllTypes allTypes = testRealm.createObject(AllTypes.class);
    allTypes.setColumnString("test data " + TEST_DATA_SIZE);
    testRealm.commitTransaction();
    assertEquals(
        resultList.last().getColumnString(),
        realmAdapter.getRealmResults().last().getColumnString());
    assertEquals(resultList.size(), realmAdapter.getRealmResults().size());

    RealmResults<AllTypes> emptyResultList =
        testRealm.where(AllTypes.class).equalTo(FIELD_STRING, "Not there").findAll();
    realmAdapter.updateRealmResults(emptyResultList);
    assertEquals(emptyResultList.size(), realmAdapter.getRealmResults().size());
  }
Exemplo n.º 23
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();
  }
Exemplo n.º 24
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();
    }
  }