[bvalid-codewatch] SF.net SVN: bvalid: [32] trunk
Status: Beta
Brought to you by:
cwilper
|
From: <cw...@us...> - 2006-05-08 23:47:27
|
Revision: 32 Author: cwilper Date: 2006-05-08 13:18:28 -0700 (Mon, 08 May 2006) ViewCVS: http://svn.sourceforge.net/bvalid/?rev=32&view=rev Log Message: ----------- embedded jetty for testing Modified Paths: -------------- trunk/build.properties trunk/build.xml trunk/src/test/net/sf/bvalid/BValidPackageTestSuite.java Added Paths: ----------- trunk/lib/jasper-compiler.jar trunk/lib/jasper-runtime.jar trunk/lib/javax.servlet.jar trunk/lib/org.mortbay.jetty.jar trunk/src/test/net/sf/bvalid/util/ trunk/src/test/net/sf/bvalid/util/JettyRunner.java trunk/src/test/net/sf/bvalid/util/JettyTestSetup.java Modified: trunk/build.properties =================================================================== --- trunk/build.properties 2006-05-05 07:15:44 UTC (rev 31) +++ trunk/build.properties 2006-05-08 20:18:28 UTC (rev 32) @@ -18,3 +18,9 @@ # JUnit 3.8.1 from http://junit.org/ lib.junit = lib/junit.jar + +# Jetty 5.1.11 from http://www.mortbay.com/jetty/ +lib.jetty = lib/org.mortbay.jetty.jar +lib.servlet = lib/javax.servlet.jar +lib.jasper-runtime = lib/jasper-runtime.jar +lib.jasper-compiler = lib/jasper-compiler.jar Modified: trunk/build.xml =================================================================== --- trunk/build.xml 2006-05-05 07:15:44 UTC (rev 31) +++ trunk/build.xml 2006-05-08 20:18:28 UTC (rev 32) @@ -6,19 +6,20 @@ <!-- defines bvalid.version --> <loadproperties srcFile="src/java/net/sf/bvalid/BValid.properties"/> - <path id="base.path"> + <path id="compile.path"> <pathelement location="${lib.xml-apis}"/> <pathelement location="${lib.xerces}"/> <pathelement location="${lib.log4j}"/> <pathelement location="${lib.httpclient}"/> <pathelement location="${lib.logging}"/> </path> - <path id="compile.path"> - <path refid="base.path"/> - <pathelement location="${lib.junit}"/> - </path> <path id="test.path"> <path refid="compile.path"/> + <pathelement location="${lib.junit}"/> + <pathelement location="${lib.jetty}"/> + <pathelement location="${lib.servlet}"/> + <pathelement location="${lib.jasper-runtime}"/> + <pathelement location="${lib.jasper-compiler}"/> <pathelement location="build/classes"/> <pathelement location="build/testclasses"/> <pathelement location="src/config"/> @@ -56,7 +57,13 @@ <target name="dist" depends="classes" description="Build the distribution in dist/"> <mkdir dir="dist/lib"/> <copy todir="dist/lib"> - <fileset dir="lib"/> + <fileset dir="lib"> + <exclude name="junit.jar"/> + <exclude name="org.mortbay.jetty.jar"/> + <exclude name="javax.servlet.jar"/> + <exclude name="jasper-runtime.jar"/> + <exclude name="jasper-compiler.jar"/> + </fileset> </copy> <copy todir="dist"> <fileset dir="src/bin"/> @@ -120,8 +127,7 @@ </target> <target name="itest" description="Run tests interactively" depends="testclasses"> - <java classname="net.sf.bvalid.BValidPackageTestSuite" - fork="yes"> + <java classname="net.sf.bvalid.BValidPackageTestSuite" fork="yes"> <classpath refid="test.path"/> <sysproperty key="org.apache.commons.logging.LogFactory" value="org.apache.commons.logging.impl.Log4jFactory"/> @@ -131,16 +137,13 @@ </java> </target> - <target name="otest" description="Run tests interactively" depends="testclasses"> - <java classname="net.sf.bvalid.BValidPackageTestSuite" - fork="yes"> + <target name="jtest" depends="testclasses"> + <java classname="net.sf.bvalid.util.JettyRunner" fork="yes"> <classpath refid="test.path"/> - <sysproperty key="org.apache.commons.logging.LogFactory" - value="org.apache.commons.logging.impl.Log4jFactory"/> - <sysproperty key="org.apache.commons.logging.Log" - value="org.apache.commons.logging.impl.Log4JLogger"/> - <sysproperty key="log4j.ignoreTCL" value="true"/> - <sysproperty key="text" value="true"/> + <arg value="7357"/> + <arg value="/"/> + <arg value="."/> + <arg value="fork"/> </java> </target> Added: trunk/lib/jasper-compiler.jar =================================================================== (Binary files differ) Property changes on: trunk/lib/jasper-compiler.jar ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/lib/jasper-runtime.jar =================================================================== (Binary files differ) Property changes on: trunk/lib/jasper-runtime.jar ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/lib/javax.servlet.jar =================================================================== (Binary files differ) Property changes on: trunk/lib/javax.servlet.jar ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: trunk/lib/org.mortbay.jetty.jar =================================================================== (Binary files differ) Property changes on: trunk/lib/org.mortbay.jetty.jar ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Modified: trunk/src/test/net/sf/bvalid/BValidPackageTestSuite.java =================================================================== --- trunk/src/test/net/sf/bvalid/BValidPackageTestSuite.java 2006-05-05 07:15:44 UTC (rev 31) +++ trunk/src/test/net/sf/bvalid/BValidPackageTestSuite.java 2006-05-08 20:18:28 UTC (rev 32) @@ -1,15 +1,17 @@ package net.sf.bvalid; import junit.framework.Test; +import junit.framework.TestCase; import junit.framework.TestSuite; import junit.swingui.TestRunner; import net.sf.bvalid.catalog.CatalogPackageTestSuite; +import net.sf.bvalid.util.JettyTestSetup; -public class BValidPackageTestSuite { - - public static Test suite() { +public class BValidPackageTestSuite extends TestCase { + public static Test suite() throws Exception { + TestSuite suite = new TestSuite(BValidPackageTestSuite.class.getName()); // classes in this package @@ -20,10 +22,10 @@ // sub-packages suite.addTest(CatalogPackageTestSuite.suite()); - return suite; + return new JettyTestSetup(suite, 7357, "/", ".", true); } - public static void main(String[] args) { + public static void main(String[] args) throws Exception { if (System.getProperty("text") != null && System.getProperty("text").equals("true")) { junit.textui.TestRunner.run(BValidPackageTestSuite.suite()); } else { Added: trunk/src/test/net/sf/bvalid/util/JettyRunner.java =================================================================== --- trunk/src/test/net/sf/bvalid/util/JettyRunner.java (rev 0) +++ trunk/src/test/net/sf/bvalid/util/JettyRunner.java 2006-05-08 20:18:28 UTC (rev 32) @@ -0,0 +1,154 @@ +package net.sf.bvalid.util; + +import java.io.*; + +import org.mortbay.jetty.Server; +import org.mortbay.util.InetAddrPort; + +/** + * Runs a Jetty instance for testing. + * + * With this utility, Jetty can be run within the current VM or + * in a subprocess. Running it within a subprocess is useful + * when classloader conflicts arise (as is the case with JUnit's + * GUI test runner). + * + * FIXME: Currently, error reporting is weak in forked mode -- only + * the top-level error message is reported. + * + * cw...@cs... + */ +public class JettyRunner { + + private Server _server; + + private Process _runnerProcess; + private BufferedReader _stdout; + private PrintWriter _stdin; + + private boolean _running; + + /** + * Create a <code>JettyRunner</code>. + * + * If <code>fork</code> is true, it will be launched in a subprocess. + */ + public JettyRunner(int port, + String contextPath, + String webappPath, + boolean fork) throws IOException { + + if (fork) { + + String cmd = "java -cp " + System.getProperty("java.class.path") + + " " + getClass().getName() + " " + + port + " " + contextPath + " " + webappPath + ""; + + _runnerProcess = Runtime.getRuntime().exec(cmd, null, new File(".")); + + _stdout = new BufferedReader(new InputStreamReader(_runnerProcess.getInputStream())); + _stdin = new PrintWriter(new OutputStreamWriter(_runnerProcess.getOutputStream())); + + } else { + _server = new Server(); + _server.addListener(new InetAddrPort(port)); + _server.addWebApplication(contextPath, webappPath); + } + } + + /** + * Start the Jetty instance. + */ + public void start() throws Exception { + if (_runnerProcess != null) { + readUntil("[Press ENTER"); + _stdin.println(); + _stdin.flush(); + readUntil("STARTED"); + } else { + _server.start(); + } + _running = true; + } + + private void readUntil(String lineStart) throws Exception { + + String line = _stdout.readLine(); + while (!line.startsWith(lineStart)) { + if (line.startsWith("ERROR: ")) { + _runnerProcess.waitFor(); + throw new Exception("Error from subprocess: " + line.substring(7)); + } + line = _stdout.readLine(); + } + } + + /** + * Stop the Jetty instance. + */ + public void stop() throws Exception { + if (_running) { + if (_runnerProcess != null) { + readUntil("[Press ENTER"); + _stdin.println(); + _stdin.flush(); + readUntil("STOPPED"); + _runnerProcess.waitFor(); + } else { + _server.stop(); + } + _running = false; + } + } + + /** + * Ensure Jetty is stopped at GC time. + */ + public void finalize() { + if (_running) try { stop(); } catch (Exception e) { } + } + + /** + * Command-line entry point. + * + * This is used to support forking. It can also be used for testing. + */ + public static void main(String[] args) { + + try { + if (args.length < 3 || args.length > 4) { + System.out.println("ERROR: Wrong number of arguments, need port contextPath webappPath [fork]"); + System.exit(1); + } + int port = Integer.parseInt(args[0]); + boolean fork = false; + if (args.length == 4) { + if (args[3].equalsIgnoreCase("true") + || args[3].equalsIgnoreCase("yes") + || args[3].equalsIgnoreCase("fork")) { + fork = true; + } + } + System.out.println("Server Port : " + port); + System.out.println("Context Path : " + args[1]); + System.out.println("Webapp Path : " + args[2]); + JettyRunner runner = new JettyRunner(port, args[1], args[2], fork); + + System.out.println("[Press ENTER to start]"); + new BufferedReader(new InputStreamReader(System.in)).readLine(); + runner.start(); + System.out.println("STARTED"); + System.out.println("[Press ENTER to stop]"); + new BufferedReader(new InputStreamReader(System.in)).readLine(); + runner.stop(); + System.out.println("STOPPED"); + System.exit(0); + } catch (Exception e) { + String msg = e.getClass().getName(); + if (e.getMessage() != null) msg += ": " + e.getMessage(); + System.out.println("ERROR: " + e.getMessage()); + System.exit(1); + } + } + +} Added: trunk/src/test/net/sf/bvalid/util/JettyTestSetup.java =================================================================== --- trunk/src/test/net/sf/bvalid/util/JettyTestSetup.java (rev 0) +++ trunk/src/test/net/sf/bvalid/util/JettyTestSetup.java 2006-05-08 20:18:28 UTC (rev 32) @@ -0,0 +1,27 @@ +package net.sf.bvalid.util; + +import junit.extensions.TestSetup; +import junit.framework.Test; + +public class JettyTestSetup extends TestSetup { + + private JettyRunner _jetty; + + public JettyTestSetup(Test test, + int port, + String contextPath, + String webappPath, + boolean fork) throws Exception { + super(test); + _jetty = new JettyRunner(port, contextPath, webappPath, fork); + } + + protected void setUp() throws Exception { + _jetty.start(); + } + + protected void tearDown() throws Exception { + _jetty.stop(); + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |