Skip to contentMethod: onStart(ITestContext)
1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2023 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.Logger;
33: import org.slf4j.LoggerFactory;
34: import org.testng.ITestContext;
35: import org.testng.ITestResult;
36: import org.testng.TestListenerAdapter;
37:
38: /***********************************************************************************************************************
39: *
40: * @author Fabrizio Giudici
41: *
42: **********************************************************************************************************************/
43: public class TestLogger extends TestListenerAdapter
44: {
45: private static final String S = "***********************************************";
46: private static final String SEPARATOR = S + S + S + S;
47:
48: @Override
49: public void onStart (@Nonnull final ITestContext testContext)
50: {
51: super.onStart(testContext);
52: final String testClass = testContext.getCurrentXmlTest().getClasses().get(0).getName();
53: final Logger log = LoggerFactory.getLogger(testClass);
54: log.info("STARTING TESTS OF {}", testClass);
55: }
56:
57: @Override
58: public void onFinish (@Nonnull final ITestContext testContext)
59: {
60: super.onFinish(testContext);
61: final String testClass = testContext.getCurrentXmlTest().getClasses().get(0).getName();
62: final Logger log = LoggerFactory.getLogger(testClass);
63: log.info("FINISHED TESTS OF {}", testClass);
64: }
65:
66: // @Override
67: // public void onConfigurationSuccess (final @Nonnull ITestResult result)
68: // {
69: // super.onConfigurationSuccess(result);
70: // final Logger log = LoggerFactory.getLogger(result.getTestClass().getRealClass());
71: // log.info("====== ON CONFIG SUCCESS");
72: // }
73:
74: @Override
75: public void onConfigurationSkip (@Nonnull final ITestResult result)
76: {
77: super.onConfigurationSkip(result);
78: final Logger log = LoggerFactory.getLogger(result.getTestClass().getRealClass());
79: final Throwable throwable = result.getThrowable();
80: log.warn("CONFIGURATION SKIPPED {}", getMessage(throwable));
81:
82: if (throwable != null)
83: {
84: log.warn("CONFIGURATION SKIPPED", result.getThrowable());
85: }
86: }
87:
88: @Override
89: public void onConfigurationFailure (@Nonnull final ITestResult result)
90: {
91: super.onConfigurationFailure(result);
92: final Logger log = LoggerFactory.getLogger(result.getTestClass().getRealClass());
93: final Throwable throwable = result.getThrowable();
94: log.error("CONFIGURATION FAILED {}", getMessage(throwable));
95:
96: if (throwable != null)
97: {
98: log.error("CONFIGURATION FAILED", result.getThrowable());
99: }
100: }
101:
102: @Override
103: public void onTestStart (@Nonnull final ITestResult result)
104: {
105: super.onTestStart(result);
106: final String args = getArgs(result);
107: final int max = Math.max(args.length() + 5, result.getName().length() + 7);
108: final String separator = SEPARATOR.substring(0, Math.min(max, SEPARATOR.length()));
109: final Logger log = LoggerFactory.getLogger(result.getTestClass().getRealClass());
110:
111: log.info(separator);
112: log.info("TEST \"{}\"", result.getName().replace('_', ' '));
113:
114: if (!"".equals(args))
115: {
116: log.info("ARGS {}", args);
117: }
118:
119: log.info(separator);
120: }
121:
122: @Override
123: public void onTestFailure (@Nonnull final ITestResult result)
124: {
125: super.onTestFailure(result);
126: final Logger log = LoggerFactory.getLogger(result.getTestClass().getRealClass());
127: final Throwable throwable = result.getThrowable();
128: final String args = getArgs(result);
129:
130: log.error("TEST FAILED in {} msec - {}{} - {}",
131: result.getEndMillis() - result.getStartMillis(),
132: result.getName().replace('_', ' '),
133: "".equals(args) ? "" : " " + args,
134: getMessage(throwable));
135:
136: if (throwable != null)
137: {
138: log.error("TEST FAILED", result.getThrowable());
139: }
140:
141: log.error("");
142: }
143:
144: @Override
145: public void onTestFailedButWithinSuccessPercentage (@Nonnull final ITestResult result)
146: {
147: super.onTestFailedButWithinSuccessPercentage(result);
148: final Logger log = LoggerFactory.getLogger(result.getTestClass().getRealClass());
149: log.info("TEST FAILED WITHIN SUCCESS PERCENTAGE in {} msec", result.getEndMillis() - result.getStartMillis());
150: log.info("");
151: }
152:
153: @Override
154: public void onTestSkipped (@Nonnull final ITestResult result)
155: {
156: super.onTestSkipped(result);
157: final Logger log = LoggerFactory.getLogger(result.getTestClass().getRealClass());
158: final Throwable throwable = result.getThrowable();
159: log.info("TEST SKIPPED {}", getMessage(throwable));
160:
161: if (throwable != null)
162: {
163: log.info("TEST SKIPPED", result.getThrowable());
164: }
165:
166: log.info("");
167: }
168:
169: @Override
170: public void onTestSuccess (@Nonnull final ITestResult result)
171: {
172: super.onTestSuccess(result);
173: final Logger log = LoggerFactory.getLogger(result.getTestClass().getRealClass());
174: log.info("TEST PASSED in {} msec", result.getEndMillis() - result.getStartMillis());
175: log.info("");
176: }
177:
178: @Nonnull
179: private String getArgs (@Nonnull final ITestResult result)
180: {
181: String args = "";
182:
183: final Object[] parameters = result.getParameters();
184:
185: if ((parameters != null) && parameters.length > 0)
186: {
187: args = Arrays.toString(parameters);
188: }
189:
190: return args;
191: }
192:
193: @Nonnull
194: private static String getMessage (@Nullable final Throwable throwable)
195: {
196: return (throwable == null) ? "" : throwable.toString();
197: // return (throwable == null) ? "" : throwable.toString().replaceAll("\n*", "");
198: }
199: }