Skip to content

Commit cc0602b

Browse files
committed
[#1420] fix ResourceBundle test
1 parent 65b52d0 commit cc0602b

File tree

5 files changed

+35
-7
lines changed

5 files changed

+35
-7
lines changed

picocli-codegen-tests-java9plus/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ test {
3131

3232
// https://github.com/google/compile-testing/issues/222
3333
jvmArgs('--add-opens', 'jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED')
34+
35+
testLogging {
36+
//showStandardStreams = true
37+
}
3438
}
3539

3640
jar {

picocli-codegen-tests-java9plus/src/test/java/picocli/annotation/processing/tests/AbstractCommandSpecProcessorTest.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.junit.AfterClass;
88
import org.junit.BeforeClass;
99
import org.junit.Test;
10+
import picocli.CommandLine;
1011
import picocli.codegen.annotation.processing.AbstractCommandSpecProcessor;
1112

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

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

290292
assertOnlySourceVersionWarning(compilation);
293+
CommandLine.tracer().setLevel(CommandLine.TraceLevel.WARN);
291294
}
292295

293296
@Test
294297
public void testCommandWithBundleNotLoaded() {
298+
CommandLine.tracer().setLevel(CommandLine.TraceLevel.DEBUG);
295299
AbstractCommandSpecProcessor.setLoadResourceBundles(false);
296-
Compilation compilation = compareCommandYamlDump(slurp("/picocli/examples/messages/CommandWithBundle-NotLoaded.yaml"),
297-
JavaFileObjects.forResource("picocli/examples/messages/CommandWithBundle.java"));
300+
try {
301+
Compilation compilation = compareCommandYamlDump(slurp("/picocli/examples/messages/CommandWithBundle-NotLoaded.yaml"),
302+
JavaFileObjects.forResource("picocli/examples/messages/CommandWithBundle2.java"));
298303

299-
assertOnlySourceVersionWarning(compilation);
304+
assertOnlySourceVersionWarning(compilation);
305+
} finally {
306+
CommandLine.tracer().setLevel(CommandLine.TraceLevel.WARN);
307+
}
300308
}
301309

302310
private void assertOnlySourceVersionWarning(Compilation compilation) {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package picocli.example.messages.CommandWithBundle;
2+
3+
import picocli.CommandLine.Command;
4+
5+
@Command(resourceBundle = "command-method-demo")
6+
public class CommandWithBundle2 {
7+
// we need a separate class, identical to CommandWithBundle, to avoid caching across tests
8+
}

src/main/java/picocli/CommandLine.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8414,10 +8414,12 @@ void updateFromCommand(Command cmd, CommandSpec commandSpec, boolean loadResourc
84148414
if (!empty(cmd.resourceBundle())) { // else preserve superclass bundle
84158415
if (loadResourceBundle) { // error if resource bundle does not exist
84168416
messages(new Messages(commandSpec, cmd.resourceBundle()));
8417-
} else {
8418-
ResourceBundle rb = null; // from annotation processor, we may not be able to load the ResourceBundle
8419-
try { rb = ResourceBundle.getBundle(cmd.resourceBundle()); } catch (MissingResourceException ignored) {}
8420-
messages(new Messages(commandSpec, cmd.resourceBundle(), rb));
8417+
} else { // from annotation processor, we may not be able to load the ResourceBundle
8418+
try {
8419+
messages(new Messages(commandSpec, cmd.resourceBundle()));
8420+
} catch (MissingResourceException ignored) {
8421+
messages(new Messages(commandSpec, cmd.resourceBundle(), null));
8422+
}
84218423
}
84228424
}
84238425
if (isNonDefault(cmd.abbreviateSynopsis(), DEFAULT_ABBREVIATE_SYNOPSIS)) {abbreviateSynopsis = cmd.abbreviateSynopsis();}
@@ -11622,8 +11624,10 @@ public Messages(CommandSpec spec, String baseName, ResourceBundle rb) {
1162211624
}
1162311625
private static ResourceBundle createBundle(String baseName) {
1162411626
if (loadBundles) {
11627+
tracer().debug("Messages: Loading ResourceBundle[base=%s]...", baseName);
1162511628
return ResourceBundle.getBundle(baseName);
1162611629
} else {
11630+
tracer().debug("Messages: Returning new dummy ResourceBundle", loadBundles);
1162711631
return new ResourceBundle() {
1162811632
@Override
1162911633
protected Object handleGetObject(String key) { return null; }
@@ -11668,6 +11672,7 @@ private static Set<String> keys(ResourceBundle rb) {
1166811672
* @param loadBundles true if bundles should be loaded (default), false if bundles should not be loaded
1166911673
*/
1167011674
public static final void setLoadBundles(boolean loadBundles) {
11675+
tracer().debug("Messages: loadBundles was set to %s", loadBundles);
1167111676
Messages.loadBundles = loadBundles;
1167211677
}
1167311678

src/test/java/picocli/I18nTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package picocli;
1717

18+
import org.junit.Ignore;
1819
import org.junit.Rule;
1920
import org.junit.Test;
2021
import org.junit.contrib.java.lang.system.ProvideSystemProperty;
@@ -774,6 +775,7 @@ public void testTracingWithResourceBundle() {
774775

775776
String expected = String.format("" +
776777
"[picocli DEBUG] Creating CommandSpec for picocli.I18nCommand@6bcae9 with factory picocli.CommandLine$DefaultFactory%n" +
778+
"[picocli DEBUG] Messages: Loading ResourceBundle[base=picocli.SharedMessages]...%n" +
777779
"[picocli DEBUG] Created Messages from resourceBundle[base=picocli.SharedMessages] for command 'i18n-top' (command 'i18n-top' (user object: picocli.I18nCommand@6bcae9))%n" +
778780
"[picocli DEBUG] Creating CommandSpec for class picocli.CommandLine$HelpCommand with factory picocli.CommandLine$DefaultFactory%n" +
779781
"[picocli DEBUG] Adding subcommand 'help' to 'i18n-top'%n" +
@@ -786,6 +788,7 @@ public void testTracingWithResourceBundle() {
786788
"[picocli DEBUG] Created Messages from resourceBundle[base=picocli.SharedMessages] for command 'i18n-sub' (command 'i18n-sub' (user object: class picocli.I18nSubcommand))%n" +
787789
"[picocli DEBUG] Created Messages from resourceBundle[base=picocli.SharedMessages] for command 'help' (command 'help' (user object: class picocli.CommandLine$HelpCommand))%n" +
788790
"[picocli DEBUG] Creating CommandSpec for class picocli.I18nSubcommand2 with factory picocli.CommandLine$DefaultFactory%n" +
791+
"[picocli DEBUG] Messages: Loading ResourceBundle[base=picocli.ResourceBundlePropagationTest]...%n" +
789792
"[picocli DEBUG] Created Messages from resourceBundle[base=picocli.ResourceBundlePropagationTest] for command 'sub2' (command 'sub2' (user object: class picocli.I18nSubcommand2))%n" +
790793
"[picocli DEBUG] Creating CommandSpec for class picocli.CommandLine$HelpCommand with factory picocli.CommandLine$DefaultFactory%n" +
791794
"[picocli DEBUG] Adding subcommand 'help' to 'sub2'%n" +

0 commit comments

Comments
 (0)