11package com .aventstack .extentreports .service ;
22
3- import java .io .FileNotFoundException ;
3+ import java .io .File ;
4+ import java .io .FileInputStream ;
5+ import java .io .IOException ;
46import java .io .InputStream ;
57import java .io .Serializable ;
8+ import java .nio .file .Paths ;
69import java .util .Arrays ;
710import java .util .Optional ;
811import java .util .Properties ;
1316import com .aventstack .extentreports .reporter .ExtentBDDReporter ;
1417import com .aventstack .extentreports .reporter .ExtentCardsReporter ;
1518import com .aventstack .extentreports .reporter .ExtentEmailReporter ;
16- import com .aventstack .extentreports .reporter .ExtentHtmlReporter ;
1719import com .aventstack .extentreports .reporter .ExtentKlovReporter ;
1820import com .aventstack .extentreports .reporter .ExtentLoggerReporter ;
19- import com .aventstack .extentreports .reporter .ExtentReporter ;
21+ import com .aventstack .extentreports .reporter .ExtentSparkReporter ;
2022import com .aventstack .extentreports .reporter .ExtentTabularReporter ;
2123
22- public class ExtentService implements Serializable {
24+ public class ExtentService
25+ implements Serializable {
2326
2427 private static final long serialVersionUID = -5008231199972325650L ;
25-
28+
29+ private static Properties properties ;
30+
2631 public static synchronized ExtentReports getInstance () {
2732 return ExtentReportsLoader .INSTANCE ;
2833 }
34+
35+ public static Object getProperty (String key ) {
36+ String sys = System .getProperty (key );
37+ return sys == null ? (properties == null ? null : properties .get (key )) : sys ;
38+ }
2939
3040 @ SuppressWarnings ("unused" )
3141 private ExtentReports readResolve () {
3242 return ExtentReportsLoader .INSTANCE ;
3343 }
34-
44+
3545 private static class ExtentReportsLoader {
36-
46+
3747 private static final ExtentReports INSTANCE = new ExtentReports ();
38- private static final String [] DEFAULT_SETUP_PATH = new String [] { "extent.properties" ,
39- "com/aventstack/adapter/extent.properties" };
48+ private static final String [] DEFAULT_SETUP_PATH = new String [] {
49+ "extent.properties" ,
50+ "com/aventstack/adapter/extent.properties"
51+ };
4052 private static final String OUTPUT_PATH = "test-output/" ;
4153 private static final String EXTENT_REPORTER = "extent.reporter" ;
4254 private static final String START = "start" ;
4355 private static final String CONFIG = "config" ;
4456 private static final String OUT = "out" ;
4557 private static final String DELIM = "." ;
46-
58+
4759 private static final String AVENT = "avent" ;
4860 private static final String BDD = "bdd" ;
4961 private static final String CARDS = "cards" ;
5062 private static final String EMAIL = "email" ;
5163 private static final String HTML = "html" ;
5264 private static final String KLOV = "klov" ;
5365 private static final String LOGGER = "logger" ;
66+ private static final String SPARK = "spark" ;
5467 private static final String TABULAR = "tabular" ;
55-
68+
5669 private static final String INIT_AVENT_KEY = EXTENT_REPORTER + DELIM + AVENT + DELIM + START ;
5770 private static final String INIT_BDD_KEY = EXTENT_REPORTER + DELIM + BDD + DELIM + START ;
5871 private static final String INIT_CARDS_KEY = EXTENT_REPORTER + DELIM + CARDS + DELIM + START ;
5972 private static final String INIT_EMAIL_KEY = EXTENT_REPORTER + DELIM + EMAIL + DELIM + START ;
6073 private static final String INIT_HTML_KEY = EXTENT_REPORTER + DELIM + HTML + DELIM + START ;
6174 private static final String INIT_KLOV_KEY = EXTENT_REPORTER + DELIM + KLOV + DELIM + START ;
6275 private static final String INIT_LOGGER_KEY = EXTENT_REPORTER + DELIM + LOGGER + DELIM + START ;
76+ private static final String INIT_SPARK_KEY = EXTENT_REPORTER + DELIM + SPARK + DELIM + START ;
6377 private static final String INIT_TABULAR_KEY = EXTENT_REPORTER + DELIM + TABULAR + DELIM + START ;
64-
78+
6579 private static final String CONFIG_AVENT_KEY = EXTENT_REPORTER + DELIM + AVENT + DELIM + CONFIG ;
6680 private static final String CONFIG_BDD_KEY = EXTENT_REPORTER + DELIM + BDD + DELIM + CONFIG ;
6781 private static final String CONFIG_CARDS_KEY = EXTENT_REPORTER + DELIM + CARDS + DELIM + CONFIG ;
6882 private static final String CONFIG_EMAIL_KEY = EXTENT_REPORTER + DELIM + EMAIL + DELIM + CONFIG ;
6983 private static final String CONFIG_HTML_KEY = EXTENT_REPORTER + DELIM + HTML + DELIM + CONFIG ;
7084 private static final String CONFIG_KLOV_KEY = EXTENT_REPORTER + DELIM + KLOV + DELIM + CONFIG ;
7185 private static final String CONFIG_LOGGER_KEY = EXTENT_REPORTER + DELIM + LOGGER + DELIM + CONFIG ;
86+ private static final String CONFIG_SPARK_KEY = EXTENT_REPORTER + DELIM + SPARK + DELIM + CONFIG ;
7287 private static final String CONFIG_TABULAR_KEY = EXTENT_REPORTER + DELIM + TABULAR + DELIM + CONFIG ;
7388
7489 private static final String OUT_AVENT_KEY = EXTENT_REPORTER + DELIM + AVENT + DELIM + OUT ;
@@ -77,127 +92,130 @@ private static class ExtentReportsLoader {
7792 private static final String OUT_EMAIL_KEY = EXTENT_REPORTER + DELIM + EMAIL + DELIM + OUT ;
7893 private static final String OUT_HTML_KEY = EXTENT_REPORTER + DELIM + HTML + DELIM + OUT ;
7994 private static final String OUT_LOGGER_KEY = EXTENT_REPORTER + DELIM + LOGGER + DELIM + OUT ;
95+ private static final String OUT_SPARK_KEY = EXTENT_REPORTER + DELIM + SPARK + DELIM + OUT ;
8096 private static final String OUT_TABULAR_KEY = EXTENT_REPORTER + DELIM + TABULAR + DELIM + OUT ;
81-
97+
8298 static {
8399 if (INSTANCE .getStartedReporters ().isEmpty ()) {
84100 createViaProperties ();
85101 createViaSystem ();
86102 }
87103 }
88-
104+
89105 private static void createViaProperties () {
90106 ClassLoader loader = ExtentReportsLoader .class .getClassLoader ();
91- Optional <InputStream > is = Arrays .stream (DEFAULT_SETUP_PATH ).map (x -> loader .getResourceAsStream (x ))
92- .filter (x -> x != null ).findFirst ();
107+ Optional <InputStream > is = Arrays .stream (DEFAULT_SETUP_PATH )
108+ .map (x -> loader .getResourceAsStream (x ))
109+ .filter (x -> x != null )
110+ .findFirst ();
93111 if (is .isPresent ()) {
94112 Properties properties = new Properties ();
95113 try {
96114 properties .load (is .get ());
97-
98- if ( properties . containsKey ( INIT_AVENT_KEY )
99- && "true" .equals (String .valueOf (properties .get (INIT_AVENT_KEY ))))
115+ ExtentService . properties = properties ;
116+
117+ if ( properties . containsKey ( INIT_AVENT_KEY ) && "true" .equals (String .valueOf (properties .get (INIT_AVENT_KEY ))))
100118 initAvent (properties );
101119
102- if (properties .containsKey (INIT_BDD_KEY )
103- && "true" .equals (String .valueOf (properties .get (INIT_BDD_KEY ))))
120+ if (properties .containsKey (INIT_BDD_KEY ) && "true" .equals (String .valueOf (properties .get (INIT_BDD_KEY ))))
104121 initBdd (properties );
105-
106- if (properties .containsKey (INIT_CARDS_KEY )
107- && "true" .equals (String .valueOf (properties .get (INIT_CARDS_KEY ))))
122+
123+ if (properties .containsKey (INIT_CARDS_KEY ) && "true" .equals (String .valueOf (properties .get (INIT_CARDS_KEY ))))
108124 initCards (properties );
109-
110- if (properties .containsKey (INIT_EMAIL_KEY )
111- && "true" .equals (String .valueOf (properties .get (INIT_EMAIL_KEY ))))
125+
126+ if (properties .containsKey (INIT_EMAIL_KEY ) && "true" .equals (String .valueOf (properties .get (INIT_EMAIL_KEY ))))
112127 initEmail (properties );
113-
114- if (properties .containsKey (INIT_HTML_KEY )
115- && "true" .equals (String .valueOf (properties .get (INIT_HTML_KEY ))))
128+
129+ if (properties .containsKey (INIT_HTML_KEY ) && "true" .equals (String .valueOf (properties .get (INIT_HTML_KEY ))))
116130 initHtml (properties );
117-
118- if (properties .containsKey (INIT_KLOV_KEY )
119- && "true" .equals (String .valueOf (properties .get (INIT_KLOV_KEY ))))
131+
132+ if (properties .containsKey (INIT_KLOV_KEY ) && "true" .equals (String .valueOf (properties .get (INIT_KLOV_KEY ))))
120133 initKlov (properties );
121134
122- if (properties .containsKey (INIT_LOGGER_KEY )
123- && "true" .equals (String .valueOf (properties .get (INIT_LOGGER_KEY ))))
135+ if (properties .containsKey (INIT_LOGGER_KEY ) && "true" .equals (String .valueOf (properties .get (INIT_LOGGER_KEY ))))
124136 initLogger (properties );
125137
126- if (properties .containsKey (INIT_TABULAR_KEY )
127- && "true" .equals (String .valueOf (properties .get (INIT_TABULAR_KEY ))))
138+ if (properties .containsKey (INIT_SPARK_KEY ) && "true" .equals (String .valueOf (properties .get (INIT_SPARK_KEY ))))
139+ initSpark (properties );
140+
141+ if (properties .containsKey (INIT_TABULAR_KEY ) && "true" .equals (String .valueOf (properties .get (INIT_TABULAR_KEY ))))
128142 initTabular (properties );
129143 } catch (Exception e ) {
130144 e .printStackTrace ();
131145 }
132146 }
133147 }
134-
148+
135149 private static void createViaSystem () {
136150 if ("true" .equals (System .getProperty (INIT_AVENT_KEY )))
137151 initAvent (null );
138152
139153 if ("true" .equals (System .getProperty (INIT_BDD_KEY )))
140154 initBdd (null );
141-
155+
142156 if ("true" .equals (System .getProperty (INIT_CARDS_KEY )))
143157 initCards (null );
144-
158+
145159 if ("true" .equals (System .getProperty (INIT_EMAIL_KEY )))
146160 initEmail (null );
147-
161+
148162 if ("true" .equals (System .getProperty (INIT_HTML_KEY )))
149163 initHtml (null );
150-
164+
151165 if ("true" .equals (System .getProperty (INIT_KLOV_KEY )))
152166 initKlov (null );
153167
154168 if ("true" .equals (System .getProperty (INIT_LOGGER_KEY )))
155169 initLogger (null );
156170
171+ if ("true" .equals (System .getProperty (INIT_SPARK_KEY )))
172+ initSpark (null );
173+
157174 if ("true" .equals (System .getProperty (INIT_TABULAR_KEY )))
158175 initTabular (null );
159176 }
160-
177+
161178 private static void initAvent (Properties properties ) {
162179 String out = getOutputPath (properties , OUT_AVENT_KEY );
163180 ExtentAventReporter avent = new ExtentAventReporter (out );
164181 attach (avent , properties , CONFIG_AVENT_KEY );
165182 }
166-
183+
167184 private static String getOutputPath (Properties properties , String key ) {
168185 String out ;
169186 if (properties != null && properties .get (key ) != null )
170187 out = String .valueOf (properties .get (key ));
171- else
188+ else
172189 out = System .getProperty (key );
173190 out = out == null || out .equals ("null" ) || out .isEmpty () ? OUTPUT_PATH + key .split ("\\ ." )[2 ] + "/" : out ;
174191 return out ;
175192 }
176-
193+
177194 private static void initBdd (Properties properties ) {
178195 String out = getOutputPath (properties , OUT_BDD_KEY );
179196 ExtentBDDReporter bdd = new ExtentBDDReporter (out );
180197 attach (bdd , properties , CONFIG_BDD_KEY );
181198 }
182-
199+
183200 private static void initCards (Properties properties ) {
184201 String out = getOutputPath (properties , OUT_CARDS_KEY );
185202 ExtentCardsReporter cards = new ExtentCardsReporter (out );
186203 attach (cards , properties , CONFIG_CARDS_KEY );
187204 }
188-
205+
189206 private static void initEmail (Properties properties ) {
190207 String out = getOutputPath (properties , OUT_EMAIL_KEY );
191208 ExtentEmailReporter email = new ExtentEmailReporter (out );
192209 attach (email , properties , CONFIG_EMAIL_KEY );
193210 }
194-
195- private static void initHtml (Properties properties ) {
211+
212+ @ SuppressWarnings ("deprecation" )
213+ private static void initHtml (Properties properties ) {
196214 String out = getOutputPath (properties , OUT_HTML_KEY );
197- ExtentHtmlReporter html = new ExtentHtmlReporter (out );
215+ com . aventstack . extentreports . reporter . ExtentHtmlReporter html = new com . aventstack . extentreports . reporter . ExtentHtmlReporter (out );
198216 attach (html , properties , CONFIG_HTML_KEY );
199217 }
200-
218+
201219 private static void initKlov (Properties properties ) {
202220 ExtentKlovReporter klov = new ExtentKlovReporter ("Default" );
203221 String configPath = properties == null ? System .getProperty (CONFIG_KLOV_KEY )
@@ -211,25 +229,33 @@ private static void initKlov(Properties properties) {
211229 }
212230 }
213231 }
214-
232+
215233 private static void initLogger (Properties properties ) {
216234 String out = getOutputPath (properties , OUT_LOGGER_KEY );
217235 ExtentLoggerReporter logger = new ExtentLoggerReporter (out );
218236 attach (logger , properties , CONFIG_LOGGER_KEY );
219237 }
220-
238+
239+ private static void initSpark (Properties properties ) {
240+ String out = getOutputPath (properties , OUT_SPARK_KEY );
241+ ExtentSparkReporter spark = new ExtentSparkReporter (out );
242+ attach (spark , properties , CONFIG_SPARK_KEY );
243+ }
244+
221245 private static void initTabular (Properties properties ) {
222246 String out = getOutputPath (properties , OUT_TABULAR_KEY );
223247 ExtentTabularReporter tabular = new ExtentTabularReporter (out );
224248 attach (tabular , properties , CONFIG_TABULAR_KEY );
225249 }
226-
250+
227251 private static void attach (ConfigurableReporter r , Properties properties , String configKey ) {
228- Object configPath = properties == null ? System .getProperty (configKey ) : properties .get (configKey );
252+ Object configPath = properties == null
253+ ? System .getProperty (configKey )
254+ : properties .get (configKey );
229255 if (configPath != null && !String .valueOf (configPath ).isEmpty ())
230256 r .loadXMLConfig (String .valueOf (configPath ));
231- INSTANCE .attachReporter ((( ExtentReporter ) r ) );
257+ INSTANCE .attachReporter (r );
232258 }
233259 }
234-
260+
235261}
0 commit comments