In pursuit of simplicity

HTML 5 – Bad days for Flash

Just found another great example of HTML 5.

http://code.google.com/p/quake2-gwt-port/

Those guys have  ported Quake via GWT to pure HTML and JavaScript. I am pretty sure it will behave well only on WebKit, yet it shows the new approach the web is going, “sadly” without Adobe Flash.

Great news. HTTP4e was released few weeks ago. Today I found some time to post it.

New features:

  • Introducing @file abilities to load external payload to body by simply passing @/file/path
  • Export HTTP sessions report as HTML
  • Export and import HTTP4e replay script
  • Importing raw HTTP packets
  • Importing http packets directly from Firefox’s Live HTTPHeaders
  • Enhancing tabs
  • Fixing Pretty view XML formatting
  • JMeterone click script generation
  • PHP, Flex/ActionScript, Cocoa/Objective-C, Ruby, Pythonone click script generation
  • C#, Visual Basic.NETone click script generation
  • JavaScript, Prototype, jQuery one click script generation
  • Apache HTTP Components 4.x one click code generation

Stay tuned for more news and examples.

Beautiful GWT animation libraries

Probably many of you needed to add Mootools, jQuery or Scriptaculous kinda effects within your GWT code.

I came across those two and I am spreading them to web:

Brand new, dead-easy to use library called GWT-FX:
http://code.google.com/p/gwt-fx/

A GWT wrapper over Mootools:
http://www.amateurinmotion.com/projects/gwt-mootools.html

Every now and then I need to use the Velocity templating. And the first thing I always experience is that nasty failure.

SEVERE: ResourceManager : unable to find resource 'template.vm'
 in any resource loader.
Exception in thread "main"
org.apache.velocity.exception.ResourceNotFoundException:
 Unable to find resource 'template.vm'
...

Velocity is so picky and always complains about the .VM template not being found. And I always start fiddling around, making sure file name is correct, making sure the VM file is right file location, class path location and yet same cruel error ResourceNotFoundException…

The answer for “properly” configuring Velocity and making it happy is bellow:

Properties p = new Properties();

// Uncomment if template.vm is being loaded from file system
//p.setProperty( "resource.loader", "file" );
// absolute or relative path
//p.setProperty( "file.resource.loader.path", "./src" );
//p.setProperty( "file.resource.loader.class",
   "org.apache.velocity.runtime.resource.loader.FileResourceLoader" );

// Uncomment if "template.vm" is being loaded from class path, jar, zip,..
//p.setProperty( "resource.loader", "class" );
//p.setProperty( "class.resource.loader.class",
  "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader" );

VelocityEngine ve = new VelocityEngine();
ve.init(p);
Template t = ve.getTemplate("template.vm");

All said, I am not sure why Velocity team designed it in such a strict way. Instead the engine should be smart enough to find the template directly from Class path location or File resource. Why so difficult?

One year ago I was involved with Selenium as part of a web automated testing project. I discovered multiple limitations with the Selenium framework, mainly with its integration with existing CruiseControl architecture. Having some time lately, I explored the idea of Selenium4j, a framework translating Selenium HTML scripts to Java Junit tests. I hosted to project at

http://code.google.com/p/selenium4j/

Problems with Selenium IDE HTML tests

  • While Selenium IDE is great tool for creating HTML tests, the HTML scripts are not Java tests and as such are not usable outside of the Selenium IDE environment
  • Using the IDE to create the Java JUnit test works one way only – from HTML to Java. Re-using the java test within Selenium IDE proved to be unreliable and buggy
  • Using Selenium RC to invoke the HTML files is not practical solution as it allows only a limited configuration such as browser, server and a single suite. E.g.
java -jar selenium-server.jar -multiwindow -htmlSuite "*iexplore"
"https://www.website.com" "C:\suite.html" "C:\results.html"

Motivation for Selenium4j

In reality we would like to:
  • Reuse the Selenium HTML suites for regression testing
  • To be able to integrate them within an automated environment (think CruiseControl f.i.)
  • Make the Selenium HTML tests configurable

WebDriver (Selenium2) solution?

Looking into WebDriver there is no analog of the Selenium IDE. The IDE is an amazing tool for recording the user clicks and it should be great if WebDriver solve the HTML automation integration. It seems that the later version of the IDE (1.0.5 as of writing) provides better support for the new Selenium2 API. Yet, the problem with HTML test integration is still on the table. As long as you record your script and export it to HTML,  you are on your own. You need to manually use the IDE to translate the HTML scripts to the desired language (Java in my case), then you need to copy the source, paste it to you Eclipse/Netbeans, make sure compiles, configure it properly. In other words, it’s not fun.  WebDriver should provide a solution that simplifies that process and at the end the recorded Selenium IDE script should be seamlessly integrated within an automated environment. In that context, Selenium4j is an example how HTML could be easily translated to a Java JUnit sources.

How does Selenium4j work?

In short Selenium4j translates the HTML tests to Java JUnit tests. It iterates through the HTML suite and tests, digest them and smartly discovers their Selenium commands. Each command is being subsequently transformed to a Java JUnit method. At the end of the transformation the HTML scripts are being translated to Java JUnit sources following same directory(package) structure as the suite and tests. In addition, Selenium4j have suite setup/teardown utilities as well as external configuration.

Once again, for all the sources and ideas refer to http://code.google.com/p/selenium4j/

Have fun and lazy automated web regression testing!

The Spirals Algorithm Applet

Interesting. I’ve build few Mathematics Applets 10 years ago. Yet, I can still discover their links on internet. I’ve just found my Spirals Applet being featured at comPADRE – a Physics and Astronomy community site.

Applet is http://www.roussev.org/applets/spiral/spiral.html.

And the Spirals Algorithm is as simple as:

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;

public class HelloSpirals extends Applet {
   int max = 100;
   int centerPosition = 150;
   double angle = 22 / (double) 128;

   public void paint( Graphics g){
      setBackground(Color.black);
      g.setColor(Color.red);

      for (int i = 0; i < max; i++) {
         int[] coordEnd = getCoordinates(i);
         int[] coordStart = getCoordinates(i - 1);
         g.drawLine(centerPosition + coordStart[0],
                         centerPosition + coordStart[1],
                         centerPosition + coordEnd[0],
                         centerPosition + coordEnd[1]);
      }
   }

   private double getAngle( int inx){
      return (inx - 1) * angle;
   }

   private int[] getCoordinates( int inx){
      double angle = getAngle(inx - 1);
      int x = (int) Math.round(inx * Math.cos(angle));
      int y = (int) Math.round(inx * Math.sin(angle));

      return new int[] { x, y };
   }
}

Jump start J2EE Maven EJB3 project

Looking for a Maven2 EJB3 ready to run project? I came across this post. I gathered all the good ideas and grouped them together in ejb3-maven Google Code project.

https://code.google.com/p/ejb3-maven/

The project consists of three sub-projects - ejb, war and ear.

The changes I’ve made:

  • Replaced hsqldb with MySQL, featuring out-of-container JPA unit testing.
  • Replaced TestNG with JUnit4
  • Encapsulated the repeatable CRUD methods in abstract BaseDAO supertype (code bellow)
  • Fixed the ejb-api not-found Maven dependency discovery by adding additional repository (ejb-api is not available at default http://download.java.net/maven/2/ so I supplement with additional jboss repository http://repository.jboss.com/maven2/).
  • Provided sample JNDI utilities helping with session bean discovery

All said, I am looking forward to a complete rails solution where by simply providing the table names, the framework will generate all the ADO classes (in our case JPA DAO classes and entities). Probably this will be my next challenge …

And the sample BaseDao implementation is:

public interface BaseDao<E extends Indexable>{

	/**
	 * Dependency injection setter method.
	 * The method should be used at unit testing
         * during out-of-container DI injection.
	 */
	void setEntityManager(EntityManager em);

	/**
	 * Persists an instance of this new entity to the database
	 */
	E create(E entity);

	/**
	 * Returns a database entity using the given primary key ID.
	 */
	E read(Serializable id);

	/**
	 * Saves changes made to an entity.
	 */
	E update(E entity);

        /**
         * Deletes an entity
         */
	void delete(E entity);

        /**
         * Returns all entities of E type. Probably this
         * would work best with JPA Criteria..
         */
	List list();

        /**
         * Returns the entity 'E' type to current
         * DAO implementation
         */
	Class getType();
}
interface Indexable {
	/**
	 * @return the primary key id
	 */
	Long getId();

	/**
	 * @param id  the primary key id to set
	 */
	void setId(Long id);
}
class BaseDaoImp<E extends Indexable> implements BaseDao<E>{

	/**
	 * The JPA entity manager to be used from all derived DAO classes
	 */
	@PersistenceContext(unitName = Globals.JTA_PERSISTENT_UNIT)
	protected EntityManager em;

	/**
	 * The SessionContext to be used from all derived DAO classes
	 */
	@Resource
	protected SessionContext sc;

	/**
	 * This method should be used only in out-of-container environment
	 * injecting a NON-"JTA" ("RESOURCE_LOCAL") EntityManager
	 */
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}

	public final E create(E entity) {
		em.persist(entity);
		em.flush();
		return entity;
	}

	public final void delete(E entity) {
		em.remove(entity);
		em.flush();
	}

	public final E read(Serializable id) {
		return em.find(getType(), id);
	}

	public boolean exists(Serializable id) {
		return (null != read(id));
	}

	public E update(E entity) {
		em.merge(entity);
		em.flush();
		return entity;
	}

	public List list() {
		Query qry = em.createQuery("from " +
                     getType().getSimpleName());
		return qry.getResultList();
	}
}

Vaadin – the Java web framewrok

I keep spreading the message. While Java gurus are still faithing on those religious best web framework wars , there is hidden winner already.

The best Java web framework award goes to … drumrolls … Finland.

Vaadin – When I saw it for a first time few months back, I was shocked. That what Java is meant to be – Innovative, slick, sexy.

Remote Debugging with JBoss 6 and Eclipse

Jboss keep changing their startup files and every time I end up looking for the right spot to attach my JPDA settings. Trying the latest and greatest jboss version 6 I was caught by surprise again searching for the right .bat/.sh file. Damn you JBoss :)

So to activate JBoss 6 remote debugging:

1. Open ~jboss/bin/run.conf.bat

2. Uncomment the following line:
rem # Sample JPDA settings for remote socket debugging
set “JAVA_OPTS=%JAVA_OPTS% -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n”

3. Go to jboss folder and fire it up.
~jboss6/run.sh

4. Pay attention. the following line should appear:
Listening for transport dt_socket at address: 8787

3. Finally go back to Eclipse and attach the debug listener
Eclipse / Run / Debug Configurations / Remote Java Application / New

I’ve always wondered why Java 5 was not consistent with @Override annotation type. Today after experiencing the compilation failure again I digged further. The problem:

abstract class Rectangle {
   abstract double getArea();
}
// deriving from abstract class
class Square extends Rectangle {
   @Override
   double getArea() {
      // ...
   }
}

interface Shape {
   double getArea();
}
// implementing an interface
class Square implements Shape {
   @Override
   // Java5 will complain about @Override
   // "method does not override a method from its superclass"
   double getArea() {
      // ...
   }
}

Java compiler will complain about @Override being used yet no such method in parent “method does not override a method from its superclass”? Well, the compiler is wrong as there is such method in parent, but instead of a superclass it is a supertype of an interface.

Luckily Java6 recognized the issue and fixed it. Not sure why this has not been implemented in a first place.