Skip to content

Grails 11795 test case #14754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@
import java.io.File;
import java.io.FilenameFilter;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
Expand All @@ -65,6 +62,7 @@ public class PluginAwareResourceBundleMessageSource extends ReloadableResourceBu
private long pluginCacheMillis = Long.MIN_VALUE;
private boolean searchClasspath = false;
private String messageBundleLocationPattern = "classpath*:*.properties";
String[] basenamesDefinition = {};

public PluginAwareResourceBundleMessageSource() {
}
Expand All @@ -86,6 +84,12 @@ public void setResourceResolver(PathMatchingResourcePatternResolver resourceReso
this.resourceResolver = resourceResolver;
}

@Override
public void setBasenames(String... basenames){
basenamesDefinition = basenames;
super.setBasenames(basenames);
}

public void afterPropertiesSet() throws Exception {
if (pluginCacheMillis == Long.MIN_VALUE) {
pluginCacheMillis = cacheMillis;
Expand Down Expand Up @@ -148,8 +152,10 @@ public boolean accept(File dir, String name) {
basenames.add(baseName);
}

setBasenames(basenames.toArray( new String[basenames.size()]));

List<String> mergedBasenames = new ArrayList<>(Arrays.asList(basenamesDefinition));
basenames.removeAll(mergedBasenames);
mergedBasenames.addAll(basenames);
super.setBasenames(mergedBasenames.toArray(new String[0]));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ class ResourceBundleMessageSourceSpec extends Specification {
void setup(){
messages = new TestResource('messages.properties','''\
foo=bar
shared.message=Messages Message
'''.stripIndent().getBytes('UTF-8'))

other = new TestResource('other.properties','''\
bar=foo
shared.message=Other Message
'''.stripIndent().getBytes('UTF-8'))
}

Expand All @@ -35,6 +37,22 @@ class ResourceBundleMessageSourceSpec extends Specification {
messageSource.getBundleCodes(locale,'other') == (['bar'] as Set)
messageSource.getBundleCodes(locale,'messages','other') == (['foo','bar'] as Set)
}

void 'Check method to verify ResourceBundle ordering prioritizes application over e.g. plugin messages'(){
given:
def messageSource = new ReloadableResourceBundleMessageSource(
resourceLoader: new DefaultResourceLoader(){
Resource getResourceByPath(String path){
path.startsWith('messages') ? messages:other
}
}
)
messageSource.setBasenames('other', 'messages')
def locale = Locale.default
expect: "other messages override plugin messages"
messageSource.getMessage('shared.message', null, locale) == 'Other Message'
messageSource.getMessage('foo', null, locale) == 'bar'
}

class TestResource extends ByteArrayResource{
String filename
Expand Down
Loading