langtools/test/tools/javac/classfiles/attributes/lib/TestResult.java
changeset 26101 d5dd2ecd2353
parent 25434 823512a72660
child 27552 8a4b2d3639c1
equal deleted inserted replaced
26099:c425126bfadf 26101:d5dd2ecd2353
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 import java.io.PrintWriter;
    24 import java.io.PrintWriter;
    25 import java.io.StringWriter;
    25 import java.io.StringWriter;
    26 import java.util.ArrayList;
    26 import java.util.*;
    27 import java.util.List;
    27 import java.util.stream.Stream;
    28 import java.util.Objects;
       
    29 
    28 
    30 import static java.lang.String.format;
    29 import static java.lang.String.format;
    31 import static java.util.stream.Collectors.joining;
    30 import static java.util.stream.Collectors.joining;
    32 
    31 
       
    32 /**
       
    33  * This class accumulates test results. Test results can be checked with method @{code checkStatus}.
       
    34  */
    33 public class TestResult extends TestBase {
    35 public class TestResult extends TestBase {
    34 
    36 
    35     private final List<Info> testCases;
    37     private final List<Info> testCases;
    36 
    38 
    37     public TestResult() {
    39     public TestResult() {
    38         testCases = new ArrayList<>();
    40         testCases = new ArrayList<>();
    39         testCases.add(new Info("Global test info"));
    41         testCases.add(new Info("Global test info"));
    40     }
    42     }
    41 
    43 
    42     public void addTestCase(String src) {
    44     /**
    43         testCases.add(new Info(src));
    45      * Adds new test case info.
       
    46      *
       
    47      * @param info the information about test case
       
    48      */
       
    49     public void addTestCase(String info) {
       
    50         testCases.add(new Info(info));
    44     }
    51     }
    45 
    52 
    46     public String errorMessage() {
    53     private String errorMessage() {
    47         return testCases.stream().filter(Info::isFailed)
    54         return testCases.stream().filter(Info::isFailed)
    48                 .map(tc -> format("Failure in test case:\n%s\n%s", tc.info(),
    55                 .map(tc -> format("Failure in test case:\n%s\n%s", tc.info(), tc.getMessage()))
    49                         (tc.asserts.size() > 0 ? tc.getAssertMessage() + "\n" : "")
       
    50                                 + tc.getErrorMessage()))
       
    51                 .collect(joining("\n"));
    56                 .collect(joining("\n"));
    52     }
    57     }
    53 
    58 
    54     @Override
    59     @Override
    55     public void assertEquals(Object actual, Object expected, String message) {
    60     public void assertEquals(Object actual, Object expected, String message) {
    74     @Override
    79     @Override
    75     public void assertTrue(boolean actual, String message) {
    80     public void assertTrue(boolean actual, String message) {
    76         getLastTestCase().assertEquals(actual, true, message);
    81         getLastTestCase().assertEquals(actual, true, message);
    77     }
    82     }
    78 
    83 
       
    84     public void assertContains(Set<?> found, Set<?> expected, String message) {
       
    85         Set<?> copy = new HashSet<>(expected);
       
    86         copy.removeAll(found);
       
    87         assertTrue(found.containsAll(expected), message + " : " + copy);
       
    88     }
       
    89 
    79     public void addFailure(Throwable th) {
    90     public void addFailure(Throwable th) {
    80         getLastTestCase().addFailure(th);
    91         testCases.get(testCases.size() - 1).addFailure(th);
    81     }
    92     }
    82 
    93 
    83     private Info getLastTestCase() {
    94     private Info getLastTestCase() {
    84         if (testCases.size() == 1) {
    95         if (testCases.size() == 1) {
    85             throw new IllegalStateException("Test case should be created");
    96             throw new IllegalStateException("Test case should be created");
    86         }
    97         }
    87         return testCases.get(testCases.size() - 1);
    98         return testCases.get(testCases.size() - 1);
    88     }
    99     }
    89 
   100 
       
   101     /**
       
   102      * Throws {@code TestFailedException} if one of the asserts are failed
       
   103      * or an exception occurs. Prints error message of failed test cases.
       
   104      *
       
   105      * @throws TestFailedException if one of the asserts are failed
       
   106      *                             or an exception occurs
       
   107      */
    90     public void checkStatus() throws TestFailedException {
   108     public void checkStatus() throws TestFailedException {
    91         if (testCases.stream().anyMatch(Info::isFailed)) {
   109         if (testCases.stream().anyMatch(Info::isFailed)) {
    92             echo(errorMessage());
   110             echo(errorMessage());
    93             throw new TestFailedException("Test failed");
   111             throw new TestFailedException("Test failed");
    94         }
   112         }
   118             errors.add(th);
   136             errors.add(th);
   119             printf("[ERROR] : %s\n", getStackTrace(th));
   137             printf("[ERROR] : %s\n", getStackTrace(th));
   120         }
   138         }
   121 
   139 
   122         public void addFailure(String message) {
   140         public void addFailure(String message) {
   123             asserts.add(message);
   141             String stackTrace = Stream.of(Thread.currentThread().getStackTrace())
       
   142                     // just to get stack trace without TestResult and Thread
       
   143                     .filter(e -> !"TestResult.java".equals(e.getFileName()) &&
       
   144                             !"java.lang.Thread".equals(e.getClassName()))
       
   145                     .map(e -> "\tat " + e)
       
   146                     .collect(joining("\n"));
       
   147             asserts.add(format("%s\n%s", message, stackTrace));
   124             printf("[ASSERT] : %s\n", message);
   148             printf("[ASSERT] : %s\n", message);
   125         }
   149         }
   126 
   150 
   127         public void assertEquals(Object actual, Object expected, String message) {
   151         public void assertEquals(Object actual, Object expected, String message) {
   128             echo("Testing : " + message);
   152             echo("Testing : " + message);
   136             if (actual == null) {
   160             if (actual == null) {
   137                 addFailure(message + " : Expected not null value");
   161                 addFailure(message + " : Expected not null value");
   138             }
   162             }
   139         }
   163         }
   140 
   164 
       
   165         public String getMessage() {
       
   166             return (asserts.size() > 0 ? getAssertMessage() + "\n" : "") + getErrorMessage();
       
   167         }
       
   168 
   141         public String getAssertMessage() {
   169         public String getAssertMessage() {
   142             return asserts.stream()
   170             return asserts.stream()
   143                     .map(failure -> "[ASSERT] : " + failure)
   171                     .map(failure -> "[ASSERT] : " + failure)
   144                     .collect(joining("\n"));
   172                     .collect(joining("\n"));
   145         }
   173         }
   146 
   174 
   147         public String getErrorMessage() {
   175         public String getErrorMessage() {
   148             return errors.stream()
   176             return errors.stream()
   149                     .map(throwable ->
   177                     .map(throwable -> format("[ERROR] : %s", getStackTrace(throwable)))
   150                             format("[ERROR] : %s", getStackTrace(throwable)))
       
   151                     .collect(joining("\n"));
   178                     .collect(joining("\n"));
   152         }
   179         }
   153 
   180 
   154         public String getStackTrace(Throwable throwable) {
   181         public String getStackTrace(Throwable throwable) {
   155             StringWriter stringWriter = new StringWriter();
   182             StringWriter stringWriter = new StringWriter();