Search in sources :

Example 1 with ClassGlobFilter

use of com.carrotsearch.randomizedtesting.ClassGlobFilter in project randomizedtesting by randomizedtesting.

the class JUnit4 method processTestResources.

/**
 * Process test resources. If there are any test resources that are _not_ class files,
 * this will cause a build error.
 */
private TestsCollection processTestResources() {
    TestsCollection collection = new TestsCollection();
    resources.setProject(getProject());
    Iterator<Resource> iter = (Iterator<Resource>) resources.iterator();
    boolean javaSourceWarn = false;
    while (iter.hasNext()) {
        final Resource r = iter.next();
        if (!r.isExists())
            throw new BuildException("Test class resource does not exist?: " + r.getName());
        try {
            if (r.getName().endsWith(".java")) {
                String pathname = r.getName();
                String className = pathname.substring(0, pathname.length() - ".java".length());
                className = className.replace(File.separatorChar, '.').replace('/', '.').replace('\\', '.');
                collection.add(new TestClass(className));
                if (!javaSourceWarn) {
                    log("Source (.java) files used for naming source suites. This is discouraged, " + "use a resource collection pointing to .class files instead.", Project.MSG_INFO);
                    javaSourceWarn = true;
                }
            } else {
                // Assume .class file.
                InputStream is = r.getInputStream();
                if (!is.markSupported()) {
                    is = new BufferedInputStream(is);
                }
                try {
                    is.mark(4);
                    if (is.read() != 0xca || is.read() != 0xfe || is.read() != 0xba || is.read() != 0xbe) {
                        throw new BuildException("File does not start with a class magic 0xcafebabe: " + r.getName() + ", " + r.getLocation());
                    }
                    is.reset();
                    // Hardcoded intentionally.
                    final String REPLICATE_CLASS = "com.carrotsearch.randomizedtesting.annotations.ReplicateOnEachVm";
                    final TestClass testClass = new TestClass();
                    ClassReader reader = new ClassReader(is);
                    @SuppressWarnings("deprecation") ClassVisitor annotationVisitor = new ClassVisitor(Opcodes.ASM9) {

                        @Override
                        public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                            String className = Type.getType(desc).getClassName();
                            if (className.equals(REPLICATE_CLASS)) {
                                testClass.replicate = true;
                            }
                            return null;
                        }
                    };
                    reader.accept(annotationVisitor, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
                    testClass.className = reader.getClassName().replace('/', '.');
                    log("Test class parsed: " + r.getName() + " as " + testClass.className, Project.MSG_DEBUG);
                    collection.add(testClass);
                } finally {
                    is.close();
                }
            }
        } catch (IOException e) {
            throw new BuildException("Could not read or parse as Java class: " + r.getName() + ", " + r.getLocation(), e);
        }
    }
    String testClassFilter = Strings.emptyToNull(getProject().getProperty(SYSPROP_TESTCLASS()));
    if (testClassFilter != null) {
        ClassGlobFilter filter = new ClassGlobFilter(testClassFilter);
        for (Iterator<TestClass> i = collection.testClasses.iterator(); i.hasNext(); ) {
            if (!filter.shouldRun(Description.createSuiteDescription(i.next().className))) {
                i.remove();
            }
        }
    }
    return collection;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) Resource(org.apache.tools.ant.types.Resource) ClassVisitor(org.objectweb.asm.ClassVisitor) IOException(java.io.IOException) ClassGlobFilter(com.carrotsearch.randomizedtesting.ClassGlobFilter) BufferedInputStream(java.io.BufferedInputStream) Iterator(java.util.Iterator) ClassReader(org.objectweb.asm.ClassReader) BuildException(org.apache.tools.ant.BuildException)

Aggregations

ClassGlobFilter (com.carrotsearch.randomizedtesting.ClassGlobFilter)1 BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Iterator (java.util.Iterator)1 BuildException (org.apache.tools.ant.BuildException)1 Resource (org.apache.tools.ant.types.Resource)1 ClassReader (org.objectweb.asm.ClassReader)1 ClassVisitor (org.objectweb.asm.ClassVisitor)1