Thursday, April 29, 2010

JUnit Hibernate Lazy initialization

To configure your JUnit testcases for testing data access through Hibernate with entity mappings using lazy initialization requires minor changes to the setUp() and tearDown() methods as follows:

     * The application context used in this test case.
    protected ApplicationContext ctx;
    protected StrainDao dao;
    // keep reference to session factory to handle lazy-initialization
    protected SessionFactory sessionFactory;
    protected Session session;
    public PhenomeBankTestCase(final String name) {

        // Override the default definition for dataSource
        final String[] files = {                    
        final String[] files = {                    

        ctx = new ClassPathXmlApplicationContext(files);
        sessionFactory = (SessionFactory)ctx.getBean(BEAN_ID_SESSION_FACTORY);
     * Sets up the test fixture.
    public void setUp() {
        dao = (StrainDao) ctx.getBean(BEAN_ID_STRAIN_DAO);         

        session = SessionFactoryUtils.getSession(this.sessionFactory, true);
        TransactionSynchronizationManager.bindResource(this.sessionFactory, new SessionHolder(session));        

    protected void tearDown() throws Exception {
        SessionFactoryUtils.releaseSession(this.session, this.sessionFactory);


Wednesday, April 28, 2010

Hudson and missing maven repository artifacts

When attempting to make a build in Hudson for the first time, it complained of missing artifacts in the maven repo. I found that I had to configure the MAVEN_HOME/conf/settings.xml file to point to the maven repo by editing the localRepository element

Here's a reference for more details:

Once the initial build was working, I had to make additional changes to make maven do a 'clean package'
and perform a build everytime a change gets submitted to SVN as shown below:

Tuesday, April 27, 2010

SFTP: Received message too long 1936094320

The problem:

Recently the server running SFTP was updated with some security patches applied. Soon afterwards, one of the users couldn't connect via SFTP with the following error:

$ sftp broken_user@server

Received message too long 1936094320

 I spent quite a bit of time searching for solutions and most talked about an echo statement in one of the .bashrc or .bash_profile scripts. However, upon inspection I couldn't find any echo statements.

However, I noticed that when I used the root user it worked fine:

$ sftp root@server 

 The solution:

I did manage to find a workaround, although I am still unable to explain why it works.
I ended up editing the /etc/passwd file and changed a line from:




Thursday, April 8, 2010

Extending an existing Web application WAR


Extend an existing web application, reusing as much as possible including not just the source code, but the web resources such as JSPs, images, CSS styling, Javascript etc...

The simple way would be to branch or copy the existing application and import it into a new project, but this has a major drawback. What if there are bug fixes or changes made to the base web application? If we were to branch, then that would mean having to merge all the changes from the base web application with the new web application! I say GAH!


If you're already using Maven as your build, then it's Maven WAR overlays to the rescue!

An article about Maven WAR overlays can be found here:

To automatically include all dependencies from the base WAR, I use the warpath plugin found here:

You are effectively creating a WAR project that has a dependency on the base WAR project found in your maven repo. Thus, any changes made to the base WAR application can easily be picked up by simply synching up with your SVN code base and rebuilding your new WAR.

In your new web app project, when you make a build, maven will automatically merge the 2 WAR files (base WAR and new app WAR) together to create a new WAR file.

Below is a sample of the pom.xml file to enable this merging:

<project xmlns="" xmlns:xsi=""
          <name>Philip Wu</name>

  <!-- ================ 
   ==================== -->  
  <!-- ================ 
   ==================== -->  



As an 'Overlay' the web.xml files from the two WARs are not merged.
If you're looking to do just that, then you might want to take a look at:

Monday, April 5, 2010

Java String.trim() not trimming Whitespace!

The problem:

I was using String.trim() to remove leading and trailing whitespace characters from an imported string.
However it turns out, that the trim() method only checks for a whitespace characters defined in ASCII.
That is, any non-ASCII whitespace characters will still persist!



Upon inspecting each character of the string, the whitespace character returned a value of 160
The code to inspect is below:
        for (int i=0; i < string160.length(); i++) {                    
            char c = string160.charAt(i);
            int intC = (int) c;


The solution was to remove all instances of this troublesome character with the following code:

string160 = string160.replaceAll("[\\u00A0]", "");