Author: David

  • JavaOne Day 2

    JavaOne Day 2

    I saw a great talk about jMaki which looks like an awesome lightweight framework to easily integrate ajax into your apps. It wraps common ajax libraries like jQuery, Dojo, etc. It uses the familiar convention over configuration to make things quick to develop and has multiple server-side language support, for example, PHP, JSP and Ruby. It also supplies an xml http proxy to interact with services outside of your domain.

    A jMaki wiget is a bundle of: component.htm, component.js and css any of which can be edited if you want to easily customise the widgets.

    jMaki implements all the best practices of YSlow such as minimizing http requests, combining css and scripts, defining css at the top and javascript at bottom and setting cache control headers as well as gzipping content.

    It has plugins for eclipse and NetBeans but the demo of the NetBeans plugin was pretty cool; you can just drag and drop widgets from the palette into a jsp page and jMaki handles all the YSlow performance stuff. jMaki charting provides some neat charting widgets but one of the coolest demos was of jMaki webtop which is a widget that allows you to deploy a set of widgets and allow users to position the widgets, set properties, and connect them to services available to the application. In fact the NetBeans plugin makes it so easy it literally took me five minutes to create this simple test webtop app while waiting for the next session.

    Functional Testing of Web Applications using Canoo WebTest and groovy was given by Dierk Konig, the founder of WebTest. He described four approaches to functional testing of webapps:

    • capture/replay
    • model based
    • data driven
    • scripted

    Capture/replay creates a script from user action. This is a typical approach in commercial testing software but it is hardly usable at all for testing.

    Capture/replay is the least cost-effective way of test automation

    Model based testing uses expected vs unexpected behaviour. Data driven uses data variations on the same workflow. So you will have tables of input parameters and expected outputs but this is really good only if you just test against data variations.

    WebTest uses a scripted approach which gives maximum power and flexibility but also maximum responsibility.

    Dierk said they aim for running about 100 tests per minute so when the number of tests grows larger than about 1000 they split the tests up into suites which can be run in parallel using ant.

    WebTest has lots of tests already including tests for PDFs, Excel and email but you can also write your own tests using either ant or groovy. Groovy is easier.

    Dierk is also a commiter to the Grails project and WebTest comes integrated with Grails. He created a very simple Grails CRUD application and then running this in webtest was as simple as:

    $ grails create-webtest Attendee
    $ grails run-webtest
    

    These commands created and ran automatically generated webtests.

    He also demo’d the FireFox WebTest Recorder extension which can help start to build a test ‘interactively’ in the browser.

    Clark Richey gave an interesting session on using DBUnit to test your database access layer code. Testing your DB access code can take a lot of code but it isn’t really ‘boilerplate’ as you have to test for corner cases and side effects. Using DBUnit can reduce the amount of code you need to write.

    He gave some best practices for testing database code:

    • Multiple databases per developer (eg, _dev & _test)
    • Ensure the state of the database prior to testing
    • Test in small chunks of data
    • Don’t try to load everything into the db for a single test

    DBUnit looks useful. You can define datasets in XML and there are lots of helpful methods for assertions. However there are some limitations:

    • Not useful for validating queries that don’t alter the state of the database but DBUnit can still be used for populating the test data
    • Dealing with foreign keys on autogenerated IDs is tricky – can’t really verify foreign key relationships

    I liked the Java University session I had with Chris Richardson so I thought I would attend his talk on using EC2 for testing. He’d obviously read my previous post as he made a reference to me calling his accent “mutant”. Chris, I hope I didn’t cause offense! After introducing AWS and EC2 his talk was mainly about his open source framework called Cloud Tools which is a framework written in Groovy that’s used for deploying and testing Java apps on EC2. It was interesting, especially considering we’ve been using EC2 for testing at work for quite a while now.

    He has a nice idea of using JetS3t to accelerate uploading of code. Basically, JetS3t is used to sync changes to code to an S3 bucket and then the deployed instances grab the code from the bucket which is obviously much faster than uploading it over DSL every time. He also has a maven plugin that gives you useful goals like deploy and db-save. It also has a JMeter goal for performance testing.

    He finished off his talk with a few highlights of the Groovy language, managing to talk about Groovy GStrings without a hint of a smile.

  • JavaOne Day 1

    JavaOne Day 1

    Today was the first day of JavaOne proper and it was packed! There are so many people here! I arrived for breakfast at about 8am and already a queue was forming for the opening keynote general session. By the time I joined the end of it it had stretched the length of the Moscone Center. When I got inside the massive hall it was more like a rock concert. Loud music was pumping and some awesome break dancers were performing on stage. Then just before the keynote was to start James Gosling started firing t-shirts into the crowd using a giant slingshot held between two other people.

    So the first general session was on the theme of the whole conference; “Java + You”. There were lots of demos of how Java is being used on the “four screens” of your life (I think these are your PC, phone, TV and car). Probably the coolest demonstrations were of JavaFX, used for creating rich internet applications similar to Adobe’s flex and AIR. You can make some cool applets using JavaFX but the coolest thing is you can drag these applets off the web page and onto your desktop and they continue to run. In fact you can then close the web page and keep the applet locally to be run on its own whenever you want. That’s cool!

    The first technical session I attended was on IBM’s WebSphere sMash and ProjectZero. This is a “commercial project with a transparent development process” and is a rapid application development environment that is geared around developing RESTful mash-ups and Web 2.0 apps. It supports PHP, Groovy and Java and contains a PHP interpreter written in Java. At the moment this interpreter runs an intermediate representation of the PHP code but in future they plan to compile it to byte code. It contains its own web-based IDE and the application that is produced contains its own web server. So you can have a number of these applications running and they are started and stopped using an inetd type service. It looks like it is targeted at the enterprise developer who wants to throw something together quickly for internal use.

    Joshua Bloch, Chief Java Architect at Google, gave an interesting talk on ‘More Effective Java’ which was a selection of points from his new book. He spent quite a bit of time on hints for using enums. For example, replacing bitfields with enums. There are lots of problems with bitfields: not typesafe, no namespaces, printed values are cryptic, eg ’12’, don’t scale – if number of constants grows beyond 64 you’re screwed. A solution is to use EnumSet and you end up with nicer code like text.applyStyles(EnumSet.of(Style.BOLD, Style.ITALIC)).

    Rod Johnson from Spring gave a session on new features of Spring 2.5. He listed a whole heap of new features like:

    • overhaul of Spring MVC
    • enhanced testing support with JUnit4
    • extended SQL error code mappings for popular RDBMSs
    • integration with latest versions of JavaEE 5 APIs (Servlet 2.5, JSP 2.1)

    but he focussed mainly on annotations.

    Using annotation based DI can reduce the external config required and is more concise but downsides include:

    • per-type not per-instance
    • won’t work for legacy code
    • need to recompile java code to change configuration
    • not suited to externalizing simple types

    Even so, Rod suggested fairly extensive use of annotations. You can use the @Autowired annotation which is Spring’s own syntax and uses the default autowire by type behaviour. This is the same as using autowiring in xml files but by using annotations you can be more selective. Of course the downside is you are using a spring specific annotation. If you don’t like this you can use the @Resource annotation although it is not as powerful as @Autowired so is not a best practice.

    Rod also went through some of the other annotations like @Component which allows component scanning (where Spring scans your classpath for annotated classes). I can see how annotations are nice but it also means you lose your ‘application blueprint’ when all your beans are defined in xml.

    Bill Pugh gave a really interesting talk on “Defective Java Code: Turning WTF Code into a Learning Experience”. Bill is responsible for the cool FindBugs static analysis tool and he had good tips for some common errors. He spent quite a bit of time discussing the equals method which can be trickier to implement than it might seem. The following must be true when implementing the equals method:

    • equals(null) returns false
    • if x.equals(y) then x.hashCode() == y.hashCode()
    • equals is reflexive, symmetric and transitive

    Symmetry is very important as non-symmetric equals can produce confusing and hard to reproduce results. He also discussed the debate of using instanceof or getClass() when implementing equals. Basically, both have problems. if you use instanceof and override equals you may break symmetry. If you use getClass then you cannot have a subclass equal to superclass. However you only need to worry about this when designing with subclasses.
    Bill described different types of equality:

    • Object equality (the default and useful more than you would suspect)
    • Value equality (eg ArrayList and LinkedList representing {1,2,3} are equal)
    • Behaviour equality (objects are equal if you can’t distinguish them based on behaviuor) – more subtle

    Use of getClass will cause any subclasses to break equality. Also, use of getClass in hibernate objects will cause failures. However, if you use instanceof and override it you must not change the semantics of equals – all you can do is use a more efficient algorithm or instrument for debugging/performance monitoring. getClass is sometimes used in abstract base classes (eg BasicPermission class). If you use instanceof consider declaring the equals method final. Basically the moral is you should document your equals semantics. FindBugs can detect common problems with equals methods, for example, there are two equals methods in the JDK and four in eclipse that always return false plus one in eclipse that always returns true.

    After the Rails stuff from yesterday I was interested to hear Guillaume Laforge talking about Groovy and Grails. This is a cool platform that uses Groovy in a Rails-type framework. What’s interesting is that it makes use of such projects as Spring and Hibernate yet simplifies their use so there is much less configuration required. For example, you don’t need to create hibernate mapping XML files.
    The syntax is very Java-like (unlike Ruby) and integrates tightly with Java at every level. You can reuse legacy code and while Grails comes with jetty for development it is simple to package it as a war file for deployment on tomcat or some other app server.

  • Java University

    Java University

    Today was the Java University day. It was a long day. There were three sessions, each of three hours duration which meant a twelve hour day (including breaks).

    My first session was Developing Enterprise Applications With the Spring Framework with Chris Richardson, the author of POJOs in Action. He was a good speaker although he had one of those slightly mutant delicately nuanced accents that Brits seem to get after spending a long time living in the US. The session was interesting although he spent most of the time on all the dependency injection stuff I was quite familiar with and seemed to run out of time toward the end and had to rush a bit over the Hibernate stuff. But he went into some detail over Spring’s AOP support as well as the annotations that can eliminate a lot of the XML. While annotations can reduce the amount of XML required I’m not sure this is entirely a good thing as then your POJOs become more tightly coupled to the Spring framework. I think I prefer keeping it all in XML then your POJOs don’t need to know anything about Spring. He also described configuring the Application Context using JavaConfig which is pure Java but this looks even worse than annotations to me.

    After lunch I attended a session on Using the Power of JRuby and Rails to Develop Robust Applications. This was a joint presentation by Brian Leonard and Sang Shin of JavaPassion fame. I found this really interesting. Brian gave a (brief) introduction to Ruby and JRuby in the first hour and the next two hours Sang went through a hands-on lab building various Rails applications.

    The ability to create a working CRUD application using the Rails framework in about five minutes is amazing and it definitely looks like a fun language/framework to learn. An interesting Rails feature is the database migrations which manage the evolution of database schema. The transformations are described in self-contained ruby classes that have up and down methods that are called in sequence when migrating between versions of a database schema. Eg:

    class AddSsl < ActiveRecord::Migration def self.up add_column :accounts, :ssl_enabled, :boolean, :default => 1
        end
    
        def self.down
            remove_column :accounts, :ssl_enabled
        end
    end
    

    You can put any ruby code you want in these up and down methods.

    It’s interesting that when you generate a new model class it actually contains no real code, eg:

    class Message < ActiveRecord::Base
    end
    

    Rails actually makes extensive use of ruby’s metaprogramming features here to create dynamically generated classes and methods.

    The neat thing about JRuby (a ruby interpreter that runs on the JVM) is that you can call all your java code from your ruby program, eg:

    include Java
    
    import javax.swing.JFrame
    
    frame = JFrame.new("Ruby SWINGS!")
    frame.set_size(640, 480)
    frame.show
    

    I’m definitely going to have a play around with this and Netbeans has some awesome ruby/rails support now.

    After the reception I stayed for a session on Netbeans 6.x. This covered quite a few features of the latest Netbeans but particularly interesting was the profiling tools. This uses JVMTI and lets you specify “root methods” which will only add instrumentation to those methods. This saves adding any profiling overhead to other areas of your application. You then run the appserver in profiling mode and take a “snapshot” which shows methods called, the number of invocations and the time spent in them, including “self time” which doesn’t include time spent in methods called by that method. You can then view this output in a few different ways and jump to the code. It looks really useful.

    They also covered Netbeans’s new support for scripting languages like Ruby and JavaScript. Both the ruby editor and the javascript editor have support for refactoring and code completion. The javascript editor also has code completion support for frameworks like JQuery, scriptaculous and yui. A nice touch are the little icons that indicate browser compatibility. I also scored a Netbeans USB drive which includes an early access release of the new PHP suport in Netbeans.

    This morning It had been lovely and sunny when I left at 7.30 but it was quite chilly when I emerged at 9pm. A sea fog had started to obscure the tops of the tallest buildings and the air was damp. I stopped for a while to listen to a loud three piece band playing on the corner of O’Farrell St and Powell.

    It was a long day but a good start to what looks to be a busy week.

  • My First JavaOne

    My First JavaOne

    Today I arrived in sunny San Francisco ahead of JavaOne 2008. Tomorrow is the Java University day before the main conference starts on Tuesday.

    I’m staying in the Sir Francis Drake Hotel on Powell Street. This is an interesting hotel, built in 1928, with lots of quirky touches like doormen in Beefeater uniforms and a cool “Cutler Mailing System” which seems to be a chute that runs down the elevator shaft. Presumably you can drop a postcard in and it will shoot down to a big bin somewhere in the bowels of the hotel.

  • Facebook mirror

    Facebook mirror

    While I was doing a yum update today I noticed it was trying mirror.facebook.com. It turns out they mirror a lot of open source projects there.

  • Mock Objects and test driven design

    Mock Objects and test driven design

    I thought I may as well put some of the work I did for my MSc online rather than leave it languishing on my hard drive where it will never be seen again.

    I’m not ready to publish my final project yet but here is a research essay I wrote about Mock Objects and test driven design (TDD).