0

I have a JUnit test class starting with this declaration

@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource(properties = { 
        "cache.working.dir=./src/test/resources/cacheDirRepository/" } )

How can I launch this test class on a linux based file separator system as well ('/') as on a Windows base file separator system ('\') ?

Thanks by advance,

  • Is there any problem with `/`? Should work fine everywhere, actually – andreoss Jul 01 '20 at 20:33
  • No. It's not working on a **native** windows platform. A possible workaround is to start a linux command interpreter. But I am looking for a pure Windows solution. I would like to execute a `init.bat`, and not a `init.sh`. – Frédéric Vidal Jul 02 '20 at 21:09

3 Answers3

1

Put the following

// Platform independent and safe to use across Linux and Windows

"cache.working.dir=."+File.separator+"src"+File.separator+"test"+File.separator+"resources"+File.separator+"cacheDirRepository"
Pankaj
  • 67
  • 7
  • Yes. This solution is working, but if you have multiple similar test cases, it's a real job to transform all test cases. – Frédéric Vidal Jul 02 '20 at 21:13
  • Yes but you can create generic path by creating a method java.util.List pathParts = java.util.Arrays.asList("test", "resources", "cacheDirRepository"); genPath = StringUtils.join(pathParts, File.separator); – Pankaj Jul 02 '20 at 21:22
  • You cannot use StringUtils static functions inside an annotation declaration. You'll get the error : `The value for annotation attribute TestPropertySource.properties must be a constant expression` – Frédéric Vidal Jul 03 '20 at 05:22
  • I cannot use as well File.separator... :-( – Frédéric Vidal Jul 03 '20 at 06:10
1

Ok. I share my last solution. May-be there's one smarter. And I'll be pleased to learn...

3 actions :

  1. I'll use placeholders to process my annotation at runtime. The declaration of my annotation is parameterized with a file separator placeholder.
@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource(properties = { "cache.working.dir=." + 
                                        "${file_separator}" + "src" +
                                        "${file_separator}" + "test" + 
                                        "${file_separator}" + "resources" + 
                                        "${file_separator}" + "cacheDirRepository" + 
                                        "${file_separator}", ...
  1. I declare a specific bean in my container to process these placeholders.
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertiesResolver() {
        return new PropertySourcesPlaceholderConfigurer();
    }
  1. Finally, I pass my os-dependent file separator char at runtime as a JVM parameter

For Unix/MacOsX

mvn clean install -DargLine="-Dfile_separator=/"

For Windows

mvn clean install -DargLine="-Dfile_separator=\\"
0

The java.io.File package have some variables for this, you can use them to resolve it:

File.separator // use '/' in linux and '\' in windows
File.separatorChar // use '/' in linux and '\' in windows
File.pathSeparator // use ':' in linux and ';' in windows
File.pathSeparatorChar // use ':' in linux and ';' in windows