ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Leal "Valmaņa" <>
Subject Properties files are not properly loaded when testing class under junit task (possible bug)
Date Tue, 11 Nov 2003 12:15:22 GMT
Dear Members,
I have detected that the junit task doesn't take into account a change on the variable user.language,
for example on the invokation: ant -Duser.language=en (my deafult language is spanish). The
user.language takes effect, but the junit test class read wrong the properties file (for the
default language instead of the user.language value). This situation ONLY occurs when I test
the class using the junit task under Ant, if run java for my testing class for english and
spanish language it works!!!
I am sending to you my testing example in order to be reproduced the problem by some of you:
Purpose of the Test: The class AntAndProperties, load a properties file, when it is defined
the key hello. The method, printHello(), just returns and print the value of the returned
key hello. I want to test that when I change the user.language value, the value associated
with the key hello changes and the value of the key is the expected.
Result: The expected result is to the appropiate key value on each case for the given user.language
value. The expected result holds when I run the testing class directly from java, but it get
the wrong result when a test the class using the junit task.
Description of the files:
AntAndProperties: This class simply loads the Properties Resources, for a given default language
(that is the value of user.language). Properties file for the default language: let say english. Defines the
key hello. The corresponding Properties file for the spanish  locale.
AntAndPropertiesTest: Testing class for checking under testing process that the key hello
has the expected value for a given value of the property user.language.
build.xml: Buld files, that simply infokes the junit task.
If you run the class AntAndPropertiesTest 
java -Duser.language=es AntAndPropertiesTest
java -Duser.language=en AntAndPropertiesTest
for both configurations it works.
Nevertheless, the simple build file, for testing the class using the junit task:

P:\bugs>ant -Duser.language=es
Buildfile: build.xml
     [echo]          user language: es
    [junit] Running AntAndPropertiesTest
    [junit] testPrintHello
    [junit] user.language=es
    [junit] Hola Mundo!!!
    [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0,016 sec
Total time: 1 second
P:\bugs>ant -Duser.language=en
Buildfile: build.xml
     [echo]          user language: en
    [junit] Running AntAndPropertiesTest
    [junit] testPrintHello
    [junit] user.language=en
    [junit] Hola Mundo!!!
    [junit] Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0,047 sec
    [junit] TEST AntAndPropertiesTest FAILED

so, the test failed for the english configuration, so the test is running with the user.language
with value: "en", but the value of the key hello is in spanish!!!!!
You can see the build.log file in order to check the printed value of the key hello is in
spanish for the english configuration.
I am sending the files used for the test:
1. AntAndProperties:
public class AntAndProperties {
    public String printHello() {
        java.util.ResourceBundle bundle 
                = java.util.ResourceBundle.getBundle("Resources");
        final String HELLO = bundle.getString("hello");
        return HELLO;
2. AntAndPropertiesTest:
import junit.framework.*;
public class AntAndPropertiesTest extends TestCase {    
    public AntAndPropertiesTest(java.lang.String testName) {
    public static Test suite() {
        TestSuite suite = new TestSuite(AntAndPropertiesTest.class);
        return suite;
    public void testPrintHello() {
        final String USER_LANGUAGE = System.getProperty("user.language");
        System.out.println("user.language=" + USER_LANGUAGE);        
        AntAndProperties bug = new AntAndProperties();
        final String RESULT = bug.printHello();
        if ("es".equals(USER_LANGUAGE)) {
            assertTrue("Hola Mundo!!!".equals(RESULT));
        } else if ("en".equals(USER_LANGUAGE)) {
            assertTrue("Hello World!!!".equals(RESULT));
    public static void main (String[] args){ (AntAndPropertiesTest.class);
hello Hello World!!!
hello Hola Mundo!!!
5. build.xml: Stefan Bodewin suggested me to use the <sysproperty> nested node in order
to change the user.language value on the junit invokation.
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="invoke-junit" name="bug">
    <property name = "lib" location = "S:\lib"/>
    <path id = "classpath">
        <pathelement location = "${basedir}"/>
        <pathelement location = "${lib}/junit.jar"/>
    <target name = "invoke-junit">
         user language: ${user.language}
            printsummary        = "true"
            includeantruntime   = "false"
            showoutput          = "true">
            <sysproperty key = "user.language" value = "${user.language}"/>
            <classpath refid = "classpath"/>
            <test name = "AntAndPropertiesTest"/>
            <formatter type = "plain" usefile = "true"/>            

On the zip file you will find all such information.
Thanks in advance for any help,
David Leal


Do you Yahoo!?
Protect your identity with Yahoo! Mail AddressGuard
View raw message