You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by bu...@apache.org on 2003/11/12 16:06:10 UTC

DO NOT REPLY [Bug 24648] New: - Properties files are not properly loaded when testing class under junit task

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=24648>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=24648

Properties files are not properly loaded when testing class under junit task

           Summary: Properties files are not properly loaded when testing
                    class under junit task
           Product: Ant
           Version: 1.5.4
          Platform: PC
        OS/Version: Other
            Status: NEW
          Severity: Critical
          Priority: Other
         Component: Optional Tasks
        AssignedTo: dev@ant.apache.org
        ReportedBy: achepati67@yahoo.es


Dear Sr,
 
Overview:
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 
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.
 
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).
 
Resources.properties Properties file for the default language: let say english. 
Defines the key hello.
 
Resources_es.properties 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 invokes 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
invoke-junit:
     [echo]
     [echo]          user language: es
     [echo]
    [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
BUILD SUCCESSFUL
Total time: 1 second
P:\bugs>ant -Duser.language=en
Buildfile: build.xml
invoke-junit:
     [echo]
     [echo]          user language: en
     [echo]
    [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: Hola Mundo, instead of Hello World and testing class 
detect this contradiction.
  
I am copying the files used for the test:
1. AntAndProperties.java:
public class AntAndProperties {
    public String printHello() {
        java.util.ResourceBundle bundle 
                = java.util.ResourceBundle.getBundle("Resources");
        final String HELLO = bundle.getString("hello");
        System.out.println(HELLO);
        return HELLO;
    }
}
 
2. AntAndPropertiesTest.java:
import junit.framework.*;
public class AntAndPropertiesTest extends TestCase {    
    public AntAndPropertiesTest(java.lang.String testName) {
        super(testName);
    }    
    public static Test suite() {
        TestSuite suite = new TestSuite(AntAndPropertiesTest.class);
        return suite;
    }
    public void testPrintHello() {
        System.out.println("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){
        junit.textui.TestRunner.run (AntAndPropertiesTest.class);
    }        
}
 
3. Resources.properties:
hello Hello World!!!
 
4. Resources_es.properties:
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"/>
    </path>
    <target name = "invoke-junit">
        <echo>
         user language: ${user.language}
        </echo>
        <junit
            printsummary        = "true"
            includeantruntime   = "false"
            showoutput          = "true">
            <sysproperty key = "user.language" value = "${user.language}"/>
            <classpath refid = "classpath"/>
            <test name = "AntAndPropertiesTest"/>
            <formatter type = "plain" usefile = "true"/>            
        </junit>
    </target>
</project>
 
Thanks in advance,

If you want I can sent to you a zip file with all this information.


David Leal

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org