ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "William Surowiec" <wsurow...@msn.com>
Subject Re: junit and log4j within ant
Date Sun, 06 May 2001 14:51:42 GMT
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


Mime
View raw message