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 org.as2lib.app.exec.Process;
    18  import org.as2lib.test.unit.TestRunner;
    19  import org.as2lib.test.unit.TestCaseResult;
    20  import org.as2lib.env.except.IllegalArgumentException;
    21  import org.as2lib.env.log.LogSupport;
    22  import org.as2lib.test.unit.TestCaseMethodInfo;
    23  import org.as2lib.app.exec.ProcessStartListener;
    24  import org.as2lib.app.exec.ProcessErrorListener;
    25  import org.as2lib.app.exec.ProcessFinishListener;
    26  import org.as2lib.app.exec.ProcessPauseListener;
    27  import org.as2lib.app.exec.ProcessResumeListener;
    28  import org.as2lib.app.exec.ProcessUpdateListener;
    29  
    30  /**
    31   * {@code LoggerTestListener} is the default listener for Tests.
    32   * Listener as default logger for the Testrunner. To be used as standard outwriter for the TestRunner.
    33   *
    34   * @author Martin Heidegger
    35   * @see LogManager#getLoggerRepository
    36   * @see Logger
    37   */
    38  class org.as2lib.test.unit.LoggerTestListener extends LogSupport
    39  	implements ProcessStartListener,
    40  		ProcessErrorListener,
    41  		ProcessFinishListener,
    42  		ProcessPauseListener,
    43  		ProcessResumeListener,
    44  		ProcessUpdateListener {
    45  	
    46  	/* Instance holfer of the default LoggerTestListener */
    47  	private static var instance:LoggerTestListener;
    48  	
    49  	/**
    50  	 * Returns a instance of {@code LoggerTestListener}
    51  	 * 
    52  	 * @return Instance of {@code LoggerTestListener}
    53  	 */
    54  	public static function getInstance(Void):LoggerTestListener {
    55  		if (!instance) {
    56  			instance = new LoggerTestListener();
    57  		}
    58  		return instance;
    59  	}
    60  	
    61  	/** Stores former displayed TestCase. */
    62  	private var formerTest:TestCaseResult;
    63  	
    64  	/**
    65  	 * Private constructor, use {@link #getInstance} to create a instance.
    66  	 */
    67  	private function LoggerTestListener() {}
    68  	
    69  	
    70  	/**
    71  	 * Start event, fired by start of a TestRunner.
    72  	 * 
    73  	 * @param startInfo Informations about the TestRunner that started.
    74  	 */
    75  	public function onProcessStart(process:Process):Void {
    76  		logger.info("TestRunner started execution.");
    77  	}
    78  	
    79  	/**
    80  	 * Progress event, fired after each executed method within a TestRunner.
    81  	 * 
    82  	 * @param progressInfo Extended informations the current progress.
    83  	 */
    84  	public function onProcessUpdate(process:Process):Void {
    85  		var testRunner:TestRunner = TestRunner(process);
    86  		if (testRunner) {
    87  			var methodInfo:TestCaseMethodInfo = testRunner.getCurrentTestCaseMethodInfo();
    88  			if (methodInfo) {
    89  				logger.info("executing ... "+testRunner.getCurrentTestCase().getName()+"."+methodInfo.getMethodInfo().getName());
    90  			}
    91  		}
    92  	}
    93  	
    94  	/**
    95  	 * Redirects the string representation of the testrunner to the logger
    96  	 * 
    97  	 * @param finishInfo Informations about the TestRunner that finished.
    98  	 */
    99  	public function onProcessFinish(process:Process):Void {
   100  		var testRunner:TestRunner = TestRunner(process);
   101  		if(testRunner) {
   102  			logger.info("TestRunner finished with the result: \n"+testRunner.getTestResult().toString());
   103  		} else {
   104  			throw new IllegalArgumentException("LoggerTestListener added to a different Process", this, arguments);
   105  		}
   106  	}
   107  	
   108  	/**
   109  	 * Pause event, fired after by pausing the execution of a TestRunner.
   110  	 * 
   111  	 * @param pauseInfo Informations about the TestRunner that paused.
   112  	 */
   113  	public function onProcessPause(process:Process):Void {
   114  		var test:TestRunner = TestRunner(process);
   115  		logger.info("TestRunner paused execution at "+test.getCurrentTestCaseMethodInfo().getName());
   116  	}
   117  	
   118  	/**
   119  	 * Pause event, fired after by resuming the execution of a TestRunner.
   120  	 * 
   121  	 * @param resumeInfo Informations about the TestRunner that resumed working.
   122  	 */
   123  	public function onProcessResume(process:Process):Void {
   124  		var test:TestRunner = TestRunner(process);
   125  		logger.info("TestRunner resumed execution at "+test.getCurrentTestCaseMethodInfo().getName());
   126  	}
   127  	
   128  	/**
   129  	 * Executed if a Exeception was thrown during the execution
   130  	 * 
   131  	 * @param process where the execution paused.
   132  	 * @param error Error that occured.
   133  	 */
   134  	public function onProcessError(process:Process, error):Boolean {
   135  		logger.error("Exception was thrown during the execution of the TestRunner: " + error + ".");
   136  		return true;
   137  	}
   138  	
   139  }