@Test
public void testRunWithLog() throws Exception {

    try (ByteArrayOutputStream bout = new ByteArrayOutputStream();
         ByteArrayOutputStream berr = new ByteArrayOutputStream();
         PrintStream out = new PrintStream(bout);
         PrintStream err = new PrintStream(berr);
         ComputationManager cm = mock(ComputationManager.class)) {
        CommandLine cl = mockCommandLine(ImmutableMap.of("case-file", "network.xml",
                "log-file", "out.zip"), ImmutableSet.of("skip-postproc"));
        ToolRunningContext context = new ToolRunningContext(out, err, fileSystem, cm, cm);
        SecurityAnalysisFactory saFactory = new SecurityAnalysisMockFactory();
        SecurityAnalysis sa = saFactory.create(null, cm, 1);
        SecurityAnalysisExecutionBuilder builder = new SecurityAnalysisExecutionBuilder(ExternalSecurityAnalysisConfig::new,
            () -> saFactory,
            executionInput -> new SecurityAnalysisInput(executionInput.getNetworkVariant()));
        // execute
        tool.run(cl, context, builder,
                SecurityAnalysisParameters::new,
                new ImportersLoaderList(new NetworkImporterMock()),
                TableFormatterConfig::new);
        // verify that runWithLog() called instead of run();
        verify(sa, never()).run(any(), any(), any());
        verify(sa, times(1)).runWithLog(any(), any(), any());
        when(cl.hasOption("log-file")).thenReturn(false);
        // execute
        tool.run(cl, context, builder,
                SecurityAnalysisParameters::new,
                new ImportersLoaderList(new NetworkImporterMock()),
                TableFormatterConfig::new);
        verify(sa, times(1)).run(any(), any(), any());
        // exception happens
        SecurityAnalysisFactory saFactory2 = new SecurityAnalysisMockFactory(true);
        SecurityAnalysisExecutionBuilder builder2 = new SecurityAnalysisExecutionBuilder(ExternalSecurityAnalysisConfig::new,
            () -> saFactory2,
            executionInput -> new SecurityAnalysisInput(executionInput.getNetworkVariant()));
        try {
            tool.run(cl, context, builder2,
                    SecurityAnalysisParameters::new,
                    new ImportersLoaderList(new NetworkImporterMock()),
                    TableFormatterConfig::new);
            fail();
        } catch (CompletionException exception) {
            assertTrue(exception.getCause() instanceof ComputationException);
        }
    }
}