Set surefire plugin Locale in Maven pom file
Asked Answered
R

4

20

I read in maven-surefire-plugin and default locale that Maven runs tests forked and thus might lose any locale you might have set.

Is there a way to run tests in Maven in forked mode and still retain locale?

-- EDIT --

So, to clarify a bit: It is fully possible to set language and region in System Properties using:

<systemPropertyVariables>
  <user.language>en</user.language>
  <user.region>GB</user.region>
</systemPropertyVariables>

And they are actually passed to the running process. This does not however set locale accordingly; locale remains as System Default.

Rosinweed answered 17/1, 2012 at 21:21 Comment(4)
Have you tried setting user.country instead of user.region?Aristarchus
Setting country doesn't work. It sets it as a System Property just fine, doesn't affect Locale in any way.Rosinweed
Sun's documentation [1] states: "Second, on some Java runtime implementations, the application user can override the host's default locale by providing this information on the command line by setting the user.language, user.country, and user.variant system properties." So, which JVM are you using? [1] java.sun.com/developer/technicalArticles/J2SE/localeAristarchus
Tried with Java 5, 6 and 7. Once again, user.language, user.country and user.variant are passed to the process just fine. Locale remains default.Rosinweed
P
25

Try this:

   <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
            <argLine>-Duser.language=en -Duser.region=GB</argLine>
        </configuration>
   </plugin>
Pahari answered 26/7, 2013 at 15:6 Comment(0)
M
2

I don't have a way to test this, but give it a try:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.9</version>
    <configuration>
        <project>
            <properties>
                <user.language>en</user.language>
                <user.region>GB</user.region>
            </properties>
        </project>
        <includes>
            <include>**/*Test.java</include>
        </includes>
        <forkMode>pertest</forkMode>
    </configuration>
</plugin>

EDIT: OK try this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.9</version>
    <configuration>
        <systemPropertyVariables>
            <user.language>en</user.language>
            <user.region>GB</user.region>
        </systemPropertyVariables>
        <includes>
            <include>**/*Test.java</include>
        </includes>
        <forkMode>pertest</forkMode>
    </configuration>
</plugin>
Moreover answered 23/1, 2012 at 16:45 Comment(1)
This one sets System Properties just fine. Locale remains default. Thanks for the effort though.Rosinweed
P
2

The default locale of your application is determined in three ways. First, unless you have explicitly changed the default, the getDefault() method returns the locale that was initially determined by the Java Virtual Machine (JVM) when it first loaded. That is, the JVM determines the default locale from the host environment. The host environment's locale is determined by the host operating system and the user preferences established on that system.

Second, on some Java runtime implementations, the application user can override the host's default locale by providing this information on the command line by setting the user.language, user.country, and user.variant system properties. [Source]

I think you are a victim of the first part, so second never gets a chance.

Instead, what you can do is in your unit test (or maybe a base class thereof) set the default locale programatically as stated later in the same text:

Third, your application can call the setDefault(Locale aLocale) method. The setDefault(Locale aLocale) method lets your application set a systemwide resource. After you set the default locale with this method, subsequent calls to Locale.getDefault() will return the newly set locale.

static{
        Locale.setDefault(Locale.UK);
}
Prod answered 25/1, 2012 at 16:0 Comment(1)
Yep, that would work for sure. The only problem is that i have a huge number of tests. And also, in the future someone might, and most definitely will, write a unit test without explicitly setting the Locale programmatically.Rosinweed
D
2

I had the same issue, but I had to solve it without ingerence to the pom.xml file. This is possible through Maven's global configuration file (usually located at ~/.m2/settings.xml). And to do it you add a profile like below, that will be activated by default:

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
        http://maven.apache.org/xsd/settings-1.0.0.xsd">

    ...

    <profiles>
        <profile>    
            <id>my-prof</id>
            <properties>
                <argLine>-Duser.language=en -Duser.region=GB</argLine>
            </properties>            
        </profile>
    </profiles>

    <activeProfiles>
        <activeProfile>my-prof</activeProfile>
    </activeProfiles>

    ...

</settings>
Dziggetai answered 13/1, 2018 at 20:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.