use of io.realm.entities.AllJavaTypes in project realm-java by realm.
the class OrderedRealmCollectionTests method createCollectionWithMultipleCopies.
private Pair<AllJavaTypes, OrderedRealmCollection<AllJavaTypes>> createCollectionWithMultipleCopies(Realm realm, CollectionClass collectionClass) {
OrderedRealmCollection<AllJavaTypes> orderedCollection;
AllJavaTypes obj;
switch(collectionClass) {
case REALMRESULTS_SNAPSHOT_LIST_BASE:
case MANAGED_REALMLIST:
obj = realm.where(AllJavaTypes.class).equalTo(AllJavaTypes.FIELD_LONG, 1).findFirst();
RealmList<AllJavaTypes> list = obj.getFieldList();
realm.beginTransaction();
list.add(obj);
realm.commitTransaction();
orderedCollection = list;
break;
case UNMANAGED_REALMLIST:
obj = new AllJavaTypes(1);
return new Pair<AllJavaTypes, OrderedRealmCollection<AllJavaTypes>>(obj, new RealmList<AllJavaTypes>(obj, obj));
case REALMRESULTS_SNAPSHOT_RESULTS_BASE:
case REALMRESULTS:
RealmResults<AllJavaTypes> result = realm.where(AllJavaTypes.class).equalTo(AllJavaTypes.FIELD_LONG, 1).findAll();
obj = result.first();
orderedCollection = result;
break;
default:
throw new AssertionError("Unsupported class: " + collectionClass);
}
if (isSnapshot(collectionClass)) {
orderedCollection = orderedCollection.createSnapshot();
}
return new Pair<AllJavaTypes, OrderedRealmCollection<AllJavaTypes>>(obj, orderedCollection);
}
use of io.realm.entities.AllJavaTypes in project realm-java by realm.
the class OrderedRealmCollectionTests method first_withDefault.
@Test
public void first_withDefault() {
AllJavaTypes defaultObject = new AllJavaTypes();
collection = createEmptyCollection(realm, collectionClass);
assertEquals(defaultObject, collection.first(defaultObject));
// Null is an acceptable default
assertEquals(null, collection.first(null));
}
use of io.realm.entities.AllJavaTypes in project realm-java by realm.
the class RealmObjectTests method setter_changePrimaryKeyThrows.
@Test
public void setter_changePrimaryKeyThrows() {
realm.beginTransaction();
AllJavaTypes allJavaTypes = realm.createObject(AllJavaTypes.class, 42);
thrown.expect(RealmException.class);
allJavaTypes.setFieldId(111);
realm.cancelTransaction();
}
use of io.realm.entities.AllJavaTypes in project realm-java by realm.
the class DynamicRealmObjectTests method untypedSetter_usingStringConversion.
@Test
public void untypedSetter_usingStringConversion() {
realm.beginTransaction();
AllJavaTypes obj = realm.createObject(AllJavaTypes.class, 0);
DynamicRealmObject dObj = new DynamicRealmObject(obj);
try {
for (SupportedType type : SupportedType.values()) {
switch(type) {
case BOOLEAN:
dObj.set(AllJavaTypes.FIELD_BOOLEAN, "true");
assertTrue(dObj.getBoolean(AllJavaTypes.FIELD_BOOLEAN));
break;
case SHORT:
dObj.set(AllJavaTypes.FIELD_SHORT, "42");
assertEquals((short) 42, dObj.getShort(AllJavaTypes.FIELD_SHORT));
break;
case INT:
dObj.set(AllJavaTypes.FIELD_INT, "42");
assertEquals(42, dObj.getInt(AllJavaTypes.FIELD_INT));
break;
case LONG:
dObj.set(AllJavaTypes.FIELD_LONG, "42");
assertEquals((long) 42, dObj.getLong(AllJavaTypes.FIELD_LONG));
break;
case FLOAT:
dObj.set(AllJavaTypes.FIELD_FLOAT, "1.23");
assertEquals(1.23f, dObj.getFloat(AllJavaTypes.FIELD_FLOAT), 0f);
break;
case DOUBLE:
dObj.set(AllJavaTypes.FIELD_DOUBLE, "1.234");
assertEquals(1.234d, dObj.getDouble(AllJavaTypes.FIELD_DOUBLE), 0f);
break;
case DATE:
dObj.set(AllJavaTypes.FIELD_DATE, "1000");
assertEquals(new Date(1000), dObj.getDate(AllJavaTypes.FIELD_DATE));
break;
// These types don't have a string representation that can be parsed.
case OBJECT:
case LIST:
case STRING:
case BINARY:
case BYTE:
break;
default:
fail("Unknown type: " + type);
break;
}
}
} finally {
realm.cancelTransaction();
}
}
use of io.realm.entities.AllJavaTypes in project realm-java by realm.
the class LinkingObjectsManagedTests method json_jsonUpdateCausesNotification.
// A JSON update should generate a notifcation
@Test
@RunTestInLooperThread
public void json_jsonUpdateCausesNotification() {
final Realm looperThreadRealm = looperThread.realm;
looperThreadRealm.beginTransaction();
AllJavaTypes child = looperThreadRealm.createObject(AllJavaTypes.class, 1);
AllJavaTypes parent = looperThreadRealm.createObject(AllJavaTypes.class, 2);
parent.setFieldObject(child);
looperThreadRealm.commitTransaction();
RealmResults<AllJavaTypes> results = looperThreadRealm.where(AllJavaTypes.class).equalTo("fieldId", 1).findAll();
assertNotNull(results);
assertEquals(results.size(), 1);
child = results.first();
RealmResults<AllJavaTypes> parents = child.getObjectParents();
assertNotNull(parents);
assertEquals(1, parents.size());
final AtomicInteger counter = new AtomicInteger(0);
RealmChangeListener<AllJavaTypes> listener = new RealmChangeListener<AllJavaTypes>() {
@Override
public void onChange(AllJavaTypes object) {
counter.incrementAndGet();
}
};
child.addChangeListener(listener);
looperThreadRealm.beginTransaction();
try {
looperThreadRealm.createOrUpdateAllFromJson(AllJavaTypes.class, "[{ \"fieldId\" : 2, \"fieldObject\" : null }]");
} catch (RealmException e) {
fail("Failed loading JSON" + e);
}
looperThreadRealm.commitTransaction();
verifyPostConditions(looperThreadRealm, new PostConditions() {
public void run(Realm realm) {
RealmResults<AllJavaTypes> results = looperThreadRealm.where(AllJavaTypes.class).equalTo("fieldId", 1).findAll();
assertNotNull(results);
assertEquals(results.size(), 1);
AllJavaTypes child = results.first();
RealmResults<AllJavaTypes> parents = child.getObjectParents();
assertNotNull(parents);
assertEquals(0, parents.size());
assertEquals(1, counter.get());
}
}, child, parent);
}
Aggregations