How to use subscribe method of net.serenitybdd.jbehave.embedders.monitors.CompositeEmbedderMonitor class

Best Serenity jBehave code snippet using net.serenitybdd.jbehave.embedders.monitors.CompositeEmbedderMonitor.subscribe

Run Serenity jBehave automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1package net.serenitybdd.jbehave.embedders.monitors;
2
3import org.jbehave.core.embedder.EmbedderControls;
4import org.jbehave.core.embedder.EmbedderMonitor;
5import org.jbehave.core.embedder.MetaFilter;
6import org.jbehave.core.failures.BatchFailures;
7import org.jbehave.core.model.*;
8import org.jbehave.core.reporters.ReportsCount;
9import org.slf4j.Logger;
10import org.slf4j.LoggerFactory;
11
12import java.io.File;
13import java.util.*;
14import java.util.concurrent.ConcurrentHashMap;
15import java.util.concurrent.ExecutorService;
16
17/**
18 * User: YamStranger
19 * Date: 3/24/16
20 * Time: 11:23 PM
21 */
22public class CompositeEmbedderMonitor implements EmbedderMonitor {
23
24    private static final Logger logger = LoggerFactory.getLogger(CompositeEmbedderMonitor.class);
25
26    private Set<EmbedderMonitor> monitors =
27            Collections.newSetFromMap(new ConcurrentHashMap<EmbedderMonitor, Boolean>());
28
29    public CompositeEmbedderMonitor(final EmbedderMonitor monitor, final EmbedderMonitor... monitors) {
30        this.monitors.add(monitor);
31        this.monitors.addAll(Arrays.asList(monitors));
32    }
33
34    public void subscribe(final EmbedderMonitor monitor, final EmbedderMonitor... monitors) {
35        this.monitors.add(monitor);
36        this.monitors.addAll(Arrays.asList(monitors));
37    }
38
39    public void unsubscribe(final EmbedderMonitor monitor, final EmbedderMonitor... monitors) {
40        this.monitors.remove(monitor);
41        this.monitors.removeAll(Arrays.asList(monitors));
42    }
43
44    @Override
45    public void runningEmbeddable(final String name) {
46        logger.debug("runningEmbeddable {}", name);
47        final LinkedList<Exception> exceptions = new LinkedList<>();
48        for (final EmbedderMonitor monitor : this.monitors) {
49            try {
50                monitor.runningEmbeddable(name);
51            } catch (final Exception suppressed) {
52                logger.error("exception during calling {}#runningEmbeddable", monitor.getClass(), suppressed);
53                exceptions.add(suppressed);
54            }
55        }
56        processSuppressed(exceptions);
57    }
58
59    @Override
60    public void embeddableFailed(final String name, final Throwable cause) {
61        logger.debug("embeddableFailed {}", name);
62        final LinkedList<Exception> exceptions = new LinkedList<>();
63        for (final EmbedderMonitor monitor : this.monitors) {
64            try {
65                monitor.embeddableFailed(name, cause);
66            } catch (final Exception suppressed) {
67                logger.error("exception during calling {}#embeddableFailed", monitor.getClass(), suppressed);
68                exceptions.add(suppressed);
69            }
70        }
71        processSuppressed(exceptions);
72    }
73
74    @Override
75    public void embeddableNotConfigurable(final String name) {
76        logger.debug("embeddableNotConfigurable {}", name);
77        final LinkedList<Exception> exceptions = new LinkedList<>();
78        for (final EmbedderMonitor monitor : this.monitors) {
79            try {
80                monitor.embeddableNotConfigurable(name);
81            } catch (final Exception suppressed) {
82                logger.error("exception during calling {}#embeddableNotConfigurable", monitor.getClass(), suppressed);
83                exceptions.add(suppressed);
84            }
85        }
86        processSuppressed(exceptions);
87    }
88
89    @Override
90    public void embeddablesSkipped(final List<String> classNames) {
91        logger.debug("embeddablesSkipped {}", classNames.size());
92        final LinkedList<Exception> exceptions = new LinkedList<>();
93        for (final EmbedderMonitor monitor : this.monitors) {
94            try {
95                monitor.embeddablesSkipped(classNames);
96            } catch (final Exception suppressed) {
97                logger.error("exception during calling {}#embeddablesSkipped", monitor.getClass(), suppressed);
98                exceptions.add(suppressed);
99            }
100        }
101        processSuppressed(exceptions);
102    }
103
104    @Override
105    public void metaNotAllowed(final Meta meta, final MetaFilter filter) {
106        logger.debug("metaNotAllowed {} {}", meta, filter);
107        final LinkedList<Exception> exceptions = new LinkedList<>();
108        for (final EmbedderMonitor monitor : this.monitors) {
109            try {
110                monitor.metaNotAllowed(meta, filter);
111            } catch (final Exception suppressed) {
112                logger.error("exception during calling {}#metaNotAllowed", monitor.getClass(), suppressed);
113                exceptions.add(suppressed);
114            }
115        }
116        processSuppressed(exceptions);
117    }
118
119    @Override
120    public void runningStory(final String path) {
121        logger.debug("runningStory {}", path);
122        final LinkedList<Exception> exceptions = new LinkedList<>();
123        for (final EmbedderMonitor monitor : this.monitors) {
124            try {
125                monitor.runningStory(path);
126            } catch (final Exception suppressed) {
127                logger.error("exception during calling {}#runningStory", monitor.getClass(), suppressed);
128                exceptions.add(suppressed);
129            }
130        }
131        processSuppressed(exceptions);
132    }
133
134    @Override
135    public void storyFailed(final String path, final Throwable cause) {
136        logger.debug("storyFailed {}", path);
137        final LinkedList<Exception> exceptions = new LinkedList<>();
138        for (final EmbedderMonitor monitor : this.monitors) {
139            try {
140                monitor.storyFailed(path, cause);
141            } catch (final Exception suppressed) {
142                logger.error("exception during calling {}#storyFailed", monitor.getClass(), suppressed);
143                exceptions.add(suppressed);
144            }
145        }
146        processSuppressed(exceptions);
147    }
148
149    @Override
150    public void storiesSkipped(final List<String> storyPaths) {
151        logger.debug("storiesSkipped{}", storyPaths);
152        final LinkedList<Exception> exceptions = new LinkedList<>();
153        for (final EmbedderMonitor monitor : this.monitors) {
154            try {
155                monitor.storiesSkipped(storyPaths);
156            } catch (final Exception suppressed) {
157                logger.error("exception during calling {}#storiesSkipped", monitor.getClass(), suppressed);
158                exceptions.add(suppressed);
159            }
160        }
161        processSuppressed(exceptions);
162    }
163
164    @Override
165    @Deprecated
166    public void storiesNotAllowed(final List<Story> notAllowed, final MetaFilter filter) {
167        logger.debug("storiesNotAllowed {} {}", notAllowed, filter);
168        final LinkedList<Exception> exceptions = new LinkedList<>();
169        for (final EmbedderMonitor monitor : this.monitors) {
170            try {
171                monitor.storiesNotAllowed(notAllowed, filter);
172            } catch (final Exception suppressed) {
173                logger.error("exception during calling {}#storiesNotAllowed", monitor.getClass(), suppressed);
174                exceptions.add(suppressed);
175            }
176        }
177        processSuppressed(exceptions);
178    }
179
180    @Override
181    @Deprecated
182    public void storiesNotAllowed(final List<Story> notAllowed, final MetaFilter filter, final boolean verbose) {
183        logger.debug("storiesNotAllowed {} {} {}", notAllowed, filter, verbose);
184        final LinkedList<Exception> exceptions = new LinkedList<>();
185        for (final EmbedderMonitor monitor : this.monitors) {
186            try {
187                monitor.storiesNotAllowed(notAllowed, filter, verbose);
188            } catch (final Exception suppressed) {
189                logger.error("exception during calling {}#storiesNotAllowed", monitor.getClass(), suppressed);
190                exceptions.add(suppressed);
191            }
192        }
193        processSuppressed(exceptions);
194    }
195
196    @Override
197    public void scenarioNotAllowed(final Scenario scenario, final MetaFilter filter) {
198        logger.debug("scenarioNotAllowed {} {}", scenario, filter);
199        final LinkedList<Exception> exceptions = new LinkedList<>();
200        for (final EmbedderMonitor monitor : this.monitors) {
201            try {
202                monitor.scenarioNotAllowed(scenario, filter);
203            } catch (final Exception suppressed) {
204                logger.error("exception during calling {}#scenarioNotAllowed", monitor.getClass(), suppressed);
205                exceptions.add(suppressed);
206            }
207        }
208        processSuppressed(exceptions);
209    }
210
211    @Override
212    public void batchFailed(final BatchFailures failures) {
213        logger.debug("batchFailed {}", failures);
214        final LinkedList<Exception> exceptions = new LinkedList<>();
215        for (final EmbedderMonitor monitor : this.monitors) {
216            try {
217                monitor.batchFailed(failures);
218            } catch (final Exception suppressed) {
219                logger.error("exception during calling {}#batchFailed", monitor.getClass(), suppressed);
220                exceptions.add(suppressed);
221            }
222        }
223        processSuppressed(exceptions);
224    }
225
226    @Override
227    public void beforeOrAfterStoriesFailed() {
228        logger.debug("beforeOrAfterStoriesFailed");
229        final LinkedList<Exception> exceptions = new LinkedList<>();
230        for (final EmbedderMonitor monitor : this.monitors) {
231            try {
232                monitor.beforeOrAfterStoriesFailed();
233            } catch (final Exception suppressed) {
234                logger.error("exception during calling {}#beforeOrAfterStoriesFailed", monitor.getClass(), suppressed);
235                exceptions.add(suppressed);
236            }
237        }
238        processSuppressed(exceptions);
239    }
240
241    @Override
242    public void generatingReportsView(final File outputDirectory, final List<String> formats,
243                                      final Properties viewProperties) {
244        logger.debug("generatingReportsView {} {} {}", outputDirectory, formats, viewProperties);
245        final LinkedList<Exception> exceptions = new LinkedList<>();
246        for (final EmbedderMonitor monitor : this.monitors) {
247            try {
248                monitor.generatingReportsView(outputDirectory, formats, viewProperties);
249            } catch (final Exception suppressed) {
250                logger.error("exception during calling {}#generatingReportsView", monitor.getClass(), suppressed);
251                exceptions.add(suppressed);
252            }
253        }
254        processSuppressed(exceptions);
255    }
256
257    @Override
258    public void reportsViewGenerationFailed(final File outputDirectory, final List<String> formats,
259                                            final Properties viewProperties, final Throwable cause) {
260        logger.debug("reportsViewGenerationFailed {} {} {} {}", outputDirectory, formats, viewProperties, cause);
261        final LinkedList<Exception> exceptions = new LinkedList<>();
262        for (final EmbedderMonitor monitor : this.monitors) {
263            try {
264                monitor.reportsViewGenerationFailed(outputDirectory, formats, viewProperties, cause);
265            } catch (final Exception suppressed) {
266                logger.error("exception during calling {}#reportsViewGenerationFailed", monitor.getClass(), suppressed);
267                exceptions.add(suppressed);
268            }
269        }
270        processSuppressed(exceptions);
271    }
272
273    @Override
274    public void reportsViewGenerated(final ReportsCount count) {
275        logger.debug("reportsViewGenerated {}", count);
276        final LinkedList<Exception> exceptions = new LinkedList<>();
277        for (final EmbedderMonitor monitor : this.monitors) {
278            try {
279                monitor.reportsViewGenerated(count);
280            } catch (final Exception suppressed) {
281                logger.error("exception during calling {}#reportsViewGenerated", monitor.getClass(), suppressed);
282                exceptions.add(suppressed);
283            }
284        }
285        processSuppressed(exceptions);
286    }
287
288    @Override
289    public void reportsViewFailures(final ReportsCount count) {
290        logger.debug("reportsViewFailures {}", count);
291        final LinkedList<Exception> exceptions = new LinkedList<>();
292        for (final EmbedderMonitor monitor : this.monitors) {
293            try {
294                monitor.reportsViewFailures(count);
295            } catch (final Exception suppressed) {
296                logger.error("exception during calling {}#reportsViewFailures", monitor.getClass(), suppressed);
297                exceptions.add(suppressed);
298            }
299        }
300        processSuppressed(exceptions);
301    }
302
303    @Override
304    public void reportsViewNotGenerated() {
305        logger.debug("reportsViewNotGenerated");
306        final LinkedList<Exception> exceptions = new LinkedList<>();
307        for (final EmbedderMonitor monitor : this.monitors) {
308            try {
309                monitor.reportsViewNotGenerated();
310            } catch (final Exception suppressed) {
311                logger.error("exception during calling {}#reportsViewNotGenerated", monitor.getClass(), suppressed);
312                exceptions.add(suppressed);
313            }
314        }
315        processSuppressed(exceptions);
316    }
317
318    @Override
319    public void runningWithAnnotatedEmbedderRunner(final String className) {
320        logger.debug("runningWithAnnotatedEmbedderRunner {}", className);
321        final LinkedList<Exception> exceptions = new LinkedList<>();
322        for (final EmbedderMonitor monitor : this.monitors) {
323            try {
324                monitor.runningWithAnnotatedEmbedderRunner(className);
325            } catch (final Exception suppressed) {
326                logger.error("exception during calling {}#runningWithAnnotatedEmbedderRunner", monitor.getClass(),
327                        suppressed);
328                exceptions.add(suppressed);
329            }
330        }
331        processSuppressed(exceptions);
332    }
333
334    @Override
335    public void annotatedInstanceNotOfType(final Object annotatedInstance, final Class<?> type) {
336        logger.debug("annotatedInstanceNotOfType {} {}", annotatedInstance, type);
337        final LinkedList<Exception> exceptions = new LinkedList<>();
338        for (final EmbedderMonitor monitor : this.monitors) {
339            try {
340                monitor.annotatedInstanceNotOfType(annotatedInstance, type);
341            } catch (final Exception suppressed) {
342                logger.error("exception during calling {}#annotatedInstanceNotOfType", monitor.getClass(), suppressed);
343                exceptions.add(suppressed);
344            }
345        }
346        processSuppressed(exceptions);
347    }
348
349    @Override
350    public void mappingStory(final String storyPath, final List<String> metaFilters) {
351        logger.debug("mappingStory {} {}", storyPath, metaFilters);
352        final LinkedList<Exception> exceptions = new LinkedList<>();
353        for (final EmbedderMonitor monitor : this.monitors) {
354            try {
355                monitor.mappingStory(storyPath, metaFilters);
356            } catch (final Exception suppressed) {
357                logger.error("exception during calling {}#mappingStory", monitor.getClass(), suppressed);
358                exceptions.add(suppressed);
359            }
360        }
361        processSuppressed(exceptions);
362    }
363
364    @Override
365    public void generatingMapsView(final File outputDirectory, final StoryMaps storyMaps,
366                                   final Properties viewProperties) {
367        logger.debug("generatingMapsView {} {}", outputDirectory, storyMaps);
368        final LinkedList<Exception> exceptions = new LinkedList<>();
369        for (final EmbedderMonitor monitor : this.monitors) {
370            try {
371                monitor.generatingMapsView(outputDirectory, storyMaps, viewProperties);
372            } catch (final Exception suppressed) {
373                logger.error("exception during calling {}#generatingMapsView", monitor.getClass(), suppressed);
374                exceptions.add(suppressed);
375            }
376        }
377        processSuppressed(exceptions);
378    }
379
380    @Override
381    public void mapsViewGenerationFailed(final File outputDirectory, final StoryMaps storyMaps,
382                                         final Properties viewProperties, final Throwable cause) {
383        logger.debug("mapsViewGenerationFailed {} {} {} {}", outputDirectory, storyMaps, viewProperties, cause);
384        final LinkedList<Exception> exceptions = new LinkedList<>();
385        for (final EmbedderMonitor monitor : this.monitors) {
386            try {
387                monitor.mapsViewGenerationFailed(outputDirectory, storyMaps, viewProperties, cause);
388            } catch (final Exception suppressed) {
389                logger.error("exception during calling {}#mapsViewGenerationFailed", monitor.getClass(), suppressed);
390                exceptions.add(suppressed);
391            }
392        }
393        processSuppressed(exceptions);
394    }
395
396    @Override
397    public void generatingNavigatorView(final File outputDirectory, final Properties viewResources) {
398        logger.debug("generatingNavigatorView {} {}", outputDirectory, viewResources);
399        final LinkedList<Exception> exceptions = new LinkedList<>();
400        for (final EmbedderMonitor monitor : this.monitors) {
401            try {
402                monitor.generatingNavigatorView(outputDirectory, viewResources);
403            } catch (final Exception suppressed) {
404                logger.error("exception during calling {}#generatingNavigatorView", monitor.getClass(), suppressed);
405                exceptions.add(suppressed);
406            }
407        }
408        processSuppressed(exceptions);
409    }
410
411    @Override
412    public void navigatorViewGenerationFailed(final File outputDirectory, final Properties viewResources,
413                                              final Throwable cause) {
414        logger.debug("navigatorViewGenerationFailed {} {} {}", outputDirectory, viewResources, cause);
415        final LinkedList<Exception> exceptions = new LinkedList<>();
416        for (final EmbedderMonitor monitor : this.monitors) {
417            try {
418                monitor.navigatorViewGenerationFailed(outputDirectory, viewResources, cause);
419            } catch (final Exception suppressed) {
420                logger.error("exception during calling {}#navigatorViewGenerationFailed", monitor.getClass(),
421                        suppressed);
422                exceptions.add(suppressed);
423            }
424        }
425        processSuppressed(exceptions);
426    }
427
428    @Override
429    public void navigatorViewNotGenerated() {
430        logger.debug("navigatorViewNotGenerated");
431        final LinkedList<Exception> exceptions = new LinkedList<>();
432        for (final EmbedderMonitor monitor : this.monitors) {
433            try {
434                monitor.navigatorViewNotGenerated();
435            } catch (final Exception suppressed) {
436                logger.error("exception during calling {}#navigatorViewNotGenerated", monitor.getClass(), suppressed);
437                exceptions.add(suppressed);
438            }
439        }
440        processSuppressed(exceptions);
441    }
442
443    @Override
444    public void processingSystemProperties(final Properties properties) {
445        logger.debug("processingSystemProperties {}", properties);
446        final LinkedList<Exception> exceptions = new LinkedList<>();
447        for (final EmbedderMonitor monitor : this.monitors) {
448            try {
449                monitor.processingSystemProperties(properties);
450            } catch (final Exception suppressed) {
451                logger.error("exception during calling {}#processingSystemProperties", monitor.getClass(), suppressed);
452                exceptions.add(suppressed);
453            }
454        }
455        processSuppressed(exceptions);
456    }
457
458    @Override
459    public void systemPropertySet(final String name, final String value) {
460        logger.debug("systemPropertySet {} {}", name, value);
461        final LinkedList<Exception> exceptions = new LinkedList<>();
462        for (final EmbedderMonitor monitor : this.monitors) {
463            try {
464                monitor.systemPropertySet(name, value);
465            } catch (final Exception suppressed) {
466                logger.error("exception during calling {}#systemPropertySet", monitor.getClass(), suppressed);
467                exceptions.add(suppressed);
468            }
469        }
470        processSuppressed(exceptions);
471    }
472
473    @Override
474    public void storyTimeout(final Story story, final StoryDuration storyDuration) {
475        logger.debug("storyTimeout {} {}", story, storyDuration);
476        final LinkedList<Exception> exceptions = new LinkedList<>();
477        for (final EmbedderMonitor monitor : this.monitors) {
478            try {
479                monitor.storyTimeout(story, storyDuration);
480            } catch (final Exception suppressed) {
481                logger.error("exception during calling {}#storyTimeout", monitor.getClass(), suppressed);
482                exceptions.add(suppressed);
483            }
484        }
485        processSuppressed(exceptions);
486    }
487
488    @Override
489    public void usingThreads(final int threads) {
490        logger.debug("usingThreads {}", threads);
491        final LinkedList<Exception> exceptions = new LinkedList<>();
492        for (final EmbedderMonitor monitor : this.monitors) {
493            try {
494                monitor.usingThreads(threads);
495            } catch (final Exception suppressed) {
496                logger.error("exception during calling {}#usingThreads", monitor.getClass(), suppressed);
497                exceptions.add(suppressed);
498            }
499        }
500        processSuppressed(exceptions);
501    }
502
503    @Override
504    public void usingExecutorService(final ExecutorService executorService) {
505        logger.debug("usingExecutorService {}", executorService);
506        final LinkedList<Exception> exceptions = new LinkedList<>();
507        for (final EmbedderMonitor monitor : this.monitors) {
508            try {
509                monitor.usingExecutorService(executorService);
510            } catch (final Exception suppressed) {
511                logger.error("exception during calling {}#usingExecutorService", monitor.getClass(), suppressed);
512                exceptions.add(suppressed);
513            }
514        }
515        processSuppressed(exceptions);
516    }
517
518    @Override
519    public void usingControls(final EmbedderControls embedderControls) {
520        logger.debug("usingControls{}", embedderControls);
521        final LinkedList<Exception> exceptions = new LinkedList<>();
522        for (final EmbedderMonitor monitor : this.monitors) {
523            try {
524                monitor.usingControls(embedderControls);
525            } catch (final Exception suppressed) {
526                logger.error("exception during calling {}#usingControls", monitor.getClass(), suppressed);
527                exceptions.add(suppressed);
528            }
529        }
530        processSuppressed(exceptions);
531    }
532
533    @Override
534    public void invalidTimeoutFormat(final String path) {
535        logger.debug("invalidTimeoutFormat {}", path);
536        final LinkedList<Exception> exceptions = new LinkedList<>();
537        for (final EmbedderMonitor monitor : this.monitors) {
538            try {
539                monitor.invalidTimeoutFormat(path);
540            } catch (final Exception suppressed) {
541                logger.error("exception during calling {}#invalidTimeoutFormat", monitor.getClass(), suppressed);
542                exceptions.add(suppressed);
543            }
544        }
545        processSuppressed(exceptions);
546    }
547
548    @Override
549    public void usingTimeout(final String path, final long timeout) {
550        logger.debug("usingTimeout {} {}", path, timeout);
551        final LinkedList<Exception> exceptions = new LinkedList<>();
552        for (final EmbedderMonitor monitor : this.monitors) {
553            try {
554                monitor.usingTimeout(path, timeout);
555            } catch (final Exception suppressed) {
556                logger.error("exception during calling {}#usingTimeout", monitor.getClass(), suppressed);
557                exceptions.add(suppressed);
558            }
559        }
560        processSuppressed(exceptions);
561    }
562
563    private void processSuppressed(final List<Exception> exceptions) {
564        if (exceptions.size() > 0) {
565            StringBuilder builder = new StringBuilder();
566            for (Exception suppressed : exceptions) {
567                builder.append("\"").append(suppressed.getMessage()).append("\" ; ");
568            }
569            final RuntimeException chained = new RuntimeException("Exceptions thrown with messages:"
570                    + builder.toString());
571            for (Exception suppressed : exceptions) {
572                chained.addSuppressed(suppressed);
573            }
574            throw chained;
575        }
576    }
577}
578
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)