Search in sources :

Example 1 with Resource

use of org.apache.tools.ant.types.Resource in project randomizedtesting by randomizedtesting.

the class ExecutionTimesReport method mergeHints.

/**
   * Read hints from all resources in a collection, retaining 
   * <code>suiteNames</code>. If <code>suiteNames</code> is null,
   * everything is retained.
   */
public static Map<String, List<Long>> mergeHints(Collection<ResourceCollection> resources, Collection<String> suiteNames) {
    final Map<String, List<Long>> hints = new HashMap<>();
    for (ResourceCollection rc : resources) {
        Iterator<Resource> i = rc.iterator();
        while (i.hasNext()) {
            InputStream is = null;
            Resource r = i.next();
            try {
                is = r.getInputStream();
                mergeHints(is, hints);
                // Early prune the hints to those we have on the list.
                if (suiteNames != null) {
                    hints.keySet().retainAll(suiteNames);
                }
            } catch (IOException e) {
                throw new BuildException("Could not read hints from resource: " + r.getDescription(), e);
            } finally {
                try {
                    if (is != null)
                        is.close();
                } catch (IOException e) {
                    throw new BuildException("Could not close hints file: " + r.getDescription());
                }
            }
        }
    }
    return hints;
}
Also used : HashMap(java.util.HashMap) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Resource(org.apache.tools.ant.types.Resource) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) ResourceCollection(org.apache.tools.ant.types.ResourceCollection)

Example 2 with Resource

use of org.apache.tools.ant.types.Resource in project lombok by rzwitserloot.

the class WebUpToDate method eval.

/**
	 * Evaluate (all) target and source file(s) to
	 * see if the target(s) is/are up-to-date.
	 * @return true if the target(s) is/are up-to-date
	 */
public boolean eval() {
    if (sourceFileSets.size() == 0 && sourceResources.size() == 0 && sourceFile == null) {
        throw new BuildException("At least one srcfile or a nested <srcfiles> or <srcresources> element must be set.");
    }
    if ((sourceFileSets.size() > 0 || sourceResources.size() > 0) && sourceFile != null) {
        throw new BuildException("Cannot specify both the srcfile attribute and a nested <srcfiles> or <srcresources> element.");
    }
    if (urlbase == null) {
        throw new BuildException("The urlbase attribute must be set.");
    }
    // if the source file isn't there, throw an exception
    if (sourceFile != null && !sourceFile.exists()) {
        throw new BuildException(sourceFile.getAbsolutePath() + " not found.");
    }
    boolean upToDate = true;
    if (sourceFile != null) {
        Resource fileResource = new FileResource(sourceFile);
        upToDate = isUpToDate(fileResource);
    }
    if (upToDate) {
        Enumeration e = sourceFileSets.elements();
        while (upToDate && e.hasMoreElements()) {
            FileSet fs = (FileSet) e.nextElement();
            Iterator it = fs.iterator();
            while (upToDate && it.hasNext()) {
                Resource r = (Resource) it.next();
                upToDate = isUpToDate(r);
            }
        }
    }
    if (upToDate) {
        Resource[] r = sourceResources.listResources();
        for (int i = 0; upToDate && i < r.length; i++) {
            upToDate = isUpToDate(r[i]);
        }
    }
    return upToDate;
}
Also used : Enumeration(java.util.Enumeration) FileSet(org.apache.tools.ant.types.FileSet) Resource(org.apache.tools.ant.types.Resource) FileResource(org.apache.tools.ant.types.resources.FileResource) URLResource(org.apache.tools.ant.types.resources.URLResource) FileResource(org.apache.tools.ant.types.resources.FileResource) Iterator(java.util.Iterator) BuildException(org.apache.tools.ant.BuildException)

Example 3 with Resource

use of org.apache.tools.ant.types.Resource in project lombok by rzwitserloot.

the class WebUpToDate method isUpToDate.

private boolean isUpToDate(Resource r) throws BuildException {
    String url = urlbase + r.getName();
    Resource urlResource;
    try {
        urlResource = new URLResource(new URL(url));
    } catch (MalformedURLException e) {
        throw new BuildException("url is malformed: " + url, e);
    }
    if (SelectorUtils.isOutOfDate(r, urlResource, 20)) {
        log(r.getName() + " is newer than " + url, Project.MSG_VERBOSE);
        return false;
    } else {
        return true;
    }
}
Also used : URLResource(org.apache.tools.ant.types.resources.URLResource) MalformedURLException(java.net.MalformedURLException) Resource(org.apache.tools.ant.types.Resource) FileResource(org.apache.tools.ant.types.resources.FileResource) URLResource(org.apache.tools.ant.types.resources.URLResource) BuildException(org.apache.tools.ant.BuildException) URL(java.net.URL)

Example 4 with Resource

use of org.apache.tools.ant.types.Resource in project jslint4java by happygiraffe.

the class JSLintTask method execute.

/**
     * Scan the specified directories for JavaScript files and lint them.
     */
@Override
public void execute() throws BuildException {
    if (resources.size() == 0) {
        // issue 53: this isn't a fail, just a notice.
        log(NO_FILES_TO_LINT);
    }
    JSLint lint = makeLint();
    applyOptions(lint);
    for (ResultFormatter rf : formatters) {
        rf.begin();
    }
    int failedCount = 0;
    int totalErrorCount = 0;
    for (Resource resource : resources.listResources()) {
        try {
            int errorCount = lintStream(lint, resource);
            if (errorCount > 0) {
                totalErrorCount += errorCount;
                failedCount++;
            }
        } catch (IOException e) {
            throw new BuildException(e);
        }
    }
    for (ResultFormatter rf : formatters) {
        rf.end();
    }
    if (failedCount != 0) {
        String msg = failureMessage(failedCount, totalErrorCount);
        if (haltOnFailure) {
            throw new BuildException(msg);
        } else {
            log(msg);
            if (failureProperty != null) {
                getProject().setProperty(failureProperty, msg);
            }
        }
    }
}
Also used : JSLint(com.googlecode.jslint4java.JSLint) Resource(org.apache.tools.ant.types.Resource) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) JSLint(com.googlecode.jslint4java.JSLint)

Example 5 with Resource

use of org.apache.tools.ant.types.Resource 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);
                    ClassVisitor annotationVisitor = new ClassVisitor(Opcodes.ASM5) {

                        @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

BuildException (org.apache.tools.ant.BuildException)8 Resource (org.apache.tools.ant.types.Resource)8 IOException (java.io.IOException)5 Iterator (java.util.Iterator)5 FileResource (org.apache.tools.ant.types.resources.FileResource)5 File (java.io.File)3 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 URLResource (org.apache.tools.ant.types.resources.URLResource)2 SAXException (org.xml.sax.SAXException)2 ClassGlobFilter (com.carrotsearch.randomizedtesting.ClassGlobFilter)1 JSLint (com.googlecode.jslint4java.JSLint)1 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1