From 1a2ddfaa2a51568782d3b916847fa57625956203 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Fri, 20 Sep 2019 17:11:24 +0200 Subject: [PATCH] #1075 - Improvements in I18N configuration setup. We now avoid a double-lookup of the default messages in the resource bundle setup. Tightened the test case to make sure the sample resource bundle is properly used, even if no default messages bundle is present. --- .../hateoas/config/HateoasConfiguration.java | 19 +++++++-------- ...nableHypermediaSupportIntegrationTest.java | 23 +++++++++++++++---- .../hateoas/config/rest-messages.properties | 1 + 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/springframework/hateoas/config/HateoasConfiguration.java b/src/main/java/org/springframework/hateoas/config/HateoasConfiguration.java index b8702246..7f91932c 100644 --- a/src/main/java/org/springframework/hateoas/config/HateoasConfiguration.java +++ b/src/main/java/org/springframework/hateoas/config/HateoasConfiguration.java @@ -31,7 +31,6 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Primary; import org.springframework.context.support.AbstractMessageSource; -import org.springframework.context.support.AbstractResourceBasedMessageSource; import org.springframework.context.support.ReloadableResourceBundleMessageSource; import org.springframework.core.io.Resource; import org.springframework.hateoas.client.LinkDiscoverer; @@ -63,6 +62,9 @@ import org.springframework.util.ClassUtils; @EnablePluginRegistries({ LinkDiscoverer.class }) public class HateoasConfiguration { + static String I18N_BASE_NAME = "rest-messages"; + static String I18N_DEFAULTS_BASE_NAME = "rest-default-messages"; + private @Autowired ApplicationContext context; @Bean @@ -119,14 +121,15 @@ public class HateoasConfiguration { @Nullable private final AbstractMessageSource lookupMessageSource() { - List candidates = loadProperties("rest-default-messages", false); + List candidates = loadResourceBundleResources(I18N_DEFAULTS_BASE_NAME, false); - if (candidates.isEmpty() && loadProperties("rest-messages", true).isEmpty()) { + if (candidates.isEmpty() && loadResourceBundleResources(I18N_BASE_NAME, true).isEmpty()) { return null; } - AbstractResourceBasedMessageSource messageSource = new ReloadableResourceBundleMessageSource(); - messageSource.setBasename("classpath:rest-messages"); + ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); + messageSource.setResourceLoader(context); + messageSource.setBasename(I18N_BASE_NAME); messageSource.setDefaultEncoding(StandardCharsets.UTF_8.toString()); if (!candidates.isEmpty()) { @@ -139,10 +142,8 @@ public class HateoasConfiguration { @Nullable private final Properties loadProperties(List sources) { - Resource[] resources = loadProperties("rest-default-messages", false).stream().toArray(Resource[]::new); - PropertiesFactoryBean factory = new PropertiesFactoryBean(); - factory.setLocations(resources); + factory.setLocations(sources.toArray(new Resource[sources.size()])); try { @@ -154,7 +155,7 @@ public class HateoasConfiguration { } } - private final List loadProperties(String baseName, boolean withWildcard) { + private final List loadResourceBundleResources(String baseName, boolean withWildcard) { try { return Arrays // diff --git a/src/test/java/org/springframework/hateoas/config/EnableHypermediaSupportIntegrationTest.java b/src/test/java/org/springframework/hateoas/config/EnableHypermediaSupportIntegrationTest.java index 852a30f7..8662719f 100755 --- a/src/test/java/org/springframework/hateoas/config/EnableHypermediaSupportIntegrationTest.java +++ b/src/test/java/org/springframework/hateoas/config/EnableHypermediaSupportIntegrationTest.java @@ -16,6 +16,7 @@ package org.springframework.hateoas.config; import static org.assertj.core.api.Assertions.*; +import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; import static org.springframework.hateoas.mediatype.hal.HalConfiguration.RenderSingleLinks.*; import static org.springframework.hateoas.support.ContextTester.*; @@ -537,9 +538,23 @@ class EnableHypermediaSupportIntegrationTest { @Test // #1019 void registersMessageResolverIfMessagesBundleAvailable() { - withServletContext(HateoasConfiguration.class, simulateResourceBundle(), context -> { - assertThat(context.getBean(MessageResolver.class)).isNotEqualTo(MessageResolver.of(null)); - }); + String originalBaseName = HateoasConfiguration.I18N_BASE_NAME; + + try { + + HateoasConfiguration.I18N_BASE_NAME = "org/springframework/hateoas/config/rest-messages"; + + withServletContext(HateoasConfiguration.class, simulateResourceBundle(), context -> { + + MessageResolver bean = context.getBean(MessageResolver.class); + + assertThat(bean).isNotEqualTo(MessageResolver.of(null)); + assertThat(bean.resolve(() -> new String[] { "key" })).isEqualTo("Schlüssel"); + }); + + } finally { + HateoasConfiguration.I18N_BASE_NAME = originalBaseName; + } } @Test // #1019, DATAREST-686 @@ -654,7 +669,7 @@ class EnableHypermediaSupportIntegrationTest { try { doReturn(new Resource[0]).when(spy).getResources("classpath:rest-default-messages.properties"); - doReturn(new Resource[] { resource }).when(spy).getResources("classpath:rest-messages*.properties"); + doReturn(new Resource[] { resource }).when(spy).getResources(contains("rest-messages")); } catch (IOException o_O) { fail("Couldn't mock resource lookup!", o_O); diff --git a/src/test/resources/org/springframework/hateoas/config/rest-messages.properties b/src/test/resources/org/springframework/hateoas/config/rest-messages.properties index e69de29b..2ddaf967 100644 --- a/src/test/resources/org/springframework/hateoas/config/rest-messages.properties +++ b/src/test/resources/org/springframework/hateoas/config/rest-messages.properties @@ -0,0 +1 @@ +key=Schlüssel