Datamal Blog

Thursday, September 23, 2004

JUnit thru Ant



public MyTestSuite() {
this.add(new MyTest("someTest));
this.add(new MyTest("someOtherTest));
}


This can go to the Ant build. This way you no need to rebuild your test classes and for me Ant's Fileset look very similar to the groups in TestNG. For example


<junit ...>
<batchtest ...>
<fileset dir="${src.dir}">
<include name="**/*Test.java"/>
</fileset>
</batchtest>
...
</junit>


Printing Ant properties for dubugging or logging


Say you have some id that you want the value for :-


<path id="tools.class.path">
<pathelement location="${xerces.jar}"/>
<pathelement location="${testlet.jar}"/>
<pathelement location="${junit.jar}"/>
<pathelement location="${tools.jar}"/>
<fileset dir="${tools.dir}/lib">
<include name="*.jar" />
<exclude name="testlet.jar"/>
<exclude name="xerces.jar"/>
</fileset>
</path>


It would be nice to be able to print the contents of this path, for debugging. The obvious approach:-


<echo message="tools.class.path is ${tools.class.path}"/>


Does _not_ work. Here's how to do it:-


  • First declare a property, referring to the path.
  • Then print the property:

<property name="cp" refid="tools.class.path"/>
<echo message="Classpath is ${cp}"/>


Wednesday, September 22, 2004

Searching files with Grep on Windows using Cygwin


The following is an extract taken from Bruce Eckel's weblog relating to this title.

Just go to http://www.cygwin.com and follow the directions, and you'll end up with the equivalent of a DOS command window running Linux, with all the tools like grep built in. If you want more information about grep, just type info grep. If you'd like to be able to have a "bash prompt here" facility, see this page.

Later: someone pointed out that Windows now has findstr already installed. I've been using cygwin and grep for awhile now so I wasn't looking for this, and didn't know it had been added (or when). But it appears to be quite powerful and includes regular expressions -- and has the benefit of already being on your machine. I use cygwin to solve many other problems, so it has far more value than just grep.

With cygwin installed, go to the root of the Java source code directory (after you've installed the lastest version of JDK 5.0 from java.sun.com, of course). Here's the grep command that I used:

grep -R -n -B 3 -A 10 --include="*.java" "([A-Z]\[\])" .

The -R flag tells grep to recurse through all subdirectories. The --include="*.java" flag tells it to only include Java files in the search. -n says to print line numbers. -B 3 says to print three lines before the match, and -A 10 says to print 10 lines after the match. And the regular expression itself says to find everything that looks like an array cast to a generic type, for example '(T[])'. You can see that GNU grep is a powerful tool for searching the Java sources for example code.

The findstr equivalent is:

findstr /N /S /R "([A-Z]\[\])" *.java

Unfortunately, you do not get the benefit of being able to print lines before and after the match, as you do with GNU grep. (It is possible that someone has ported GNU grep to Windows by itself, so that you don't have to install all of Cygwin).

In windows the classic way to get the ip address of your machine is to use the DOS prompt and type ipconfig.

Try :-

ipconfig /all

and you'll get a whole lot more info.


App Server Vendor Independence



The best thing to do is to use a DAO (Data Accesss Object) pattern with your BMP beans.

Basically, you write a base DAO object that does all the CRUD. Create, Delete, Lookup, Store / Update. Typically, you can use a factory that returns the BASE DAO, driven by an EJB env property for the class name of the DAO impl class.

For example, say you have an OrderBMP. The order bean would use

Factory.getOrderDAO(ejbcontext.getProperty("OrderDAO.className")

to get the proper DAO for the installation.

Normally, DAOs are singleton.

The BaseOrderDAO implements all the methods it can in a platform independant way, it might be abstract.

Then you implement an Oracle9iOrderDAO, a TeraDBOrderDAO etc satisfying all those things you need.

When you deliver to the customer, all that is needed is a single line change in the env entries for the bean in the deployment descriptor.