use of io.realm.entities.AllTypes in project realm-java by realm.
the class RealmTests method runMethodOnWrongThread.
// Calling methods on a wrong thread will fail.
private boolean runMethodOnWrongThread(final Method method) throws InterruptedException, ExecutionException {
if (method != Method.METHOD_BEGIN) {
realm.beginTransaction();
realm.createObject(Dog.class);
}
ExecutorService executorService = Executors.newSingleThreadExecutor();
Future<Boolean> future = executorService.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
try {
switch(method) {
case METHOD_BEGIN:
realm.beginTransaction();
break;
case METHOD_COMMIT:
realm.commitTransaction();
break;
case METHOD_CANCEL:
realm.cancelTransaction();
break;
case METHOD_EXECUTE_TRANSACTION:
realm.executeTransaction(realm -> fail());
break;
case METHOD_EXECUTE_TRANSACTION_ASYNC:
realm.executeTransactionAsync(realm -> fail());
break;
case METHOD_DELETE_TYPE:
realm.delete(AllTypes.class);
break;
case METHOD_DELETE_ALL:
realm.deleteAll();
break;
case METHOD_CREATE_OBJECT:
realm.createObject(AllTypes.class);
break;
case METHOD_CREATE_OBJECT_WITH_PRIMARY_KEY:
realm.createObject(AllJavaTypes.class, 1L);
break;
case METHOD_COPY_TO_REALM:
realm.copyToRealm(new AllTypes());
break;
case METHOD_COPY_TO_REALM_OR_UPDATE:
realm.copyToRealm(new AllTypesPrimaryKey());
break;
case METHOD_CREATE_ALL_FROM_JSON:
realm.createAllFromJson(AllTypes.class, "[{}]");
break;
case METHOD_CREATE_OR_UPDATE_ALL_FROM_JSON:
realm.createOrUpdateAllFromJson(AllTypesPrimaryKey.class, "[{\"columnLong\":1," + " \"columnBoolean\": true}]");
break;
case METHOD_CREATE_FROM_JSON:
realm.createObjectFromJson(AllTypes.class, "{}");
break;
case METHOD_CREATE_OR_UPDATE_FROM_JSON:
realm.createOrUpdateObjectFromJson(AllTypesPrimaryKey.class, "{\"columnLong\":1," + " \"columnBoolean\": true}");
break;
case METHOD_INSERT_COLLECTION:
realm.insert(Arrays.asList(new AllTypes(), new AllTypes()));
break;
case METHOD_INSERT_OBJECT:
realm.insert(new AllTypes());
break;
case METHOD_INSERT_OR_UPDATE_COLLECTION:
realm.insert(Arrays.asList(new AllTypesPrimaryKey(), new AllTypesPrimaryKey()));
break;
case METHOD_INSERT_OR_UPDATE_OBJECT:
realm.insertOrUpdate(new AllTypesPrimaryKey());
break;
}
return false;
} catch (IllegalStateException ignored) {
return true;
} catch (RealmException jsonFailure) {
// TODO: Eew. Reconsider how our JSON methods reports failure. See https://github.com/realm/realm-java/issues/1594
return (jsonFailure.getMessage().equals("Could not map Json"));
}
}
});
boolean result = future.get();
if (method != Method.METHOD_BEGIN) {
realm.cancelTransaction();
}
return result;
}
use of io.realm.entities.AllTypes in project realm-java by realm.
the class RealmTests method copyToRealm.
@Test
public void copyToRealm() {
Date date = new Date();
Dog dog = new Dog();
dog.setName("Fido");
RealmList<Dog> list = new RealmList<Dog>();
list.add(dog);
AllTypes allTypes = new AllTypes();
allTypes.setColumnString("String");
allTypes.setColumnLong(1L);
allTypes.setColumnFloat(1F);
allTypes.setColumnDouble(1D);
allTypes.setColumnBoolean(true);
allTypes.setColumnDate(date);
allTypes.setColumnBinary(new byte[] { 1, 2, 3 });
allTypes.setColumnDecimal128(new Decimal128(new BigDecimal("12345")));
allTypes.setColumnObjectId(new ObjectId(TestHelper.generateObjectIdHexString(7)));
allTypes.setColumnUUID(UUID.fromString(TestHelper.generateUUIDString(7)));
allTypes.setColumnRealmAny(RealmAny.valueOf(UUID.fromString(TestHelper.generateUUIDString(6))));
allTypes.setColumnRealmObject(dog);
allTypes.setColumnRealmList(list);
allTypes.setColumnStringList(new RealmList<String>("1"));
allTypes.setColumnBinaryList(new RealmList<byte[]>(new byte[] { 1 }));
allTypes.setColumnBooleanList(new RealmList<Boolean>(true));
allTypes.setColumnLongList(new RealmList<Long>(1L));
allTypes.setColumnDoubleList(new RealmList<Double>(1D));
allTypes.setColumnFloatList(new RealmList<Float>(1F));
allTypes.setColumnDateList(new RealmList<Date>(new Date(1L)));
allTypes.setColumnDecimal128List(new RealmList<Decimal128>(new Decimal128(new BigDecimal("54321"))));
allTypes.setColumnObjectIdList(new RealmList<ObjectId>(new ObjectId(TestHelper.generateObjectIdHexString(5))));
allTypes.setColumnUUIDList(new RealmList<>(UUID.fromString(TestHelper.generateUUIDString(5))));
allTypes.setColumnRealmAnyList(new RealmList<>(RealmAny.valueOf(UUID.fromString(TestHelper.generateUUIDString(7)))));
realm.beginTransaction();
AllTypes realmTypes = realm.copyToRealm(allTypes);
realm.commitTransaction();
// Objects should not be considered equal.
assertNotSame(allTypes, realmTypes);
// But they contain the same data.
assertEquals(allTypes.getColumnString(), realmTypes.getColumnString());
assertEquals(allTypes.getColumnLong(), realmTypes.getColumnLong());
assertEquals(allTypes.getColumnFloat(), realmTypes.getColumnFloat(), 0);
assertEquals(allTypes.getColumnDouble(), realmTypes.getColumnDouble(), 0);
assertEquals(allTypes.isColumnBoolean(), realmTypes.isColumnBoolean());
assertEquals(allTypes.getColumnDate(), realmTypes.getColumnDate());
assertArrayEquals(allTypes.getColumnBinary(), realmTypes.getColumnBinary());
assertEquals(allTypes.getColumnDecimal128(), realmTypes.getColumnDecimal128());
assertEquals(allTypes.getColumnObjectId(), realmTypes.getColumnObjectId());
assertEquals(allTypes.getColumnUUID(), realmTypes.getColumnUUID());
assertEquals(allTypes.getColumnRealmAny(), realmTypes.getColumnRealmAny());
assertEquals(allTypes.getColumnRealmObject().getName(), dog.getName());
assertEquals(list.size(), realmTypes.getColumnRealmList().size());
// noinspection ConstantConditions
assertEquals(list.get(0).getName(), realmTypes.getColumnRealmList().get(0).getName());
assertEquals(1, realmTypes.getColumnStringList().size());
assertEquals("1", realmTypes.getColumnStringList().get(0));
assertEquals(1, realmTypes.getColumnBooleanList().size());
assertEquals(true, realmTypes.getColumnBooleanList().get(0));
assertEquals(1, realmTypes.getColumnBinaryList().size());
assertArrayEquals(new byte[] { 1 }, realmTypes.getColumnBinaryList().get(0));
assertEquals(1, realmTypes.getColumnLongList().size());
assertEquals((Long) 1L, realmTypes.getColumnLongList().get(0));
assertEquals(1, realmTypes.getColumnDoubleList().size());
assertEquals((Double) 1D, realmTypes.getColumnDoubleList().get(0));
assertEquals(1, realmTypes.getColumnFloatList().size());
assertEquals((Float) 1F, realmTypes.getColumnFloatList().get(0));
assertEquals(1, realmTypes.getColumnDateList().size());
assertEquals(new Date(1), realmTypes.getColumnDateList().get(0));
assertEquals(1, realmTypes.getColumnDecimal128List().size());
assertEquals(new Decimal128(new BigDecimal("54321")), realmTypes.getColumnDecimal128List().get(0));
assertEquals(1, realmTypes.getColumnObjectIdList().size());
assertEquals(new ObjectId(TestHelper.generateObjectIdHexString(5)), realmTypes.getColumnObjectIdList().get(0));
assertEquals(1, realmTypes.getColumnUUIDList().size());
assertEquals(UUID.fromString(TestHelper.generateUUIDString(5)), realmTypes.getColumnUUIDList().get(0));
assertEquals(1, realmTypes.getColumnRealmAnyList().size());
assertEquals(RealmAny.valueOf(UUID.fromString(TestHelper.generateUUIDString(7))), realmTypes.getColumnRealmAnyList().get(0));
}
use of io.realm.entities.AllTypes in project realm-java by realm.
the class RealmTests method utf8Tests.
@Test
public void utf8Tests() {
realm.beginTransaction();
realm.delete(AllTypes.class);
realm.commitTransaction();
String file = "assets/unicode_codepoints.csv";
Scanner scanner = new Scanner(getClass().getClassLoader().getResourceAsStream(file), "UTF-8");
int i = 0;
String currentUnicode = null;
try {
realm.beginTransaction();
while (scanner.hasNextLine()) {
currentUnicode = scanner.nextLine();
char[] chars = Character.toChars(Integer.parseInt(currentUnicode, 16));
String codePoint = new String(chars);
AllTypes o = realm.createObject(AllTypes.class);
o.setColumnLong(i);
o.setColumnString(codePoint);
if (i > 1) {
assertEquals("Codepoint: " + i + " / " + currentUnicode, codePoint, // codepoint 0 is NULL, ignore for now.
o.getColumnString());
}
i++;
}
realm.commitTransaction();
} catch (Exception e) {
fail("Failure, Codepoint: " + i + " / " + currentUnicode + " " + e.getMessage());
}
}
use of io.realm.entities.AllTypes in project realm-java by realm.
the class RxJavaTests method realmList_parentDeletionCompleteFlowable.
@Test
@RunTestInLooperThread
public void realmList_parentDeletionCompleteFlowable() {
realm.beginTransaction();
final AllTypes parent = realm.createObject(AllTypes.class);
final RealmList<Dog> list = parent.getColumnRealmList();
list.add(new Dog("Fido"));
realm.commitTransaction();
looperThread.keepStrongReference(parent);
// We should only emit valid lists. If the parent of the list is invalidated
// it should close the stream gracefully resulting in onComplete being called.
subscription = list.asFlowable().subscribe(change -> {
assertTrue(change.isValid());
}, error -> {
fail(error.toString());
}, () -> {
// Deleting the parent will gracefully close the stream
disposeSuccessfulTest(realm);
});
looperThread.postRunnable(() -> {
realm.beginTransaction();
parent.deleteFromRealm();
realm.commitTransaction();
});
}
use of io.realm.entities.AllTypes in project realm-java by realm.
the class RxJavaTests method wrongGenericClassThrows.
@Test
@RunTestInLooperThread
public void wrongGenericClassThrows() {
realm.beginTransaction();
final AllTypes obj = realm.createObject(AllTypes.class);
realm.commitTransaction();
Flowable<CyclicType> obs = obj.asFlowable();
subscription = obs.subscribe(cyclicType -> fail(), ignoredError -> {
disposeSuccessfulTest(realm);
});
}
Aggregations