1  /*
     2   * Copyright the original author or authors.
     3   *
     4   * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      http://www.mozilla.org/MPL/MPL-1.1.html
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  import asunit.errors.AssertionFailedError;
    18  import asunit.framework.ITestListener;
    19  import asunit.framework.Test;
    20  import asunit.framework.TestFailure;
    21  import asunit.framework.TestResult;
    22  import asunit.runner.BaseTestRunner;
    23  import asunit.runner.IResultPrinter;
    24  
    25  /**
    26   * {@code XmlSocketResultPrinter} writes-out received test execution information with
    27   * the xml socket.
    28   *
    29   * <p>This result printer is intended to be used with the Unit Test Task.
    30   *
    31   * @author Simon Wacker
    32   */
    33  class org.as2lib.test.unit.XmlSocketResultPrinter extends MovieClip implements
    34  		IResultPrinter, ITestListener {
    35  
    36  	private static var linkageId:String = "__Packages.org.as2lib.test.unit.XmlSocketResultPrinter";
    37  	private static var classRef:Function = XmlSocketResultPrinter;
    38  	private static var serializable:Boolean = Object.registerClass(linkageId, classRef);
    39  
    40  	private var socket:XMLSocket;
    41  
    42  	/**
    43  	 * Constructs a new {@code XmlSocketResultPrinter} instance.
    44  	 *
    45  	 * <p>If {@code host} is not specified, {@code "localhost"} is used. If
    46  	 * {@code port} is not specified, {@code 3212} is used.
    47  	 *
    48  	 * @param host the host of the connection to open
    49  	 * @param port the port of the connection to open
    50  	 */
    51  	public function XmlSocketResultPrinter(host:String, port:Number) {
    52  		if (host == null) {
    53  			host = "localhost";
    54  		}
    55  		if (port == null) {
    56  			port = 3212;
    57  		}
    58  		socket = new XMLSocket();
    59  		socket.connect(host, port);
    60  	}
    61  
    62  	public function trace():Void {
    63  		socket.send("<message>" + arguments.toString() + "</message>");
    64  	}
    65  
    66  	public function startTest(test:Test):Void {
    67  	}
    68  
    69  	public function addError(test:Test, e:Error):Void {
    70  	}
    71  
    72  	public function addFailure(test:Test, e:AssertionFailedError):Void {
    73  	}
    74  
    75  	public function endTest(test:Test):Void {
    76  	}
    77  
    78  	public function printResult(result:TestResult, runTime:Number):Void {
    79  		printHeader(runTime);
    80  		printErrors(result);
    81  		if (result.errorCount() > 0 && result.failureCount() > 0) {
    82  			socket.send("<message>-</message>");
    83  		}
    84  		printFailures(result);
    85  		printFooter(result);
    86  	}
    87  
    88  	private function printHeader(runTime:Number):Void {
    89  		socket.send("<start>Time: " + elapsedTimeAsString(runTime) + "</start>");
    90  	}
    91  
    92  	private function elapsedTimeAsString(runTime:Number):String {
    93  		return (runTime/1000).toString() + " sec";
    94  	}
    95  
    96  	private function printErrors(result:TestResult):Void {
    97  		printDefects(result.errors(), result.errorCount(), "error");
    98  	}
    99  
   100  	private function printFailures(result:TestResult):Void {
   101  		printDefects(result.failures(), result.failureCount(), "failure");
   102  	}
   103  
   104  	private function printDefects(defects:Array, count:Number, type:String):Void {
   105  		if (count == 0) {
   106  			return;
   107  		}
   108  		if (count == 1) {
   109  			socket.send("<message>There was " + count + " " + type + ":</message>");
   110  		}
   111  		else {
   112  			socket.send("<message>There were " + count + " " + type + "s:</message>");
   113  		}
   114  		for (var i:Number = 0; i < defects.length; i++) {
   115  			printDefect(defects[i], i, type);
   116  		}
   117  	}
   118  
   119  	private function printDefect(defect:TestFailure, index:Number, type:String):Void {
   120  		var message:String = "<" + type + ">";
   121  		message += "(" + index + ") " + defect.failedTest() + " ";
   122  		message += BaseTestRunner.getFilteredTrace(defect.thrownException().toString());
   123  		message += "</" + type + ">";
   124  		socket.send(message);
   125  	}
   126  
   127  	private function printFooter(result:TestResult):Void {
   128  		socket.send("<finish hasErrors='" + result.wasSuccessful() +
   129  				"'>Tests run: " + result.runCount() +
   130  				", Failures: " + result.failureCount() +
   131  				", Errors: " + result.errorCount() +
   132  				"</finish>");
   133  	}
   134  
   135  }