OakMachine { static Consumer<ProgressCheck> newProgressCheckEventConsumer(final boolean silenced, final @NotNull Fun.ThrowingConsumer<ProgressCheck> checkVisitor, final @NotNull BiConsumer<ProgressCheck, Exception> onError) { Consumer<ProgressCheck> consumer = check -> { try { if (silenced && check instanceof SilenceableCheck) { ((SilenceableCheck) check).setSilenced(silenced); } if (!silenced || check instanceof SilenceableCheck) { checkVisitor.tryAccept(check); } } catch (final Exception e) { if (!silenced) { onError.accept(check, e); } } }; return consumer.andThen(check -> { if (silenced && check instanceof SilenceableCheck) { ((SilenceableCheck) check).setSilenced(false); } }); } private  OakMachine(final Packaging packagingService,
                       final List<ProgressCheck> progressChecks,
                       final ErrorListener errorListener,
                       final List<URL> preInstallUrls,
                       final List<InitStage> initStages,
                       final JcrCustomizer jcrCustomizer,
                       final InstallHookProcessorFactory installHookProcessorFactory,
                       final ClassLoader installHookClassLoader,
                       final boolean enablePreInstallHooks,
                       final InstallHookPolicy scanInstallHookPolicy,
                       final Supplier<NodeStore> nodeStoreSupplier,
                       final SubpackageSilencer subpackageSilencer,
                       final RepoInitProcessor repoInitProcessor,
                       final SlingSimulatorBackend slingSimulator,
                       final Set<String> runModes); List<ProgressCheck> getProgressChecks(); ErrorListener getErrorListener(); @Deprecated List<File> getPreInstallFiles(); List<URL> getPreInstallUrls(); @SuppressWarnings("WeakerAccess") void initAndInspect(final InspectBody<E> inspectBody); void adminInitAndInspect(final InspectBody<E> inspectBody); List<CheckReport> scanPackage(final @NotNull File... file); List<CheckReport> scanPackages(final List<File> files); static Packaging newOakpalPackagingService(); static Packaging newOakpalPackagingService(final @NotNull ClassLoader classLoader); static final String NS_URI_OAKPAL; static final String NS_PREFIX_OAKPAL; static final String LN_UNDECLARED; static final String NT_UNDECLARED; }
@Test public void testNewProgressCheckEventConsumer() { final List<Map.Entry<ProgressCheck, Exception>> errorEvents = new ArrayList<>(); final BiConsumer<ProgressCheck, Exception> onError = (check, error) -> errorEvents.add(toEntry(check, error)); final Consumer<ProgressCheck> silencedConsumer = OakMachine.newProgressCheckEventConsumer(true, ProgressCheck::finishedScan, onError); final Consumer<ProgressCheck> vocalConsumer = OakMachine.newProgressCheckEventConsumer(false, ProgressCheck::finishedScan, onError); final AtomicInteger normalCallCount = new AtomicInteger(0); final ProgressCheck normalCheck = mock(ProgressCheck.class); doAnswer(call -> normalCallCount.incrementAndGet()).when(normalCheck).finishedScan(); silencedConsumer.accept(normalCheck); assertEquals("expect normal:silent empty errors", 0, errorEvents.size()); assertEquals("expect normal:silent call count", 0, normalCallCount.get()); errorEvents.clear(); normalCallCount.set(0); vocalConsumer.accept(normalCheck); assertEquals("expect normal:vocal empty errors", 0, errorEvents.size()); assertEquals("expect normal:vocal call count", 1, normalCallCount.get()); errorEvents.clear(); normalCallCount.set(0); doThrow(RuntimeException.class).when(normalCheck).finishedScan(); silencedConsumer.accept(normalCheck); assertEquals("expect normal:silent empty errors", 0, errorEvents.size()); assertEquals("expect normal:silent call count", 0, normalCallCount.get()); errorEvents.clear(); normalCallCount.set(0); vocalConsumer.accept(normalCheck); assertEquals("expect normal:vocal one error", 1, errorEvents.size()); assertEquals("expect normal:vocal call count", 0, normalCallCount.get()); errorEvents.clear(); normalCallCount.set(0); final AtomicInteger watcherCallCount = new AtomicInteger(0); final List<Boolean> silencedEvents = new ArrayList<>(); final SilenceableCheck silenceableCheck = mock(SilenceableCheck.class); doAnswer(call -> silencedEvents.add(call.getArgument(0))) .when(silenceableCheck).setSilenced(anyBoolean()); doAnswer(call -> watcherCallCount.incrementAndGet()).when(silenceableCheck).finishedScan(); silencedConsumer.accept(silenceableCheck); assertEquals("expect watcher:silent empty errors", 0, errorEvents.size()); assertEquals("expect watcher:silent call count", 1, watcherCallCount.get()); assertEquals("expect watcher:silent silenced events", Arrays.asList(true, false), silencedEvents); errorEvents.clear(); watcherCallCount.set(0); silencedEvents.clear(); vocalConsumer.accept(silenceableCheck); assertEquals("expect watcher:vocal empty errors", 0, errorEvents.size()); assertEquals("expect watcher:vocal call count", 1, watcherCallCount.get()); assertEquals("expect watcher:vocal silenced events", Collections.emptyList(), silencedEvents); errorEvents.clear(); watcherCallCount.set(0); silencedEvents.clear(); doThrow(RuntimeException.class).when(silenceableCheck).finishedScan(); silencedConsumer.accept(silenceableCheck); assertEquals("expect watcher:silent empty errors", 0, errorEvents.size()); assertEquals("expect watcher:silent call count", 0, watcherCallCount.get()); assertEquals("expect watcher:silent silenced events", Arrays.asList(true, false), silencedEvents); errorEvents.clear(); watcherCallCount.set(0); silencedEvents.clear(); vocalConsumer.accept(silenceableCheck); assertEquals("expect watcher:vocal one error", 1, errorEvents.size()); assertEquals("expect watcher:vocal call count", 0, watcherCallCount.get()); assertEquals("expect watcher:vocal silenced events", Collections.emptyList(), silencedEvents); }