Search in sources :

Example 1 with SettableByIndexData

use of com.datastax.driver.core.SettableByIndexData in project SimpleFlatMapper by arnaudroger.

the class DataTypeTest method testNumberSetter.

private <N> void testNumberSetter(Class<N> numberClass, Class<?> primitiveSetter) throws Exception {
    Method[] methods = DataType.class.getMethods();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        if (method.getReturnType().equals(DataType.class) && Modifier.isStatic(method.getModifiers()) && method.getParameterTypes().length == 0) {
            DataType dataType = (DataType) method.invoke(null);
            if (DataTypeHelper.isNumber(dataType)) {
                Class<?> dataTypeClass = DataTypeHelper.asJavaClass(dataType);
                Setter<? super SettableByIndexData, N> setter = getSetter(numberClass, dataType);
                assertNotNull("No setter for " + numberClass + ", " + dataType, setter);
                if (numberClass.isPrimitive() && dataTypeClass.equals(numberClass) && primitiveSetter != null) {
                    primitiveSetter.isInstance(setter);
                }
                RecorderInvocationHandler recorder = new RecorderInvocationHandler();
                SettableByIndexData settableByDataInstance = (SettableByIndexData) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { SettableByIndexData.class }, recorder);
                N value = getValue(numberClass);
                setter.set(settableByDataInstance, value);
                recorder.invokedOnce(setterMethodFor(dataType), 1, getValue(dataTypeClass));
                recorder.reset();
                setter.set(settableByDataInstance, null);
                recorder.invokedOnce("setToNull", 1);
            }
        }
    }
}
Also used : RecorderInvocationHandler(org.simpleflatmapper.datastax.test.utils.RecorderInvocationHandler) SettableByIndexData(com.datastax.driver.core.SettableByIndexData) DataType(com.datastax.driver.core.DataType) Method(java.lang.reflect.Method)

Example 2 with SettableByIndexData

use of com.datastax.driver.core.SettableByIndexData in project SimpleFlatMapper by arnaudroger.

the class Datastax3 method testLocalDateSetter.

@SuppressWarnings("unchecked")
@Test
public void testLocalDateSetter() throws Exception {
    Setter<SettableByIndexData, Object> setter = DataTypeTest.getSetter((Class<Object>) localDateClass, (DataType) DataType.class.getMethod("date").invoke(null));
    RecorderInvocationHandler recorder = new RecorderInvocationHandler();
    SettableByIndexData settableByDataInstance = (SettableByIndexData) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { SettableByIndexData.class }, recorder);
    Object localDateInstance = localDateClass.getDeclaredMethod("fromMillisSinceEpoch", long.class).invoke(null, System.currentTimeMillis());
    setter.set(settableByDataInstance, localDateInstance);
    recorder.invokedOnce("setDate", 1, localDateInstance);
    recorder.reset();
    setter.set(settableByDataInstance, null);
    recorder.invokedOnce("setToNull", 1);
}
Also used : SettableByIndexData(com.datastax.driver.core.SettableByIndexData) RecorderInvocationHandler(org.simpleflatmapper.datastax.test.utils.RecorderInvocationHandler) DataType(com.datastax.driver.core.DataType) Test(org.junit.Test)

Aggregations

DataType (com.datastax.driver.core.DataType)2 SettableByIndexData (com.datastax.driver.core.SettableByIndexData)2 RecorderInvocationHandler (org.simpleflatmapper.datastax.test.utils.RecorderInvocationHandler)2 Method (java.lang.reflect.Method)1 Test (org.junit.Test)1