@Test public void testMessagesGetStringNullKey() { String def = "abc"; assertSame(def, new Messages(CommandSpec.create(), (ResourceBundle) null).getString(null, def)); assertSame(def, new Messages(CommandSpec.create(), (ResourceBundle) null).getString("help", def)); ResourceBundle rb = ResourceBundle.getBundle("picocli.SharedMessages"); assertSame(def, new Messages(CommandSpec.create(), rb).getString(null, def)); assertNotEquals(def, new Messages(CommandSpec.create(), rb).getString("help", def)); }
@Test public void testMessagesGetStringArrayNullKey() { String[] def = {"abc"}; assertSame(def, new Messages(CommandSpec.create(), (ResourceBundle) null).getStringArray(null, def)); assertSame(def, new Messages(CommandSpec.create(), (ResourceBundle) null).getStringArray("help", def)); ResourceBundle rb = ResourceBundle.getBundle("picocli.SharedMessages"); assertSame(def, new Messages(CommandSpec.create(), rb).getStringArray(null, def)); assertNotEquals(def, new Messages(CommandSpec.create(), rb).getStringArray("usage.description", def)); } }
@Test public void testMessagesGetStringArrayNullKey() { String[] def = {"abc"}; assertSame(def, new Messages(CommandSpec.create(), null).getStringArray(null, def)); assertSame(def, new Messages(CommandSpec.create(), null).getStringArray("help", def)); ResourceBundle rb = ResourceBundle.getBundle("picocli.SharedMessages"); assertSame(def, new Messages(CommandSpec.create(), rb).getStringArray(null, def)); assertNotEquals(def, new Messages(CommandSpec.create(), rb).getStringArray("usage.description", def)); } }
void updateFromCommand(Command cmd, CommandSpec commandSpec) { if (isNonDefault(cmd.synopsisHeading(), DEFAULT_SYNOPSIS_HEADING)) {synopsisHeading = cmd.synopsisHeading();} if (isNonDefault(cmd.commandListHeading(), DEFAULT_COMMAND_LIST_HEADING)) {commandListHeading = cmd.commandListHeading();} if (isNonDefault(cmd.requiredOptionMarker(), DEFAULT_REQUIRED_OPTION_MARKER)) {requiredOptionMarker = cmd.requiredOptionMarker();} if (isNonDefault(cmd.abbreviateSynopsis(), DEFAULT_ABBREVIATE_SYNOPSIS)) {abbreviateSynopsis = cmd.abbreviateSynopsis();} if (isNonDefault(cmd.sortOptions(), DEFAULT_SORT_OPTIONS)) {sortOptions = cmd.sortOptions();} if (isNonDefault(cmd.showDefaultValues(), DEFAULT_SHOW_DEFAULT_VALUES)) {showDefaultValues = cmd.showDefaultValues();} if (isNonDefault(cmd.hidden(), DEFAULT_HIDDEN)) {hidden = cmd.hidden();} if (isNonDefault(cmd.customSynopsis(), DEFAULT_MULTI_LINE)) {customSynopsis = cmd.customSynopsis().clone();} if (isNonDefault(cmd.description(), DEFAULT_MULTI_LINE)) {description = cmd.description().clone();} if (isNonDefault(cmd.descriptionHeading(), DEFAULT_SINGLE_VALUE)) {descriptionHeading = cmd.descriptionHeading();} if (isNonDefault(cmd.header(), DEFAULT_MULTI_LINE)) {header = cmd.header().clone();} if (isNonDefault(cmd.headerHeading(), DEFAULT_SINGLE_VALUE)) {headerHeading = cmd.headerHeading();} if (isNonDefault(cmd.footer(), DEFAULT_MULTI_LINE)) {footer = cmd.footer().clone();} if (isNonDefault(cmd.footerHeading(), DEFAULT_SINGLE_VALUE)) {footerHeading = cmd.footerHeading();} if (isNonDefault(cmd.parameterListHeading(), DEFAULT_SINGLE_VALUE)) {parameterListHeading = cmd.parameterListHeading();} if (isNonDefault(cmd.optionListHeading(), DEFAULT_SINGLE_VALUE)) {optionListHeading = cmd.optionListHeading();} if (isNonDefault(cmd.usageHelpWidth(), DEFAULT_USAGE_WIDTH)) {width(cmd.usageHelpWidth());} // validate ResourceBundle rb = empty(cmd.resourceBundle()) ? null : ResourceBundle.getBundle(cmd.resourceBundle()); if (rb != null) { messages(new Messages(commandSpec, rb)); } // else preserve superclass bundle } void initFromMixin(UsageMessageSpec mixin, CommandSpec commandSpec) {
/** Initializes the resource bundle for this command: sets the {@link UsageMessageSpec#messages(Messages) UsageMessageSpec.messages} to * a {@link Messages Messages} object created from this command spec and the specified bundle, and then sets the * {@link ArgSpec#messages(Messages) ArgSpec.messages} of all options and positional parameters in this command * to the same {@code Messages} instance. Subcommands are not modified. * @param bundle the ResourceBundle to set, may be {@code null} * @return this commandSpec * @see #addSubcommand(String, CommandLine) * @since 3.6 */ public CommandSpec resourceBundle(ResourceBundle bundle) { usageMessage().messages(new Messages(this, bundle)); updateArgSpecMessages(); return this; } private void updateArgSpecMessages() {
@Test public void testMessagesGetStringNullKey() { String def = "abc"; assertSame(def, new Messages(CommandSpec.create(), null).getString(null, def)); assertSame(def, new Messages(CommandSpec.create(), null).getString("help", def)); ResourceBundle rb = ResourceBundle.getBundle("picocli.SharedMessages"); assertSame(def, new Messages(CommandSpec.create(), rb).getString(null, def)); assertNotEquals(def, new Messages(CommandSpec.create(), rb).getString("help", def)); }
/** Returns a copy of the specified Messages object with the CommandSpec replaced by the specified one. * @param spec the CommandSpec of the returned Messages * @param original the Messages object whose ResourceBundle to reference * @return a Messages object with the specified CommandSpec and the ResourceBundle of the specified Messages object */ public static Messages copy(CommandSpec spec, Messages original) { return original == null ? null : new Messages(spec, original.rb); } /** Returns {@code true} if the specified {@code Messages} is {@code null} or has a {@code null ResourceBundle}. */
@Test public void testMessagesCommandSpec() { CommandSpec spec = CommandSpec.create(); Messages orig = new Messages(spec, null); assertSame(spec, orig.commandSpec()); }
@Test public void testMessagesCopyNonNull() { Messages orig = new Messages(CommandSpec.create(), null); Messages copy = Messages.copy(CommandSpec.create(), orig); assertNull(copy.resourceBundle()); }
@Test public void testMessagesEmpty() { assertTrue(Messages.empty((Messages) null)); assertTrue(Messages.empty(new Messages(CommandSpec.create(), null))); }
void updateFromCommand(Command cmd, CommandSpec commandSpec) { if (isNonDefault(cmd.synopsisHeading(), DEFAULT_SYNOPSIS_HEADING)) {synopsisHeading = cmd.synopsisHeading();} if (isNonDefault(cmd.commandListHeading(), DEFAULT_COMMAND_LIST_HEADING)) {commandListHeading = cmd.commandListHeading();} if (isNonDefault(cmd.requiredOptionMarker(), DEFAULT_REQUIRED_OPTION_MARKER)) {requiredOptionMarker = cmd.requiredOptionMarker();} if (isNonDefault(cmd.abbreviateSynopsis(), DEFAULT_ABBREVIATE_SYNOPSIS)) {abbreviateSynopsis = cmd.abbreviateSynopsis();} if (isNonDefault(cmd.sortOptions(), DEFAULT_SORT_OPTIONS)) {sortOptions = cmd.sortOptions();} if (isNonDefault(cmd.showDefaultValues(), DEFAULT_SHOW_DEFAULT_VALUES)) {showDefaultValues = cmd.showDefaultValues();} if (isNonDefault(cmd.hidden(), DEFAULT_HIDDEN)) {hidden = cmd.hidden();} if (isNonDefault(cmd.customSynopsis(), DEFAULT_MULTI_LINE)) {customSynopsis = cmd.customSynopsis().clone();} if (isNonDefault(cmd.description(), DEFAULT_MULTI_LINE)) {description = cmd.description().clone();} if (isNonDefault(cmd.descriptionHeading(), DEFAULT_SINGLE_VALUE)) {descriptionHeading = cmd.descriptionHeading();} if (isNonDefault(cmd.header(), DEFAULT_MULTI_LINE)) {header = cmd.header().clone();} if (isNonDefault(cmd.headerHeading(), DEFAULT_SINGLE_VALUE)) {headerHeading = cmd.headerHeading();} if (isNonDefault(cmd.footer(), DEFAULT_MULTI_LINE)) {footer = cmd.footer().clone();} if (isNonDefault(cmd.footerHeading(), DEFAULT_SINGLE_VALUE)) {footerHeading = cmd.footerHeading();} if (isNonDefault(cmd.parameterListHeading(), DEFAULT_SINGLE_VALUE)) {parameterListHeading = cmd.parameterListHeading();} if (isNonDefault(cmd.optionListHeading(), DEFAULT_SINGLE_VALUE)) {optionListHeading = cmd.optionListHeading();} if (isNonDefault(cmd.usageHelpWidth(), DEFAULT_USAGE_WIDTH)) {width(cmd.usageHelpWidth());} // validate if (!empty(cmd.resourceBundle())) { // else preserve superclass bundle messages(new Messages(commandSpec, cmd.resourceBundle())); } } void initFromMixin(UsageMessageSpec mixin, CommandSpec commandSpec) {
@Test public void testMessagesCopyNonNull() { Messages orig = new Messages(CommandSpec.create(), (ResourceBundle) null); Messages copy = Messages.copy(CommandSpec.create(), orig); assertNull(copy.resourceBundle()); }
/** Returns a copy of the specified Messages object with the CommandSpec replaced by the specified one. * @param spec the CommandSpec of the returned Messages * @param original the Messages object whose ResourceBundle to reference * @return a Messages object with the specified CommandSpec and the ResourceBundle of the specified Messages object */ public static Messages copy(CommandSpec spec, Messages original) { return original == null ? null : new Messages(spec, original.bundleBaseName, original.rb); } /** Returns {@code true} if the specified {@code Messages} is {@code null} or has a {@code null ResourceBundle}. */
@Test public void testMessagesCommandSpec() { CommandSpec spec = CommandSpec.create(); Messages orig = new Messages(spec, (ResourceBundle) null); assertSame(spec, orig.commandSpec()); }
private void setBundle(String bundleBaseName, ResourceBundle bundle) { usageMessage().messages(new Messages(this, bundleBaseName, bundle)); updateArgSpecMessages(); } private void updateArgSpecMessages() {
@Test public void testMessagesEmpty() { assertTrue(Messages.empty((Messages) null)); assertTrue(Messages.empty(new Messages(CommandSpec.create(), (ResourceBundle) null))); }