Using Jmock annotations with Junit 4

Recent versions of JMock have support for configuration by annotation using JUnit 4 MethodRule

package mypackage;
 
import org.jmock.auto.Mock;
import org.jmock.integration.junit4.JUnitRuleMockery;
import org.junit.Rule;
import org.junit.Test;
 
public class TestMyClass {
   @Rule public final JUnitRuleMockery context = new JUnitRuleMockery();
   @Mock private MyCollaborator collaborator;
 
   @Test public void somethingHappensWhenSomethingElse() {
 
   }
}

Using proguard 4.8 with maven

The proguard/maven ecosystem seems to be in a bit of a mess. Luckily wvengen and github have stepped up, and released a fork of the com.pyx4me proguard maven plugin.

Anyone that wants to put stuff in maven central should check out this guide to putting stuff in maven central using sonatype.

<plugin>
    <groupId>com.github.wvengen</groupId>
    <artifactId>proguard-maven-plugin</artifactId>
    <version>2.0.6</version>
    <executions>
       <execution>
           <phase>package</phase>
           <goals><goal>proguard</goal></goals>
       </execution>
    </executions>
    <configuration>
        <libs>
            <lib>${java.home}/lib/rt.jar</lib>
        </libs>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>net.sf.proguard</groupId>
            <artifactId>proguard-base</artifactId>
            <version>4.8</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</plugin>

Use new version of Jackson JSON with Jersey

Guice Module:

import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import com.google.inject.Singleton;
import com.sun.jersey.guice.JerseyServletModule;
import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
 
public final class JerseyModule extends JerseyServletModule
{
   @Override
   protected void configureServlets()
   {
      bind(JacksonJaxbJsonProvider.class).in(Singleton.class);
      filter("/*").through(GuiceContainer.class);
   }
}

And the Maven pom:

<dependency>
   <groupId>com.fasterxml.jackson.jaxrs</groupId>
   <artifactId>jackson-jaxrs-json-provider</artifactId>
   <version>2.0.5</version>
</dependency>

Jersey + Guice ExceptionMapper

import com.google.inject.Singleton;
 
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
 
@Provider
@Singleton
public class MyExceptionMapper implements ExceptionMapper<MyException>
{
   @Override
   public Response toResponse(final MyException exception)
   {
      return Response.status(Status.NOT_FOUND).entity(exception.getMessage()).build();
   }
}
import com.google.inject.AbstractModule;
 
public class MyModule extends AbstractModule
{
   @Override
   protected void configure()
   {
      bind(MyExceptionMapper.class);
   }
}

Implement toString method on DOMSource when marshalling with JAXB

When serialising with JAXB to a DOMSource the object that results does not have a useful implementation of toString and alot of boiler plate is required to set up the marshalling. Here is some code that returns a DOMSource from an arbitrary JAXB annotated object with the toString method of the object return the XML representation:

public class SerialisationHelper
{
   private final String marshalledBean;
   private final Document document;
 
   private SerialisationHelper(final Object bean) throws JAXBException, ParserConfigurationException
   {
      document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
 
      final Marshaller marshaller = JAXBContext.newInstance(bean.getClass()).createMarshaller();
      marshaller.marshal(bean, document);
      final StringWriter writer = new StringWriter();
      marshaller.marshal(bean, writer);
      marshalledBean = writer.toString();
   }
 
   @Override
   public String toString()
   {
      return marshalledBean;
   }
 
   private Document toDocument()
   {
      return document;
   }
 
   public static DOMSource marshalled(final Object entity)
   {
      try
      {
         final SerialisationHelper serialisationHelper = new SerialisationHelper(entity);
         return new DOMSource(serialisationHelper.toDocument()) {
            @Override
            public String toString()
            {
               return serialisationHelper.toString();
            }};
      }
      catch (final JAXBException e)
      {
         throw new RuntimeException("unable to marshal " + entity, e);
      }
      catch (final ParserConfigurationException e)
      {
         throw new RuntimeException("unable to marshal " + entity, e);
      }
   }
}

Refactoring Ideas: inline method with flag parameter

Check all calls to see if the value of the flag can be analysed, if so inline everywhere (otherwise tell the developer where it is ambiguous):

   public static String pad(final String string, final int length, final char character, final boolean before)
   {
      if(before)
      {
         return leftPad(string, length, character);
      }
      else
      {
         return rightPad(string, length, character);
      }
   }