Home | History | Annotate | Download | only in runner
      1 package org.junit.runner;
      2 
      3 import java.io.Serializable;
      4 import java.util.ArrayList;
      5 import java.util.List;
      6 import java.util.Collections;
      7 import java.util.concurrent.atomic.AtomicInteger;
      8 
      9 import org.junit.runner.notification.Failure;
     10 import org.junit.runner.notification.RunListener;
     11 
     12 /**
     13  * A <code>Result</code> collects and summarizes information from running multiple
     14  * tests. Since tests are expected to run correctly, successful tests are only noted in
     15  * the count of tests that ran.
     16  */
     17 public class Result implements Serializable {
     18 	private static final long serialVersionUID = 1L;
     19 	private AtomicInteger fCount = new AtomicInteger();
     20 	private AtomicInteger fIgnoreCount= new AtomicInteger();
     21 	private final List<Failure> fFailures= Collections.synchronizedList(new ArrayList<Failure>());
     22 	private long fRunTime= 0;
     23 	private long fStartTime;
     24 
     25 	/**
     26 	 * @return the number of tests run
     27 	 */
     28 	public int getRunCount() {
     29 		return fCount.get();
     30 	}
     31 
     32 	/**
     33 	 * @return the number of tests that failed during the run
     34 	 */
     35 	public int getFailureCount() {
     36 		return fFailures.size();
     37 	}
     38 
     39 	/**
     40 	 * @return the number of milliseconds it took to run the entire suite to run
     41 	 */
     42 	public long getRunTime() {
     43 		return fRunTime;
     44 	}
     45 
     46 	/**
     47 	 * @return the {@link Failure}s describing tests that failed and the problems they encountered
     48 	 */
     49 	public List<Failure> getFailures() {
     50 		return fFailures;
     51 	}
     52 
     53 	/**
     54 	 * @return the number of tests ignored during the run
     55 	 */
     56 	public int getIgnoreCount() {
     57 		return fIgnoreCount.get();
     58 	}
     59 
     60 	/**
     61 	 * @return <code>true</code> if all tests succeeded
     62 	 */
     63 	public boolean wasSuccessful() {
     64 		return getFailureCount() == 0;
     65 	}
     66 
     67 	private class Listener extends RunListener {
     68 		@Override
     69 		public void testRunStarted(Description description) throws Exception {
     70 			fStartTime= System.currentTimeMillis();
     71 		}
     72 
     73 		@Override
     74 		public void testRunFinished(Result result) throws Exception {
     75 			long endTime= System.currentTimeMillis();
     76 			fRunTime+= endTime - fStartTime;
     77 		}
     78 
     79 		@Override
     80 		public void testFinished(Description description) throws Exception {
     81 			fCount.getAndIncrement();
     82 		}
     83 
     84 		@Override
     85 		public void testFailure(Failure failure) throws Exception {
     86 			fFailures.add(failure);
     87 		}
     88 
     89 		@Override
     90 		public void testIgnored(Description description) throws Exception {
     91 			fIgnoreCount.getAndIncrement();
     92 		}
     93 
     94 		@Override
     95 		public void testAssumptionFailure(Failure failure) {
     96 			// do nothing: same as passing (for 4.5; may change in 4.6)
     97 		}
     98 	}
     99 
    100 	/**
    101 	 * Internal use only.
    102 	 */
    103 	public RunListener createListener() {
    104 		return new Listener();
    105 	}
    106 }
    107