Skip to contentMethod: onFinish(ITestContext)
1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2024 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
23: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.util.test;
28:
29: import javax.annotation.Nonnull;
30: import javax.annotation.Nullable;
31: import java.util.Arrays;
32: import org.slf4j.LoggerFactory;
33: import org.testng.ITestContext;
34: import org.testng.ITestResult;
35: import org.testng.TestListenerAdapter;
36:
37: /***********************************************************************************************************************
38: *
39: * @author Fabrizio Giudici
40: *
41: **********************************************************************************************************************/
42: public class TestLogger extends TestListenerAdapter
43: {
44: private static final String S = "***********************************************";
45: private static final String SEPARATOR = S + S + S + S;
46:
47: @Override
48: public void onStart (@Nonnull final ITestContext testContext)
49: {
50: super.onStart(testContext);
51: final var testClass = testContext.getCurrentXmlTest().getClasses().get(0).getName();
52: final var log = LoggerFactory.getLogger(testClass);
53: log.info("STARTING TESTS OF {}", testClass);
54: }
55:
56: @Override
57: public void onFinish (@Nonnull final ITestContext testContext)
58: {
59: super.onFinish(testContext);
60: final var testClass = testContext.getCurrentXmlTest().getClasses().get(0).getName();
61: final var log = LoggerFactory.getLogger(testClass);
62: log.info("FINISHED TESTS OF {}", testClass);
63: }
64:
65: // @Override
66: // public void onConfigurationSuccess (final @Nonnull ITestResult result)
67: // {
68: // super.onConfigurationSuccess(result);
69: // final Logger log = LoggerFactory.getLogger(result.getTestClass().getRealClass());
70: // log.info("====== ON CONFIG SUCCESS");
71: // }
72:
73: @Override
74: public void onConfigurationSkip (@Nonnull final ITestResult result)
75: {
76: super.onConfigurationSkip(result);
77: final var log = LoggerFactory.getLogger(result.getTestClass().getRealClass());
78: final var throwable = result.getThrowable();
79: log.warn("CONFIGURATION SKIPPED {}", getMessage(throwable));
80:
81: if (throwable != null)
82: {
83: log.warn("CONFIGURATION SKIPPED", result.getThrowable());
84: }
85: }
86:
87: @Override
88: public void onConfigurationFailure (@Nonnull final ITestResult result)
89: {
90: super.onConfigurationFailure(result);
91: final var log = LoggerFactory.getLogger(result.getTestClass().getRealClass());
92: final var throwable = result.getThrowable();
93: log.error("CONFIGURATION FAILED {}", getMessage(throwable));
94:
95: if (throwable != null)
96: {
97: log.error("CONFIGURATION FAILED", result.getThrowable());
98: }
99: }
100:
101: @Override
102: public void onTestStart (@Nonnull final ITestResult result)
103: {
104: super.onTestStart(result);
105: final var args = getArgs(result);
106: final var max = Math.max(args.length() + 5, result.getName().length() + 7);
107: final var separator = SEPARATOR.substring(0, Math.min(max, SEPARATOR.length()));
108: final var log = LoggerFactory.getLogger(result.getTestClass().getRealClass());
109:
110: log.info(separator);
111: log.info("TEST \"{}\"", result.getName().replace('_', ' '));
112:
113: if (!"".equals(args))
114: {
115: log.info("ARGS {}", args);
116: }
117:
118: log.info(separator);
119: }
120:
121: @Override
122: public void onTestFailure (@Nonnull final ITestResult result)
123: {
124: super.onTestFailure(result);
125: final var log = LoggerFactory.getLogger(result.getTestClass().getRealClass());
126: final var throwable = result.getThrowable();
127: final var args = getArgs(result);
128:
129: log.error("TEST FAILED in {} msec - {}{} - {}",
130: result.getEndMillis() - result.getStartMillis(),
131: result.getName().replace('_', ' '),
132: "".equals(args) ? "" : " " + args,
133: getMessage(throwable));
134:
135: if (throwable != null)
136: {
137: log.error("TEST FAILED", result.getThrowable());
138: }
139:
140: log.error("");
141: }
142:
143: @Override
144: public void onTestFailedButWithinSuccessPercentage (@Nonnull final ITestResult result)
145: {
146: super.onTestFailedButWithinSuccessPercentage(result);
147: final var log = LoggerFactory.getLogger(result.getTestClass().getRealClass());
148: log.info("TEST FAILED WITHIN SUCCESS PERCENTAGE in {} msec", result.getEndMillis() - result.getStartMillis());
149: log.info("");
150: }
151:
152: @Override
153: public void onTestSkipped (@Nonnull final ITestResult result)
154: {
155: super.onTestSkipped(result);
156: final var log = LoggerFactory.getLogger(result.getTestClass().getRealClass());
157: final var throwable = result.getThrowable();
158: log.info("TEST SKIPPED {}", getMessage(throwable));
159:
160: if (throwable != null)
161: {
162: log.info("TEST SKIPPED", result.getThrowable());
163: }
164:
165: log.info("");
166: }
167:
168: @Override
169: public void onTestSuccess (@Nonnull final ITestResult result)
170: {
171: super.onTestSuccess(result);
172: final var log = LoggerFactory.getLogger(result.getTestClass().getRealClass());
173: log.info("TEST PASSED in {} msec", result.getEndMillis() - result.getStartMillis());
174: log.info("");
175: }
176:
177: @Nonnull
178: private String getArgs (@Nonnull final ITestResult result)
179: {
180: var args = "";
181:
182: final var parameters = result.getParameters();
183:
184: if ((parameters != null) && parameters.length > 0)
185: {
186: args = Arrays.toString(parameters);
187: }
188:
189: return args;
190: }
191:
192: @Nonnull
193: private static String getMessage (@Nullable final Throwable throwable)
194: {
195: return (throwable == null) ? "" : throwable.toString();
196: // return (throwable == null) ? "" : throwable.toString().replaceAll("\n*", "");
197: }
198: }