@SuppressWarnings("unchecked") private byte[] callMethodAndConvertToCassandraType(Object obj, Method meth, @SuppressWarnings("rawtypes") Converter converter) { try { Object retVal = meth.invoke(obj, (Object[]) null); return converter.convertObjTypeToCassType(retVal); } catch (IllegalArgumentException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } }
private <T> void setPropertyUsingColumn(T obj, HColumn<String, byte[]> col, PropertyMappingDefinition md) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { PropertyDescriptor pd = md.getPropDesc(); if (null == pd.getWriteMethod()) { throw new RuntimeException("property, " + pd.getName() + ", on class, " + obj.getClass().getName() + ", does not have a setter and therefore cannot be set"); } Object value = md.getConverter().convertCassTypeToObjType(md, col.getValue()); pd.getWriteMethod().invoke(obj, value); }
@Test public void testInteger() throws Exception { byte[] ba; int i1 = 123; ba = conv.convertObjTypeToCassType(i1); assertEquals(i1, conv.convertCassTypeToObjType(new PropertyMappingDefinition( new PropertyDescriptor("i1", TestClass.class), "foo", VariableIntegerConverter.class), ba)); Integer i2 = 1; ba = conv.convertObjTypeToCassType(i2); assertEquals(i2, conv.convertCassTypeToObjType(new PropertyMappingDefinition( new PropertyDescriptor("i2", TestClass.class), "foo", VariableIntegerConverter.class), ba)); }
@SuppressWarnings("unchecked") private byte[] generateColumnFamilyKeyFromPkObj(CFMappingDef<?> cfMapDef, Object pkObj) { List<byte[]> segmentList = new ArrayList<byte[]>(cfMapDef.getKeyDef().getIdPropertyMap().size()); if (cfMapDef.getKeyDef().isComplexKey()) { Map<String, PropertyDescriptor> propertyDescriptorMap = cfMapDef.getKeyDef().getPropertyDescriptorMap(); for (String key : cfMapDef.getKeyDef().getIdPropertyMap().keySet()) { PropertyDescriptor pd = propertyDescriptorMap.get(key); segmentList.add(callMethodAndConvertToCassandraType(pkObj, pd.getReadMethod(), new DefaultConverter())); } } else { PropertyMappingDefinition md = cfMapDef.getKeyDef().getIdPropertyMap().values().iterator() .next(); segmentList.add(md.getConverter().convertObjTypeToCassType(pkObj)); } return keyConcatStrategy.concat(segmentList); }
@Test(expected=HectorObjectMapperException.class) public void testStringFromCassNotWork() throws Exception { conv.convertCassTypeToObjType(new PropertyMappingDefinition( new PropertyDescriptor("str1", TestClass.class), "foo", VariableIntegerConverter.class), new String("123").getBytes()); }
@Test public void testByte() throws Exception { byte[] ba; byte b1 = 123; ba = conv.convertObjTypeToCassType(b1); assertEquals(b1, conv.convertCassTypeToObjType(new PropertyMappingDefinition( new PropertyDescriptor("b1", TestClass.class), "foo", VariableIntegerConverter.class), ba)); Byte b2 = 1; ba = conv.convertObjTypeToCassType(b2); assertEquals(b2, conv.convertCassTypeToObjType(new PropertyMappingDefinition( new PropertyDescriptor("b2", TestClass.class), "foo", VariableIntegerConverter.class), ba)); }
private byte[] createBytesFromPropertyValue(Object obj, PropertyMappingDefinition md) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { Object retVal = reflectionHelper.invokeGetter(obj, md); // if no value, then signal with null bytes if (null == retVal) { return null; } @SuppressWarnings("unchecked") byte[] bytes = md.getConverter().convertObjTypeToCassType(retVal); return bytes; }
@Test public void testShort() throws Exception { byte[] ba; short s1 = 123; ba = conv.convertObjTypeToCassType(s1); assertEquals(s1, conv.convertCassTypeToObjType(new PropertyMappingDefinition( new PropertyDescriptor("s1", TestClass.class), "foo", VariableIntegerConverter.class), ba)); Short s2 = 1; ba = conv.convertObjTypeToCassType(s2); assertEquals(s2, conv.convertCassTypeToObjType(new PropertyMappingDefinition( new PropertyDescriptor("s2", TestClass.class), "foo", VariableIntegerConverter.class), ba)); }
@Test public void testLong() throws Exception { byte[] ba; long l1 = 123; ba = conv.convertObjTypeToCassType(l1); assertEquals(l1, conv.convertCassTypeToObjType(new PropertyMappingDefinition( new PropertyDescriptor("l1", TestClass.class), "foo", VariableIntegerConverter.class), ba)); Long l2 = 1L; ba = conv.convertObjTypeToCassType(l2); assertEquals(l2, conv.convertCassTypeToObjType(new PropertyMappingDefinition( new PropertyDescriptor("l2", TestClass.class), "foo", VariableIntegerConverter.class), ba)); }
@Test(expected=HectorObjectMapperException.class) public void testStringToCassNotWork() throws Exception { byte[] ba; String s1 = new String("123"); ba = conv.convertObjTypeToCassType(s1); assertEquals(s1, conv.convertCassTypeToObjType(new PropertyMappingDefinition( new PropertyDescriptor("str1", TestClass.class), "foo", VariableIntegerConverter.class), ba)); }
@Test public void testBigInteger() throws Exception { byte[] ba; BigInteger b1 = BigInteger.valueOf(123); ba = conv.convertObjTypeToCassType(b1); assertEquals(b1, conv.convertCassTypeToObjType(new PropertyMappingDefinition( new PropertyDescriptor("bigInt", TestClass.class), "foo", VariableIntegerConverter.class), ba)); }