Code Generation using Annotation Processors in the Java language – part 2: Annotation Processors

This post is the second part in my series about Code Generation using Annotation Processors in the Java language. In part 1 (read it here) we introduced what Annotations are in the Java language and some of their common uses.

Now in part 2 we are going to introduce Annotation Processors, how to build them and how to run them.

Code Generation using Annotation Processors in the Java language – part 2: Annotation Processors

Annotations are great, sure. You can set any kind of metadata or configuration with them, with a well defined syntax and different types to use.

From what we have seen until now, annotations have advantages compared with Javadocs but not enough to justify their inclusion into the language. Therefore, is it possible to interact with annotations and get the most from them? Sure it is:

  • At runtime, annotations with runtime retention policy are accessible through reflection. The methods getAnnotation() and getAnnotations() in Class class will do the magic (1).
  • At compile time, Annotation Processors, a specialized type of classes, will handle the different annotations found in code being compiled.

Continue reading “Code Generation using Annotation Processors in the Java language – part 2: Annotation Processors”

Integrate HtmlUnit Based Tests with Apache Maven and Cargo

This blog post is a continuation of the one titled “Automating Assembly and Integration Tests with HtmlUnit” that can be read here.

In the previous post we learned how to make use of HtmlUnit API to write automated integration tests. In this post we are going to show how to integrate those tests in an Apache Maven project lifecycle while keeping a clear distinction between the unit test suite and the integration test suite.

Continue reading “Integrate HtmlUnit Based Tests with Apache Maven and Cargo”

Code Generation using Annotation Processors in the Java language – part 1: Annotation Types

With this post I would like to start a series about Code Generation using Annotation Processors in the Java language, how powerful they are, and at the end show how to make use of them to generate source code on compile time.

During the series we will:

  • Introduce what are Annotations in the Java language.
  • Understand the common uses of Annotations and their scopes.
  • Understand what are Annotation Processors and their defined role.
  • Learn how to build Annotation Processors.
  • Learn how to run Annotation Processors from command line, Eclipse and Maven.
  • Learn how to use Annotation Processors to generate source code.
  • Learn how to use Annotation Processors to generate source code using an external template engine as Apache Velocity.

Code Generation using Annotation Processors in the Java language – part 1: Annotation Types

Annotations were first introduced in the Java language with the third edition of the Java Language Specification (1) and first implemented in Java 5.

Using annotations, we are able to add metadata information to our source code – build or deployment information, configuration properties, compilation behavior or quality checks.

Unlike Javadocs, annotations are strong typed, having that any annotation in use has a corresponding Annotation Type defined in the classpath. Besides that, annotations can be defined to be available at run-time – not possible with Javadocs.

Continue reading “Code Generation using Annotation Processors in the Java language – part 1: Annotation Types”

Automating Assembly and Integration Tests with HtmlUnit

With this blog entry I want to start a mini-series about automating assembly and integration tests in Java. Mini-series because there are many different tools and techniques to choose from, and depending on the software being developed you will choose and combine to get the best results.

This entry will show the basics on how to use HtmlUnit to automate assembly and integration tests of web applications.

What is HtmlUnit? HtmlUnit is a framework that runs on top of the popular JUnit testing framework that simulates a web browser, including JavaScript. Using the HtmlUnit API you can interact with the web pages and get insights of the layout and contents to write assertions. An interesting fact about HtmlUnit is that you can use to automate tests of web applications written in other languages than Java.

HtmlUnit can be downloaded from the tool web site at http://htmlunit.sourceforge.net/.

With HtmlUnit there is no practical distinction when you intend to write an assembly or an integration test case. If you choose to run with an embedded (or in memory) database and web application server or with a dedicated one will mark the (sometimes controversial) distinction between an assembly or an integration test.

In this entry we are focusing on HtmlUnit setup and common use cases and for a later entry we will focus in assembly vs. integration setup, best practices and how to run them within a continuous build/integration process.

Continue reading “Automating Assembly and Integration Tests with HtmlUnit”

IzPack – Open Source Java installer engine

Old post from Feb 11th, 2010

—————

Recently we faced the need for an installer for a Java tool. Nothing too refined, just select the path, select the packages to install and create some shortcuts.

First I googled for the obvious query and found many alternatives, some free and some commercial, and then we do a preliminary testing to some of them (OpenInstaller, Advanced Installer, install4j) and finally we knew of IzPack.

In this post I would to highlight some of the characteristics of IzPack and why we decided to use it, and then show you how to make a simple yet powerful installer.

Continue reading “IzPack – Open Source Java installer engine”

Architecture Rules 3.0.0 goes RC1

Old post from Dec 11th, 2009

———————-

After some months evaluating Architecture Rules (AR for sort, web http://wiki.architecturerules.org/index.php?title=Main_Page) for serious use, and fiddling in a hate-love relationship with this tool (very good idea, not so well documented, some extrange behaviours that almost makes you resign), I found in their SVN and Maven repository that they have posted there the RC1 (release candidate) of the new 3.0.0 release.

For those that do not know about AR, this tool builds on top of JDepend to analyze the architecture, understood as module relationships and valid dependencies, of a Java project.

JDepend calculates OO metrics (Afferent, Efferent couplings, Instability, Abstractness, Distance) and also checks for circular dependencies between Java packages. JDepend is very fine, produces reports, has a fine GUI and there is an Eclipse plug-in available for your convenient use, but it is not easy to do validations in automated builds and continuous integration.

AR offers that. You can automate your architecture validation as an Ant task, Maven plug-in or, as it is most interesting to me, as a JUnit test case.

Thus, why this post? AR can be frustrating. You can easily get lost in dependencies, not found files and folders and different behaviours in unit tests if executed from Eclipse or from Maven. And this is why I would like to share some of my findings.

First, consider how to run it in an automated way. The Ant task or Maven plug-in have a disadvantage, they break the build if the architecture validation does not pass. It can be good for you but in my opinion I just want to know whether rules are being followed and continue with the rest of the build process, eventually running Sonar and publishing my results in Sonar dashboard.

So I finally decided to go for the JUnit test case. I can give you details on running in Ant and Maven but in this post from now on I will focus on JUnit and I will split the rest of the contents in these areas:

1. Dependencies.

2. Configuration.

3. Writing the test case.

4. Running.

Continue reading “Architecture Rules 3.0.0 goes RC1”

Testing Java Swing components with Fest

entrada antigua del 28/3/2011 – old post from 3/28/2011

—–

While exploring into new ways of writing automated unit test cases in Java, I discovered the Fest framework.

Besides some nice tools for mocking and wrapping calls to reflection, Fest has a convenient API to automate unit tests for Swing components leveraging the AWT Robot, which is too low-level as to be of real use for massive unit testing.

Of course, this is valid for integrated/assembly tests, not real unit tests, because Fest launches a Swing frame or dialog and then reproduces user gestures in the application (go to that field, enter that text, select that on the combo, press this button and so on). I find it useful because, especially in Swing, a thorough unit test suite is not enough to ensure proper user experience and bug-free deliveries. You need to test integrated, all the Swing widgets, event handlers and even business logic.

Results have been very good. Fest has some bugs but even with that I was able to write a good quantity of unit tests in a decent time. I will show some examples in this post.

Continue reading “Testing Java Swing components with Fest”