While trying to define rest-messages.properties files to generate HAL Forms property prompts, I discovered the following - IMHO buggy - behavior.
Steps to reproduce
Given a maven project, suppose you have the following src/main/resources/rest-message.properties
The compilation copies rest-message.properties to target/classes/rest-message.properties.
Now you run a Spring Boot test. The context initialization triggers the following method:
|
private final AbstractMessageSource lookupMessageSource() { |
|
|
|
List<Resource> candidates = loadResourceBundleResources(I18N_DEFAULTS_BASE_NAME, false); |
|
|
|
if (candidates.isEmpty() && loadResourceBundleResources(I18N_BASE_NAME, true).isEmpty()) { |
|
return null; |
|
} |
|
|
|
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); |
|
messageSource.setResourceLoader(context); |
|
messageSource.setBasename("classpath:".concat(I18N_BASE_NAME)); |
|
messageSource.setDefaultEncoding(StandardCharsets.UTF_8.toString()); |
|
|
|
if (!candidates.isEmpty()) { |
|
messageSource.setCommonMessages(loadProperties(candidates)); |
|
} |
|
|
|
return messageSource; |
|
} |
Actual result
The method returns null because loadResourceBundleResources(I18N_BASE_NAME, true) returns an empty collection. Consequently, the prompts are never rendered in the test context.
Expected result
The method should return a non null AbstractMessageSource to allow prompt rendering in the test context.
Digging
In this case, if we ignore line 138 to 142 and build a ReloadableResourceBundleMessageSource anyway, the latter is able to resolve any message from the main file:
@Test
void test() {
ReloadableResourceBundleMessageSource messageSource =
new ReloadableResourceBundleMessageSource();
messageSource.setResourceLoader(context);
messageSource.setBasename("classpath:".concat("rest-messages"));
messageSource.setDefaultEncoding(StandardCharsets.UTF_8.toString());
String message = messageSource.getMessage("foo._prompt", new Object[0], Locale.getDefault());
assertThat(message).isEqualTo("Foo");
}
Therefore, I think the preliminary checks (138 to 142) are not "in sync" with the way ReloadableResourceBundleMessageSource actually works.
While trying to define
rest-messages.propertiesfiles to generate HAL Forms property prompts, I discovered the following - IMHO buggy - behavior.Steps to reproduce
Given a maven project, suppose you have the following
src/main/resources/rest-message.propertiesfoo._prompt=FooThe compilation copies
rest-message.propertiestotarget/classes/rest-message.properties.Now you run a Spring Boot test. The context initialization triggers the following method:
spring-hateoas/src/main/java/org/springframework/hateoas/config/HateoasConfiguration.java
Lines 136 to 154 in d6265fb
Actual result
The method returns null because
loadResourceBundleResources(I18N_BASE_NAME, true)returns an empty collection. Consequently, the prompts are never rendered in the test context.Expected result
The method should return a non null
AbstractMessageSourceto allow prompt rendering in the test context.Digging
In this case, if we ignore line 138 to 142 and build a
ReloadableResourceBundleMessageSourceanyway, the latter is able to resolve any message from themainfile:Therefore, I think the preliminary checks (138 to 142) are not "in sync" with the way
ReloadableResourceBundleMessageSourceactually works.