use of io.realm.RealmFieldType in project realm-java by realm.
the class SortDescriptorTests method getInstanceForSort_numOfFeildsAndSortOrdersNotMatch.
@Test
public void getInstanceForSort_numOfFeildsAndSortOrdersNotMatch() {
RealmFieldType stringType = RealmFieldType.STRING;
table.addColumn(stringType, stringType.name());
RealmFieldType intType = RealmFieldType.INTEGER;
table.addColumn(intType, intType.name());
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Number of fields and sort orders do not match.");
SortDescriptor.getInstanceForSort(table, new String[] { stringType.name(), intType.name() }, new Sort[] { Sort.ASCENDING });
}
use of io.realm.RealmFieldType in project realm-java by realm.
the class JNITableTest method defaultValue_setAndGet.
@Test
public void defaultValue_setAndGet() {
// t is not used in this test.
t = null;
final SharedRealm sharedRealm = SharedRealm.getInstance(configFactory.createConfiguration());
//noinspection TryFinallyCanBeTryWithResources
try {
sharedRealm.beginTransaction();
final Table table = sharedRealm.getTable(Table.TABLE_PREFIX + "DefaultValueTest");
sharedRealm.commitTransaction();
List<Pair<RealmFieldType, Object>> columnInfoList = Arrays.asList(new Pair<RealmFieldType, Object>(RealmFieldType.STRING, "string value"), new Pair<RealmFieldType, Object>(RealmFieldType.INTEGER, 100L), new Pair<RealmFieldType, Object>(RealmFieldType.BOOLEAN, true), new Pair<RealmFieldType, Object>(RealmFieldType.BINARY, new byte[] { 123 }), new Pair<RealmFieldType, Object>(RealmFieldType.DATE, new Date(123456)), new Pair<RealmFieldType, Object>(RealmFieldType.FLOAT, 1.234f), new Pair<RealmFieldType, Object>(RealmFieldType.DOUBLE, Math.PI), new Pair<RealmFieldType, Object>(RealmFieldType.OBJECT, 0L));
for (Pair<RealmFieldType, Object> columnInfo : columnInfoList) {
final RealmFieldType type = columnInfo.first;
if (type == RealmFieldType.OBJECT || type == RealmFieldType.LIST) {
table.addColumnLink(type, type.name().toLowerCase(Locale.ENGLISH) + "Col", table);
} else {
table.addColumn(type, type.name().toLowerCase(Locale.ENGLISH) + "Col");
}
}
sharedRealm.beginTransaction();
table.addEmptyRow();
ListIterator<Pair<RealmFieldType, Object>> it = columnInfoList.listIterator();
for (int columnIndex = 0; columnIndex < columnInfoList.size(); columnIndex++) {
Pair<RealmFieldType, Object> columnInfo = it.next();
final RealmFieldType type = columnInfo.first;
final Object value = columnInfo.second;
switch(type) {
case STRING:
table.setString(columnIndex, 0, (String) value, true);
assertEquals(value, table.getString(columnIndex, 0));
break;
case INTEGER:
table.setLong(columnIndex, 0, (long) value, true);
assertEquals(value, table.getLong(columnIndex, 0));
break;
case BOOLEAN:
table.setBoolean(columnIndex, 0, (boolean) value, true);
assertEquals(value, table.getBoolean(columnIndex, 0));
break;
case BINARY:
table.setBinaryByteArray(columnIndex, 0, (byte[]) value, true);
assertTrue(Arrays.equals((byte[]) value, table.getBinaryByteArray(columnIndex, 0)));
break;
case DATE:
table.setDate(columnIndex, 0, (Date) value, true);
assertEquals(value, table.getDate(columnIndex, 0));
break;
case FLOAT:
table.setFloat(columnIndex, 0, (float) value, true);
assertEquals(value, table.getFloat(columnIndex, 0));
break;
case DOUBLE:
table.setDouble(columnIndex, 0, (double) value, true);
assertEquals(value, table.getDouble(columnIndex, 0));
break;
case OBJECT:
table.setLink(columnIndex, 0, (long) value, true);
assertEquals(value, table.getLink(columnIndex, 0));
break;
default:
throw new RuntimeException("unexpected field type: " + type);
}
}
sharedRealm.commitTransaction();
// Checks if the value can be read after committing transaction.
it = columnInfoList.listIterator();
for (int columnIndex = 0; columnIndex < columnInfoList.size(); columnIndex++) {
Pair<RealmFieldType, Object> columnInfo = it.next();
final RealmFieldType type = columnInfo.first;
final Object value = columnInfo.second;
switch(type) {
case STRING:
assertEquals(value, table.getString(columnIndex, 0));
break;
case INTEGER:
assertEquals(value, table.getLong(columnIndex, 0));
break;
case BOOLEAN:
assertEquals(value, table.getBoolean(columnIndex, 0));
break;
case BINARY:
assertTrue(Arrays.equals((byte[]) value, table.getBinaryByteArray(columnIndex, 0)));
break;
case DATE:
assertEquals(value, table.getDate(columnIndex, 0));
break;
case FLOAT:
assertEquals(value, table.getFloat(columnIndex, 0));
break;
case DOUBLE:
assertEquals(value, table.getDouble(columnIndex, 0));
break;
case OBJECT:
assertEquals(value, table.getLink(columnIndex, 0));
break;
default:
throw new RuntimeException("unexpected field type: " + type);
}
}
} finally {
sharedRealm.close();
}
}
use of io.realm.RealmFieldType in project realm-java by realm.
the class JNITableTest method convertToNotNullable.
@Test
public void convertToNotNullable() {
RealmFieldType[] columnTypes = { RealmFieldType.BOOLEAN, RealmFieldType.DATE, RealmFieldType.DOUBLE, RealmFieldType.FLOAT, RealmFieldType.INTEGER, RealmFieldType.BINARY, RealmFieldType.STRING };
for (RealmFieldType columnType : columnTypes) {
// Tests various combinations of column names and nullability.
String[] columnNames = { "foobar", "__TMP__0" };
for (boolean nullable : new boolean[] { Table.NOT_NULLABLE, Table.NULLABLE }) {
for (String columnName : columnNames) {
Table table = new Table();
long colIndex = table.addColumn(columnType, columnName, nullable);
table.addColumn(RealmFieldType.BOOLEAN, "bool");
table.addEmptyRow();
if (columnType == RealmFieldType.BOOLEAN)
table.setBoolean(colIndex, 0, true, false);
else if (columnType == RealmFieldType.DATE)
table.setDate(colIndex, 0, new Date(1), false);
else if (columnType == RealmFieldType.DOUBLE)
table.setDouble(colIndex, 0, 1.0, false);
else if (columnType == RealmFieldType.FLOAT)
table.setFloat(colIndex, 0, 1.0f, false);
else if (columnType == RealmFieldType.INTEGER)
table.setLong(colIndex, 0, 1, false);
else if (columnType == RealmFieldType.BINARY)
table.setBinaryByteArray(colIndex, 0, new byte[] { 0 }, false);
else if (columnType == RealmFieldType.STRING)
table.setString(colIndex, 0, "Foo", false);
try {
table.addEmptyRow();
if (columnType == RealmFieldType.BINARY) {
table.setBinaryByteArray(colIndex, 1, null, false);
} else if (columnType == RealmFieldType.STRING) {
table.setString(colIndex, 1, null, false);
} else {
table.getCheckedRow(1).setNull(colIndex);
}
if (!nullable) {
fail();
}
} catch (IllegalArgumentException ignored) {
}
assertEquals(2, table.size());
table.convertColumnToNotNullable(colIndex);
assertFalse(table.isColumnNullable(colIndex));
assertEquals(2, table.size());
assertEquals(2, table.getColumnCount());
assertTrue(table.getColumnIndex(columnName) >= 0);
assertEquals(colIndex, table.getColumnIndex(columnName));
table.addEmptyRow();
try {
if (columnType == RealmFieldType.BINARY) {
table.setBinaryByteArray(colIndex, 0, null, false);
} else if (columnType == RealmFieldType.STRING) {
table.setString(colIndex, 0, null, false);
} else {
table.getCheckedRow(0).setNull(colIndex);
}
if (!nullable) {
fail();
}
} catch (IllegalArgumentException ignored) {
}
table.removeLast();
assertEquals(2, table.size());
if (columnType == RealmFieldType.BINARY) {
assertNotNull(table.getBinaryByteArray(colIndex, 1));
} else if (columnType == RealmFieldType.STRING) {
assertNotNull(table.getString(colIndex, 1));
assertEquals("", table.getString(colIndex, 1));
} else {
assertFalse(table.getUncheckedRow(1).isNull(colIndex));
if (columnType == RealmFieldType.BOOLEAN)
assertEquals(false, table.getBoolean(colIndex, 1));
else if (columnType == RealmFieldType.DATE)
assertEquals(0, table.getDate(colIndex, 1).getTime());
else if (columnType == RealmFieldType.DOUBLE)
assertEquals(0.0, table.getDouble(colIndex, 1));
else if (columnType == RealmFieldType.FLOAT)
assertEquals(0.0f, table.getFloat(colIndex, 1));
else if (columnType == RealmFieldType.INTEGER)
assertEquals(0, table.getLong(colIndex, 1));
}
}
}
}
}
use of io.realm.RealmFieldType in project realm-java by realm.
the class JNITableTest method defaultValue_overwrittenByNonDefault.
@Test
public void defaultValue_overwrittenByNonDefault() {
// t is not used in this test.
t = null;
final SharedRealm sharedRealm = SharedRealm.getInstance(configFactory.createConfiguration());
//noinspection TryFinallyCanBeTryWithResources
try {
sharedRealm.beginTransaction();
final Table table = sharedRealm.getTable(Table.TABLE_PREFIX + "DefaultValueTest");
sharedRealm.commitTransaction();
List<Pair<RealmFieldType, Object>> columnInfoList = Arrays.asList(new Pair<RealmFieldType, Object>(RealmFieldType.STRING, new String[] { "string value1", "string value2" }), new Pair<RealmFieldType, Object>(RealmFieldType.INTEGER, new Long[] { 100L, 102L }), new Pair<RealmFieldType, Object>(RealmFieldType.BOOLEAN, new Boolean[] { false, true }), new Pair<RealmFieldType, Object>(RealmFieldType.BINARY, new byte[][] { new byte[] { 123 }, new byte[] { -123 } }), new Pair<RealmFieldType, Object>(RealmFieldType.DATE, new Date[] { new Date(123456), new Date(13579) }), new Pair<RealmFieldType, Object>(RealmFieldType.FLOAT, new Float[] { 1.234f, 100f }), new Pair<RealmFieldType, Object>(RealmFieldType.DOUBLE, new Double[] { Math.PI, Math.E }), new Pair<RealmFieldType, Object>(RealmFieldType.OBJECT, new Long[] { 0L, 1L }));
for (Pair<RealmFieldType, Object> columnInfo : columnInfoList) {
final RealmFieldType type = columnInfo.first;
if (type == RealmFieldType.OBJECT || type == RealmFieldType.LIST) {
table.addColumnLink(type, type.name().toLowerCase(Locale.ENGLISH) + "Col", table);
} else {
table.addColumn(type, type.name().toLowerCase(Locale.ENGLISH) + "Col");
}
}
sharedRealm.beginTransaction();
table.addEmptyRow();
// For link field update.
table.addEmptyRow();
// Sets as default.
ListIterator<Pair<RealmFieldType, Object>> it = columnInfoList.listIterator();
for (int columnIndex = 0; columnIndex < columnInfoList.size(); columnIndex++) {
Pair<RealmFieldType, Object> columnInfo = it.next();
final RealmFieldType type = columnInfo.first;
final Object value1 = ((Object[]) columnInfo.second)[0];
switch(type) {
case STRING:
table.setString(columnIndex, 0, (String) value1, true);
break;
case INTEGER:
table.setLong(columnIndex, 0, (long) value1, true);
break;
case BOOLEAN:
table.setBoolean(columnIndex, 0, (boolean) value1, true);
break;
case BINARY:
table.setBinaryByteArray(columnIndex, 0, (byte[]) value1, true);
break;
case DATE:
table.setDate(columnIndex, 0, (Date) value1, true);
break;
case FLOAT:
table.setFloat(columnIndex, 0, (float) value1, true);
break;
case DOUBLE:
table.setDouble(columnIndex, 0, (double) value1, true);
break;
case OBJECT:
table.setLink(columnIndex, 0, (long) value1, true);
break;
default:
throw new RuntimeException("unexpected field type: " + type);
}
}
sharedRealm.commitTransaction();
// Updates as non default.
sharedRealm.beginTransaction();
it = columnInfoList.listIterator();
for (int columnIndex = 0; columnIndex < columnInfoList.size(); columnIndex++) {
Pair<RealmFieldType, Object> columnInfo = it.next();
final RealmFieldType type = columnInfo.first;
final Object value2 = ((Object[]) columnInfo.second)[1];
switch(type) {
case STRING:
table.setString(columnIndex, 0, (String) value2, false);
assertEquals(value2, table.getString(columnIndex, 0));
break;
case INTEGER:
table.setLong(columnIndex, 0, (long) value2, false);
assertEquals(value2, table.getLong(columnIndex, 0));
break;
case BOOLEAN:
table.setBoolean(columnIndex, 0, (boolean) value2, false);
assertEquals(value2, table.getBoolean(columnIndex, 0));
break;
case BINARY:
table.setBinaryByteArray(columnIndex, 0, (byte[]) value2, false);
assertTrue(Arrays.equals((byte[]) value2, table.getBinaryByteArray(columnIndex, 0)));
break;
case DATE:
table.setDate(columnIndex, 0, (Date) value2, false);
assertEquals(value2, table.getDate(columnIndex, 0));
break;
case FLOAT:
table.setFloat(columnIndex, 0, (float) value2, false);
assertEquals(value2, table.getFloat(columnIndex, 0));
break;
case DOUBLE:
table.setDouble(columnIndex, 0, (double) value2, false);
assertEquals(value2, table.getDouble(columnIndex, 0));
break;
case OBJECT:
table.setLink(columnIndex, 0, (long) value2, false);
assertEquals(value2, table.getLink(columnIndex, 0));
break;
default:
throw new RuntimeException("unexpected field type: " + type);
}
}
sharedRealm.commitTransaction();
// Checks if the value was overwritten.
it = columnInfoList.listIterator();
for (int columnIndex = 0; columnIndex < columnInfoList.size(); columnIndex++) {
Pair<RealmFieldType, Object> columnInfo = it.next();
final RealmFieldType type = columnInfo.first;
final Object value2 = ((Object[]) columnInfo.second)[1];
switch(type) {
case STRING:
assertEquals(value2, table.getString(columnIndex, 0));
break;
case INTEGER:
assertEquals(value2, table.getLong(columnIndex, 0));
break;
case BOOLEAN:
assertEquals(value2, table.getBoolean(columnIndex, 0));
break;
case BINARY:
assertTrue(Arrays.equals((byte[]) value2, table.getBinaryByteArray(columnIndex, 0)));
break;
case DATE:
assertEquals(value2, table.getDate(columnIndex, 0));
break;
case FLOAT:
assertEquals(value2, table.getFloat(columnIndex, 0));
break;
case DOUBLE:
assertEquals(value2, table.getDouble(columnIndex, 0));
break;
case OBJECT:
assertEquals(value2, table.getLink(columnIndex, 0));
break;
default:
throw new RuntimeException("unexpected field type: " + type);
}
}
} finally {
sharedRealm.close();
}
}
use of io.realm.RealmFieldType in project realm-java by realm.
the class SortDescriptorTests method getInstanceForSort_shouldThrowOnInvalidField.
@Test
public void getInstanceForSort_shouldThrowOnInvalidField() {
List<RealmFieldType> types = new ArrayList<RealmFieldType>();
for (RealmFieldType type : RealmFieldType.values()) {
if (!SortDescriptor.validFieldTypesForSort.contains(type) && type != RealmFieldType.UNSUPPORTED_DATE && type != RealmFieldType.UNSUPPORTED_TABLE && type != RealmFieldType.UNSUPPORTED_MIXED) {
if (type == RealmFieldType.LIST || type == RealmFieldType.OBJECT) {
table.addColumnLink(type, type.name(), table);
} else {
table.addColumn(type, type.name());
}
types.add(type);
}
}
for (RealmFieldType type : types) {
try {
SortDescriptor.getInstanceForSort(table, type.name(), Sort.ASCENDING);
fail();
} catch (IllegalArgumentException ignored) {
assertTrue(ignored.getMessage().contains("Sort is not supported"));
}
}
}
Aggregations