Skip to content

Commit

Permalink
Merge pull request #1657 from schemacrawler/system-exit
Browse files Browse the repository at this point in the history
Throw an exception instead of System.exit
  • Loading branch information
sualeh authored Aug 14, 2024
2 parents cee32ec + 1b76093 commit c9950e0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,32 @@ public class CommandLineNegativeTest {
private static final String COMMAND_LINE_NEGATIVE_OUTPUT = "command_line_negative_output/";

@Test
public void commandLine_BadCommand(
public void mainBadCommand(
final TestContext testContext,
final DatabaseConnectionInfo connectionInfo,
final CapturedSystemStreams streams)
throws Exception {
final Map<String, String> argsMapOverride = new HashMap<>();
argsMapOverride.put("--command", "badcommand");

restoreSystemProperties(
() -> {
System.setProperty("SC_EXIT_WITH_EXCEPTION", "true");
assertThrows(
SystemExitException.class,
() -> run(testContext, argsMapOverride, connectionInfo, streams));
});
}

@Test
public void mainForceCatalogError(
final TestContext testContext,
final DatabaseConnectionInfo connectionInfo,
final CapturedSystemStreams streams)
throws Exception {

final Map<String, String> argsMapOverride = new HashMap<>();

restoreSystemProperties(
() -> {
System.setProperty(TestCatalogLoader.class.getName() + ".force-load-failure", "throw");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ public void testSqliteMainMissingDatabase(final CapturedSystemStreams streams) t
restoreSystemProperties(
() -> {
System.setProperty("SC_EXIT_WITH_EXCEPTION", "true");
assertThrows(
SystemExitException.class, () -> Main.main(flattenCommandlineArgs(argsMap)));
assertThrows(SystemExitException.class, () -> Main.main(flattenCommandlineArgs(argsMap)));
});

assertThat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import schemacrawler.tools.catalogloader.BaseCatalogLoader;
import schemacrawler.tools.executable.commandline.PluginCommand;
import us.fatehi.utility.SystemExitException;
import us.fatehi.utility.property.PropertyName;

public class TestCatalogLoader extends BaseCatalogLoader {
Expand Down Expand Up @@ -66,11 +67,10 @@ private void forceInstantiationFailureIfConfigured() {
}

private void forceLoadFailureIfConfigured() {
final String propertyValue =
System.getProperty(this.getClass().getName() + ".force-load-failure");
final String key = this.getClass().getName() + ".force-load-failure";
final String propertyValue = System.getProperty(key);
if (propertyValue != null) {
// Hard fail, do not throw, since exceptions are caught
System.exit(2);
throw new SystemExitException(2, key);
}
}
}

0 comments on commit c9950e0

Please sign in to comment.