Skip to content

Commit

Permalink
[#1959] Add ability to test loading resource bundles in annotation pr…
Browse files Browse the repository at this point in the history
…ocessor

Closes #1959
  • Loading branch information
remkop committed Feb 17, 2023
1 parent 06fb221 commit 0ae482f
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 5 deletions.
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.codegen.annotation.processing.AbstractCommandSpecProcessor;

import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;
Expand All @@ -19,8 +20,7 @@
import static com.google.testing.compile.CompilationSubject.assertThat;
import static com.google.testing.compile.Compiler.javac;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import static picocli.annotation.processing.tests.Resources.slurp;
import static picocli.annotation.processing.tests.Resources.slurpAll;
import static picocli.annotation.processing.tests.YamlAssert.compareCommandYamlDump;
Expand Down Expand Up @@ -282,13 +282,23 @@ private void validateErrorMessages(Compilation compilation, List<String> expecte
}

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

assertOnlySourceVersionWarning(compilation);
}

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

assertOnlySourceVersionWarning(compilation);
}

private void assertOnlySourceVersionWarning(Compilation compilation) {
assertThat(compilation).hadWarningCount(0); // #826 version warnings are now suppressed
// assertThat(compilation).hadWarningContaining("Supported source version 'RELEASE_6' from annotation processor 'picocli.annotation.processing.tests");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.testing.compile.Compilation;
import com.google.testing.compile.JavaFileObjects;
import org.junit.Test;
import picocli.codegen.annotation.processing.AbstractCommandSpecProcessor;

import javax.annotation.processing.Processor;

Expand All @@ -26,6 +27,7 @@ public void testIssue1134() {

@Test
public void testIssue1134Details() {
AbstractCommandSpecProcessor.setLoadResourceBundles(true);

Compilation compilation = compareCommandYamlDump(slurp("/picocli/issue1134/Issue1134.yaml"),
JavaFileObjects.forResource("picocli/issue1134/Issue1134.java"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.google.testing.compile.Compilation;
import com.google.testing.compile.JavaFileObjects;
import org.junit.Test;
import picocli.codegen.annotation.processing.AbstractCommandSpecProcessor;

import javax.annotation.processing.Processor;
import javax.tools.StandardLocation;

import static com.google.testing.compile.CompilationSubject.assertThat;
import static com.google.testing.compile.Compiler.javac;
Expand All @@ -27,6 +27,7 @@ public void testIssue1137() {

@Test
public void testIssue1137Details() {
AbstractCommandSpecProcessor.setLoadResourceBundles(true);

Compilation compilation = compareCommandYamlDump(slurp("/picocli/issue1137/Issue1137.yaml"),
JavaFileObjects.forResource("picocli/issue1137/Issue1137.java"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
CommandSpec:
name: '<main class>'
aliases: []
userObject: picocli.example.messages.CommandWithBundle.CommandWithBundle
helpCommand: false
defaultValueProvider: null
versionProvider: null
version: []
ArgGroups: []
Options: []
PositionalParams: []
UnmatchedArgsBindings: []
Mixins: []
UsageMessageSpec:
width: 80
abbreviateSynopsis: false
hidden: false
showDefaultValues: false
sortOptions: true
requiredOptionMarker: ' '
headerHeading: ''
header: []
synopsisHeading: 'Usage: '
customSynopsis: []
descriptionHeading: ''
description: []
parameterListHeading: ''
optionListHeading: ''
commandListHeading: 'Commands:%n'
footerHeading: ''
footer: []
ParserSpec:
separator: '='
endOfOptionsDelimiter: '--'
expandAtFiles: true
atFileCommentChar: '#'
overwrittenOptionsAllowed: false
unmatchedArgumentsAllowed: false
unmatchedOptionsArePositionalParams: false
stopAtUnmatched: false
stopAtPositional: false
posixClusteredShortOptionsAllowed: true
aritySatisfiedByAttachedOptionParam: false
caseInsensitiveEnumValuesAllowed: false
collectErrors: false
limitSplit: false
toggleBooleanFlags: false
ResourceBundle:
Subcommands: []
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
public abstract class AbstractCommandSpecProcessor extends AbstractProcessor {
private static final String COMMAND_DEFAULT_NAME = CommandSpec.DEFAULT_COMMAND_NAME;
private static Logger logger = Logger.getLogger(AbstractCommandSpecProcessor.class.getName());
private static boolean loadBundlesDuringAnnotationProcessing;

/** The ProcessingEnvironment set by the {@link #init(ProcessingEnvironment)} method. */
protected ProcessingEnvironment processingEnv;
Expand Down Expand Up @@ -172,6 +173,18 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
return false;
}
}
/**
* During annotation processing, resource bundles may not be available on the
* classpath and thereby cause failures.
* For that reason, by default, resource bundles are not loaded during annotation processing.
* This method allows for enabling loading of resource bundles during annotation processing.
*
* @since 4.8.0
* @param loadBundles true if bundles should be loaded, false (default) if bundles should not be loaded
*/
public static final void setLoadResourceBundles(boolean loadBundles) {
loadBundlesDuringAnnotationProcessing = loadBundles;
}

private static String stacktrace(Exception e) {
StringWriter writer = new StringWriter();
Expand All @@ -180,7 +193,7 @@ private static String stacktrace(Exception e) {
}

private boolean tryProcess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
Model.Messages.setLoadBundles(false);
Model.Messages.setLoadBundles(loadBundlesDuringAnnotationProcessing);
new AnnotationValidator(processingEnv).validateAnnotations(roundEnv);

Context context = new Context();
Expand Down

0 comments on commit 0ae482f

Please sign in to comment.