26 Sep

create annotation instances at runtime

Yesterday I stumbled upon an easy way to create annotation instances at runtime: javax.enterprise.util.AnnotationLiteral

This util class from CDI (JSR-299) implements all standard methods (toString(), equals(), hashCode(), annotationType()) just like an annotation instance created by Java would. Thus there is no real difference between subclasses from this class and the annotations created by the JRE.

Here you can find the source code of AnnotationLiteral from Apache Geronimo Specs: AnnotationLiteral.java

Example

For example you have the following annotation (which is also a CDI qualifier):

@Target({PARAMETER, FIELD, METHOD, CONSTRUCTOR, TYPE})
@Retention(RUNTIME)
@Documented

@Qualifier
public @interface ConversationQualifier1
{
    String value();
}

If you want to create an instance of it with a certain value at runtime, just create the following class:

public class MyConversationQualifier1
    extends AnnotationLiteral<ConversationQualifier1>
    implements ConversationQualifier1
{

    public String value()
    {
        return "my assigned value";
    }
}

Now you just need to create an instance of it and you can use it like any other annotation instance created by Java.

Annotation a = new MyConversationQualifier1();
09 Sep

MyFaces webapptest meets Apache SVN

Since GSoC is over now, I integrated the “Automated webapp tests for MyFaces core and extensions”-project done by Cosmin Martinconi into the MyFaces codebase.

This test framework uses Arquillian to automatically configure and start a container (e.g. Tomcat or Jetty), to deploy the test cases on it and to execute them and run assertions using HtmlUnit and a JSF-PhaseListener. Currently this already works, however with a very limited set of features (and stability). You can check out a working example at https://svn.apache.org/repos/asf/myfaces/gsoc/webapptest/examples/simple

The project currently lives in the gsoc folder at https://svn.apache.org/repos/asf/myfaces/gsoc/webapptest and has its own component in the MyFaces test issue tracker (“webapptest“). In addition Cosmin created a wiki page showing the current state of the project (see http://wiki.apache.org/myfaces/AutomatedWebappTest_Status).

The MyFaces community already showed great interest on this project and I think we will be able to do a first alpha release of it in the near future. Afterwards MyFaces Webapptest will be used on MyFaces core and its subprojects to provide a fast and easy way of doing integration testing within a JUnit test case.