Search in sources :

Example 1 with AnnotationDescriptor

use of org.apache.drill.common.scanner.persistence.AnnotationDescriptor in project drill by apache.

the class TestClassPathScanner method test.

@Test
public void test() throws Exception {
    ScanResult result = ClassPathScanner.fromPrescan(DrillConfig.create());
    // if the build has run properly. BuildTimeScan.REGISTRY_FILE was created with a prescan
    //    assertListEqualsUnordered(result.getPrescannedPackages(),
    //      "org.apache.drill.common.logical",
    //      "org.apache.drill.exec.expr",
    //      "org.apache.drill.exec.physical.base",
    //      "org.apache.drill.exec.expr.fn.impl",
    //      "org.apache.drill.exec.physical.impl",
    //      "org.apache.drill.exec.rpc.user.security",
    //      "org.apache.drill.exec.store",
    //      "org.apache.drill.exec.store.mock",
    //      "org.apache.drill.exec.physical.config",
    //      "org.apache.drill.storage"
    //    );
    //    // this is added in the unit test folder that was not scanned so far
    //    assertListEqualsUnordered(result.getScannedPackages(),
    //      "org.apache.drill.exec.testing",
    //      "org.apache.drill.exec.fn.impl.testing",
    //      "org.apache.drill.exec.rpc.user.security.testing"
    //    );
    List<AnnotatedClassDescriptor> functions = result.getAnnotatedClasses();
    Set<String> scanned = new HashSet<>();
    AnnotatedClassDescriptor functionRandomBigIntGauss = null;
    for (AnnotatedClassDescriptor function : functions) {
        assertTrue(function.getClassName() + " scanned twice", scanned.add(function.getClassName()));
        if (function.getClassName().equals(RandomBigIntGauss.class.getName())) {
            functionRandomBigIntGauss = function;
        }
    }
    if (functionRandomBigIntGauss == null) {
        Assert.fail("functionRandomBigIntGauss not found");
    }
    // TODO: use Andrew's randomized test framework to verify a subset of the functions
    for (AnnotatedClassDescriptor function : functions) {
        Class<?> c = Class.forName(function.getClassName(), false, this.getClass().getClassLoader());
        Field[] fields = c.getDeclaredFields();
        assertEquals("fields count for " + function, fields.length, function.getFields().size());
        for (int i = 0; i < fields.length; i++) {
            FieldDescriptor fieldDescriptor = function.getFields().get(i);
            Field field = fields[i];
            assertEquals("Class fields:\n" + Arrays.toString(fields) + "\n != \nDescriptor fields:\n" + function.getFields(), field.getName(), fieldDescriptor.getName());
            verifyAnnotations(field.getDeclaredAnnotations(), fieldDescriptor.getAnnotations());
            assertEquals(field.getType(), fieldDescriptor.getFieldClass());
        }
        Annotation[] annotations = c.getDeclaredAnnotations();
        List<AnnotationDescriptor> scannedAnnotations = function.getAnnotations();
        verifyAnnotations(annotations, scannedAnnotations);
        FunctionTemplate bytecodeAnnotation = function.getAnnotationProxy(FunctionTemplate.class);
        FunctionTemplate reflectionAnnotation = c.getAnnotation(FunctionTemplate.class);
        assertEquals(reflectionAnnotation.name(), bytecodeAnnotation.name());
        assertArrayEquals(reflectionAnnotation.names(), bytecodeAnnotation.names());
        assertEquals(reflectionAnnotation.scope(), bytecodeAnnotation.scope());
        assertEquals(reflectionAnnotation.nulls(), bytecodeAnnotation.nulls());
        assertEquals(reflectionAnnotation.isBinaryCommutative(), bytecodeAnnotation.isBinaryCommutative());
        assertEquals(reflectionAnnotation.desc(), bytecodeAnnotation.desc());
        assertEquals(reflectionAnnotation.costCategory(), bytecodeAnnotation.costCategory());
    }
    for (String baseType : result.getScannedClasses()) {
        validateType(result, baseType);
    }
    assertTrue(result.getImplementations(PhysicalOperator.class).size() > 0);
    assertTrue(result.getImplementations(DrillFunc.class).size() > 0);
}
Also used : AnnotationDescriptor(org.apache.drill.common.scanner.persistence.AnnotationDescriptor) ScanResult(org.apache.drill.common.scanner.persistence.ScanResult) Annotation(java.lang.annotation.Annotation) FieldDescriptor(org.apache.drill.common.scanner.persistence.FieldDescriptor) AnnotatedClassDescriptor(org.apache.drill.common.scanner.persistence.AnnotatedClassDescriptor) Field(java.lang.reflect.Field) FunctionTemplate(org.apache.drill.exec.expr.annotations.FunctionTemplate) PhysicalOperator(org.apache.drill.exec.physical.base.PhysicalOperator) RandomBigIntGauss(org.apache.drill.exec.fn.impl.testing.GeneratorFunctions.RandomBigIntGauss) HashSet(java.util.HashSet) DrillFunc(org.apache.drill.exec.expr.DrillFunc) Test(org.junit.Test)

Example 2 with AnnotationDescriptor

use of org.apache.drill.common.scanner.persistence.AnnotationDescriptor in project drill by axbaretto.

the class TestClassPathScanner method verifyAnnotations.

private void verifyAnnotations(Annotation[] annotations, List<AnnotationDescriptor> scannedAnnotations) throws Exception {
    assertEquals(Arrays.toString(annotations) + " expected but got " + scannedAnnotations, annotations.length, scannedAnnotations.size());
    for (int i = 0; i < annotations.length; i++) {
        Annotation annotation = annotations[i];
        AnnotationDescriptor scannedAnnotation = scannedAnnotations.get(i);
        Class<? extends Annotation> annotationType = annotation.annotationType();
        assertEquals(annotationType.getName(), scannedAnnotation.getAnnotationType());
        if (annotation instanceof FunctionTemplate) {
            FunctionTemplate ft = (FunctionTemplate) annotation;
            if (ft.name() != null && !ft.name().equals("")) {
                assertEquals(ft.name(), scannedAnnotation.getSingleValue("name"));
            }
        }
        // generally verify all properties
        Annotation proxy = scannedAnnotation.getProxy(annotationType);
        Method[] declaredMethods = annotationType.getDeclaredMethods();
        for (Method method : declaredMethods) {
            if (method.getParameterTypes().length == 0) {
                Object reflectValue = method.invoke(annotation);
                Object byteCodeValue = method.invoke(proxy);
                String message = annotationType.getSimpleName() + "." + method.getName();
                if (method.getReturnType().isArray()) {
                    assertArrayEquals(message, (Object[]) reflectValue, (Object[]) byteCodeValue);
                } else {
                    assertEquals(message, reflectValue, byteCodeValue);
                }
            }
        }
    }
}
Also used : AnnotationDescriptor(org.apache.drill.common.scanner.persistence.AnnotationDescriptor) FunctionTemplate(org.apache.drill.exec.expr.annotations.FunctionTemplate) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation)

Example 3 with AnnotationDescriptor

use of org.apache.drill.common.scanner.persistence.AnnotationDescriptor in project drill by axbaretto.

the class TestClassPathScanner method test.

@Test
public void test() throws Exception {
    ScanResult result = ClassPathScanner.fromPrescan(DrillConfig.create());
    List<AnnotatedClassDescriptor> functions = result.getAnnotatedClasses();
    Set<String> scanned = new HashSet<>();
    AnnotatedClassDescriptor functionRandomBigIntGauss = null;
    for (AnnotatedClassDescriptor function : functions) {
        assertTrue(function.getClassName() + " scanned twice", scanned.add(function.getClassName()));
        if (function.getClassName().equals(RandomBigIntGauss.class.getName())) {
            functionRandomBigIntGauss = function;
        }
    }
    if (functionRandomBigIntGauss == null) {
        Assert.fail("functionRandomBigIntGauss not found");
    }
    // TODO: use Andrew's randomized test framework to verify a subset of the functions
    for (AnnotatedClassDescriptor function : functions) {
        Class<?> c = Class.forName(function.getClassName(), false, this.getClass().getClassLoader());
        Field[] fields = c.getDeclaredFields();
        assertEquals("fields count for " + function, fields.length, function.getFields().size());
        for (int i = 0; i < fields.length; i++) {
            FieldDescriptor fieldDescriptor = function.getFields().get(i);
            Field field = fields[i];
            assertEquals("Class fields:\n" + Arrays.toString(fields) + "\n != \nDescriptor fields:\n" + function.getFields(), field.getName(), fieldDescriptor.getName());
            verifyAnnotations(field.getDeclaredAnnotations(), fieldDescriptor.getAnnotations());
            assertEquals(field.getType(), fieldDescriptor.getFieldClass());
        }
        Annotation[] annotations = c.getDeclaredAnnotations();
        List<AnnotationDescriptor> scannedAnnotations = function.getAnnotations();
        verifyAnnotations(annotations, scannedAnnotations);
        FunctionTemplate bytecodeAnnotation = function.getAnnotationProxy(FunctionTemplate.class);
        FunctionTemplate reflectionAnnotation = c.getAnnotation(FunctionTemplate.class);
        assertEquals(reflectionAnnotation.name(), bytecodeAnnotation.name());
        assertArrayEquals(reflectionAnnotation.names(), bytecodeAnnotation.names());
        assertEquals(reflectionAnnotation.scope(), bytecodeAnnotation.scope());
        assertEquals(reflectionAnnotation.nulls(), bytecodeAnnotation.nulls());
        assertEquals(reflectionAnnotation.isBinaryCommutative(), bytecodeAnnotation.isBinaryCommutative());
        assertEquals(reflectionAnnotation.desc(), bytecodeAnnotation.desc());
        assertEquals(reflectionAnnotation.costCategory(), bytecodeAnnotation.costCategory());
    }
    for (String baseType : result.getScannedClasses()) {
        validateType(result, baseType);
    }
    assertTrue(result.getImplementations(PhysicalOperator.class).size() > 0);
    assertTrue(result.getImplementations(DrillFunc.class).size() > 0);
}
Also used : AnnotationDescriptor(org.apache.drill.common.scanner.persistence.AnnotationDescriptor) ScanResult(org.apache.drill.common.scanner.persistence.ScanResult) Annotation(java.lang.annotation.Annotation) FieldDescriptor(org.apache.drill.common.scanner.persistence.FieldDescriptor) AnnotatedClassDescriptor(org.apache.drill.common.scanner.persistence.AnnotatedClassDescriptor) Field(java.lang.reflect.Field) FunctionTemplate(org.apache.drill.exec.expr.annotations.FunctionTemplate) PhysicalOperator(org.apache.drill.exec.physical.base.PhysicalOperator) RandomBigIntGauss(org.apache.drill.exec.fn.impl.testing.GeneratorFunctions.RandomBigIntGauss) HashSet(java.util.HashSet) DrillFunc(org.apache.drill.exec.expr.DrillFunc) SlowTest(org.apache.drill.categories.SlowTest) Test(org.junit.Test)

Example 4 with AnnotationDescriptor

use of org.apache.drill.common.scanner.persistence.AnnotationDescriptor in project drill by apache.

the class TestClassPathScanner method testFunctionTemplates.

@Test
public void testFunctionTemplates() throws Exception {
    List<AnnotatedClassDescriptor> functions = result.getAnnotatedClasses(FunctionTemplate.class.getName());
    Set<String> scanned = new HashSet<>();
    AnnotatedClassDescriptor functionRandomBigIntGauss = null;
    for (AnnotatedClassDescriptor function : functions) {
        assertTrue(function.getClassName() + " scanned twice", scanned.add(function.getClassName()));
        if (function.getClassName().equals(RandomBigIntGauss.class.getName())) {
            functionRandomBigIntGauss = function;
        }
    }
    if (functionRandomBigIntGauss == null) {
        Assert.fail("functionRandomBigIntGauss not found");
    }
    // TODO: use Andrew's randomized test framework to verify a subset of the functions
    for (AnnotatedClassDescriptor function : functions) {
        Class<?> c = Class.forName(function.getClassName(), false, this.getClass().getClassLoader());
        Field[] fields = c.getDeclaredFields();
        assertEquals("fields count for " + function, fields.length, function.getFields().size());
        for (int i = 0; i < fields.length; i++) {
            FieldDescriptor fieldDescriptor = function.getFields().get(i);
            Field field = fields[i];
            assertEquals("Class fields:\n" + Arrays.toString(fields) + "\n != \nDescriptor fields:\n" + function.getFields(), field.getName(), fieldDescriptor.getName());
            verifyAnnotations(field.getDeclaredAnnotations(), fieldDescriptor.getAnnotations());
            assertEquals(field.getType(), fieldDescriptor.getFieldClass());
        }
        Annotation[] annotations = c.getDeclaredAnnotations();
        List<AnnotationDescriptor> scannedAnnotations = function.getAnnotations();
        verifyAnnotations(annotations, scannedAnnotations);
        FunctionTemplate bytecodeAnnotation = function.getAnnotationProxy(FunctionTemplate.class);
        assertNotNull(bytecodeAnnotation);
        FunctionTemplate reflectionAnnotation = c.getAnnotation(FunctionTemplate.class);
        assertEquals(reflectionAnnotation.name(), bytecodeAnnotation.name());
        assertArrayEquals(reflectionAnnotation.names(), bytecodeAnnotation.names());
        assertEquals(reflectionAnnotation.scope(), bytecodeAnnotation.scope());
        assertEquals(reflectionAnnotation.nulls(), bytecodeAnnotation.nulls());
        assertEquals(reflectionAnnotation.isBinaryCommutative(), bytecodeAnnotation.isBinaryCommutative());
        assertEquals(reflectionAnnotation.desc(), bytecodeAnnotation.desc());
        assertEquals(reflectionAnnotation.costCategory(), bytecodeAnnotation.costCategory());
    }
    for (String baseType : result.getScannedClasses()) {
        validateType(result, baseType);
    }
    assertTrue(result.getImplementations(PhysicalOperator.class).size() > 0);
    assertTrue(result.getImplementations(DrillFunc.class).size() > 0);
}
Also used : AnnotationDescriptor(org.apache.drill.common.scanner.persistence.AnnotationDescriptor) Annotation(java.lang.annotation.Annotation) FieldDescriptor(org.apache.drill.common.scanner.persistence.FieldDescriptor) AnnotatedClassDescriptor(org.apache.drill.common.scanner.persistence.AnnotatedClassDescriptor) FunctionTemplate(org.apache.drill.exec.expr.annotations.FunctionTemplate) Field(java.lang.reflect.Field) PhysicalOperator(org.apache.drill.exec.physical.base.PhysicalOperator) RandomBigIntGauss(org.apache.drill.exec.fn.impl.testing.GeneratorFunctions.RandomBigIntGauss) HashSet(java.util.HashSet) DrillFunc(org.apache.drill.exec.expr.DrillFunc) BaseTest(org.apache.drill.test.BaseTest) SlowTest(org.apache.drill.categories.SlowTest) Test(org.junit.Test)

Example 5 with AnnotationDescriptor

use of org.apache.drill.common.scanner.persistence.AnnotationDescriptor in project drill by apache.

the class TestClassPathScanner method verifyAnnotations.

private void verifyAnnotations(Annotation[] annotations, List<AnnotationDescriptor> scannedAnnotations) throws Exception {
    assertEquals(Arrays.toString(annotations) + " expected but got " + scannedAnnotations, annotations.length, scannedAnnotations.size());
    for (int i = 0; i < annotations.length; i++) {
        Annotation annotation = annotations[i];
        AnnotationDescriptor scannedAnnotation = scannedAnnotations.get(i);
        Class<? extends Annotation> annotationType = annotation.annotationType();
        assertEquals(annotationType.getName(), scannedAnnotation.getAnnotationType());
        if (annotation instanceof FunctionTemplate) {
            FunctionTemplate ft = (FunctionTemplate) annotation;
            if (!"".equals(ft.name())) {
                assertEquals(ft.name(), scannedAnnotation.getSingleValue("name"));
            }
        }
        // generally verify all properties
        Annotation proxy = scannedAnnotation.getProxy(annotationType);
        Method[] declaredMethods = annotationType.getDeclaredMethods();
        for (Method method : declaredMethods) {
            if (method.getParameterTypes().length == 0) {
                Object reflectValue = method.invoke(annotation);
                Object byteCodeValue = method.invoke(proxy);
                String message = annotationType.getSimpleName() + "." + method.getName();
                if (method.getReturnType().isArray()) {
                    assertArrayEquals(message, (Object[]) reflectValue, (Object[]) byteCodeValue);
                } else {
                    assertEquals(message, reflectValue, byteCodeValue);
                }
            }
        }
    }
}
Also used : AnnotationDescriptor(org.apache.drill.common.scanner.persistence.AnnotationDescriptor) FunctionTemplate(org.apache.drill.exec.expr.annotations.FunctionTemplate) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation)

Aggregations

Annotation (java.lang.annotation.Annotation)5 AnnotationDescriptor (org.apache.drill.common.scanner.persistence.AnnotationDescriptor)5 FunctionTemplate (org.apache.drill.exec.expr.annotations.FunctionTemplate)5 Field (java.lang.reflect.Field)3 HashSet (java.util.HashSet)3 AnnotatedClassDescriptor (org.apache.drill.common.scanner.persistence.AnnotatedClassDescriptor)3 FieldDescriptor (org.apache.drill.common.scanner.persistence.FieldDescriptor)3 DrillFunc (org.apache.drill.exec.expr.DrillFunc)3 RandomBigIntGauss (org.apache.drill.exec.fn.impl.testing.GeneratorFunctions.RandomBigIntGauss)3 PhysicalOperator (org.apache.drill.exec.physical.base.PhysicalOperator)3 Test (org.junit.Test)3 Method (java.lang.reflect.Method)2 SlowTest (org.apache.drill.categories.SlowTest)2 ScanResult (org.apache.drill.common.scanner.persistence.ScanResult)2 BaseTest (org.apache.drill.test.BaseTest)1