2011-06-22

Using maven to build a qooxdoo application

Qooxdoo is a framework to build rich Internet applications. It is very promising. The qooxdoo language use a syntax similar to javascript, but it is fully object oriented. It generates optimized javascript that runs on most populars browsers.

Qooxdoo code is compiled using python script provided with the SDK.

There are few receipe to build qooxdoo apps using maven, but I rolled up my own, and I share it with you, because I think it is hasslefree. You even don't need to install python if you want!

This is possible thanks to jspresso maven repository that hosts the latest qooxdoo-sdk releases

I'm a maven newbee, so please help me make theses pom.xml snipets simplier or standard.

Let's begin.

Repository

First you need to specify the jspresso repository.

    <repositories>
        <repository>
            <id>jspresso.org</id>
            <url>http://repository.jspresso.org/maven2/</url>
        </repository>
    </repositories>

Plugin

During the compile phase, It fetches jython (Python on the JVM)
Then extracts jython.jar and the Lib directory from the jython-installer.jar
After that, it updates jython.jar (it adds the Lib directory to the jython.jar so it contains all the necessary stuff to compile qooxdoo).

On next compile, it won't do this long process again, since it checks for the existence of the file before fetching it from the Internet.


During the clean phase, it removes all the created files and directories.
As you can see, I've used maven-antrun-plugin since I'm more familiar with ant.
  
<plugin>
    <!-- we download the jython.jar from internet using ant, since it doesn't
        exists in maven repositories -->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.6</version>
    <executions>
        <execution>
            <id>getJython</id>
            <phase>compile</phase>
            <configuration>
                        <target name="getJython">
                        <taskdef resource="net/sf/antcontrib/antcontrib.properties"
                            classpathref="maven.plugin.classpath" />
                        <if>
                            <not>
                                <available file="${project.basedir}/tools/jython.jar" />
                            </not>
                            <then>
                                <get
                                    src="http://sourceforge.net/projects/jython/files/jython/2.5.2/jython_installer-2.5.2.jar/download"
                                    dest="${project.basedir}/tools/jython_installer-2.5.2.jar"
                                    usetimestamp="true" />
                                <unzip src="${project.basedir}/tools/jython_installer-2.5.2.jar"
                                    dest="${project.basedir}/tools" overwrite="false">
                                    <patternset>
                                        <include name="jython.jar" />
                                        <include name="Lib/**" />
                                    </patternset>
                                </unzip>
                                <zip zipfile="${project.basedir}/tools/jython.jar" update="true">
                                    <fileset dir="${project.basedir}/tools/">
                                        <include name="Lib/**" />
                                    </fileset>
                                </zip>
                            </then>
                        </if>                   
                        </target>               
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
        <execution>
            <id>deleteJython</id>
            <phase>clean</phase>
            <configuration>
                <target>
                    <delete dir="${project.basedir}/tools/qooxdoo-sdk"/>
                    <delete file="${project.basedir}/tools/jython_installer-2.5.2.jar" />
                    <delete file="${project.basedir}/tools/jython.jar" />
                    <delete dir="${project.basedir}/tools/Lib" />
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
   <dependencies>
      <dependency>
        <groupId>ant-contrib</groupId>
        <artifactId>ant-contrib</artifactId>
        <version>20020829</version>
      </dependency>
    </dependencies>
</plugin>

Dependencies

In the dependency part, the only needed thing is qooxdoo-sdk :

<dependencies>
        <dependency>
            <groupId>org.qooxdoo</groupId>
            <artifactId>qooxdoo-sdk</artifactId>
            <version>1.4.1</version>
            <type>zip</type>
        </dependency>
<!-- ... -->
</dependencies>


Profiles

The most interesting part is happening in the profiles.
This is where we unpack the qooxdoo-sdk, and we compile the qooxdoo code.

Qooxdoo code is compiled from src/main/qooxdoo and the compiled code will end up in
target/qooxdoo.

The pom.xml file allow two different way of invoking the qooxdoo compiler, one with "native" python. The other one using the jython we downloaded, using jython to compile is a lot slower, but it is convenient since  there is nothing more to install.

If you wish to build the qooxdoo code using native python, you need to download and install it. You will notice that it is a lot faster to compile the qooxdoo source code this way. BEWARE Python 3 is not supported, 2.5+ required.

Here is the profile to build using native python. It activates when you pass -DbuildQooxdoo=true.
You can specify where to look for the python executable if it is not in the PATH.

<profile>
    <id>qooxdoo-building</id>
    <properties>
        <pythonExecutable>python</pythonExecutable>           
    </properties>
    <activation>
        <property>
            <name>buildQooxdoo</name>
            <value>true</value>
        </property>
    </activation>
   
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2</version>
                <executions>
                    <execution>
                        <id>compile-js</id>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <phase>compile</phase>
                    </execution>
                </executions>
        <configuration>
                  <executable>${pythonExecutable}</executable>
                  <workingDirectory>${project.basedir}</workingDirectory>
                  <arguments>
                     <argument>${project.basedir}/tools/qooxdoo-sdk/tool/bin/generator.py</argument>
                     <argument>build</argument>
                     <argument>--macro=QOOXDOO_PATH:${project.basedir}/tools/qooxdoo-sdk</argument>
                <argument>--macro=BUILD_PATH:${project.build.directory}/qooxdoo</argument>
                     <argument>--config=${project.basedir}/src/main/qooxdoo/config.json</argument>
                  </arguments>
              </configuration>
            </plugin>
        </plugins>
    </build>
</profile>



Here is the profile to build using jython. It activates when you pass -DbuildQooxdooJython=true.

<profile>
    <id>qooxdoo-building-jython</id>
    <activation>
        <property>
            <name>buildQooxdooJython</name>
            <value>true</value>
        </property>
    </activation>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2</version>
                <executions>
                    <execution>
                        <id>compile-js</id>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <phase>compile</phase>
                    </execution>
                </executions>
                <configuration>
                    <executable>${java.home}/bin/java</executable>
                    <workingDirectory>${project.basedir}</workingDirectory>
                    <arguments>
                        <argument>-Dpython.home=${project.build.directory}</argument>
                        <argument>-classpath</argument>
                        <argument>${project.basedir}/tools/jython.jar</argument>
                        <argument>org.python.util.jython</argument>
                        <argument>${project.basedir}/tools/qooxdoo-sdk/tool/bin/generator.py</argument>
                        <argument>-v</argument>
                        <argument>build</argument>
                        <argument>--macro=QOOXDOO_PATH:${project.basedir}/tools/qooxdoo-sdk</argument>
                        <argument>--macro=BUILD_PATH:${project.build.directory}/qooxdoo</argument>
                        <argument>--config=${project.basedir}/src/main/qooxdoo/config.json</argument>
                    </arguments>
                </configuration>
            </plugin>
        </plugins>
    </build>
</profile>

Here is the profile to unpack qooxdoo-sdk, it activates when tools/qooxdoo-sdk is missing.

        <profile>
            <id>qooxdoo-unpacking</id>
            <activation>
                <file>
                    <missing>tools/qooxdoo-sdk</missing>
                </file>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <version>2.2</version>
                        <executions>
                            <!--Import qooxdoo sdk and add it to target directory -->
                            <execution>
                                <id>extract-qooxdoo-sdk</id>
                                <phase>process-resources</phase>
                                <goals>
                                    <goal>unpack-dependencies</goal>
                                </goals>
                                <configuration>
                                    <includeArtifactIds>qooxdoo-sdk</includeArtifactIds>
                                    <outputDirectory>tools/qooxdoo-sdk</outputDirectory>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>


Using this pom.xml snipets you should be up and running in a minute!

As I write this post, I notice that I could do better by invoking getJython in the qooxdoo-building-jython profile only. This is one improvement I  see, but you will probably spot more than one!

Happy Qooxdooing!

2011-02-28

FreeFileSync : A free tool to synchronize folders

I recently wanted to synchronize few folders of a Windows machine.
I had some experience with DeltaCopy which is a front end to rsync (cygwin).
It was OK, but I had problems synchronizing some Cyrillic files, and honestly I don't think it is the easiest tool to use...

I wanted something simpler.

I ended up to play with FreeFileSync, an open source application hosted on SourceForge.
I was amazed by the quality of this tool. It is quite efficient and easy to use.

The interface is available in many languages, it is intuitive and has very interesting options. The help is very comprehensive. The tool is well polished and has professional features.

It supports larges files, it has a 64 bits version, it supports synchronization of a huge amount of files and it supports very deep paths. You can use it to backup folders on a network drive. You can mirror, update, and customize the way you want to synchronize folders. The synchronization operation is very fast. You can compare files using the combination of timestamp and size or by content. (I guess md5 is used to compare).  If you wish, you can save your sync configuration and use it later.


There are binaries available for Ubuntu 10.04, however I haven't tried this version yet.


It is a matter of minutes to get used to the tool and to get your job done.

If you need to synchronize folders in a few clicks, and you want a software that is easy to understand FreeFileSync is the tool you need!

Happy syncing! 

2011-02-03

Mockito Recipes

Last year I met a very interesting framework : Mockito.

Mockito is a mock framework for Java. It helps the developer to create mock objects for unit testing.  If you are new to mocking, I suggest you to read Martin Fowler's "Mocks Aren't Stubs".

There are many mock frameworks in Java. I tried JMock before, however I found that Mockito was easier to use. With JMock you must specify all the invocations that occurs on the mock object. If you don't do it, your test will fail. Mockito is more permissive in this way. With Mockito, if you really need to verify an invocation, you just specify it.

Of course, if you practice Test Driven Development, I think it’s better to use JMock’s style. But it’s good to know that you can use Mockito in the JMock’s way, by telling Mockito to verify all the invocations. For this reason, I think Mockito is a more versatile framework for mocking objects.

When you verify the interactions with a mock object, you make sure that it doesn’t make unplanned interactions with an unrelated object. There's an interesting post on the subject by J.B. Rainsberger on his blog.

However, I don't want to spark a debate on mocking techniques, the goal of this post is to get you started with Mockito.

So to start with Mockito :

Add JUnit’s and Mockito’s static imports. It helps the readability of the code.

import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;

Now you are ready to do some mocking!

Here are some Mockito recipes :

List<String> list = mock(List.class);

list.add("Test");
list.clear();

// verification
verify(list, times(1)).add("Test");
verify(list, times(1)).clear();

list.add("Test");
list.clear();
list.contains("Test");

//in order verification
InOrder inOrder = inOrder(list);
inOrder.verify(list).add("Test");
inOrder.verify(list).clear();
try {
// verify that there is no more interaction with the object
 verifyNoMoreInteractions(list);
 fail();
} catch (NoInteractionsWanted e) {
 // Expected
}

// thenThrow pattern
when(list.add(eq("Test"))).thenThrow(
  new RuntimeException("Major failure"));
try {
 list.add("Test");
 fail();
} catch (Exception e) {
 // Expected
}

// thenReturn pattern + chaining
when(list.contains(anyObject())).thenReturn(false).thenReturn(true);
assertFalse(list.contains("Test")); // first thenReturn
assertTrue(list.contains("Test")); // second thenReturn

// thenAnswer pattern
when(list.contains(any(String.class))).thenAnswer(
  new Answer<Boolean>() {
   @Override
   public Boolean answer(InvocationOnMock invocation_)
     throws Throwable {
    // do some stuff here (during the invocation,

    // you could play with the invocation parameters for
    // example)

    return ((String) invocation_.getArguments()[0])
      .startsWith("T");
   }
  });

assertTrue(list.contains("Test"));
assertFalse(list.contains("Best"));

// this boolean is used only for showing
// that the    code in the     answer object
// is really invocated
final AtomicBoolean invocated = new AtomicBoolean();

// mock of a void method call
doAnswer(new Answer<Object>() {
 @Override
 public Object answer(InvocationOnMock invocation_) throws
Throwable {
  // do something useful if it make sense :)
  invocated.set(true);
  return null;
 }
}).when(list).clear();

assertFalse(invocated.get());
list.clear();
// assert that the code in the Answer has been executed.
assertTrue(invocated.get());


// spy on an instance
// lets make a dummy class.
class TestSpy {
 private String _name;

 public void setName(String name_) {
  _name = name_;
 }

 public String getName() {
  return _name;
 }
}
;
// spy on it.
// (be sure to read the javadoc of the spy method prior using it)
TestSpy t = spy(new TestSpy());
t.setName("will");
assertEquals("will", t.getName());

// change the behavior of the getName() method
when(t.getName()).thenReturn("bob");
assertEquals("bob", t.getName());


Happy mocking!

2011-01-08

keepass in a console

Recently I needed to access my keepass database file from a console.  Keepass is a wonderful tool to store passwords. It's Linux counterpart keepassX is available in most major distributions.

However, keepassX is a graphical tool and I was booting from a console based distro with a very limited set of libraries. I said to myself, that it would be cool to be able to open keepass files from the console.

After some Google searches, I've found the ckpass project on sourceforge that does exactly what I wanted.
It depends on ncurses, openssl and libkpass.

I wanted to compile that all statically, so I could put it on my usbkey / dropbox and use it in case of emergency.

Here's a recipe I've made to compile ckpass-0.1 statically (32bit ELF) on Ubuntu 10.04 64bit.
(I needed a 32bit version so it can run on most small Linux distro available)

First of all create a work directory where you are going to download everything. (e.g /tmp/work)

mkdir /tmp/work
cd /tmp/work
wget http://ftp.gnu.org/pub/gnu/ncurses/ncurses-5.7.tar.gz

wget http://mirror.switch.ch/ftp/mirror/openssl/source/openssl-1.0.0c.tar.gz
wget http://downloads.sourceforge.net/project/ckpass/ckpass-0.1.tar.gz
wget http://downloads.sourceforge.net/project/libkpass/libkpass-3/libkpass-3.tar.gz

tar xzf ncurses-5.7.tar.gz
tar xzf openssl-1.0.0c.tar.gz
tar xzf ckpass-0.1.tar.gz
tar xzf libkpass-3.tar.gz

you might need to get the package install libc6-dev-i386
sudo apt-get install libc6-dev-i386


BASEDIR=/tmp/work
PREFIX=${BASEDIR}/install/

OPENSSL_HOME=${BASEDIR}/openssl-1.0.0c
LIBKPASS_HOME=${BASEDIR}/libkpass-3
CKPASS_HOME=${BASEDIR}/ckpass-0.1
NCURSES_HOME=${BASEDIR}/ncurses-5.7

cd ${OPENSSL_HOME}
make clean;./Configure linux-elf -m32  no-dso no-shared --prefix=${PREFIX}  && make install

cd ${LIBKPASS_HOME}
make clean;./configure --prefix=${PREFIX} --disable-shared  LDFLAGS="-L${OPENSSL_HOME} -m32" CFLAGS="-m32 -I${OPENSSL_HOME}/include" && make install

cd ${NCURSES_HOME}
make clean;./configure --prefix=${PREFIX}  --without-cxx  --disable-shared LDFLAGS="-L${OPENSSL_HOME} -m32" CFLAGS="-m32 -I${OPENSSL_HOME}/include" && make install

cd ${CKPASS_HOME}
make clean; ./configure LDFLAGS="-static -L${OPENSSL_HOME}  -L ${LIBKPASS_HOME}/.libs -m32 $(${PREFIX}/bin/ncurses5-config --libs)" CFLAGS="-m32 -I${OPENSSL_HOME}/include -I${LIBKPASS_HOME}/src $(${PREFIX}/bin/ncurses5-config --cflags)" LIBS="-lssl -lcrypto -lkpass -lncurses" && make && strip ckpass

After everything is done  "file ckpass" should output this :

ckpass: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, for GNU/Linux 2.6.15, stripped


I recommend to build every library one by one (do not copy & paste the whole thing). If you have an error during the execution of configure, check config.log, it contains precious informations to help you debug.

If you want to try it yourself and you have some problems doing it, feel free to post a comment, I will try to help you out.

For your convenience, I've uploaded the binary here


UPDATE
If you have a problem when starting ckpass (error opening terminal)

Don't forget to export the TERMINFO before executing ckpass.

export TERMINFO=/usr/share/terminfo/

on some distribution you will have to do :

export TERMINFO=/lib/terminfo/