From 694271e5382183be73a3feea6244b13ab7c2c380 Mon Sep 17 00:00:00 2001 From: Arthit Suriyawongkul Date: Mon, 20 Jul 2026 03:52:22 +0100 Subject: [PATCH 1/2] Exit code for MatchingStandardLicenses Signed-off-by: Arthit Suriyawongkul --- .../spdx/tools/MatchingStandardLicenses.java | 35 ++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/spdx/tools/MatchingStandardLicenses.java b/src/main/java/org/spdx/tools/MatchingStandardLicenses.java index 272d95b..df298b6 100644 --- a/src/main/java/org/spdx/tools/MatchingStandardLicenses.java +++ b/src/main/java/org/spdx/tools/MatchingStandardLicenses.java @@ -31,6 +31,13 @@ * Tool to compare a license text to standard licenses. Lists all standard * license ID's that are equivalent using the SPDX Legal team's license matching * guidelines (http://spdx.org/spdx-license-list/matching-guidelines) + *
+ * Exit codes: + * * @author Gary O'Neall */ public class MatchingStandardLicenses { @@ -44,15 +51,27 @@ private MatchingStandardLicenses() { static int MIN_ARGS = 1; static int MAX_ARGS = 1; - static final int ERROR_STATUS = 1; + /** + * Main entry point for the MatchingStandardLicenses tool. + * Delegates to {@link #run(String[])} and terminates the JVM with its exit status. * @param args */ public static void main(String[] args) { + System.exit(run(args)); + } + + /** + * Runs the MatchingStandardLicenses command logic and reports results to + * standard out, without terminating the JVM - allows the logic to be unit tested. + * @param args + * @return process exit status, see {@link ExitCode} + */ + static int run(String[] args) { if (args == null || args.length < MIN_ARGS || args.length > MAX_ARGS) { System.out.println("Invalid arguments"); usage(); - System.exit(ERROR_STATUS); + return ExitCode.USAGE_ERROR; } @SuppressWarnings("null") File textFile = new File(args[0]); @@ -60,16 +79,16 @@ public static void main(String[] args) { if (!textFile.exists()) { System.out.println("Text file "+textFile.getName()+" does not exist"); usage(); - System.exit(ERROR_STATUS); + return ExitCode.ERROR; } - + SpdxToolsHelper.initialize(); String licenseText = null; try { licenseText = readAll(textFile); } catch (IOException e) { System.out.println("Error reading file: "+e.getMessage()); - System.exit(ERROR_STATUS); + return ExitCode.ERROR; } String[] matchingLicenseIds = null; @@ -77,10 +96,10 @@ public static void main(String[] args) { matchingLicenseIds = LicenseCompareHelper.matchingStandardLicenseIds(licenseText); } catch (InvalidSPDXAnalysisException e) { System.out.println("Error reading standard licenses: "+e.getMessage()); - System.exit(ERROR_STATUS); + return ExitCode.ERROR; } catch (SpdxCompareException e) { System.out.println("Error comparing licenses: "+e.getMessage()); - System.exit(ERROR_STATUS); + return ExitCode.ERROR; } if (matchingLicenseIds == null || matchingLicenseIds.length == 0) { @@ -94,7 +113,7 @@ public static void main(String[] args) { } System.out.println(sb.toString()); } - System.exit(0); + return ExitCode.SUCCESS; } /** From 9e5f4318af064a62c4444457874e081672090dab Mon Sep 17 00:00:00 2001 From: Arthit Suriyawongkul Date: Mon, 20 Jul 2026 03:58:16 +0100 Subject: [PATCH 2/2] Handle null argument Signed-off-by: Arthit Suriyawongkul --- .../java/org/spdx/tools/MatchingStandardLicenses.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/spdx/tools/MatchingStandardLicenses.java b/src/main/java/org/spdx/tools/MatchingStandardLicenses.java index df298b6..58a097c 100644 --- a/src/main/java/org/spdx/tools/MatchingStandardLicenses.java +++ b/src/main/java/org/spdx/tools/MatchingStandardLicenses.java @@ -63,7 +63,7 @@ public static void main(String[] args) { /** * Runs the MatchingStandardLicenses command logic and reports results to - * standard out, without terminating the JVM - allows the logic to be unit tested. + * standard out. * @param args * @return process exit status, see {@link ExitCode} */ @@ -73,8 +73,13 @@ static int run(String[] args) { usage(); return ExitCode.USAGE_ERROR; } - @SuppressWarnings("null") - File textFile = new File(args[0]); + String textFilePath = args[0]; + if (textFilePath == null) { + System.out.println("Invalid arguments"); + usage(); + return ExitCode.USAGE_ERROR; + } + File textFile = new File(textFilePath); if (!textFile.exists()) { System.out.println("Text file "+textFile.getName()+" does not exist");