You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Dominique Devienne <DD...@lgc.com> on 2002/08/16 16:42:06 UTC
RE: System.getProperties() returns an empty list when unit test
runwithin Ant
I don't know why you have this symptom, but I can tell you when you can
System.getProperties().size() return 0, and still access individual
properties normally... It's because Properties can take a 'default'
Properties instance that backs it up. So the code:
Properties defaults = new Properties();
Defaults.setProperty("p1", "v1");
Properties properties = new Properties(defaults);
assertEquals(0, properties.size());
assertEquals("v1", properties.getProperty("p1"));
will succeed. You can get the 'real' size of the environment using the
following code:
Enumeration e = System.getProperties().propertyNames();
final int count = Collections.list(e).size();
Of course, that doesn't tell you why in your particular case the Java System
properties would come from the 'defaults'... --DD
-----Original Message-----
From: Jerome Lacoste [mailto:lacostejbbc@altern.org]
Sent: Friday, August 16, 2002 9:24 AM
To: ant-user@jakarta.apache.org
Subject: System.getProperties() returns an empty list when unit test
runwithin Ant
Hi Anties,
I've got this strange problem: one of my UnitTest fails when I run it
within Ant, while it doens't fail when I run it directly from the same VM.
I've had trouble like that before and they are usualy environment
related but this one is very strange:
Here's the code of my Unit Test:
public void testSystemGetProperties() {
// Skip debug code to printout System.getProperties()
...
if (System.getProperties().size() == 0)
{
fail("WARNING something is wrong in your environment.
System.getProperties().size() == 0");
}
}
As you can see, System.getProperties().size() == 0 fails when I run this
test within ant. According to the API this should never happen.
Even more strange is that if I try to access any single System property
within the same code, I will obtain it without problem!
I.e. System.getProperty("user.dir") works OK but System.getProperties()
still returns an empty list!!
The following batch file demonstrate the problem for me:
> type runtest.bat
%JAVA_HOME%\bin\java -cp
classes\test;classes;lib\junit3.7\junit.jar;lib\log4j\log4j-1.2.4.jar
no.bbc.basis.util.PropertyFileReaderTest
ant test.case -Dtestcase=no.bbc.basis.util.PropertyFileReaderTest
The first call works while the second doesn't
My Ant test.case target is realy simple:
<target name="test.case" if="testcase" depends="compile.tests"
description="Runs the specified JUnit test case">
<junit printsummary="withOutAndErr" haltonfailure="yes">
<classpath>
<path refid="tests.classpath" />
</classpath>
<formatter type="plain" usefile="false" />
<test name="${testcase}" />
</junit>
</target>
where the classpath is set using a fileset filter of all jar file in my
lib directories + my classes dir
It seems to be an environment problem, but I can't figure it out.
I first though it to be a JDK problem, but given the fact that my test
works when I invoke it from the command line but not within Ant, I am
confused.
I've searched throught the Ant mailing list, the releases notes for Ant
1.5, Sun's bug database and Google without luck.
I am currently downloading a newer version of Ant (1.5) and of the JDK
(1.4.1beta) to rerun my tests, but on my modem line, it's going to take
some time.
In the meantime I submit my problem to you in case one of you spots a
problem.
Some more information about my environment:
I am developing on WinNT 4.0.
Here's my environment:
ANT_HOME=D:\dev\jakarta-ant-1.4.1
CLASSPATH=D:\j2sdk1.4.0_01\lib\tools.jar
JAVA_HOME=D:\j2sdk1.4.0_01
Path=D:\dev\jakarta-ant-1.4.1\bin;C:\WINNT\SYSTEM32;C:\WINNT;C:\WINNT\SYSTEM
32\WBEM;C:\DMI\WIN32\BIN;D:\PROGRA~1\ULTRAE~1
D:\>ant -version
Ant version 1.4.1 compiled on October 11 2001
>%JAVA_HOME%\bin\java -version
java version "1.4.0_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0_01-b03)
Java HotSpot(TM) Client VM (build 1.4.0_01-b03, mixed mode)
Attached are:
- SysProp_fromJDK: output of Properties printed out by my test code when
code executed from the command line:
- SysProp_fromAnt: Same properties printed out by my test code when
code is executed within Ant
Notice how System.getProperties() displays 0 elements but all its
properties are effectively found!
Here's the diff of the two output files:
Comparing:
D:\Projects\Basis\ProofOfConcept\bugs\System_getProperties_in_Ant\SysProp_fr
omAnt.txt
To:
D:\Projects\Basis\ProofOfConcept\bugs\System_getProperties_in_Ant\SysProp_fr
omJDK.txt
====
====
2 <! Displaying 0 element(s) in properties [System.getProperties()]:
!> Displaying 50 element(s) in properties [System.getProperties()]:
32 <! p->user.timezone=Europe/Berlin
!> p->user.timezone=
36 <!
p->java.class.path=D:\j2sdk1.4.0_01\lib\tools.jar;D:\dev\jakarta-ant-1.4.1\l
ib\optional.jar;D:\dev\jakarta-ant-1.4.1\lib\junit.jar;D:\dev\jakarta-ant-1.
4.1\lib\jaxp.jar;D:\dev\jakarta-ant-1.4.1\lib\crimson.jar;D:\dev\jakarta-ant
-1.4.1\lib\ant.jar;D:\j2sdk1.4.0_01\lib\tools.jar
!>
p->java.class.path=classes\test;classes;lib\junit3.7\junit.jar;lib\log4j\log
4j-1.2.4.jar
[Don't ask me why the "user.timezone" is not properly propagated.]
Only noticeable difference is the java.class.path, but isn't that normal?
Cheers,
Jerome
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>