11package com .aventstack .extentreports .service ;
22
3- import java .io .FileNotFoundException ;
43import java .io .InputStream ;
54import java .io .Serializable ;
65import java .util .Arrays ;
76import java .util .Optional ;
87import java .util .Properties ;
98
109import com .aventstack .extentreports .ExtentReports ;
11- import com .aventstack .extentreports .reporter .ConfigurableReporter ;
12- import com .aventstack .extentreports .reporter .ExtentAventReporter ;
13- import com .aventstack .extentreports .reporter .ExtentBDDReporter ;
14- import com .aventstack .extentreports .reporter .ExtentCardsReporter ;
15- import com .aventstack .extentreports .reporter .ExtentEmailReporter ;
16- import com .aventstack .extentreports .reporter .ExtentKlovReporter ;
17- import com .aventstack .extentreports .reporter .ExtentLoggerReporter ;
10+ import com .aventstack .extentreports .observer .ExtentObserver ;
1811import com .aventstack .extentreports .reporter .ExtentSparkReporter ;
19- import com .aventstack .extentreports .reporter .ExtentTabularReporter ;
12+ import com .aventstack .extentreports .reporter .ReporterConfigurable ;
2013
21- public class ExtentService
22- implements Serializable {
14+ public class ExtentService
15+ implements
16+ Serializable {
2317
2418 private static final long serialVersionUID = -5008231199972325650L ;
25-
19+
2620 private static Properties properties ;
27-
21+
2822 public static synchronized ExtentReports getInstance () {
2923 return ExtentReportsLoader .INSTANCE ;
3024 }
31-
25+
3226 public static Object getProperty (String key ) {
3327 String sys = System .getProperty (key );
3428 return sys == null ? (properties == null ? null : properties .get (key )) : sys ;
@@ -38,11 +32,11 @@ public static Object getProperty(String key) {
3832 private ExtentReports readResolve () {
3933 return ExtentReportsLoader .INSTANCE ;
4034 }
41-
35+
4236 private static class ExtentReportsLoader {
43-
37+
4438 private static final ExtentReports INSTANCE = new ExtentReports ();
45- private static final String [] DEFAULT_SETUP_PATH = new String [] {
39+ private static final String [] DEFAULT_SETUP_PATH = new String []{
4640 "extent.properties" ,
4741 "com/aventstack/adapter/extent.properties"
4842 };
@@ -52,207 +46,71 @@ private static class ExtentReportsLoader {
5246 private static final String CONFIG = "config" ;
5347 private static final String OUT = "out" ;
5448 private static final String DELIM = "." ;
55-
56- private static final String AVENT = "avent" ;
57- private static final String BDD = "bdd" ;
58- private static final String CARDS = "cards" ;
59- private static final String EMAIL = "email" ;
60- private static final String HTML = "html" ;
61- private static final String KLOV = "klov" ;
62- private static final String LOGGER = "logger" ;
49+
6350 private static final String SPARK = "spark" ;
64- private static final String TABULAR = "tabular" ;
65-
66- private static final String INIT_AVENT_KEY = EXTENT_REPORTER + DELIM + AVENT + DELIM + START ;
67- private static final String INIT_BDD_KEY = EXTENT_REPORTER + DELIM + BDD + DELIM + START ;
68- private static final String INIT_CARDS_KEY = EXTENT_REPORTER + DELIM + CARDS + DELIM + START ;
69- private static final String INIT_EMAIL_KEY = EXTENT_REPORTER + DELIM + EMAIL + DELIM + START ;
70- private static final String INIT_HTML_KEY = EXTENT_REPORTER + DELIM + HTML + DELIM + START ;
71- private static final String INIT_KLOV_KEY = EXTENT_REPORTER + DELIM + KLOV + DELIM + START ;
72- private static final String INIT_LOGGER_KEY = EXTENT_REPORTER + DELIM + LOGGER + DELIM + START ;
7351 private static final String INIT_SPARK_KEY = EXTENT_REPORTER + DELIM + SPARK + DELIM + START ;
74- private static final String INIT_TABULAR_KEY = EXTENT_REPORTER + DELIM + TABULAR + DELIM + START ;
75-
76- private static final String CONFIG_AVENT_KEY = EXTENT_REPORTER + DELIM + AVENT + DELIM + CONFIG ;
77- private static final String CONFIG_BDD_KEY = EXTENT_REPORTER + DELIM + BDD + DELIM + CONFIG ;
78- private static final String CONFIG_CARDS_KEY = EXTENT_REPORTER + DELIM + CARDS + DELIM + CONFIG ;
79- private static final String CONFIG_EMAIL_KEY = EXTENT_REPORTER + DELIM + EMAIL + DELIM + CONFIG ;
80- private static final String CONFIG_HTML_KEY = EXTENT_REPORTER + DELIM + HTML + DELIM + CONFIG ;
81- private static final String CONFIG_KLOV_KEY = EXTENT_REPORTER + DELIM + KLOV + DELIM + CONFIG ;
82- private static final String CONFIG_LOGGER_KEY = EXTENT_REPORTER + DELIM + LOGGER + DELIM + CONFIG ;
8352 private static final String CONFIG_SPARK_KEY = EXTENT_REPORTER + DELIM + SPARK + DELIM + CONFIG ;
84- private static final String CONFIG_TABULAR_KEY = EXTENT_REPORTER + DELIM + TABULAR + DELIM + CONFIG ;
85-
86- private static final String OUT_AVENT_KEY = EXTENT_REPORTER + DELIM + AVENT + DELIM + OUT ;
87- private static final String OUT_BDD_KEY = EXTENT_REPORTER + DELIM + BDD + DELIM + OUT ;
88- private static final String OUT_CARDS_KEY = EXTENT_REPORTER + DELIM + CARDS + DELIM + OUT ;
89- private static final String OUT_EMAIL_KEY = EXTENT_REPORTER + DELIM + EMAIL + DELIM + OUT ;
90- private static final String OUT_HTML_KEY = EXTENT_REPORTER + DELIM + HTML + DELIM + OUT ;
91- private static final String OUT_LOGGER_KEY = EXTENT_REPORTER + DELIM + LOGGER + DELIM + OUT ;
9253 private static final String OUT_SPARK_KEY = EXTENT_REPORTER + DELIM + SPARK + DELIM + OUT ;
93- private static final String OUT_TABULAR_KEY = EXTENT_REPORTER + DELIM + TABULAR + DELIM + OUT ;
94-
54+
9555 static {
96- if (INSTANCE .getStartedReporters ().isEmpty ()) {
97- createViaProperties ();
98- createViaSystem ();
99- }
56+ createViaProperties ();
57+ createViaSystem ();
10058 }
101-
59+
10260 private static void createViaProperties () {
10361 ClassLoader loader = ExtentReportsLoader .class .getClassLoader ();
10462 Optional <InputStream > is = Arrays .stream (DEFAULT_SETUP_PATH )
105- .map (x -> loader .getResourceAsStream (x ))
106- .filter (x -> x != null )
107- .findFirst ();
63+ .map (x -> loader .getResourceAsStream (x ))
64+ .filter (x -> x != null )
65+ .findFirst ();
10866 if (is .isPresent ()) {
10967 Properties properties = new Properties ();
11068 try {
11169 properties .load (is .get ());
11270 ExtentService .properties = properties ;
113-
114- if (properties .containsKey (INIT_AVENT_KEY ) && "true" .equals (String .valueOf (properties .get (INIT_AVENT_KEY ))))
115- initAvent (properties );
116-
117- if (properties .containsKey (INIT_BDD_KEY ) && "true" .equals (String .valueOf (properties .get (INIT_BDD_KEY ))))
118- initBdd (properties );
119-
120- if (properties .containsKey (INIT_CARDS_KEY ) && "true" .equals (String .valueOf (properties .get (INIT_CARDS_KEY ))))
121- initCards (properties );
122-
123- if (properties .containsKey (INIT_EMAIL_KEY ) && "true" .equals (String .valueOf (properties .get (INIT_EMAIL_KEY ))))
124- initEmail (properties );
125-
126- if (properties .containsKey (INIT_HTML_KEY ) && "true" .equals (String .valueOf (properties .get (INIT_HTML_KEY ))))
127- initHtml (properties );
128-
129- if (properties .containsKey (INIT_KLOV_KEY ) && "true" .equals (String .valueOf (properties .get (INIT_KLOV_KEY ))))
130- initKlov (properties );
131-
132- if (properties .containsKey (INIT_LOGGER_KEY ) && "true" .equals (String .valueOf (properties .get (INIT_LOGGER_KEY ))))
133- initLogger (properties );
13471
135- if (properties .containsKey (INIT_SPARK_KEY ) && "true" .equals (String .valueOf (properties .get (INIT_SPARK_KEY ))))
72+ if (properties .containsKey (INIT_SPARK_KEY )
73+ && "true" .equals (String .valueOf (properties .get (INIT_SPARK_KEY ))))
13674 initSpark (properties );
137-
138- if (properties .containsKey (INIT_TABULAR_KEY ) && "true" .equals (String .valueOf (properties .get (INIT_TABULAR_KEY ))))
139- initTabular (properties );
14075 } catch (Exception e ) {
14176 e .printStackTrace ();
14277 }
14378 }
14479 }
145-
146- private static void createViaSystem () {
147- if ("true" .equals (System .getProperty (INIT_AVENT_KEY )))
148- initAvent (null );
149-
150- if ("true" .equals (System .getProperty (INIT_BDD_KEY )))
151- initBdd (null );
152-
153- if ("true" .equals (System .getProperty (INIT_CARDS_KEY )))
154- initCards (null );
155-
156- if ("true" .equals (System .getProperty (INIT_EMAIL_KEY )))
157- initEmail (null );
158-
159- if ("true" .equals (System .getProperty (INIT_HTML_KEY )))
160- initHtml (null );
161-
162- if ("true" .equals (System .getProperty (INIT_KLOV_KEY )))
163- initKlov (null );
164-
165- if ("true" .equals (System .getProperty (INIT_LOGGER_KEY )))
166- initLogger (null );
16780
81+ private static void createViaSystem () {
16882 if ("true" .equals (System .getProperty (INIT_SPARK_KEY )))
16983 initSpark (null );
170-
171- if ("true" .equals (System .getProperty (INIT_TABULAR_KEY )))
172- initTabular (null );
17384 }
174-
175- private static void initAvent (Properties properties ) {
176- String out = getOutputPath (properties , OUT_AVENT_KEY );
177- ExtentAventReporter avent = new ExtentAventReporter (out );
178- attach (avent , properties , CONFIG_AVENT_KEY );
179- }
180-
85+
18186 private static String getOutputPath (Properties properties , String key ) {
18287 String out ;
18388 if (properties != null && properties .get (key ) != null )
18489 out = String .valueOf (properties .get (key ));
185- else
90+ else
18691 out = System .getProperty (key );
18792 out = out == null || out .equals ("null" ) || out .isEmpty () ? OUTPUT_PATH + key .split ("\\ ." )[2 ] + "/" : out ;
18893 return out ;
18994 }
190-
191- private static void initBdd (Properties properties ) {
192- String out = getOutputPath (properties , OUT_BDD_KEY );
193- ExtentBDDReporter bdd = new ExtentBDDReporter (out );
194- attach (bdd , properties , CONFIG_BDD_KEY );
195- }
196-
197- private static void initCards (Properties properties ) {
198- String out = getOutputPath (properties , OUT_CARDS_KEY );
199- ExtentCardsReporter cards = new ExtentCardsReporter (out );
200- attach (cards , properties , CONFIG_CARDS_KEY );
201- }
202-
203- private static void initEmail (Properties properties ) {
204- String out = getOutputPath (properties , OUT_EMAIL_KEY );
205- ExtentEmailReporter email = new ExtentEmailReporter (out );
206- attach (email , properties , CONFIG_EMAIL_KEY );
207- }
208-
209- @ SuppressWarnings ("deprecation" )
210- private static void initHtml (Properties properties ) {
211- String out = getOutputPath (properties , OUT_HTML_KEY );
212- com .aventstack .extentreports .reporter .ExtentHtmlReporter html = new com .aventstack .extentreports .reporter .ExtentHtmlReporter (out );
213- attach (html , properties , CONFIG_HTML_KEY );
214- }
215-
216- private static void initKlov (Properties properties ) {
217- ExtentKlovReporter klov = new ExtentKlovReporter ("Default" );
218- String configPath = properties == null ? System .getProperty (CONFIG_KLOV_KEY )
219- : String .valueOf (properties .get (CONFIG_KLOV_KEY ));
220- if (configPath != null && !configPath .isEmpty ()) {
221- try {
222- klov .loadInitializationParams (configPath );
223- INSTANCE .attachReporter (klov );
224- } catch (FileNotFoundException e ) {
225- e .printStackTrace ();
226- }
227- }
228- }
229-
230- private static void initLogger (Properties properties ) {
231- String out = getOutputPath (properties , OUT_LOGGER_KEY );
232- ExtentLoggerReporter logger = new ExtentLoggerReporter (out );
233- attach (logger , properties , CONFIG_LOGGER_KEY );
234- }
235-
95+
23696 private static void initSpark (Properties properties ) {
23797 String out = getOutputPath (properties , OUT_SPARK_KEY );
23898 ExtentSparkReporter spark = new ExtentSparkReporter (out );
23999 attach (spark , properties , CONFIG_SPARK_KEY );
240100 }
241-
242- private static void initTabular (Properties properties ) {
243- String out = getOutputPath (properties , OUT_TABULAR_KEY );
244- ExtentTabularReporter tabular = new ExtentTabularReporter (out );
245- attach (tabular , properties , CONFIG_TABULAR_KEY );
246- }
247-
248- private static void attach (ConfigurableReporter r , Properties properties , String configKey ) {
249- Object configPath = properties == null
101+
102+ private static void attach (ReporterConfigurable r , Properties properties , String configKey ) {
103+ Object configPath = properties == null
250104 ? System .getProperty (configKey )
251105 : properties .get (configKey );
252106 if (configPath != null && !String .valueOf (configPath ).isEmpty ())
253- r .loadXMLConfig (String .valueOf (configPath ));
254- INSTANCE .attachReporter (r );
107+ try {
108+ r .loadXMLConfig (String .valueOf (configPath ));
109+ } catch (Exception e ) {
110+ e .printStackTrace ();
111+ }
112+ INSTANCE .attachReporter ((ExtentObserver <?>) r );
255113 }
256114 }
257-
115+
258116}
0 commit comments