Search in sources :

Example 1 with RecorderInvocationHandler

use of org.simpleflatmapper.datastax.test.utils.RecorderInvocationHandler 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 RecorderInvocationHandler

use of org.simpleflatmapper.datastax.test.utils.RecorderInvocationHandler in project SimpleFlatMapper by arnaudroger.

the class DataTypeTest method testNumberGetter.

private <N> void testNumberGetter(Class<N> numberClass, Class<?> primitiveGetter) 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);
                Getter<? super GettableByIndexData, N> getter = getGetter(numberClass, dataType);
                assertNotNull("No getter for " + numberClass + ", " + dataType, getter);
                if (numberClass.isPrimitive() && dataTypeClass.equals(numberClass) && primitiveGetter != null) {
                    primitiveGetter.isInstance(getter);
                }
                RecorderInvocationHandler recorder = new RecorderInvocationHandler();
                GettableByIndexData gettableByDataInstance = (GettableByIndexData) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { GettableByIndexData.class }, recorder);
                getter.get(gettableByDataInstance);
                recorder.invokedOnce(getterMethodFor(dataType), 1);
                if (!(dataTypeClass.equals(numberClass) && (BigInteger.class.equals(numberClass)) || BigDecimal.class.equals(numberClass))) {
                    recorder.when("isNull", 1, true);
                    assertNull(" fail isNull check " + numberClass + " - " + dataTypeClass, getter.get(gettableByDataInstance));
                    recorder.reset();
                }
            }
        }
    }
}
Also used : RecorderInvocationHandler(org.simpleflatmapper.datastax.test.utils.RecorderInvocationHandler) GettableByIndexData(com.datastax.driver.core.GettableByIndexData) DataType(com.datastax.driver.core.DataType) BigInteger(java.math.BigInteger) Method(java.lang.reflect.Method) BigDecimal(java.math.BigDecimal)

Example 3 with RecorderInvocationHandler

use of org.simpleflatmapper.datastax.test.utils.RecorderInvocationHandler in project SimpleFlatMapper by arnaudroger.

the class Datastax3 method testLocalDateGetter.

@Test
public void testLocalDateGetter() throws Exception {
    Getter<GettableByIndexData, ?> getter = DataTypeTest.getGetter(localDateClass, (DataType) DataType.class.getMethod("date").invoke(null));
    RecorderInvocationHandler recorder = new RecorderInvocationHandler();
    GettableByIndexData gettableByDataInstance = (GettableByIndexData) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { GettableByIndexData.class }, recorder);
    getter.get(gettableByDataInstance);
    recorder.invokedOnce("getDate", 1);
}
Also used : GettableByIndexData(com.datastax.driver.core.GettableByIndexData) RecorderInvocationHandler(org.simpleflatmapper.datastax.test.utils.RecorderInvocationHandler) DataType(com.datastax.driver.core.DataType) Test(org.junit.Test)

Example 4 with RecorderInvocationHandler

use of org.simpleflatmapper.datastax.test.utils.RecorderInvocationHandler 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)4 RecorderInvocationHandler (org.simpleflatmapper.datastax.test.utils.RecorderInvocationHandler)4 GettableByIndexData (com.datastax.driver.core.GettableByIndexData)2 SettableByIndexData (com.datastax.driver.core.SettableByIndexData)2 Method (java.lang.reflect.Method)2 Test (org.junit.Test)2 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1