use of io.realm.entities.AllJavaTypes in project realm-java by realm.
the class ManagedOrderedRealmCollectionTests method sort_twoFields.
@Test
public void sort_twoFields() {
if (isSnapshot(collectionClass)) {
thrown.expect(UnsupportedOperationException.class);
}
OrderedRealmCollection<AllJavaTypes> sortedList = collection.sort(AllJavaTypes.FIELD_BOOLEAN, Sort.ASCENDING, AllJavaTypes.FIELD_LONG, Sort.DESCENDING);
AllJavaTypes obj = sortedList.first();
assertFalse(obj.isFieldBoolean());
assertEquals(TEST_SIZE - 1, obj.getFieldLong());
}
use of io.realm.entities.AllJavaTypes in project realm-java by realm.
the class ManagedRealmCollectionTests method where_shouldNotContainRemovedItem.
@Test
public void where_shouldNotContainRemovedItem() {
RealmQuery<AllJavaTypes> query = realm.where(AllJavaTypes.class).findAll().where();
AllJavaTypes item = realm.where(AllJavaTypes.class).findFirst();
realm.beginTransaction();
item.deleteFromRealm();
realm.commitTransaction();
assertFalse("Should not contain a removed item.", query.findAll().contains(item));
}
use of io.realm.entities.AllJavaTypes in project realm-java by realm.
the class ManagedRealmCollectionTests method where_queryDateField.
@Test
public void where_queryDateField() {
RealmQuery<AllJavaTypes> query = realm.where(AllJavaTypes.class).equalTo(AllJavaTypes.FIELD_DATE, new Date(YEAR_MILLIS * 20));
RealmResults<AllJavaTypes> all = query.findAll();
assertEquals(1, query.count());
assertEquals(1, all.size());
// before 1901
query = realm.where(AllJavaTypes.class).equalTo(AllJavaTypes.FIELD_DATE, new Date(YEAR_MILLIS * -100));
all = query.findAll();
assertEquals(1, query.count());
assertEquals(1, all.size());
// after 2038
query = realm.where(AllJavaTypes.class).equalTo(AllJavaTypes.FIELD_DATE, new Date(YEAR_MILLIS * 80));
all = query.findAll();
assertEquals(1, query.count());
assertEquals(1, all.size());
}
use of io.realm.entities.AllJavaTypes in project realm-java by realm.
the class ManagedRealmCollectionTests method methodsThrowOnWrongThread.
@Test
public void methodsThrowOnWrongThread() throws ExecutionException, InterruptedException {
realm.beginTransaction();
AllJavaTypes allJavaTypes = realm.createObject(AllJavaTypes.class, 42);
realm.commitTransaction();
for (RealmCollectionMethod method : RealmCollectionMethod.values()) {
assertTrue(method + " failed", runMethodOnWrongThread(method));
}
for (CollectionMethod method : CollectionMethod.values()) {
assertTrue(method + " failed", runMethodOnWrongThread(method, allJavaTypes));
}
}
use of io.realm.entities.AllJavaTypes in project realm-java by realm.
the class ManagedRealmCollectionTests method runMethodOnWrongThread.
private boolean runMethodOnWrongThread(final CollectionMethod method, final AllJavaTypes tempObject) throws ExecutionException, InterruptedException {
realm.beginTransaction();
ExecutorService executorService = Executors.newSingleThreadExecutor();
Future<Boolean> future = executorService.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
// Defines expected exception.
Class<? extends Throwable> expected = IllegalStateException.class;
if (collectionClass == ManagedCollection.REALMRESULTS || isSnapshot(collectionClass)) {
switch(method) {
case ADD_OBJECT:
case ADD_ALL_OBJECTS:
case CLEAR:
case REMOVE_OBJECT:
case REMOVE_ALL:
case RETAIN_ALL:
expected = UnsupportedOperationException.class;
}
}
try {
switch(method) {
case ADD_OBJECT:
collection.add(new AllJavaTypes());
break;
case ADD_ALL_OBJECTS:
collection.addAll(Collections.singletonList(new AllJavaTypes()));
break;
case CLEAR:
collection.clear();
case CONTAINS:
case CONTAINS_ALL:
collection.containsAll(Collections.singletonList(tempObject));
break;
case EQUALS:
//noinspection ResultOfMethodCallIgnored
collection.equals(createCollection(collectionClass));
break;
case HASHCODE:
//noinspection ResultOfMethodCallIgnored
collection.hashCode();
break;
case IS_EMPTY:
collection.isEmpty();
break;
// Creating an iterator should be safe. Accessing it will fail, but tested elsewhere.
case ITERATOR:
return true;
case REMOVE_OBJECT:
collection.remove(new AllJavaTypes());
break;
case REMOVE_ALL:
collection.removeAll(Collections.singletonList(new AllJavaTypes()));
break;
case RETAIN_ALL:
collection.retainAll(Collections.singletonList(new AllJavaTypes()));
break;
case SIZE:
collection.size();
break;
case TO_ARRAY:
collection.toArray();
break;
case TO_ARRAY_INPUT:
collection.toArray(new Object[collection.size()]);
break;
}
return false;
} catch (Throwable t) {
if (!t.getClass().equals(expected)) {
return false;
}
}
return true;
}
});
Boolean result = future.get();
realm.cancelTransaction();
return result;
}
Aggregations