Skip to content

Commit

Permalink
[#1420] fix ResourceBundle test
Browse files Browse the repository at this point in the history
  • Loading branch information
remkop committed Feb 24, 2023
1 parent 65b52d0 commit cc0602b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
4 changes: 4 additions & 0 deletions picocli-codegen-tests-java9plus/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ test {

// https://github.com/google/compile-testing/issues/222
jvmArgs('--add-opens', 'jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED')

testLogging {
//showStandardStreams = true
}
}

jar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import picocli.CommandLine;
import picocli.codegen.annotation.processing.AbstractCommandSpecProcessor;

import javax.tools.Diagnostic;
Expand Down Expand Up @@ -283,20 +284,27 @@ private void validateErrorMessages(Compilation compilation, List<String> expecte

@Test
public void testCommandWithBundleLoaded() {
CommandLine.tracer().setLevel(CommandLine.TraceLevel.DEBUG);
AbstractCommandSpecProcessor.setLoadResourceBundles(true);
Compilation compilation = compareCommandYamlDump(slurp("/picocli/examples/messages/CommandWithBundle.yaml"),
JavaFileObjects.forResource("picocli/examples/messages/CommandWithBundle.java"));

assertOnlySourceVersionWarning(compilation);
CommandLine.tracer().setLevel(CommandLine.TraceLevel.WARN);
}

@Test
public void testCommandWithBundleNotLoaded() {
CommandLine.tracer().setLevel(CommandLine.TraceLevel.DEBUG);
AbstractCommandSpecProcessor.setLoadResourceBundles(false);
Compilation compilation = compareCommandYamlDump(slurp("/picocli/examples/messages/CommandWithBundle-NotLoaded.yaml"),
JavaFileObjects.forResource("picocli/examples/messages/CommandWithBundle.java"));
try {
Compilation compilation = compareCommandYamlDump(slurp("/picocli/examples/messages/CommandWithBundle-NotLoaded.yaml"),
JavaFileObjects.forResource("picocli/examples/messages/CommandWithBundle2.java"));

assertOnlySourceVersionWarning(compilation);
assertOnlySourceVersionWarning(compilation);
} finally {
CommandLine.tracer().setLevel(CommandLine.TraceLevel.WARN);
}
}

private void assertOnlySourceVersionWarning(Compilation compilation) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package picocli.example.messages.CommandWithBundle;

import picocli.CommandLine.Command;

@Command(resourceBundle = "command-method-demo")
public class CommandWithBundle2 {
// we need a separate class, identical to CommandWithBundle, to avoid caching across tests
}
13 changes: 9 additions & 4 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -8414,10 +8414,12 @@ void updateFromCommand(Command cmd, CommandSpec commandSpec, boolean loadResourc
if (!empty(cmd.resourceBundle())) { // else preserve superclass bundle
if (loadResourceBundle) { // error if resource bundle does not exist
messages(new Messages(commandSpec, cmd.resourceBundle()));
} else {
ResourceBundle rb = null; // from annotation processor, we may not be able to load the ResourceBundle
try { rb = ResourceBundle.getBundle(cmd.resourceBundle()); } catch (MissingResourceException ignored) {}
messages(new Messages(commandSpec, cmd.resourceBundle(), rb));
} else { // from annotation processor, we may not be able to load the ResourceBundle
try {
messages(new Messages(commandSpec, cmd.resourceBundle()));
} catch (MissingResourceException ignored) {
messages(new Messages(commandSpec, cmd.resourceBundle(), null));
}
}
}
if (isNonDefault(cmd.abbreviateSynopsis(), DEFAULT_ABBREVIATE_SYNOPSIS)) {abbreviateSynopsis = cmd.abbreviateSynopsis();}
Expand Down Expand Up @@ -11622,8 +11624,10 @@ public Messages(CommandSpec spec, String baseName, ResourceBundle rb) {
}
private static ResourceBundle createBundle(String baseName) {
if (loadBundles) {
tracer().debug("Messages: Loading ResourceBundle[base=%s]...", baseName);
return ResourceBundle.getBundle(baseName);
} else {
tracer().debug("Messages: Returning new dummy ResourceBundle", loadBundles);
return new ResourceBundle() {
@Override
protected Object handleGetObject(String key) { return null; }
Expand Down Expand Up @@ -11668,6 +11672,7 @@ private static Set<String> keys(ResourceBundle rb) {
* @param loadBundles true if bundles should be loaded (default), false if bundles should not be loaded
*/
public static final void setLoadBundles(boolean loadBundles) {
tracer().debug("Messages: loadBundles was set to %s", loadBundles);
Messages.loadBundles = loadBundles;
}

Expand Down
3 changes: 3 additions & 0 deletions src/test/java/picocli/I18nTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package picocli;

import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.ProvideSystemProperty;
Expand Down Expand Up @@ -774,6 +775,7 @@ public void testTracingWithResourceBundle() {

String expected = String.format("" +
"[picocli DEBUG] Creating CommandSpec for picocli.I18nCommand@6bcae9 with factory picocli.CommandLine$DefaultFactory%n" +
"[picocli DEBUG] Messages: Loading ResourceBundle[base=picocli.SharedMessages]...%n" +
"[picocli DEBUG] Created Messages from resourceBundle[base=picocli.SharedMessages] for command 'i18n-top' (command 'i18n-top' (user object: picocli.I18nCommand@6bcae9))%n" +
"[picocli DEBUG] Creating CommandSpec for class picocli.CommandLine$HelpCommand with factory picocli.CommandLine$DefaultFactory%n" +
"[picocli DEBUG] Adding subcommand 'help' to 'i18n-top'%n" +
Expand All @@ -786,6 +788,7 @@ public void testTracingWithResourceBundle() {
"[picocli DEBUG] Created Messages from resourceBundle[base=picocli.SharedMessages] for command 'i18n-sub' (command 'i18n-sub' (user object: class picocli.I18nSubcommand))%n" +
"[picocli DEBUG] Created Messages from resourceBundle[base=picocli.SharedMessages] for command 'help' (command 'help' (user object: class picocli.CommandLine$HelpCommand))%n" +
"[picocli DEBUG] Creating CommandSpec for class picocli.I18nSubcommand2 with factory picocli.CommandLine$DefaultFactory%n" +
"[picocli DEBUG] Messages: Loading ResourceBundle[base=picocli.ResourceBundlePropagationTest]...%n" +
"[picocli DEBUG] Created Messages from resourceBundle[base=picocli.ResourceBundlePropagationTest] for command 'sub2' (command 'sub2' (user object: class picocli.I18nSubcommand2))%n" +
"[picocli DEBUG] Creating CommandSpec for class picocli.CommandLine$HelpCommand with factory picocli.CommandLine$DefaultFactory%n" +
"[picocli DEBUG] Adding subcommand 'help' to 'sub2'%n" +
Expand Down

0 comments on commit cc0602b

Please sign in to comment.