On Tue, 2 Jul 2024 15:21:49 GMT, Sonia Zaldana Calles <szald...@openjdk.org> wrote:
>> Hi all, >> >> This PR addresses [8332124](https://bugs.openjdk.org/browse/JDK-8332124) >> enabling jcmd to accept "help" as an argument to subcommands. >> >> Testing: >> - [x] Verified running `jcmd 4711 VM.metaspace help` works along with other >> subcommands. >> - [x] Added test case passes. >> >> Thanks, >> Sonia > > Sonia Zaldana Calles has updated the pull request incrementally with one > additional commit since the last revision: > > Making enabling help more restrictive. Will not accept -help Additional test possibility. *** orig.txt 2024-07-03 13:08:46.460514793 +0100 --- test/jdk/sun/tools/jcmd/TestJcmdSubcommandHelp.java 2024-07-03 12:50:48.324022729 +0100 *************** *** 39,48 **** --- 39,49 ---- public class TestJcmdSubcommandHelp { private static final String HELP_ONE_DASH = "-h"; private static final String HELP_TWO_DASH = "--help"; private static final String CMD = "VM.metaspace"; + private static final String ILLEGAL = "IllegalArgumentException: Unknown argument"; public static void main(String[] args) throws Exception { // Sanity check with empty input OutputAnalyzer output = JcmdBase.jcmd(); *************** *** 59,68 **** --- 60,72 ---- testIgnoreAdditionalArgs(HELP_ONE_DASH, expectedOutput); testIgnoreAdditionalArgs(HELP_TWO_DASH, expectedOutput); testIgnoreTrailingSpaces(HELP_ONE_DASH, expectedOutput); testIgnoreTrailingSpaces(HELP_TWO_DASH, expectedOutput); + + testSimilarCommand(HELP_ONE_DASH + "ello", ILLEGAL); + testSimilarCommand(HELP_TWO_DASH + "me", ILLEGAL); } private static void testExpectedUsage(String helpOption, String expectedOutput) throws Exception { verifyOutput(new String[] {CMD, helpOption}, expectedOutput, "Expected jcmd to accept '%s' suboption as a command argument and issue the same help output.".formatted(helpOption)); *************** *** 76,93 **** --- 80,114 ---- private static void testIgnoreTrailingSpaces(String helpOption, String expectedOutput) throws Exception { verifyOutput(new String[] {CMD, "%s ".formatted(helpOption)}, expectedOutput, "Expected jcmd to accept '%s' suboption with trailing spaces".formatted(helpOption)); } + private static void testSimilarCommand(String helpOption, String expectedOutput) throws Exception { + verifyOutputContains(new String[] {CMD, helpOption}, expectedOutput, + "Expected jcmd to NOT accept '%s' suboption with trailing content".formatted(helpOption)); + } + private static void verifyOutput(String[] args, String expectedOutput, String errorMessage) throws Exception { OutputAnalyzer output = JcmdBase.jcmd(args); String issuedOutput = output.getOutput(); if (!expectedOutput.equals(issuedOutput)) { System.out.println("Expected output: "); System.out.println(expectedOutput); System.out.println("Issued output: "); System.out.println(issuedOutput); throw new Exception(errorMessage); + } + } + + private static void verifyOutputContains(String[] args, String expectedOutput, String errorMessage) throws Exception { + OutputAnalyzer output = JcmdBase.jcmd(args); + String issuedOutput = output.getOutput(); + if (!issuedOutput.contains(expectedOutput)) { + System.out.println("Expected output: "); + System.out.println(expectedOutput); + System.out.println("Issued output: "); + System.out.println(issuedOutput); + throw new Exception(errorMessage); } } } ------------- PR Comment: https://git.openjdk.org/jdk/pull/19776#issuecomment-2205934378