1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
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 java.io.PrintStream;
18 
19 // Results collector for VarHandle Unit tests
20 public final class VarHandleUnitTestCollector {
21     private final PrintStream out = System.out;
22     private final boolean verbose = false;
23 
24     private int numberOfSuccesses;
25     private int numberOfSkips;
26     private int numberOfFailures;
27     private int consecutiveResults = 0;
28     private String current;
29     private long startMillis;
30 
start(String testName)31     public void start(String testName) {
32         out.append(testName)
33                 .append("...");
34         consecutiveResults = 0;
35         current = testName;
36         startMillis = System.currentTimeMillis();
37     }
38 
printStatus(String status)39     private void printStatus(String status) {
40         out.print(status);
41         if (verbose) {
42             out.print('[');
43             out.print(System.currentTimeMillis() - startMillis);
44             out.print(']');
45         }
46         out.println();
47     }
48 
skip()49     public void skip() {
50         numberOfSkips += 1;
51         printStatus("SKIP");
52         consecutiveResults++;
53     }
54 
success()55     public void success() {
56         numberOfSuccesses += 1;
57         printStatus("OK");
58         if (consecutiveResults++ > 1) {
59             throw new AssertionError("Oops: " + consecutiveResults);
60         }
61     }
62 
fail(String errorMessage)63     public void fail(String errorMessage) {
64         numberOfFailures += 1;
65         printStatus("FAIL");
66         out.print(errorMessage);
67         consecutiveResults++;
68     }
69 
printSummary()70     public void printSummary() {
71         out.append(Integer.toString(numberOfSuccesses))
72                 .append(" successes, ")
73                 .append(Integer.toString(numberOfSkips))
74                 .append(" skips, ")
75                 .append(Integer.toString(numberOfFailures))
76                 .append(" failures.");
77         out.println();
78     }
79 
failuresOccurred()80     boolean failuresOccurred() {
81         return numberOfFailures != 0;
82     }
83 }
84