1+ package task .Tester ;
2+
3+ import org .jetbrains .annotations .NotNull ;
4+ import org .jetbrains .annotations .Nullable ;
5+
6+ import java .io .PrintStream ;
7+ import java .lang .reflect .Method ;
8+
9+ public class TestResult {
10+ public final @ NotNull Method method ;
11+ public final boolean ok ;
12+ public final @ NotNull String message ;
13+ public final @ Nullable Throwable exception ;
14+ public final long time ;
15+
16+ private TestResult (@ NotNull Method method , boolean ok , @ NotNull String message , @ Nullable Throwable exception , long time ) {
17+ this .method = method ;
18+ this .ok = ok ;
19+ this .message = message ;
20+ this .exception = exception ;
21+ this .time = time ;
22+ }
23+
24+ public static TestResult ok (@ NotNull Method method , long time ) {
25+ return new TestResult (method , true , "ok." , null , time );
26+ }
27+
28+ public static TestResult expectedException (@ NotNull Method method , @ NotNull Class <? extends Throwable > exceptionClass , long time ) {
29+ return new TestResult (method , false , "expected " + exceptionClass .getName (), null , time );
30+ }
31+
32+ public static TestResult exception (@ NotNull Method method , @ NotNull Throwable exception , long time ) {
33+ return new TestResult (method , false , "exception: " , exception , time );
34+ }
35+
36+ public static TestResult ignored (@ NotNull Method method , @ NotNull String message ) {
37+ return new TestResult (method , true , "ignore. \n cause:" + message , null , 0 );
38+ }
39+
40+ public void print (PrintStream out ) {
41+ out .println (method );
42+ if (ok ) {
43+ out .println ("Ok" );
44+ out .println (message );
45+ } else {
46+ out .println ("Fail" );
47+ out .println (message );
48+ if (exception != null ) {
49+ exception .printStackTrace (out );
50+ }
51+ }
52+ out .println ("time: " + time + "ms" );
53+ out .println ();
54+ out .println ();
55+ }
56+ }
0 commit comments