Archive for category: Scala

Automatically test your Wicket panel HTML markup

Automatically test your Wicket panel HTML markup

Are you testing your HTML markup automatically yet? If your markup does not match the Java code, your Wicket panel does not work. It’s easy to get early feedback when your panels are broken: just unit test them!

In this blog post I describe a way of automatically verifying that the HTML markup of Wicket panels match the Java code. Scroll down to download the demo project!

Unit test

Unit testing Wicket panels with EclipseI created a JUnit test that you can add to your project to automatically test as much Wicket Panels as possible. Automatic testing works by resolving all panels on the Java class path and feeding them to the WicketTester. When a panel has invalid markup, the WicketTester will give an error: early feedback!

How does it work?

The Wicket panels that can automatically be tested should have a ‘default’ Wicket constructor, like this:

public DemoPanel(String id)

Resolving all panels is done with Spring 2.5’s ClassPathScanningCandidateComponentProvider (don’t you love those names! How can they stay below 120 characters per line?)

The ClassPathScanningCandidateComponentProvider is a component provider that scans the classpath from a base package. It then applies exclude and include filters to the resulting classes to find candidates.

Testing panels

@Test
    public void testAllWicketPanels() throws Exception {
        WicketTester wicketTester = new WicketTester(new WicketApplication());
 
    	ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
        provider.addIncludeFilter(new AssignableTypeFilter(Panel.class));
 
        Set components = provider.findCandidateComponents("nl/stuq/demo");
        for (BeanDefinition component : components) {
            Class clazz = Class.forName(component.getBeanClassName());
            if(hasDefaultConstructor(clazz)){
            	testWicketPanel(wicketTester, clazz);
            }
        }
    }
 
    private void testWicketPanel(WicketTester wicketTester, Class clazz) {
    	wicketTester.startPanel(clazz);
    	wicketTester.assertNoErrorMessage();
    	wicketTester.assertNoInfoMessage();
    }
 
    private boolean hasDefaultConstructor(Class clazz) {
        for(Constructor constructor : clazz.getConstructors()){
            if(constructor.getParameterTypes().length == 1 &&
               constructor.getParameterTypes()[0].getSimpleName().equals("String")){
                return true;
            }
        }
 
        return false;
    }

The test instantiates a new WicketTester. After that, the ClassPathScanningCandidateComponentProvider is created and configured to scan only for Panel classes in the nl.stuq.demo package and below.
After that, the found components are all checked. If they have a default constructor, the test is executed.

The boolean hasDefaultConstructor(clazz) method checks if the class has a constructor with only one String argument, the Wicket id.

Keep in mind

  • This is only for lazy people (good developers are lazy in some ways).
  • You need a dependency to Spring.
  • Only Panels with a certain constructor are tested.
  • Only instantiating the Panel is tested, so code coverage has no meaning for this test. Real testing is still needed.

Download demo project

The demo project is a Maven project based on the Wicket 1.4.3 quickstart that contains the test class and an example panel to be tested.
Download demo project.

Extract the zip file and run mvn test to run the tests. Then fire up your IDE and check how it’s done.

You can change or adapt the given JUnit test to also automatically test classes extending from Page or Component.

Join in with your opinions and code! I’m curious to see what clever way you have of testing code.

November 1, 2009 7 comments Read More
ApacheCon Europe 2009 community meetup experiences

ApacheCon Europe 2009 community meetup experiences

ApacheConIn this guest post, Minze Tolsma, software developer at Capgemini shares his experiences attending the ApacheCon community meetups.

This year I went to the ApacheCon community meetups. There were several, but I chose to attend the Maven and the Wicket meetup. I made some short notes on the most interesting things.

Monday 23-03-2009: Maven

The meetup on Maven was about new features and new projects.

Eclipse Integration

The community is working on a new tool for Eclipse Integration called Eclipse IAM (Integration for Apache Maven). The plugin aims at simplicity and will bring the best Eclipse integration for what Maven offers. It will integrate with JDT and WTP. The following features will be in the package:

  • Direct project import with a POM. Instead of using the Eclipse archetype.
  • You can setup projects in Eclipse by using predefined archetypes.
  • You can configure autobuilds so building isn’t annoying.
  • There will be a FormBased POM editor, just like you are used to in Eclipse.
  • Dependency search
  • Dependency management
  • Dependency graphing and analysis

Eclipse IAM will be worth looking at, it can be found at http://www.eclipse.org/iam/.

Maven 3.0 new features

The community is working on Maven 3.0. Some new features:

  • Embeddable, so it will be faster
  • Some improvements to make it easier to use with the repository
  • New transport mechanism for simultaneous downloads.

Eclipse 3.0 projectpage: http://docs.codehaus.org/display/MAVEN/Maven+3.0.x
After this two subjects and some live demos there was an open discussion. Because it was late in the evening I left. One thing I wrote down is that SourceSense made an Alfresco archetype for Maven.

Tuesday 24-03-2009: Wicket

The Wicket meetup was quite different in comparison with the Wicket meeting. There where several presentations about testing, Scala and Wicket, Wicket and DB4O.

JDave

For testing nothing was really new… Except the following framework for Behaviour Driven Development: JDave. More info on: http://www.jdave.org/.
Presentation of the ApacheCon and examples on: http://www.jdave.org/bdd-wicket/.

DB4O

DB4O is a Object Database for Java and .NET shipped with two different licenses. More detailed information about DB4O can be found on the DB4O website.

You can look at DB4O as a large factory (like the Factory design pattern (GoF)) just for storage. It is a good practice to use some XML storage together with DB4O. For this you can use XStream. Using XML for storage together with object storage offers you a good backup mechanism.

Some disadvantages of DB4O are:

  • When data is corrupt there are strange exceptions.
  • Version Management for the objects is hard to do.

Scala and Wicket

The presentation about Scala and Wicket was really a hands-on presentation. The slides, code examples and handouts can be found at http://stuq.nl/weblog/2009-02-04/download-the-basic-and-wicket-scala-talk-materials. Just try it and get introduced to the concepts of functional programming using the JVM and Wicket.

After all I had two nice evenings and learned a lot. I hope this article gives you some nice and hopefully new useful information!

April 3, 2009 0 comments Read More
Amsterdam Wicket meetup 2009

Amsterdam Wicket meetup 2009

At the Amsterdam Wicket meetup 2009 I did a presentation about Scala and Wicket. These are all the materials that you can download to get started with Scala and Wicket today!

Downloads

  • Code samples (5.5 MB)
    • Simple HelloWorld Scala demo
    • Hello Wicket World demo (Scala and Wicket)
    • Hello Wicket World demo built with Maven
  • Slides (13.1 MB)

The slides are also on slideshare:

March 24, 2009 0 comments Read More
Download the Basic and Wicket Scala talk materials

Download the Basic and Wicket Scala talk materials

On February 4th I gave a talk at the London Wicket User Group. These are all the materials that you can download to get started with Scala and Wicket today!

If you want to attend one of the Wicket User group meetings in London, just visit the jWeekend site and register there. It’s really cool to attend, there is a good atmosphere and nice and smart people everywhere…

Downloads

  • Code samples (5.5 MB)
    • Simple HelloWorld Scala demo
    • Hello Wicket World demo (Scala and Wicket)
    • Hello Wicket World demo built with Maven
  • Handout (1.9 MB)
  • Slides (16.1 MB)

Or download everything at once (22.2 MB)

Scala resources

In this separate post you find links to Scala resources on the web (they are also in the slides, but this is much easier to click on).

6 Scala resources for Java programmers

February 4, 2009 4 comments Read More
6 Scala resources for Java programmers

6 Scala resources for Java programmers

During my Basic Scala and Wicket talk at the London Wicket Event I showed some good Scala starting points for Java programmers. Here they are, clickable and all.

If you want to attend one of the Wicket User group meetings in London, just visit the jWeekend site and register there. It’s really cool to attend, there is a good atmosphere and nice and smart people everywhere…

Scala homepageScala Homepage

The Scala home on the web.

Contains reference manuals, tutorials,
news, specifications.

First Steps to ScalaFirst steps to Scala

When you don’t know anything about Scala, start here.

Covers the interpreter, variables, methods, loops, arrays, lists, tuples, sets, maps, classes, singletons, traits, mixins.

roundup_for_java_refugeesScala for Java refugees

Series of 6 great articles covering a lot of Scala.

Aimed at Java developers.

Scala WikiScala Wiki

An ever growing collection of resources on Scala.

FAQ, code samples, design patterns, Scala job openings

Scala for Java ProgrammersScala for Java programmers

Multiple articles covering a feature by feature comparison of Scala and Java.

Mailing ListsScala Mailing Lists

Official mailing lists

Subscribe: empty message to scala-subscribe@listes.epfl.ch

February 4, 2009 0 comments Read More