About eric

Founder and CTO of Audaxis.

Offline Blogging

I’m not a regular blogger and usually when I decide to post something, I’m disconnected. So I looked for a linux offline blog editor. I found two editors:

I’m just posting this with BloGTK to see the result. BloGTK let you select different API and I used the MetaWeblog API. I tried the Blogger API but then I can’t edit the title. I also tried Drivel it doesn’t offer the MetaWeblog API (apparently) and the Blogger API does not allow to enter the title of the blog (It’s probably a setting issue but I can’t figure out how to fix it).

By the way, I will test BloGTK for a while.

Mock Objects with EasyMock and JMockit

Static method mocking


Performing unit testing with mock object is quite common. I’ve been used to do it with Easymock and sometimes JMock. However, in some cases, I had to face the issue where I wanted to mock the call to a static method. Of course, this could be due to a wrong design, but sometimes you don’t have much choice.

Using EasyMock or JMock, you can’t mock static method. But recently I found the project JMockit. JMockit depends on the JVM class redefinition mechanism exposed by java.lang.instrumentation to redefine specific method of a java class. Therefore, if you want to mock a static method, all you have to do is to create a mock class redefining the static method and to call Mockit.redefineMethods with the original class and the mock class as parameters.

Of course, since JMockit depends on the the JVM class redefinition mechanism, the units tests must be run with a JAVA 5 SE VM. Although the test code can still be compiled with a older compiler version.

In the following example, the method getDates of the ROCalParutionTitre class was calling the static method getAllDatesCalendrier of ROCalParutionDate class.

So I first create a mock class ROCalParutionDateMockit redefining the method getAllDatesCalendrier and during the unit test I used Mockit.redefineMethods(ROCalParutionDate.class, ROCalParutionDateMockit.class) to mock RoCalParutionDate class. To avoid troubles with the other tests, at the end of the unit test, I call Mockit.restoreAllOriginalDefinitions() to restore the normal situation.

The code below show a example where both EasyMock and JMockit are used to perform the unit testing.

    public class ROCalParutionDateMockit {
   
        public static ROCalParutionDate [] returnValue;
   
        public static ROCalParutionDate[] getAllDatesCalendrier(ROCalParutionTitre titreCal) {
            return returnValue;
        }
    }

        public void testGetDates() throws NoSuchMethodException {
                MockClassControl tC =  MockClassControl.createControl(ROCalParutionTitre.class,
                                   new Method[] { ROCalParutionTitre.class.getMethod(“getExactDate”,new Class[] { Timestamp.class }) } );
                ROCalParutionTitre t = (ROCalParutionTitre) tC.getMock();
               
                MockControl d1C = MockClassControl.createControl(ROCalParutionDate.class);
                ROCalParutionDate d1 = (ROCalParutionDate) d1C.getMock();
               
                MockControl d2C = MockClassControl.createControl(ROCalParutionDate.class);
                ROCalParutionDate d2 = (ROCalParutionDate) d2C.getMock();
               
                MockControl d3C = MockClassControl.createControl(ROCalParutionDate.class);
                ROCalParutionDate d3 = (ROCalParutionDate) d3C.getMock();
               
                ROCalParutionDate[] dateArray = {d1, d2, d3};
                GregorianCalendar c = new GregorianCalendar(2006, 07, 01);
                Timestamp t_from = new Timestamp(c.getTimeInMillis());
                c.add(Calendar.DATE, 1);
                Timestamp t1 = new Timestamp(c.getTimeInMillis());
                c.add(Calendar.DATE, 1);
                Timestamp t2 = new Timestamp(c.getTimeInMillis());
                c.add(Calendar.DATE, 1);
                Timestamp t3 = new Timestamp(c.getTimeInMillis());
                c.add(Calendar.DATE, 1);
                Timestamp t_to = new Timestamp(c.getTimeInMillis());
               
                String freq = “L”;
               
                ROCalParutionDateMockit.returnValue = dateArray;
                Mockit.redefineMethods(ROCalParutionDate.class, ROCalParutionDateMockit.class);

                d1.getDateTraiteDu();
                d1C.setReturnValue(t1);
               
                d1.getDateTraiteAu();
                d1C.setReturnValue(t1);
               
                d1.include(freq);
                d1C.setReturnValue(false);
               
                d2.getDateTraiteDu();
   &nbs
p;            d2C.setReturnValue(t2);
               
                d2.getDateTraiteAu();
                d2C.setReturnValue(t2);
               
                d2.include(freq);
                d2C.setReturnValue(true);
               
                d3.getDateTraiteDu();
                d3C.setReturnValue(t3);
               
                d3.getDateTraiteAu();
                d3C.setReturnValue(t3);
               
                d3.include(freq);
                d3C.setReturnValue(false);
               
                d1C.replay();
                d2C.replay();
                d3C.replay();
               
                ROCalParutionDate[] dates = t.getDates(t_from, t_to, freq);
               
                assertNotNull(dates);
                assertEquals(1, dates.length);
                assertEquals(d2, dates[0]);
               
                d1C.verify();
                d2C.verify();
                d3C.verify();

                Mockit.restoreAllOriginalDefinitions();

        }
   

Constructor mocking


As I was incresing my unit test coverage, I came to a situation where I wanted to mock the constructor of an object. Unfortunately, neither EasyMock, JMock nor JMockit could help me on that. Of course a better design (using a factory and interfaces in place of object) could have help me.

In this case, the only solution I could see is AOP. With AOP, you can intercept the constructor call and use an aspect to overwrite it. In fact, using AOP, you could almost do everything you want. 

Ruby on Rails

Recently, I tested Ruby on Rails. By testing, I means I decided to use Rails to develop the administration tools the backend of a web site (mainly a CRUD application). I wanted to compare Rails to Tapestry in term of development time for such a thing.

Even if Rails is a nice framework and allow to quickly develop web application, I’m not sure I will switch over from Tapestry to Rails. In fact, with my actual Tapestry knowledge, there are things I found easier to do with Tapestry compare Rails (but I admit it, my Rails knowledge was very limited).

It’s true that Tapestry as it is, is missing some of the facilities existing in Rails for developing CRUD application (like the scaffold generation). Although, tools like Trails or cognition are solving part of this issue.

Of course, helper methods like dynamic find_by in Rails are really helpfull and eliminate repetitive coding and Rails is plenty of such “tricks”. I think that if Trails had a set of such helper methods (and it probably has already some of them), it would really compete with Rails.

I didn’t test the Ajax integration between in Rails and again it’s probably a point where it’s really simple with Rails and where it should be really simple with Tapestry once you’ve found the right components (Tacos, XTile).

The scripting aspect of Rails, of course, helps the agile development approach. There isn’t any deployment issue, the modifications are immediately accessible. With Tapestry (as we most of the Java web application framework), there is always a deployment phase (by the way, the dynamic class reloading foreseen in Tapestry 5 should facilitate such a development approach).

In conclusion, I’m really willing to learn from Rails and implement around Tapestry a set of equivalent features. By implementing such feature in Tapestry, we could still improve its productivity.

Genetic algorithms and Java

Recently I had to quickly find a way to find the best gaussian curve fitting a set of data. After having search on the web for a specific algorithm, I end up with the JGAP project.

The JGAP project offers a library to easily create an application using a genetic algorithm. Since genetic algorithm can be used to solve regression problems, I decide to make a test with JGAP.

The JGAP project has a very clear API, with true “genetician” vocabulary. In less that 2 hours, I had my curve fitting application. I still have some convergence issues when the number of samples become high but I’m confident in the result.

CropCircles ou AgroGlyphes a Waterloo

 

Durant une des nuits entre le 15 et le 17 juillet, un “CropCircle” est apparu dans un champs.

Je me suis rendu sur place et je dois dire que c’est assez impressionnant. Quelles sont les origines d’un tel phénomène? La taille et la régularité du motif m’intrigue. Si c’est l’oeuvre d’un artiste, je me demande comment il arrive à un tel résultat.

En faisant une recherche sur le Net, je suis tombé sur la page de Anne Marie et François Godet. Apparemment, dans la nuit du 17 juin, un premier agroglyphe était déjà apparu au pied du lion de Waterloo.

Google PageRank

I was recently looking at the referencing of http://www.audaxis.com and specially at the page ranking of the home page.

There is a lot of litterature on the web concerning page ranking and I’m not intend to rewrite all this here. The basic principle is the more you have links pointing to your pages, the better will be the page rank.

I just came accros a Page Rank calculator and I found that the page rank of http://www.audaxis.com was 5. In order to see if it will have any impact, I decide to add a link to the Audaxis home page on this blog. It will probably have a invisible effect, but that was just for the fun

instanceof and Hibernate lazy collection

I discovered the Proxy Visitor Pattern after having encountered the “proxy problem”.

In short, the “proxy problem” occurs when you want to cast or test the class of an instance coming from a lazy loaded polymorphic collection. By polymorphic collection, I mean a hibernate collection containing instances of polymorphic object.

Because of the lazy loading process, the true instance are replaced by proxies, therefore the instanceof does not work.

Luckily, The Proxy Visitor Pattern proposes a solution to this issue.

However, recently I had to extend this pattern. I needed the reference of the true instance to pass it as a parameter to another method. Because of the proxy, I couldn’t cast the collection instance. I create a Visitor as followed:

public class VisitorImpl implements Visitor {
public static final int FORM_FIELD = 1;
public static final int FORM_BLOCK = 2;
protected FormElement lastChecked;
protected boolean lastCheckedTrace = 0;
public boolean isFormField() {
return (lastCheckedTrace & FORM_FIELD) != 0;
}
public boolean isFormBlock() {
return (lastCheckedTrace & FORM_BLOCK) != 0;
}
public FormElement getLastChecked() {
return lastChecked;
}
public void visit(FormField field) {
lastChecked = field;
lastCheckedTrace = FORM_FIELD;
}
public void visit(FormBlock block) {
lastChecked = block;
lastCheckedTrace = FORM_BLOCK;
}
}

I can check the type of the collection instance proceeding as followed:

...
Visitor checker = new Visitor();
for (Object tabElObj : tab.getFormElements()) {
FormElement element = (FormElement) tabElObj;
//Is the element a form field
element.accept(visitor);
if (visitor.isFormField())
{
// do something with (FormField) visitor.getLastChecked() ...
}
}
...

HTML large table construction

Recently, I had to build a HTML page with 8 large tables (number of line > 150) on it. I tried 3 solutions:

  • Download of the table content through AJAX call and javascript creation with table.insertRow() and row.insertCell().
  • Download of the table content through AJAX call and javascript creation by creating a large string with the table content and then set a div.innerHTML content.
  • Server side generation.

The results are quite surprising. From the 3 solutions, the second one is the most performing.

Before explaining in detail the difference of the 3 approaches, I should mention that using the 3rd solution, the page size was about 600K. This give an idea of the tables size.

1. AJAX and insertRow() approach

This solution works quite well. However, it has some drawbacks.

It only works on IE. Indeed, insertRow() is not standard Javascript. And the table generation is quite expansive in term of CPU load.

2. Ajax and table construction using string and innerHTML

This is the most performing approach. The table construction take half the time of the 1st approach

3. No AJAX and table construction on the server side

This really surprised me. I was expecting this approach to be the most performing not taking into account the download time. And surprisingly, this is not the case. The table rendering on the client is 2 times slower that the 1st approach and 4 times slower than the second. Why is that, I can’t really explain. Maybe, is it due to the page structure (the 8 tables are rendered in hidden div used to create tab rendering).