Saturday, June 25, 2011

JUnit Theories

A Theory is slightly different from a parameterised test. A theory defines a "universal truth" which should hold in possibly infinite numbers of potential scenarios (as long as assumptions hold). For example, the statement, "if two objects are equal, then they must have the same hashcode", is a theory.

To set up a Theory in JUnit 4.4, you need to:

  • Annotate the test class with @RunWith(Theories.class)
  • Create test data using the @DataPoints or @DataPoint annotation
  • Write a method (the "theory") annotated with @Theory and accepting arguments of your data points
The example below shows a test for the Theory that if two objects are equal, they must have the same hashcode. You can define your data points in different ways as I've demonstrated below. When you run the test, the method testHashcodeEquals will be called with all possible permutations of your test data e.g. (foo,foo), (foo,bar), (foo,baz), (foo,1), (foo,2), (bar, bar), (bar, foo) etc.
import static;
import static org.junit.Assert.assertThat;
import static org.junit.Assume.assumeThat;

import org.junit.experimental.theories.DataPoint;
import org.junit.experimental.theories.DataPoints;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;

public class HashcodeEqualsTheory {

  public static String[] data = new String[]{"foo", "bar"};

  public static String baz = "baz";

  public static Integer[] getData(){
      return new Integer[] {1, 2};

   * If two objects are equal, they must have the same hashcode
   * @param x
   * @param y
  public void testHashcodeEquals(Object x, Object y){
      assumeThat(x.equals(y), is(true));
      assertThat(x.hashCode(), is(y.hashCode()));
Related Posts:
Parameterized Tests in JUnit

Parameterized Tests in JUnit

A parameterised test allows you to run a test against a varying set of data. If you find yourself calling the same test but with different inputs, over and over again, a parameterised test would help make your code cleaner. To create one in JUnit you need to:
  • Annotate the test class with @RunWith(Parameterized.class)
  • Create a static method annotated with @Parameters which returns a collection of arrays. Each array in the collection represents the test data and will be injected into the constructor of the test class
  • Make sure that the test class constructor matches the array items in the Parameters method and stores the test data
  • Write some test methods annotated with @Test
The example below shows a parameterised test which tests array sorting. The data method returns a collection of arrays. Each array contains an input array and an expected output (sorted) array. When you run the test, the class is instantiated with each of these arrays and then the testSort method is called.
import static org.junit.Assert.assertArrayEquals;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

public class ArraySortTest {

  public static Collection<Object[]> data() {
      return Arrays.asList(new Object[][] {
         { new int[] { 1, 3, 2 }, new int[] { 1, 2, 3 } },
         { new int[] { 3, 2, 1 }, new int[] { 1, 2, 3 } },
         { new int[] { 1, 2, 3 }, new int[] { 1, 2, 3 } },
         { new int[] { 1, 1, 1 }, new int[] { 1, 1, 1 } },
         { new int[] { 1 }, new int[] { 1 } },
         { new int[] {}, new int[] {} },

  private final int[] actual;
  private final int[] expected;

  public ArraySortTest(int[] actual, int[] expected){
      this.actual = actual;
      this.expected = expected;

  public void testSort(){
      assertArrayEquals(expected, actual);
Javadocs: Class Parameterized

Saturday, June 18, 2011

Efficiently Navigating Directories on UNIX

I find myself, like most developers, spending a lot of time navigating directories. Flipping back and forth between logs and application directories with long names can be quite tedious. So, with the help of a few new functions, aliases and config tweaks I've made the navigation process easier and more efficient. You no longer need to remember long paths because you can jump straight to them using their names. You can also choose to bookmark your favourite directories. Here is my setup:

1. Go up to a specific directory
I have a function called upto which allows you to jump up to any directory, on the current path, just by name. This is very useful if you are deep in a directory. I also have autocompletion for this function, so that it shows me valid directory names and completes them for me.

# Go up to the specified directory
  if [ -z $1 ]; then
      echo "Usage: upto [directory]"
      return 1
  local upto=$1
  cd "${PWD/\/$upto\/*//$upto}"

# Completion function for upto
  local cur=${COMP_WORDS[COMP_CWORD]}
  d=${PWD//\//\ }
  COMPREPLY=( $( compgen -W "$d" -- $cur ) )
complete -F _upto upto
[/www/public_html/animals/hippopotamus/habitat/swamps/images] $ upto h[TAB][TAB]
habitat       hippopotamus
[/www/public_html/animals/hippopotamus/habitat/swamps/images] $ upto hippopotamus
[/www/public_html/animals/hippopotamus] $
2. Go up a specific number of directories
If you know how many levels you want to go up, you can use the up function e.g. up 5 will move you up 5 directories.
# Go up a specified number of directories
  if [ -z $1 ]
    cd ..
  local levels=$1
  local result="."
  while [ $levels -gt 0 ]
  cd $result
3. Go down to a specific directory
Sometimes you want to change to a directory but can't remember the path, or the path name is too long to type. I have a function called jd which allows you to jump down to a directory any level below the current one. It uses Bash's globstar feature so make sure you have it enabled (using shopt -s globstar). (Warning: this may be slow on large directory structures because of the searching involved.)
# Jumps to a directory at any level below.
# using globstar
  if [ -z $1 ]; then
      echo "Usage: jd [directory]";
      return 1
      cd **/$1
[/www/public_html/animals/hippopotamus/habitat/swamps/images] $ upto hippopotamus
[/www/public_html/animals/hippopotamus] $ jd images
[/www/public_html/animals/hippopotamus/habitat/swamps/images] $
The CDPATH variable is a colon-separated list of directories in which the shell looks for destination directories specified by the cd command. Mine is shown below. No matter what directory I am currently in, I can quickly jump to a project in my dev directory with cd <project> because it is on my path.
export CDPATH=".::..:../..:~:~/dev/"
5. Shell Options
I have set the following useful shell options in my .bashrc. The autocd option allows you to change to a directory without using the cd command and cdspell automatically corrects typos in directory names.
shopt -s cdspell     # correct dir spelling errors on cd
shopt -s autocd      # if a command is a dir name, cd to it
shopt -s cdable_vars # if cd arg is not a dir, assume it is a var
6. Quick Aliases
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'
alias ......='cd ../../../../..'
7. Keeping a history of visited directories
I came across a useful post on Linux Gazette: History of visited directories in BASH. It contains a script which maintains a history of directories you have visited and then allows you to switch to them easily using a reference number. The command cd -- shows you your history and cd -2 would take you to the second item in your history list. For example:
[/www/public_html/animals] $ cd --
 1  /tmp
 2  /www/public_html/animals/hippopotamus/habitat/swamps/images
 3  /www/public_html/animals/lion
[/www/public_html/animals] $ cd -2
[/www/public_html/animals/hippopotamus/habitat/swamps/images] $
I spend a lot of time moving between different directories especially between logs and application directories. I have implemented a bookmarking feature which allows you to bookmark your favourite directories and then change to them easily.
  • bm: bookmark the current directory
  • bcd: change to the specified bookmark
  • brm: remove a bookmark
  • bcl: clear all bookmarks
  • bll: list all bookmarks
# Directory Bookmark Functions

# Add a bookmark, if it doesn't exist
  local val=$(pwd)
  for i in ${bookmarks[@]}
    if [ "$i" == "$val" ]
       return 1

# Goto specified bookmark
# or previous one by default
  if [ -z $index ]
  local val=${bookmarks[$index]}
  if [ -z $val ]
     echo "No such bookmark. Type bll to list bookmarks."
     return 1
     cd "$val"

# Remove a bookmark
  if [ $# -lt 1 ]
     echo "Usage: brm [bookmark-index]"
     return 1
  if [ -z ${bookmarks[$1]} ]
     echo "No such bookmark"
     return 1
  bookmarks=(${bookmarks[@]:0:$1} ${bookmarks[@]:$(($1 + 1))})

# Remove all bookmarks

# List all bookmarks
  if [ ${#bookmarks[@]} -ne 0 ]
     local i=0
     while [ $i -lt ${#bookmarks[@]} ]
       echo $i: ${bookmarks[$i]}
  return 0

If you have any useful directory-related functions, share them in the comments below!

Related Posts:

Saturday, June 11, 2011

HTML5 - Adding Links to a Canvas

There is no easy way to create links on an HTML5 Canvas. It would be nice if there was, but you have to do it all manually. First draw the link text and then listen to mouse move and mouse click events. When the mouse moves over your link, you need to change the cursor to a "hand" and when the mouse clicks on your link you need to make the browser go to that page.

Here is a demo html page. Save the code to a file and open it in Firefox:

  <script type="text/javascript">

var canvas = document.getElementById("myCanvas");
var ctx;
var linkText="";
var linkX=5;
var linkY=15;
var linkHeight=10;
var linkWidth;
var inLink = false;

function draw(){
  canvas = document.getElementById("myCanvas");
  // check if supported


    //clear canvas
    ctx.clearRect(0, 0, canvas.width, canvas.height);

    //draw the link
    ctx.font='10px sans-serif';
    ctx.fillStyle = "#0000ff";

    //add mouse listeners
    canvas.addEventListener("mousemove", on_mousemove, false);
    canvas.addEventListener("click", on_click, false);


//check if the mouse is over the link and change cursor style
function on_mousemove (ev) {
  var x, y;

  // Get the mouse position relative to the canvas element.
  if (ev.layerX || ev.layerX) { //for firefox
    x = ev.layerX;
    y = ev.layerY;

  //is the mouse over the link?
  if(x>=linkX && x <= (linkX + linkWidth) &&
     y<=linkY && y>= (linkY-linkHeight)){ = "pointer";
  else{ = "";

//if the link has been clicked, go to link
function on_click(e) {
  if (inLink)  {
    window.location = linkText;
 <body onload="draw()">
  <canvas id="myCanvas" width="150" height="200">Canvas not supported.</canvas>
Related Posts:
HTML5 Canvas - Bouncing Balls

Saturday, June 04, 2011

Associative Arrays in Bash 4

Associative arrays allow you to store key-value pairs and retrieve values using their keys. You can think of them as maps (or hashes) in other programming languages. The following example illustrates how associative arrays can be used to map countries to their capital cities:

#declare an associative array
declare -A capital

#there are different ways to populate the array:
capital=([UK]="London" [Japan]="Tokyo")


#you can even append using +=
capital+=([Belgium]="Brussels" [Egypt]="Cairo")

#print the number of entries
echo "Size: ${#capital[@]}"

#retrieve the capital of Germany
echo "Capital of Germany: ${capital[Germany]}"

#iterate over the keys and print all the entries
echo "Country -> Capital"
for country in "${!capital[@]}"
   echo "$country -> ${capital[$country]}"
Size: 6
Capital of Germany: Berlin
Country -> Capital
UK -> London
Germany -> Berlin
Belgium -> Brussels
China -> Beijing
Japan -> Tokyo
Egypt -> Cairo
Further reading:
Bash, version 4, Associative Arrays