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