Hi Chris,

great, thanks a lot! I'll incorporate the changes.


Cheers,
Simon

Chris Allen wrote:

> Hey everyone,
>
> I know that I said I would look into fixing this last week, but I ran
> out of time and had to push it to this week. The good news is that I
> now have a patch that works. That's right, I fixed it! I have tested
> it on Windows XP Professional, Mac OSX 10.4.7 for PPC and Mac OSX
> 10.4.7 for Intel. All seem to be running correctly.
>
> Below is the code for you java guys to review. Basically since Flash
> Player on the Macintosh can't be executed by itself, it needs to be
> run with "open" instead; thus the process finishes before the Unit
> Test SWF is done doing its thing. The fix was to create a while loop
> that checked for a flag to see if it was done rather than just do a
> process.waitFor();.
>
> I also altered the class so that the build would fail if there were
> any errors or tests that did not pass. This seemed much more useful
> for me since now I can run the unit tests as part of the build, and if
> any one of them fails, then it prevents the build process from going
> any further.
>
> I have included a jar file with this email as well so that you can
> start using it right away. Just update your build.xml to point to this
> jar for that task:
>
> <taskdef name="unittest"
> classname="org.as2lib.ant.UnitTestForMacAndPC"
> classpath="jar/as2ant4MacAndPC.jar" />
>
> Keep in mind that I didn't include the other tasks in this jar, so you
> will still need to point to the old jar for the MTASC and SWFMill
> tasks.
>
> I hope this helps some of you out there. Let me know if you have any
> suggestions on how to improve it or if you experience any bugs.
>
> -Chris
>
> /*
> * Copyright the original author or authors.
> *
> * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
> * you may not use this file except in compliance with the License.
> * You may obtain a copy of the License at
> *
> *      http://www.mozilla.org/MPL/MPL-1.1.html
> *
> * Unless required by applicable law or agreed to in writing, software
> * distributed under the License is distributed on an "AS IS" BASIS,
> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
> implied.
> * See the License for the specific language governing permissions and
> * limitations under the License.
> */
>
> package org.as2lib.ant;
>
> import java.io.BufferedReader;
> import java.io.ByteArrayInputStream;
> import java.io.File;
> import java.io.IOException;
> import java.io.InputStreamReader;
> import java.net.ServerSocket;
> import java.net.Socket;
>
> import javax.xml.parsers.DocumentBuilder;
> import javax.xml.parsers.DocumentBuilderFactory;
> import javax.xml.parsers.ParserConfigurationException;
>
> import org.apache.tools.ant.BuildException;
> import org.apache.tools.ant.Project;
> import org.apache.tools.ant.Task;
> import org.apache.tools.ant.taskdefs.Execute;
> import org.apache.tools.ant.types.Commandline;
> import org.w3c.dom.Document;
> import org.w3c.dom.Element;
> import org.xml.sax.SAXException;
>
> /**
>  * [EMAIL PROTECTED] UnitTest} executes a unit test swf and writes the result 
> to the
> * console.
> *
> * <p>
>  * The unit test swf must send the result over the XML socket, with host
>  * "localhost" and port 3212 by default or the one declared in the 
> build file.
> *
> * <p>
>  * If <b>As2lib Unit Test</b> is used the unit test swf may register the
>  * [EMAIL PROTECTED] XmlSocketTestListener} at the test runner which sends 
> the test
> * execution information properly formatted to this task.
> *
> * <p>
> * <p>
>  * If <b>AsUnit</b> is used the unit test swf may register the
>  * [EMAIL PROTECTED] XmlSocketResultPrinter} at the test runner which sends 
> the test
> * execution information properly formatted to this task.
> *
> * <p>
> * The sent test execution information must be formatted as follows:
> * <ul>
> * <li>&lt;start&gt;Start message.&lt;/start&gt;</li>
> * <li>&lt;update&gt;Update message.&lt;/update&gt;</li>
> * <li>&lt;pause&gt;Pause message.&lt;/pause&gt;</li>
> * <li>&lt;resume&gt;Resume message.&lt;/resume&gt;
> * <li>&lt;error&gt;Error message.&lt;/error&gt;</li>
> * <li>&lt;failure&gt;Failure message.&lt;/failure&gt;</li>
>  * <li>&lt;finish hasErrors="false/true"&gt;Finish 
> message.&lt;/finish&gt;</li>
> * <li>&lt;message&gt;Arbitrary message.&lt;/message&gt;</li>
> * </ul>
> *
> * <p>
> * As soon as the ant task receives the finish information it will 
> close the
> * opened unit test swf and finish its execution.
> *
> * <p>
> * This task can take the following arguments:
> * <ul>
> * <li> [EMAIL PROTECTED] #setSwf(File) swf} (test swf file to execute and 
> receive 
> unit
> * test results from) </li>
> * <li> [EMAIL PROTECTED] #setFlashPlayer(File) flashplayer} (location of the 
> flashplayer
> * to execute the swf with) </li>
> * <li>[EMAIL PROTECTED] #setPort(int) port} (port of the xml socket to listen 
> to)</li>
> * </ul>
> *
> * <p>
> * You must supply 'swf' and 'flashplayer'.
> *
> * @author Simon Wacker
> * @author Christophe Herreman
> * @author Chris Allen
> */
> public class UnitTestForMacAndPC extends Task {
>
>     public static final String START_ELEMENT = "start";
>
>     public static final String UPDATE_ELEMENT = "update";
>
>     public static final String PAUSE_ELEMENT = "pause";
>
>     public static final String RESUME_ELEMENT = "resume";
>
>     public static final String ERROR_ELEMENT = "error";
>
>     public static final String FAILURE_ELEMENT = "failure";
>
>     public static final String FINISH_ELEMENT = "finish";
>
>     public static final String HAS_ERRORS_ATTRIBUTE = "hasErrors";
>
>     public static final String TRUE_VALUE = "true";
>
>     private File swf;
>
>     private File flashPlayer;
>
>     private int port;
>
>     private boolean isFinished = false;
>
>     private boolean hasFailed = false;
>
>     /**
>      * Constructs a new [EMAIL PROTECTED] UnitTest} instance, with the 
> default 
> port 3212.
>      */
>     public UnitTestForMacAndPC() {
>         port = 3212;
>         //log("OS Name: " + System.getProperty("os.name"));
>     }
>
>     /**
>      * Sets the swf file which executes the compiled unit tests and 
> sends the
>      * result through the xml socket.
>      */
>     public void setSwf(File swf) {
>         this.swf = swf;
>     }
>
>     /**
>      * Returns the swf file which executes the compiled unit tests and 
> sends the
>      * result through the xml socket.
>      */
>     public File getSwf() {
>         return swf;
>     }
>
>     /**
>      * Sets the flash player to run the unit test swf in.
>      */
>     public void setFlashPlayer(File flashPlayer) {
>         this.flashPlayer = flashPlayer;
>     }
>
>     /**
>      * Returns the flash player to run the unit test swf in.
>      */
>     public File getFlashPlayer() {
>         return flashPlayer;
>     }
>
>     /**
>      * Sets the port to listen for unit test results on. The default 
> port is
>      * 3212.
>      */
>     public void setPort(int port) {
>         this.port = port;
>     }
>
>     /**
>      * Returns the port to listen for unit test results on. The 
> default port is
>      * 3212.
>      */
>     public int getPort() {
>         return port;
>     }
>
>     /**
>      * Returns whether the process has finished. This is nedded on Mac 
> OSX
>      * because the open command finishes before the Flash Player is done
>      * executing.
>      */
>     public boolean isFinished() {
>         return isFinished;
>     }
>
>     /**
>      * Sets a flag for whether the process has finished. This is 
> nedded on Mac
>      * OSX because the open command finishes before the Flash Player 
> is done
>      * executing.
>      *
>      * @param isFinished
>      */
>
>     public void setFinished(boolean isFinished) {
>         this.isFinished = isFinished;
>     }
>     
>     public void setHasFailed(boolean hasFailed) {
>         this.hasFailed = hasFailed;
>     }
>
>     /**
>      * Executes this task.
>      *
>      * @throws BuildException
>      *             if swf or flash player is not specified
>      */
>     public void execute() throws BuildException {
>         if (swf == null) {
>             throw new BuildException("A unit test swf must be supplied.",
>                     getLocation());
>         }
>         if (flashPlayer == null) {
>             throw new BuildException("A flash player must be supplied.",
>                     getLocation());
>         }
>         Commandline command = new Commandline();
>         boolean isAMac = System.getProperty("os.name").equals("Mac OS 
> X");
>
>         if (isAMac) {
>             command.setExecutable("open");
>             // command.createArgument().setValue(flashPlayer.getPath());
>         } else {
>             // for windows
>             command.setExecutable(flashPlayer.getPath());
>         }
>
>         command.createArgument().setFile(swf);
>         Receiver receiver = new Receiver(this);
>         receiver.startServer(port);
>         try {
>             log(command.toString());
>             Process process = Execute.launch(getProject(), command
>                     .getCommandline(), null, 
> getProject().getBaseDir(), true);
>             receiver.setProcess(process);
>             process.waitFor();
>
>             // loop for Mac Flash Player
>             if (isAMac) {
>                
>                 while (!isFinished) {
>                     Thread.sleep(4);
>                 }
>                
>                 if (hasFailed){
>                     throw new BuildException("The build failed because 
> a unit test
> did not pass.");
>                 }
>             } else {
>                 if (hasFailed) {
>                     throw new BuildException("The build failed because 
> a unit test
> did not pass.");
>                 }
>             }
>
>         } catch (IOException e) {
>             throw new BuildException("Error running unit tests.", e,
>                     getLocation());
>         } catch (InterruptedException e) {
>             throw new BuildException("Error running unit tests.", e,
>                     getLocation());
>         }
>         receiver.stopServer();
>     }
>
>     private static class Receiver extends Thread {
>
>         private UnitTestForMacAndPC task;
>
>         private ServerSocket server;
>
>         private BufferedReader in;
>
>         private Process process;
>
>         public Receiver(UnitTestForMacAndPC task) {
>             this.task = task;
>         }
>
>         public void setProcess(Process process) {
>             this.process = process;
>         }
>                
>         public void run() {
>             try {
>                 task.log("-\n-");
>                 task.log("running");
>                 String previousNodeName = "";
>                 while (true) {
>                     Socket socket = server.accept();
>                     in = new BufferedReader(new InputStreamReader(socket
>                             .getInputStream()));
>                     char[] charBuffer = new char[1];
>                     while (in.read(charBuffer, 0, 1) != -1) {
>                         StringBuffer stringBuffer = new 
> StringBuffer(8192);
>                         while (charBuffer[0] != '\0') {
>                             stringBuffer.append(charBuffer[0]);
>                             in.read(charBuffer, 0, 1);
>                         }
>                         DocumentBuilderFactory factory = 
> DocumentBuilderFactory
>                                 .newInstance();
>                         DocumentBuilder builder = 
> factory.newDocumentBuilder();
>                         Document document = builder
>                                 .parse(new 
> ByteArrayInputStream(stringBuffer
>                                         .toString().getBytes()));
>                         Element element = document.getDocumentElement();
>                         String message = 
> element.getFirstChild().getNodeValue();
>                         String nodeName = element.getNodeName();
>                         if (nodeName.equals(START_ELEMENT)) {
>                             task.log(message + "\n-");
>                         } else if (nodeName.equals(UPDATE_ELEMENT)) {
>                             task.log(message);
>                         } else if (nodeName.equals(PAUSE_ELEMENT)) {
>                             task.log("-\n" + message);
>                         } else if (nodeName.equals(RESUME_ELEMENT)) {
>                             task.log(message + "\n-");
>                         } else if (nodeName.equals(ERROR_ELEMENT)
>                                 || nodeName.equals(FAILURE_ELEMENT)) {
>                             task.setHasFailed(true);
>                             task.log(message, Project.MSG_ERR);
>                         } else if (nodeName.equals(FINISH_ELEMENT)) {
>                             if (!previousNodeName.equals(START_ELEMENT)
>                                     && 
> !previousNodeName.equals(RESUME_ELEMENT)) {
>                                 task.log("-");
>                             }
>                             if 
> (element.getAttribute(HAS_ERRORS_ATTRIBUTE)
>                                     .equals(TRUE_VALUE)) {
>                                 task.log(message, Project.MSG_ERR);
>                             } else {
>                                 task.log(message);
>                             }
>                             task.log("-\n-");
>                             task.setFinished(true);
>                             process.destroy();
>                             return;
>                         } else {
>                             task.log(message);
>                         }
>                         previousNodeName = nodeName;
>                     }                   
>                    
>                 }
>             } catch (IOException e) {
>                 throw new BuildException("Error on reading result.", 
> e, task
>                         .getLocation());
>             } catch (ParserConfigurationException e) {
>                 throw new BuildException("Error on reading result.", 
> e, task
>                         .getLocation());
>             } catch (SAXException e) {
>                 throw new BuildException("Error on reading result.", 
> e, task
>                         .getLocation());
>             }
>         }
>
>         public void startServer(int port) {
>             try {
>                 server = new ServerSocket(port);
>                 super.start();
>             } catch (IOException e) {
>                 stopServer();
>                 throw new BuildException("Error on starting server.", 
> e, task
>                         .getLocation());
>             }
>         }
>
>         public void stopServer() {
>             try {
>                 if (server != null) {
>                     server.close();
>                 }
>             } catch (IOException e) {
>                 throw new BuildException("Error on stopping server.", 
> e, task
>                         .getLocation());
>             } finally {
>                 try {
>                     if (in != null) {
>                         in.close();
>                     }
>                 } catch (IOException e) {
>                     throw new BuildException("Error on stopping 
> server.", e,
>                             task.getLocation());
>                 }
>             }
>         }
>
>     }
> }
>
>------------------------------------------------------------------------
>
>_______________________________________________
>osflash mailing list
>[email protected]
>http://osflash.org/mailman/listinfo/osflash_osflash.org
>  
>

-- 
Simon Wacker
www.simonwacker.com
www.as2lib.org
www.hq-heilbronn.de
www.flik-flak.de


_______________________________________________
osflash mailing list
[email protected]
http://osflash.org/mailman/listinfo/osflash_osflash.org

Reply via email to