1 /* |
1 /* |
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
54 } |
54 } |
55 |
55 |
56 public boolean checkEquals(Object actual, Object expected, String message) { |
56 public boolean checkEquals(Object actual, Object expected, String message) { |
57 echo("Testing : " + message); |
57 echo("Testing : " + message); |
58 if (!Objects.equals(actual, expected)) { |
58 if (!Objects.equals(actual, expected)) { |
59 getLastTestCase().addAssert(new AssertionFailedException( |
59 getLastTestCase().addAssert(String.format("%s\n" + |
60 String.format("%s%nGot: %s, Expected: %s", message, actual, expected))); |
60 "Expected: %s,\n" + |
|
61 " Got: %s", message, expected, actual)); |
61 return false; |
62 return false; |
62 } |
63 } |
63 return true; |
64 return true; |
64 } |
65 } |
65 |
66 |
68 } |
69 } |
69 |
70 |
70 public boolean checkNotNull(Object actual, String message) { |
71 public boolean checkNotNull(Object actual, String message) { |
71 echo("Testing : " + message); |
72 echo("Testing : " + message); |
72 if (Objects.isNull(actual)) { |
73 if (Objects.isNull(actual)) { |
73 getLastTestCase().addAssert(new AssertionFailedException( |
74 getLastTestCase().addAssert(message + " : Expected not null value"); |
74 message + " : Expected not null value")); |
|
75 return false; |
75 return false; |
76 } |
76 } |
77 return true; |
77 return true; |
78 } |
78 } |
79 |
79 |
86 } |
86 } |
87 |
87 |
88 public boolean checkContains(Set<?> found, Set<?> expected, String message) { |
88 public boolean checkContains(Set<?> found, Set<?> expected, String message) { |
89 Set<?> copy = new HashSet<>(expected); |
89 Set<?> copy = new HashSet<>(expected); |
90 copy.removeAll(found); |
90 copy.removeAll(found); |
91 return checkTrue(found.containsAll(expected), message + " : " + copy); |
91 if (!found.containsAll(expected)) { |
|
92 return checkTrue(false, message + " FAIL : not found elements : " + copy); |
|
93 } else { |
|
94 return checkTrue(true, message + " PASS : all elements found"); |
|
95 } |
92 } |
96 } |
93 |
97 |
94 public void addFailure(Throwable th) { |
98 public void addFailure(Throwable th) { |
95 testCases.get(testCases.size() - 1).addFailure(th); |
99 testCases.get(testCases.size() - 1).addFailure(th); |
96 } |
100 } |
117 } |
121 } |
118 |
122 |
119 private class Info { |
123 private class Info { |
120 |
124 |
121 private final String info; |
125 private final String info; |
122 private final List<AssertionFailedException> asserts; |
126 private final List<String> asserts; |
123 private final List<Throwable> errors; |
127 private final List<Throwable> errors; |
124 |
128 |
125 private Info(String info) { |
129 private Info(String info) { |
126 this.info = info; |
130 this.info = info; |
127 asserts = new ArrayList<>(); |
131 asserts = new ArrayList<>(); |
139 public void addFailure(Throwable th) { |
143 public void addFailure(Throwable th) { |
140 errors.add(th); |
144 errors.add(th); |
141 printf("[ERROR] : %s\n", getStackTrace(th)); |
145 printf("[ERROR] : %s\n", getStackTrace(th)); |
142 } |
146 } |
143 |
147 |
144 public void addAssert(AssertionFailedException e) { |
148 public void addAssert(String e) { |
145 asserts.add(e); |
149 asserts.add(e); |
146 printf("[ASSERT] : %s\n", getStackTrace(e)); |
150 printf("[ASSERT] : %s\n", e); |
147 } |
151 } |
148 |
152 |
149 public String getMessage() { |
153 public String getMessage() { |
150 return (asserts.size() > 0 ? getErrorMessage("[ASSERT]", asserts) + "\n" : "") |
154 return (asserts.size() > 0 ? getAssertMessages(asserts) + "\n" : "") |
151 + getErrorMessage("[ERROR]", errors); |
155 + getErrorMessages(errors); |
152 } |
156 } |
153 |
157 |
154 public String getErrorMessage(String header, List<? extends Throwable> list) { |
158 public String getAssertMessages(List<String> list) { |
155 return list.stream() |
159 return list.stream() |
156 .map(throwable -> String.format("%s : %s", header, getStackTrace(throwable))) |
160 .map(message -> String.format("[ASSERT] : %s", message)) |
|
161 .collect(Collectors.joining("\n")); |
|
162 } |
|
163 |
|
164 public String getErrorMessages(List<? extends Throwable> list) { |
|
165 return list.stream() |
|
166 .map(throwable -> String.format("[ERROR] : %s", getStackTrace(throwable))) |
157 .collect(Collectors.joining("\n")); |
167 .collect(Collectors.joining("\n")); |
158 } |
168 } |
159 |
169 |
160 public String getStackTrace(Throwable throwable) { |
170 public String getStackTrace(Throwable throwable) { |
161 StringWriter stringWriter = new StringWriter(); |
171 StringWriter stringWriter = new StringWriter(); |