001package org.junit.runner; 002 003import org.junit.runner.notification.Failure; 004import org.junit.runner.notification.RunListener; 005 006import java.io.Serializable; 007import java.util.List; 008import java.util.concurrent.CopyOnWriteArrayList; 009import java.util.concurrent.atomic.AtomicInteger; 010import java.util.concurrent.atomic.AtomicLong; 011 012/** 013 * A <code>Result</code> collects and summarizes information from running multiple tests. 014 * All tests are counted -- additional information is collected from tests that fail. 015 * 016 * @since 4.0 017 */ 018public class Result implements Serializable { 019 private static final long serialVersionUID = 2L; 020 private final AtomicInteger fCount = new AtomicInteger(); 021 private final AtomicInteger fIgnoreCount = new AtomicInteger(); 022 private final CopyOnWriteArrayList<Failure> fFailures = new CopyOnWriteArrayList<Failure>(); 023 private final AtomicLong fRunTime = new AtomicLong(); 024 private final AtomicLong fStartTime = new AtomicLong(); 025 026 /** 027 * @return the number of tests run 028 */ 029 public int getRunCount() { 030 return fCount.get(); 031 } 032 033 /** 034 * @return the number of tests that failed during the run 035 */ 036 public int getFailureCount() { 037 return fFailures.size(); 038 } 039 040 /** 041 * @return the number of milliseconds it took to run the entire suite to run 042 */ 043 public long getRunTime() { 044 return fRunTime.get(); 045 } 046 047 /** 048 * @return the {@link Failure}s describing tests that failed and the problems they encountered 049 */ 050 public List<Failure> getFailures() { 051 return fFailures; 052 } 053 054 /** 055 * @return the number of tests ignored during the run 056 */ 057 public int getIgnoreCount() { 058 return fIgnoreCount.get(); 059 } 060 061 /** 062 * @return <code>true</code> if all tests succeeded 063 */ 064 public boolean wasSuccessful() { 065 return getFailureCount() == 0; 066 } 067 068 private class Listener extends RunListener { 069 @Override 070 public void testRunStarted(Description description) throws Exception { 071 fStartTime.set(System.currentTimeMillis()); 072 } 073 074 @Override 075 public void testRunFinished(Result result) throws Exception { 076 long endTime = System.currentTimeMillis(); 077 fRunTime.addAndGet(endTime - fStartTime.get()); 078 } 079 080 @Override 081 public void testFinished(Description description) throws Exception { 082 fCount.getAndIncrement(); 083 } 084 085 @Override 086 public void testFailure(Failure failure) throws Exception { 087 fFailures.add(failure); 088 } 089 090 @Override 091 public void testIgnored(Description description) throws Exception { 092 fIgnoreCount.getAndIncrement(); 093 } 094 095 @Override 096 public void testAssumptionFailure(Failure failure) { 097 // do nothing: same as passing (for 4.5; may change in 4.6) 098 } 099 } 100 101 /** 102 * Internal use only. 103 */ 104 public RunListener createListener() { 105 return new Listener(); 106 } 107}