use of com.thinkaurelius.titan.graphdb.serializer.SpecialInt in project titan by thinkaurelius.
the class TitanGraphTest method testDataTypes.
/**
* Test the different data types that Titan supports natively and ensure that invalid data types aren't allowed
*/
@Test
public void testDataTypes() throws Exception {
clopen(option(CUSTOM_ATTRIBUTE_CLASS, "attribute10"), SpecialInt.class.getCanonicalName(), option(CUSTOM_SERIALIZER_CLASS, "attribute10"), SpecialIntSerializer.class.getCanonicalName());
PropertyKey num = makeKey("num", SpecialInt.class);
PropertyKey barr = makeKey("barr", byte[].class);
PropertyKey boolval = makeKey("boolval", Boolean.class);
PropertyKey birthday = makeKey("birthday", Instant.class);
PropertyKey geo = makeKey("geo", Geoshape.class);
PropertyKey precise = makeKey("precise", Double.class);
PropertyKey any = mgmt.makePropertyKey("any").cardinality(Cardinality.LIST).dataType(Object.class).make();
try {
//Not a valid data type - primitive
makeKey("pint", int.class);
fail();
} catch (IllegalArgumentException e) {
}
try {
//Not a valid data type - interface
makeKey("number", Number.class);
fail();
} catch (IllegalArgumentException e) {
}
finishSchema();
clopen();
boolval = tx.getPropertyKey("boolval");
num = tx.getPropertyKey("num");
barr = tx.getPropertyKey("barr");
birthday = tx.getPropertyKey("birthday");
geo = tx.getPropertyKey("geo");
precise = tx.getPropertyKey("precise");
any = tx.getPropertyKey("any");
assertEquals(Boolean.class, boolval.dataType());
assertEquals(byte[].class, barr.dataType());
assertEquals(Object.class, any.dataType());
final Instant c = Instant.ofEpochSecond(1429225756);
final Geoshape shape = Geoshape.box(10.0, 10.0, 20.0, 20.0);
TitanVertex v = tx.addVertex();
v.property(n(boolval), true);
v.property(VertexProperty.Cardinality.single, n(birthday), c);
v.property(VertexProperty.Cardinality.single, n(num), new SpecialInt(10));
v.property(VertexProperty.Cardinality.single, n(barr), new byte[] { 1, 2, 3, 4 });
v.property(VertexProperty.Cardinality.single, n(geo), shape);
v.property(VertexProperty.Cardinality.single, n(precise), 10.12345);
v.property(n(any), "Hello");
v.property(n(any), 10l);
int[] testarr = { 5, 6, 7 };
v.property(n(any), testarr);
// ######## VERIFICATION ##########
assertTrue(v.<Boolean>value("boolval"));
assertEquals(10, v.<SpecialInt>value("num").getValue());
assertEquals(c, v.value("birthday"));
assertEquals(4, v.<byte[]>value("barr").length);
assertEquals(shape, v.<Geoshape>value("geo"));
assertEquals(10.12345, v.<Double>value("precise").doubleValue(), 0.000001);
assertCount(3, v.properties("any"));
for (TitanVertexProperty prop : v.query().labels("any").properties()) {
Object value = prop.value();
if (value instanceof String)
assertEquals("Hello", value);
else if (value instanceof Long)
assertEquals(10l, value);
else if (value.getClass().isArray()) {
assertTrue(Arrays.equals(testarr, (int[]) value));
} else
fail();
}
clopen();
v = getV(tx, v);
// ######## VERIFICATION (copied from above) ##########
assertTrue(v.<Boolean>value("boolval"));
assertEquals(10, v.<SpecialInt>value("num").getValue());
assertEquals(c, v.value("birthday"));
assertEquals(4, v.<byte[]>value("barr").length);
assertEquals(shape, v.<Geoshape>value("geo"));
assertEquals(10.12345, v.<Double>value("precise").doubleValue(), 0.000001);
assertCount(3, v.properties("any"));
for (TitanVertexProperty prop : v.query().labels("any").properties()) {
Object value = prop.value();
if (value instanceof String)
assertEquals("Hello", value);
else if (value instanceof Long)
assertEquals(10l, value);
else if (value.getClass().isArray()) {
assertTrue(Arrays.equals(testarr, (int[]) value));
} else
fail();
}
}
Aggregations