diff --git a/test/src/main/java/org/jvnet/hudson/test/JellyTestSuiteBuilder.java b/test/src/main/java/org/jvnet/hudson/test/JellyTestSuiteBuilder.java index 0f925baa31..e3b4e802d2 100644 --- a/test/src/main/java/org/jvnet/hudson/test/JellyTestSuiteBuilder.java +++ b/test/src/main/java/org/jvnet/hudson/test/JellyTestSuiteBuilder.java @@ -28,6 +28,7 @@ import junit.framework.TestResult; import junit.framework.TestSuite; import org.apache.commons.io.FileUtils; import org.dom4j.Document; +import org.dom4j.ProcessingInstruction; import org.dom4j.io.SAXReader; import org.jvnet.hudson.test.junit.GroupedTest; import org.kohsuke.stapler.MetaClassLoader; @@ -51,14 +52,14 @@ public class JellyTestSuiteBuilder { * Given a jar file or a class file directory, recursively search all the Jelly files and build a {@link TestSuite} * that performs static syntax checks. */ - public static TestSuite build(File res) throws Exception { + public static TestSuite build(File res, boolean requirePI) throws Exception { TestSuite ts = new JellyTestSuite(); final JellyClassLoaderTearOff jct = new MetaClassLoader(JellyTestSuiteBuilder.class.getClassLoader()).loadTearOff(JellyClassLoaderTearOff.class); if (res.isDirectory()) { for (final File jelly : (Collection )FileUtils.listFiles(res,new String[]{"jelly"},true)) - ts.addTest(new JellyCheck(jelly.toURI().toURL(), jct)); + ts.addTest(new JellyCheck(jelly.toURI().toURL(), jct, requirePI)); } if (res.getName().endsWith(".jar")) { String jarUrl = res.toURI().toURL().toExternalForm(); @@ -67,7 +68,7 @@ public class JellyTestSuiteBuilder { while (e.hasMoreElements()) { JarEntry ent = e.nextElement(); if (ent.getName().endsWith(".jelly")) - ts.addTest(new JellyCheck(new URL("jar:"+jarUrl+"!/"+ent.getName()), jct)); + ts.addTest(new JellyCheck(new URL("jar:"+jarUrl+"!/"+ent.getName()), jct, requirePI)); } jf.close(); } @@ -77,11 +78,13 @@ public class JellyTestSuiteBuilder { private static class JellyCheck extends TestCase { private final URL jelly; private final JellyClassLoaderTearOff jct; + private final boolean requirePI; - public JellyCheck(URL jelly, JellyClassLoaderTearOff jct) { + public JellyCheck(URL jelly, JellyClassLoaderTearOff jct, boolean requirePI) { super(jelly.getPath()); this.jelly = jelly; this.jct = jct; + this.requirePI = requirePI; } @Override @@ -89,6 +92,12 @@ public class JellyTestSuiteBuilder { jct.createContext().compileScript(jelly); Document dom = new SAXReader().read(jelly); checkLabelFor(dom); + if (requirePI) { + ProcessingInstruction pi = dom.processingInstruction("jelly"); + if (pi==null || !pi.getText().contains("escape-by-default")) + throw new AssertionError(" is missing"); + + } // TODO: what else can we check statically? use of taglibs? } diff --git a/test/src/main/java/org/jvnet/hudson/test/PluginAutomaticTestBuilder.java b/test/src/main/java/org/jvnet/hudson/test/PluginAutomaticTestBuilder.java index 9c9eee02a2..dd009bf316 100644 --- a/test/src/main/java/org/jvnet/hudson/test/PluginAutomaticTestBuilder.java +++ b/test/src/main/java/org/jvnet/hudson/test/PluginAutomaticTestBuilder.java @@ -51,12 +51,18 @@ public class PluginAutomaticTestBuilder { if (params.containsKey("outputDirectory") // shouldn't happen, but be defensive || notSkipTests("JellyTest")) { File outputDirectory = new File((String)params.get("outputDirectory")); - suite.addTest(JellyTestSuiteBuilder.build(outputDirectory)); + suite.addTest(JellyTestSuiteBuilder.build(outputDirectory,toBoolean(params.get("requirePI")))); } return suite; } + private static boolean toBoolean(Object requirePI) { + if (requirePI==null) return false; + if (requirePI instanceof Boolean) return (Boolean)requirePI; + return Boolean.parseBoolean(requirePI.toString()); + } + /** * Provides an escape hatch for plugin developers to skip auto-generated tests. */ diff --git a/test/src/test/java/CoreJellyTest.java b/test/src/test/java/CoreJellyTest.java index 4f315a0879..0e42337145 100644 --- a/test/src/test/java/CoreJellyTest.java +++ b/test/src/test/java/CoreJellyTest.java @@ -35,6 +35,6 @@ import org.jvnet.hudson.test.JellyTestSuiteBuilder; */ public class CoreJellyTest extends TestCase { public static Test suite() throws Exception { - return JellyTestSuiteBuilder.build(Which.jarFile(Jenkins.class)); + return JellyTestSuiteBuilder.build(Which.jarFile(Jenkins.class),true); } } diff --git a/test/src/test/java/hudson/maven/MavenJellyTest.java b/test/src/test/java/hudson/maven/MavenJellyTest.java index d9e553e176..d501362862 100644 --- a/test/src/test/java/hudson/maven/MavenJellyTest.java +++ b/test/src/test/java/hudson/maven/MavenJellyTest.java @@ -35,6 +35,6 @@ import org.jvnet.hudson.test.JellyTestSuiteBuilder; */ public class MavenJellyTest extends TestCase { public static Test suite() throws Exception { - return JellyTestSuiteBuilder.build(Which.jarFile(MavenModuleSet.class)); + return JellyTestSuiteBuilder.build(Which.jarFile(MavenModuleSet.class),true); } }