You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by William Surowiec <ws...@msn.com> on 2001/05/04 23:06:41 UTC

junit and log4j within ant

Hi,

I've searched about 3,000+ emails from this support group and have not found information on the following:

I am using ant 1.3 beta 3, junit3.6, log4j 1.0.4, and jedit 3.1 final (maybe something in the mix is acting up).

Anyway, I can use the java task to invoke an app and pass to it a configuration file for log4j. Everything behaves as I expect.

When I try to use a junit task to actually run the tests I am running in the java task - some wheels seem to fall off. First, I do
not see how I can pass an arg value (the config file for log4j) to the class under test.

More importantly, when I try to pass the value by coding it within the class it seems I cannot create an "appender" (this is an
output handler (as I understand it) within log4j).

Is there something very basic I've missed? Any pointers (hopefully not too barbed) welcomed.

Thanks

Bill


Re: junit and log4j within ant

Posted by William Surowiec <ws...@msn.com>.
Spencer

Before I begin, I freely and humbly express my debt to those who have explicitly helped me as well as this support group in general.
Obviously there will be "problems" with what I show below. I claim authorship of these. Comments are requested, don't worry, my hide
is pretty thick.

I'm a newbie, but this is what I'm doing (God awful detail - but then, that is where the devil lies (and I do mean lying in wait for
the unsuspecting as well as whispering untruths)):

inside ant:

<target name="junitDBTest" depends="testCompile">
 <junit haltonfailure="no" printsummary="yes" fork="no" >
 <sysproperty key="log4j.configuration" value="log4j.txt"/>
 <classpath>
   <fileset dir = "${dist}/lib" >
    <include name="**/**.jar"/>
    </fileset>
   <pathelement location="${buildTest}"/>
   <pathelement location="D:\jakarta-ant-1.3\lib\jakarta-ant-1.3-optional.jar"/>
 </classpath>
 <formatter type="plain" usefile="true"/>
 <test name="gscc.objectsTest.DBTest"/>
 <!--jvmarg value ="-verbose:class"/-->
 </junit>
</target>

Key parts for me:

1) the key for the sysproperty - with this log4j can use my configuration file and this is made available inside my code
automatically
2) the last pathelement

Inside my java code:

package gscc.objectsTest;


// probably don't need everything below now, it reflects the different attempts I made to become operational and have not been
cleaned up yet

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Category;
import org.apache.log4j.PropertyConfigurator;

import junit.framework.*;

import gscc.objects.DB;
import gscc.objects.GsccException;
import gscc.objects.Person;

import gscc.mockObjects.DBImpl;

public class DBTest extends TestCase {
 private DB db;
 static Category cat = Category.getInstance (DBTest.class.getName());

 protected void setUp () {
  db = new DBImpl ();
  cat.debug("setup");
  try {
   db.connect ("test");
  }
  catch (Exception e) {
   fail (e.toString());
  }
 }

 protected void tearDown () {
  cat.debug("tearDown");
  if (db != null) {
   db.disconnect();
   db = null;
  }
 }

 public void testInvalidSecondConnection () {
  cat.debug("testInvalidSecondConnection");
  try {
   db.connect (" ");
   db.connect ("should fail");
   fail ("this connection should have failed as it is a illegal second connect");
  }
   catch (Exception ignore) {
  }
 }

 public void testValidSecondConnection () {
  cat.debug("testValidSecondConnection");
  db.disconnect();
  try {
   db.connect("should work");
  }
  catch (Exception e) {
   fail (e.toString());
  }
 }

 public void testNoConnection () {
  cat.debug("testNoConnection");
  db.disconnect();
  Person p = new Person (db);
  try {
   db.storePerson (p);
   fail ("the connection does not exist, therefore this should have failed");
  }
  catch (Exception ignore) {
  }
 }

 public DBTest (String name) {
  super (name);
  cat.debug ("new DBTest " + name);
 }

 public static void main(String args[]) {

//again, the below comments reflect alternatives I tried before things became operational

  /*
  if (args[0]!=null) {
   PropertyConfigurator.configure(args[0]);
  }
  else {
   PropertyConfigurator.configure("log4j.txt");
  }

   Category root = Category.getRoot();
   Layout layout = new PatternLayout("%p [%t] %c - %m%n");
   root.addAppender(new FileAppender(layout, System.out));
*/
  junit.textui.TestRunner.run(DBTest.class);
 }

}


Key part for me - let log4j find the configuration file


here is the log4j.txt (also reflects some experimentation):

# Set root category priority to DEBUG and its only appender to A1, R.
log4j.rootCategory=DEBUG, R

# A1 is set to be a FileAppender which outputs to System.out.
log4j.appender.A1=org.apache.log4j.FileAppender
log4j.appender.A1.File=System.out

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=log4j.log
log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

junit output gets put into a file whose name is based on the package and test being done, something like
TEST-gscc.objectsTest.DBTest.txt
sample output:

Testsuite: gscc.objectsTest.DBTest
Tests run: 3, Failures: 0, Errors: 0, Time elapsed: 0.16 sec

Testcase: testInvalidSecondConnection took 0.11 sec
Testcase: testValidSecondConnection took 0 sec
Testcase: testNoConnection took 0 sec

log4j output (chopped down some and reflecting various formatting rules):

DEBUG main gscc.objectsTest.DBTest - new DBTest testInvalidSecondConnection
DEBUG main gscc.objectsTest.DBTest - new DBTest testValidSecondConnection
DEBUG main gscc.objectsTest.DBTest - new DBTest testNoConnection
DEBUG main gscc.objectsTest.DBTest - setup
DEBUG main gscc.mockObjects.DBImpl - test
<snip>
0    [main] DEBUG gscc.objectsTest.DBTest  - new DBTest testInvalidSecondConnection
0    [main] DEBUG gscc.objectsTest.DBTest  - new DBTest testValidSecondConnection
0    [main] DEBUG gscc.objectsTest.DBTest  - new DBTest testNoConnection
160  [main] DEBUG gscc.objectsTest.DBTest  - setup
160  [main] DEBUG gscc.mockObjects.DBImpl  - test
160  [main] DEBUG gscc.mockObjects.DBImpl  -
<snip>

Inside my D:\jdk1.3.1\jre\lib\ext directory (the java I'm running looks here) I have the following jars:
junit.jar
jaxp.jar
parser.jar
interclient.jar

I know the latter has nothing to do with ant, junit, etc) but is what is there.

my autoexec.bat (ugly as sin, but look at the list in the classpath)

@C:\PROGRA~1\NORTON~1\NAVDX.EXE /Startup
SET
CLASSPATH=.;D:\JAKARTA-LOG4J-1.0.4\log4j.jar;D:\JAKARTA-ANT-1.3\LIB\ANT.JAR;D:\JAKARTA-ANT-1.3\LIB\PARSER.JAR;D:\JAKARTA-ANT-1.3\LIB
\ant.jar;D:\JAKARTA-ANT-1.3\LIB\jakarta-ant-1.3-optional.jar;D:\JAKARTA-ANT-1.3\LIB\junit3.6.jar;D:\JUNIT3.6\JUNIT.JAR
SET JAVA_HOME=D:\jdk1.3.1
set JES_JAVA_HOME=%JAVA_HOME%
SET ANT_HOME=D:\jakarta-ant-1.3
SET HOME=d:\HOME
set
path=%JAVA_HOME%\bin;%ANT_HOME%\bin;c:\cvs;C:\PROGRA~1\EMBARC~1;C:\PROGRA~1\EMBARC~1\ERSTUDIO;D:\jdk1.3.1\jre\bin\hotspot;d:\Perl\bi
n;d:\cygwin\bin


I hope I don't get flamed for being long winded. I do hope I receive suggestions on being more efficient and effective. Finally, I
hope this helps someone else trying to wire this up for themselves.

Bill


Re: junit and log4j within ant

Posted by Spencer Marks <sm...@digisolutions.com>.
Would you mind posting how you use ant log4 together. I'd like to see 
that as I suspect it could be generally useful.
Thanks,
Spencer

On Sunday, May 6, 2001, at 09:13 AM, William Surowiec wrote:

> Robert,
>
> Thank you. Using the information in your reply I have been able to make 
> use of Ant, Junit and log4j. I removed any attempt within my
> code to configure log4j and allow log4j to find the configuration file 
> which I now pass as a sysproperty. I had to tweak the
> classpath one more time and I'm now operational.
>
> Thank you again
>
> Bill
>

Re: junit and log4j within ant

Posted by William Surowiec <ws...@msn.com>.
Robert,

Thank you. Using the information in your reply I have been able to make use of Ant, Junit and log4j. I removed any attempt within my
code to configure log4j and allow log4j to find the configuration file which I now pass as a sysproperty. I had to tweak the
classpath one more time and I'm now operational.

Thank you again

Bill


Re: junit and log4j within ant

Posted by Spencer Marks <sm...@digisolutions.com>.
Thanks much for this reply. Your aside comment is particularly useful. 
Perhaps that should be rolled into the official JUnit task, if it hasn't 
been already.

Spencer


On Saturday, May 5, 2001, at 04:19 PM, Robert Leftwich wrote:

> At 05:06 PM 04-05-01 -0400, William Surowiec wrote:
>> Hi,
>>
>> I've searched about 3,000+ emails from this support group and have not 
>> found information on the following:
>>
>> I am using ant 1.3 beta 3, junit3.6, log4j 1.0.4, and jedit 3.1 final 
>> (maybe something in the mix is acting up).
>>
>> Anyway, I can use the java task to invoke an app and pass to it a 
>> configuration file for log4j. Everything behaves as I expect.
>>
>> When I try to use a junit task to actually run the tests I am running 
>> in the java task - some wheels seem to fall off. First, I do
>> not see how I can pass an arg value (the config file for log4j) to the 
>> class under test.
>
> You can set system properties in the junit task prior to running junit 
> using the following :
>
>     <junit printsummary="yes" haltonfailure="no" fork="yes" 
> dir="${unit.test.dir}">
>
>       <sysproperty key="log4j.configuration" 
> value="file:${unit.test.conf}/log4j.properties"/>
>       ...
>     </junit>
>
> As an aside I find the following generic junit task (from Mike Clark 
> <mi...@clarkware.com> on the junit mailing list) for calling a specified 
> test case very useful :
>
>      <!--
>          Test Case
>
>          Use: ant -Dtestcase=com.whatever.TestCaseName test.case
>      -->
>      <target name="test.case" if="testcase" depends="compile.tests"
>          description="Runs the specified JUnit test case">
>
>          <junit printsummary="no" haltonfailure="yes">
>              <classpath>
>                  <path refid="project.classpath" />
>                  <pathelement path="${classes.dir}" />
>              </classpath>
>              <formatter type="plain" usefile="false" />
>              <test name="${testcase}" />
>          </junit>
>
>      </target>
>
>
>> More importantly, when I try to pass the value by coding it within the 
>> class it seems I cannot create an "appender" (this is an
>> output handler (as I understand it) within log4j).
>
> What exactly do you mean by 'coding it within the class' - are you hard 
> coding the value for the log4j.configuration file name, or are you 
> calling PropertConfigurator.configure('file') or are you just calling 
> the BasicConfigurator.configure(). Does this code work when you use the 
> java task to invoke the app ?
> In my setup I rely on log4j's default mechanism for locating and using 
> the configuration file (which is documented on the log4j web sites 
> version of the introductory manual, but not in the version included 
> with the v1.0.4 distribution) and find that by using the above 
> sysproperty it all works very seamlessly.
>
> HTH
>
> Robert
>

Re: junit and log4j within ant

Posted by Robert Leftwich <di...@ix.net.au>.
At 05:06 PM 04-05-01 -0400, William Surowiec wrote:
>Hi,
>
>I've searched about 3,000+ emails from this support group and have not 
>found information on the following:
>
>I am using ant 1.3 beta 3, junit3.6, log4j 1.0.4, and jedit 3.1 final 
>(maybe something in the mix is acting up).
>
>Anyway, I can use the java task to invoke an app and pass to it a 
>configuration file for log4j. Everything behaves as I expect.
>
>When I try to use a junit task to actually run the tests I am running in 
>the java task - some wheels seem to fall off. First, I do
>not see how I can pass an arg value (the config file for log4j) to the 
>class under test.

You can set system properties in the junit task prior to running junit 
using the following :

     <junit printsummary="yes" haltonfailure="no" fork="yes" 
dir="${unit.test.dir}">

       <sysproperty key="log4j.configuration" 
value="file:${unit.test.conf}/log4j.properties"/>
       ...
     </junit>

As an aside I find the following generic junit task (from Mike Clark 
<mi...@clarkware.com> on the junit mailing list) for calling a specified 
test case very useful :

      <!--
          Test Case

          Use: ant -Dtestcase=com.whatever.TestCaseName test.case
      -->
      <target name="test.case" if="testcase" depends="compile.tests"
          description="Runs the specified JUnit test case">

          <junit printsummary="no" haltonfailure="yes">
              <classpath>
                  <path refid="project.classpath" />
                  <pathelement path="${classes.dir}" />
              </classpath>
              <formatter type="plain" usefile="false" />
              <test name="${testcase}" />
          </junit>

      </target>


>More importantly, when I try to pass the value by coding it within the 
>class it seems I cannot create an "appender" (this is an
>output handler (as I understand it) within log4j).

What exactly do you mean by 'coding it within the class' - are you hard 
coding the value for the log4j.configuration file name, or are you calling 
PropertConfigurator.configure('file') or are you just calling the 
BasicConfigurator.configure(). Does this code work when you use the java 
task to invoke the app ?
In my setup I rely on log4j's default mechanism for locating and using the 
configuration file (which is documented on the log4j web sites version of 
the introductory manual, but not in the version included with the v1.0.4 
distribution) and find that by using the above sysproperty it all works 
very seamlessly.

HTH

Robert