Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * The MIT License
   * Copyright (c) 2004-2009, Sun Microsystems, Inc.
   * Permission is hereby granted, free of charge, to any person obtaining a copy
   * of this software and associated documentation files (the "Software"), to deal
   * in the Software without restriction, including without limitation the rights
   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  * copies of the Software, and to permit persons to whom the Software is
  * furnished to do so, subject to the following conditions:
  * The above copyright notice and this permission notice shall be included in
  * all copies or substantial portions of the Software.
 package org.jvnet.hudson.test;
Builds up a junit.framework.TestSuite for performing static syntax checks on Jelly scripts.

Kohsuke Kawaguchi
 public class JellyTestSuiteBuilder {
Given a jar file or a class file directory, recursively search all the Jelly files and build a junit.framework.TestSuite that performs static syntax checks.
     public static TestSuite build(File resthrows Exception {
         TestSuite ts = new JellyTestSuite();
         if (res.isDirectory()) {
             for (final File jelly : (Collection <File>)FileUtils.listFiles(res,new String[]{"jelly"},true))
                 ts.addTest(new JellyCheck(jelly.toURI().toURL(), jct));
         if (res.getName().endsWith(".jar")) {
             String jarUrl = res.toURI().toURL().toExternalForm();
             JarFile jf = new JarFile(res);
             Enumeration<JarEntrye = jf.entries();
             while (e.hasMoreElements()) {
                 JarEntry ent =  e.nextElement();
                 if (ent.getName().endsWith(".jelly"))
                     ts.addTest(new JellyCheck(new URL("jar:"+jarUrl+"!/"+ent.getName()), jct));
         return ts;
     private static class JellyCheck extends TestCase {
         private final URL jelly;
         private final JellyClassLoaderTearOff jct;
         public JellyCheck(URL jellyJellyClassLoaderTearOff jct) {
             this. = jelly;
             this. = jct;
         protected void runTest() throws Exception {
             Document dom = new SAXReader().read();
             // TODO: what else can we check statically? use of taglibs?

Makes sure that <label for=...> is not used inside config.jelly nor global.jelly
         private void checkLabelFor(Document dom) {
             if (isConfigJelly() || isGlobalJelly()) {
                if (!dom.selectNodes("//label[@for]").isEmpty())
                    throw new AssertionError("<label for=...> shouldn't be used because it doesn't work " +
                            "when the configuration item is repeated. Use <label class=\"attach-previous\"> " +
                            "to have your label attach to the previous DOM node instead.");
        private boolean isConfigJelly() {
            return .toString().endsWith("/config.jelly");
        private boolean isGlobalJelly() {
            return .toString().endsWith("/global.jelly");

Execute all the Jelly tests in a servlet request handling context. To do so, we reuse HudsonTestCase
    private static final class JellyTestSuite extends GroupedTest {
        HudsonTestCase h = new HudsonTestCase("Jelly test wrapper") {};
        protected void setUp() throws Exception {
        protected void tearDown() throws Exception {
        private void doTests(TestResult resultthrows Exception {
        protected void runGroupedTests(final TestResult resultthrows Exception {
            .executeOnServer(new Callable<Object>() {
                // this code now inside a request handling thread
                public Object call() throws Exception {
                    return null;
New to GrepCode? Check out our FAQ X