Javascript API testing

30 12 2013

Testing your javascript library/API

I wrote a small javascript API, HtmlAssert.js, because I needed to be able to test HTML content in javascript for a project I’m working on.

I did it TDD style, with Grunt, Jasmine and PhantomJS.
I used Jasmine to write my unit tests because this API validates HTML content, so I am using the DOM of the HTML document, and in particular, the window.document element to generate an HTML document that I can easily parse in javascript.
Therefore, in order to have the window object, tests need to be executed in a browser (I tried to use jsdom but the c++ compilation part is not straightforward at all, and I don’t have much time to waste).

So with Jasmine, everything was cool, I had my library and my unit tests.

Then came the time to include that API in the project I am working on, and I realized that the best was probably to create a Node.js module.
It is when the chaos started, because my Jasmine unit tests couldn’t be used for a Node.js module with my project stack.

Client-side testing vs Server-side testing.

When you test client-side, you are basically executing your test in a browser and so you have access to the window object.
When you test server-side, you are executing your test in a javascript only stack and you can’t access to the window object.
Jasmine is the testing library that I use to execute my tests.
Grunt is the utility I use to execute my tasks, in particular executing jasmine to run my tests.
When executed from Grunt, grunt-contrib-jasmine is executing tests client-side using PhantomJs as a (headless) browser.
Which means that you don’t have access to Node.JS modules support and the require(“module”); won’t work.
And that is a problem, since you want to use this, in order to test your node module, in the same condition than when you will use your module in a real project.

The other way, running jasmine test server-side is not a good solution either, because you won’t have access to the browser’s window object, so if you are testing browser-related code, it will not work.

Finally I manage to find the solution, using CasperJS, that allows me to run test with node modules and have access to the browser (PhantomJS too) even on the server side.


You can look at my project HtmlAssert.js,
The Gruntfile.js has the definition of the CasperJS task,
inside the /test directory you will see how I wrote my tests using CasperJS’s BDD capabilities.

Please note that I’m a Java developer learning modern javascript, so if you are experienced with javascript, and any part of my explanations seems weird to you, do not hesitate to give your comments!!! Thanks!

Terracotta toolkit

2 12 2013

The Terracotta Toolkit is an API that provides you many functionalities in a clustered manner.
E.g., you can get a Clustered ReadWrite Lock, so you can do synchronization between different applications running on different machines/JVMs.

In 3.7, you get the Toolkit like this:

ClusteringToolkit toolkit = new TerracottaClient("localhost:9510").getToolkit();

In 4.0, you get the Toolkit like this:

Toolkit toolkit = ToolkitFactory.createToolkit("toolkit:terracotta://localhost:9510");

Then you can use it to get the functionalities you want, e.g.:
Clustered Collections (List, Map, Set):

ToolkitList clusteredKit = toolkit.getList("myList", String.class);

Clustered lock:

ToolkitReadWriteLock rwLock = toolkit.getReadWriteLock("myReadWriteLock");

Clustered Barrier

ToolkitBarrier clusteredBarrier = toolkit.getBarrier("myBarrier", 4);

Learn more here:

A neat iPhone trick

16 08 2013

** I usually write posts about development, merely to keep a trace for myself, but I wanted to share this information because I’ve never met yet an iPhone owner who knew that trick so I guess this must be shared, don’t hesitate to tell to the other iPhone owners you’ll meet **

So you want to take a photo with your iPhone…

Let’s take the case where you put your iPhone in landscape orientation, i.e. like this:


How do you take the picture now? Well you already know it, you’ll touch the camera icon:


However there’s another way, which is way more handy, it’s by using the “+” button :


As you see, it will let you hold the iPhone like a traditional camera and it will be more stable to take your photo…


So that’s it, hope that it will help you! Do not hesitate to share this trick!!

More tricks to come… (that you might know or not)

Remove number of missed call on phone icon
Kill an application from memory
Preview the photo you just take
Go to top of mail, sms threads
Quick access to the music player

Use SLF4J logging with Spring

11 08 2013

Long story short, Spring using Commons logging as a logging API, but chances are that you are using SLF4J in your project, so here what you’ll need to do to make Spring use SLF4J instead of commons logging.

1) Remove all commons-logging dependencies
In maven, you’ll have to exclude the commons-logging dependency from each Spring module, e.g.



2) Add SLF4J and jcl-over-slf4j
jcl-over-slf4j Will do the binding between Spring and SLF4J.
In this example I use logback as the logging api.




Reclaim some memory on Mac OS

18 07 2013

This is a recurring issue for me, having not enough memory on my Mac, so here are some useful commands:

Display how much the memory is used by processes, reclaim some and verify again:

ps -axm -o "rss,comm" | awk 'BEGIN { s=0;}; {s=s+$1;}; END { printf("%.2f GB\n", (s/1024.0/1024));}'
ps -axm -o "rss,comm" | awk 'BEGIN { s=0;}; {s=s+$1;}; END { printf("%.2f GB\n", (s/1024.0/1024));}'

Use your vps as a proxy

30 06 2013

This is an old trick that I need to note, because I always forget the exact syntax.
It creates a SSH tunnel on a server where you have access, and you can use this tunnel to connect to the internet through that server.
Knowing that you can rent a cheap VPS in the USA for a couple of dollars, it can be practical for instances when you want to watch videos that can only be accessed there.

So there are three steps:

1) Create the SSH tunnel
ssh -D 5555 yourlogin@yourserver

2) Setup Firefox to use that tunnel as a proxy

3) Check that you are really using that tunnel
That site is nice because it gives your IP and its geographical position:

hostname tab completion in your shell

20 06 2013

sometimes you work in an environment with hostnames following intricate conventions

Thankfully you can enable hostname bash completion:


add in your ~/.bashrc

complete -W "$(echo `cat ~/.ssh/known_hosts | cut -f 1 -d ' ' | sed -e s/,.*//g | uniq | grep -v "\["`;)" ssh


add in your ~/.zshrc

local knownhosts
knownhosts=( ${${${${(f)"$(<$HOME/.ssh/known_hosts)"}:#[0-9]*}%%\ *}%%,*} )
zstyle ':completion:*:(ssh|scp|sftp):*' hosts $knownhosts

I recommend to switch to Zsh by the way!


Get every new post delivered to your Inbox.