Monday, March 24, 2014

DBunit table record counting

During unit testing is useful to verify that requested operation was really performed in db. Lets have a simple test for deleting method. This method just delete user record in database. Test could look like:

public void testDelete() throws Exception{
 userDao.delete(5);
 restartSession();
 
 assertEquals(getConnection().createDataSet().getTable("user").getRowCount(),
  getDataSet().getTable("user").getRowCount() - 1);
}

This test look clear. however there is possible problem. Running this code could ends with exception:

org.dbunit.database.AmbiguousTableNameException: user
 at org.dbunit.dataset.AbstractDataSet.getTable(AbstractDataSet.java:101)
 at org.dbunit.dataset.ReplacementDataSet.getTable(ReplacementDataSet.java:182)
 ...

When you look at exception source problem is clear. The problem are testing data. When you have testing data in XML file in following shape:

 <user id_user="3" login_name="john" user_name="John" password="e"
  public_profile="1" app_right="1" />

 <color id_color="1" />
 <color id_color="2" />

 <user id_user="5" login_name="clara" user_name="Clara" password="e"
  public_profile="1" app_right="1" />

Thank DBunit took it as two table user definition. Each definition have it's own list of rows with data. So solution is obvious, just re-order data definition XML file.
 <user id_user="3" login_name="john" user_name="John" password="e"
  public_profile="1" app_right="1" />
 <user id_user="5" login_name="clara" user_name="Clara" password="e"
  public_profile="1" app_right="1" />

 <color id_color="1" />
 <color id_color="2" />

Thursday, March 13, 2014

Java3D and headless mode

Java3D headless mode is a way how to tell Java that there is not physical screen device and that there is no graphics acceleration. Headless model could be set in this way:

java -Djava.awt.headless=true 

In headless mode some object can't be instantiated, especially UI related objects from java.awt package like Button. More about headless could be found at http://www.oracle.com/technetwork/articles/javase/headless-136834.html.

Java3D application usually build 3D model and that draw it to Canvas3D. Sometimes server need to generate some images which are using Canvas3D. Sadly in headless mode it's not possible. Because it's not possible to instantiate Canvas3D. Lets look at following code:

public void testSimple() throws Exception {
 System.setProperty("java.awt.headless", "true");
 GraphicsConfigTemplate3D template = new GraphicsConfigTemplate3D();
 GraphicsConfiguration config = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getBestConfiguration(template);
 Canvas3D canvas3d = new Canvas3D(config, true);
}

In headless mode GraphicsEnvironment.getLocalGraphicsEnvironment() return instance of HeadlessGraphicsEnvironment. Method HeadlessGraphicsEnvironment is implemented in following way:

   public GraphicsDevice getDefaultScreenDevice()
         throws HeadlessException {
         throw new HeadlessException();
    }

So it always throw HeadlessException. Because of that it seems that Java3D can't be used with enabled headless mode.

Wednesday, March 12, 2014

Raspberry PI - create installed SD card

Here will be described some basic steps with raspberry PI. Following text is just notes that helps me later reproduce all steps.

Install Raspbian image to SD card

It's important find exact name device name of SD card. Try to remove and insert SD card and look at changes in /dev/ directory. Also look at "System Information" application. Final command for copy image to SD card is following:
sudo dd bs=1m if=/Users/jan/Downloads/2014-01-07-wheezy-raspbian.img of=/dev/rdisk2

How to perform install in details is described at elinux.org/RPi_Easy_SD_Card_Setup.

Backup SD card to image file

It's similar to previous example

sudo dd bs=1m if=/dev/rdisk2 of=/Users/jan/Downloads/backup-2014-3-19.img

Before copying backup to another SD card, it's necessary remove all partitions that will be overwritten. For examples I would like to put image at /dev/rdisk2 so I have to execute:

diskutil unmountDisk /dev/rdisk2s1
diskutil unmountDisk /dev/rdisk2s2

Replace SD card with backup:

sudo dd bs=1m if=/Users/jan/Downloads/backup-2014-3-19.img of=/dev/rdisk2
More details about dd command could be found at en.wikipedia.org/wiki/Dd_(Unix)

sudo apt-get update sudo apt-get upgrade sudo raspi-config http://www.raspberrypi.org/camera