11package org.grails.spring.context
22
33import org.grails.spring.context.support.ReloadableResourceBundleMessageSource
4- import org.springframework.core.io.FileSystemResourceLoader
4+ import org.springframework.core.io.ByteArrayResource
5+ import org.springframework.core.io.DefaultResourceLoader
56import org.springframework.core.io.Resource
67import spock.lang.Specification
78
89class ResourceBundleMessageSourceSpec extends Specification {
9- File resourceFolder
10-
10+ Resource messages
11+ Resource other
1112 void setup (){
12- resourceFolder = new File (System . getProperty(' user.home' ),' .grails/test-resources' )
13- if (! resourceFolder. exists()) resourceFolder. mkdirs()
14-
15- def messages = new File (resourceFolder,' messages.properties' )
16- messages. text = ''' \
13+ messages = new TestResource (' messages.properties' ,''' \
1714 foo=bar
18- ''' . stripIndent()
19- def other = new File (resourceFolder, ' other.properties ' )
20- other. text = ''' \
15+ ''' . stripIndent(). getBytes( ' UTF-8 ' ))
16+
17+ other = new TestResource ( ' other.properties ' , ''' \
2118 bar=foo
22- ''' . stripIndent()
19+ ''' . stripIndent(). getBytes( ' UTF-8 ' ))
2320 }
2421
25- void cleanup (){
26- resourceFolder. deleteDir()
27- }
28-
29-
3022 void ' Check method to retrieve bundle codes per messagebundle' (){
3123 given :
3224 def messageSource = new ReloadableResourceBundleMessageSource (
33- resourceLoader : new TempResourceLoader (resourceFolder :resourceFolder)
25+ resourceLoader : new DefaultResourceLoader (){
26+ Resource getResourceByPath (String path ){
27+ path. startsWith(' messages' ) ? messages: other
28+ }
29+ }
3430 )
3531 messageSource. setBasenames(' messages' ,' other' )
3632 def locale = Locale . default
@@ -40,15 +36,14 @@ class ResourceBundleMessageSourceSpec extends Specification {
4036 messageSource. getBundleCodes(locale,' messages' ,' other' ) == ([' foo' ,' bar' ] as Set )
4137 }
4238
43- }
39+ class TestResource extends ByteArrayResource {
40+ String filename
4441
45- class TempResourceLoader extends FileSystemResourceLoader {
46- File resourceFolder
47-
48- @Override
49- protected Resource getResourceByPath (String path ) {
50- path = new File (resourceFolder,path). absolutePath
51- return super . getResourceByPath(path)
42+ TestResource (String filename , byte [] byteArray ) {
43+ super (byteArray)
44+ this . filename= filename
45+ }
46+
5247 }
48+
5349}
54-
0 commit comments