RSS

Category Archives: FEST

Chasing Heisenbugs from an AKKA actor integration test with awaitility

Abstract : Ever had an impression you’re changing what you’re observing by simply observing it? If you so, you may have hit a Heisenbug ([1], [2]). Well, OK, I agree that a more precise definition of “observing” is needed and that we can differentiate between active and passive observing. The thing is one can hardly be a fully passive observer, especially when it comes to testing multi-threaded programs. Assuming you’re really working on the program and not simply looking a video tutorial in which case you will be a fully passive observer (and I would be wondering how the heck you’ve tumbled on my article). In short, when working on (including testing) a program we may introduce Heisenbugs through levels of indirection which are usually hard to spot. In C/C++ such a level of indirection represents uninitialized auto variables which can change every time you run you program. In Java a source of many indirection levels is the platform independence provided by the JVM. In JavaScript a source of Heisenbugs can be processing uncontrolled (browser dependent) events such as the scroll event.

Goal : Chase and fix a Heisenbug within an AKKA actor integration test using Awaitility

Acknowledgement: My gratitude goes to the open source community

An AKKA actor based program : I’ve already given an example of an AKKA actor base program here and you can find the source code here. So to save you and me some time, I’m going to re-use it.

Naïve test case : First we are going to build a test case which is prone to Heisenbugs and then we are going to fix it. I’m going to use JUnit, Spring Test Framework, Mockito, FEST-Assert, and FEST-Reflect to build up the following integration test case (access article’s full source code):

import akka.actor.ActorRef;
import org.honeysoft.akka.Bootstrap;
import org.honeysoft.akka.service.IBusinessService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.fest.reflect.core.Reflection.field;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@ContextConfiguration(classes = {Bootstrap.class})
@RunWith(SpringJUnit4ClassRunner.class)
public class BusinessActorTest {

    @Autowired
    @Qualifier(Bootstrap.BUSINESS_ACTOR)
    private ActorRef businessActorRef;

    @Autowired
    private IBusinessService businessService;

    @Mock
    private Logger mockBusinessServiceLogger;

    @Before
    public void beforeEach() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void shouldBeValidWhenNoOneIsNull() {
        //GIVEN
        field("logger").ofType(Logger.class).in(businessService).postDecorateWith(mockBusinessServiceLogger);

        //WHEN
        String testString = "test-string";
        businessActorRef.tell(testString);

        //THEN
        verify(mockBusinessServiceLogger, times(1)).info(anyString(), eq(testString));
    }
}

The thing is that this test case is prone to Heisenbugs. More precisely, the verification (at line 50) may pass some times and may fail others. Depending on the speed of execution and thread priority it can happen that the verification comes before the businessService receives the testString for processing. Luckily we have Awaitility at our disposal so the fix is straightforward:

import akka.actor.ActorRef;
import com.jayway.awaitility.Awaitility;
import com.jayway.awaitility.Duration;
import org.fest.assertions.Assertions;
import org.honeysoft.akka.Bootstrap;
import org.honeysoft.akka.service.IBusinessService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;
import org.slf4j.Marker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import static org.fest.reflect.core.Reflection.field;

@ContextConfiguration(classes = {Bootstrap.class})
@RunWith(SpringJUnit4ClassRunner.class)
public class BusinessActorTest {

    @Autowired
    @Qualifier(Bootstrap.BUSINESS_ACTOR)
    private ActorRef businessActorRef;

    @Autowired
    private IBusinessService businessService;

    @Before
    public void beforeEach() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void shouldBeValidWhenNoOneIsNull() throws Exception {
        //GIVEN
        final ConcurrentMap<String, Object> threadSafeMap = new ConcurrentHashMap<String, Object>(1);
        field("logger").ofType(Logger.class).in(businessService).postDecorateWith(new TestLogger(threadSafeMap));

        //WHEN
        String testString = "test-string";
        businessActorRef.tell(testString);

        //THEN
        Awaitility.waitAtMost(Duration.FIVE_SECONDS).until(new Callable<Boolean>() {
            @Override
            public Boolean call() throws Exception {
                return !threadSafeMap.isEmpty();
            }
        });

        Assertions.assertThat(threadSafeMap).hasSize(1);
        Assertions.assertThat(threadSafeMap.values().iterator().next()).isEqualTo(testString);
    }

    private static final class TestLogger implements Logger {

        private final ConcurrentMap<String, Object> map;

        private TestLogger(ConcurrentMap<String, Object> map) {
            this.map = map;
        }

        @Override
        public void info(String format, Object arg) {
            map.put(format, arg);
        }

        //Other overridden methods go here
    }
}

So what happened is that instead of mocking our logger target we’ve created a thread-safe TestLogger to help us. Then we’ve added an awaitility block to either wait for at most 5 secs or until our piggy-bag map is not empty. Well, that was all. Not too painful right?

 

Tags: , , , , , , , , , , , , ,

AKKA actor dependency injection using Spring

Abstract : Dependency Injection (DI) is refine flavour of Inversion of Control (IoC) design pattern. One of the software frameworks out there which provides DI implementation is Spring. AKKA on the other hand is “a toolkit and runtime for building highly concurrent, distributed, and fault tolerant event-driven applications on the JVM“. In this article we are going to concentrate on the actor model implemented by AKKA and more specifically on dependency injection in the UntypedActor class, as well as, injecting AKKA actor in a Spring enabled service.

Goal : Create a software bridge between Spring and an AKKA actor to ensure basic dependency injection

Acknowledgement : My gratitude goes to the open source community

We are going to use the following components to reach our goal: AKKA version 2.0.2, Spring 3.1.1.RELEASE, FEST Reflect 1.4, Logback 1.0.6, CGLIB 2.2, and Maven to glue everything together. All programming will be done in Java and you can find all files on GitHub. Don’t hesitate to fork the project and improve it. Here is the Maven POM file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.honeysoft.akka</groupId>
    <artifactId>akka-di</artifactId>
    <version>1.0</version>

    <properties>
        <akka.version>2.0.2</akka.version>
        <spring.version>3.1.1.RELEASE</spring.version>
        <fest-reflect.version>1.4</fest-reflect.version>
        <logback.version>1.0.6</logback.version>
    </properties>

    <dependencies>
        <!-- Akka dependencies  -->
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-actor</artifactId>
            <version>${akka.version}</version>
        </dependency>

        <!-- Spring dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Tools -->
        <dependency>
            <groupId>org.easytesting</groupId>
            <artifactId>fest-reflect</artifactId>
            <version>${fest-reflect.version}</version>
        </dependency>

        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>2.2</version>
        </dependency>

        <!-- Logging -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
        </dependency>

    </dependencies>

    <repositories>
        <repository>
            <id>akka.repository</id>
            <name>Akka Maven Repository</name>
            <url>http://repo.akka.io/releases/</url>
        </repository>
    </repositories>
 </project>

Good. Once you have all what’s needed we can start by building our simple business service which we are going to inject in our actor, here it is:

package org.honeysoft.akka.service;

public interface IBusinessService {

    void doBusiness(Object o);
}

… and it’s implementation:

package org.honeysoft.akka.service.impl;

import org.honeysoft.akka.service.IBusinessService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class BusinessService implements IBusinessService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public void doBusiness(Object o) {
        logger.info("Doing business with {}", o);
    }
}

As we can seen there is nothing fancy in our simple business service. All that it’s doing is logging an information about doing business with someone. Note, however, that it’s annotated with @Service this allows it to be discovered, created and injected by Spring.

Now, let’s see our actor:

package org.honeysoft.akka.actor;

import akka.actor.UntypedActor;
import org.honeysoft.akka.service.IBusinessService;
import org.springframework.beans.factory.annotation.Autowired;

public class BusinessActor extends UntypedActor {

    @Autowired
    private IBusinessService businessService;

    @Override
    public void onReceive(Object o) throws Exception {
        businessService.doBusiness(o);
    }
}

Once more, nothing special, just injecting our business service using the @Autowired annotation. Now comes the interesting part. We are going to need a custom implementation of akka.actor.Props class:

package org.honeysoft.akka.di;

import akka.actor.Props;
import akka.actor.UntypedActorFactory;
import org.springframework.context.ApplicationContext;

public class DependencyInjectionProps extends Props {
    /**
     * No-args constructor that sets all the default values.
     */
    public DependencyInjectionProps(ApplicationContext applicationContext, Class<?> actorClass) {
        super(new SpringUntypedActorFactory(actorClass, applicationContext));
    }

    /**
     * Java API.
     */
    public DependencyInjectionProps(ApplicationContext applicationContext, UntypedActorFactory factory) {
        super(new SpringUntypedActorFactory(factory, applicationContext));
    }
}

The interesting part here is our SpringUntypedActorFactory which is implemented as follows:

package org.honeysoft.akka.di;

import akka.actor.UntypedActor;
import akka.actor.UntypedActorFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import java.lang.reflect.Field;

import static org.fest.reflect.util.Accessibles.setAccessible;
import static org.fest.reflect.util.Accessibles.setAccessibleIgnoringExceptions;

public class SpringUntypedActorFactory implements UntypedActorFactory {

    private final DependencyInjectionFactory dependencyInjectionFactory;

    private final ApplicationContext applicationContext;

    public SpringUntypedActorFactory(Class<?> actorClass, ApplicationContext applicationContext) {
        this.dependencyInjectionFactory = new DefaultUntypedActorFactory(actorClass);
        this.applicationContext = applicationContext;
    }

    public SpringUntypedActorFactory(UntypedActorFactory customFactory, ApplicationContext applicationContext) {
        this.dependencyInjectionFactory = new SpecificUntypedActorFactory(customFactory);
        this.applicationContext = applicationContext;
    }

    private interface DependencyInjectionFactory {
        UntypedActor createAndInject();
    }


    private abstract class AbstractUntypedActorFactory implements DependencyInjectionFactory {

        @Override
        public final UntypedActor createAndInject() {
            try {
                UntypedActor untypedActor = create();

                Class<?> aClass = getActorClass();
                for (Field field : aClass.getDeclaredFields()) {

                    if (field.getAnnotation(Autowired.class) != null) {
                        boolean accessible = field.isAccessible();
                        try {
                            setAccessible(field, true);
                            field.set(untypedActor, applicationContext.getBean(field.getType()));
                        } catch (IllegalAccessException e) {
                            throw new IllegalStateException("Unable to create actor instance", e);
                        } finally {
                            setAccessibleIgnoringExceptions(field, accessible);
                        }
                    }
                }
                return untypedActor;

            } catch (InstantiationException e) {
                throw new IllegalStateException("Unable to create actor instance", e);
            } catch (IllegalAccessException e) {
                throw new IllegalStateException("Unable to create actor instance", e);
            }

        }

        protected abstract Class<?> getActorClass();

        protected abstract UntypedActor create() throws InstantiationException, IllegalAccessException;

    }

    private final class SpecificUntypedActorFactory extends AbstractUntypedActorFactory {

        private final UntypedActorFactory specificFactory;
        private volatile Class<?> actorClass;

        private SpecificUntypedActorFactory(UntypedActorFactory specificFactory) {
            this.specificFactory = specificFactory;
        }

        @Override
        protected Class<?> getActorClass() {
            return actorClass;
        }

        @Override
        protected UntypedActor create() throws InstantiationException, IllegalAccessException {
            UntypedActor untypedActor = (UntypedActor) specificFactory.create();
            actorClass = untypedActor.getClass();
            return untypedActor;
        }
    }

    private final class DefaultUntypedActorFactory extends AbstractUntypedActorFactory {
        private final Class<?> actorClass;

        public DefaultUntypedActorFactory(Class<?> actorClass) {
            this.actorClass = actorClass;
        }

        @Override
        protected Class<?> getActorClass() {
            return actorClass;
        }

        @Override
        protected UntypedActor create() throws InstantiationException, IllegalAccessException {
            return (UntypedActor) actorClass.newInstance();
        }
    }

    /**
     * This method must return a different instance upon every call.
     */
    @Override
    public UntypedActor create() {
        return dependencyInjectionFactory.createAndInject();
    }
}

Inside our custom SpringUntypedActorFactory is a Strategy pattern which allows us to inject Spring context available beans in an actor fields annotated with @Autowired.

Well, that’s almost all. Now we need a bootstrap component and we are going to provide one using XML-less Spring configuration:

package org.honeysoft.akka;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import org.honeysoft.akka.actor.BusinessActor;
import org.honeysoft.akka.di.DependencyInjectionProps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

@Configuration
@ComponentScan({"org.honeysoft.akka.service"})
public class Bootstrap {

    public static final String BUSINESS_ACTOR = "honeysoft-business-actor";
    public static final String ACTOR_SYSTEM = "honeysoft-actor-actorSystem";
    private ActorSystem actorSystem;

    @Autowired
    private ApplicationContext applicationContext;

    @Bean(name = ACTOR_SYSTEM, destroyMethod = "shutdown")
    public ActorSystem actorSystem() {
        actorSystem = ActorSystem.create(ACTOR_SYSTEM);
        return actorSystem;
    }

    @Bean(name = BUSINESS_ACTOR)
    @DependsOn({ACTOR_SYSTEM})
    public ActorRef businessActor() {
        return actorSystem.actorOf(//
                new DependencyInjectionProps(applicationContext, BusinessActor.class), BUSINESS_ACTOR);
    }
}

Oh, you can also inject our BusinessActor in another Spring enabled service (or component in general) like this:


import akka.actor.ActorRef;
import org.honeysoft.akka.Bootstrap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class MyCoolService {

    @Autowired
    @Qualifier(Bootstrap.BUSINESS_ACTOR)
    private ActorRef businessActor;
    
    public void doSomething() {
       businessActor.tell("message"); 
       // more logic goes here ...
    }
}

That was all folks. Now it’s up to you to leave a comment or contribute on GitHub.

 
8 Comments

Posted by on August 5, 2012 in AKKA, FEST-Reflect, Java, Maven, Spring

 

Tags: , ,

Testing AKKA actors with Mockito and FEST-Reflect

Abstract : One of the few frameworks implementing the actor model is AKKA. In fact AKKA is a lot more than just an implementation of the actor model, but within this post we are going to concentrate on combining Mockito, JUnit and FEST-Reflect in order to facilitate actor testing and thus we won’t need all the fancy features of AKKA.
Goal : Mocking an instance field within an AKKA actor.
Acknowledgement: My gratitude goes to the open source community and to the following people:
Munish K Gupta – Using AKKA testkit with Java

In order to reach our goal will need the following maven configuration. Note that you need to add the AKKA repository:

<!-- other maven configuration -->
    <dependencies>
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-actor</artifactId>
            <version>2.0.2</version>
        </dependency>

        <dependency>
            <groupId>org.easytesting</groupId>
            <artifactId>fest-reflect</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.9</version>
        </dependency>

        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-testkit</artifactId>
            <version>2.0.2</version>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.0</version>
        </dependency>

    </dependencies>

    <repositories>
        <repository>
            <id>akka.repository</id>
            <name>Akka Maven Repository</name>
            <url>http://repo.akka.io/releases/</url>
        </repository>
        <!-- other repositories ... -->
    </repositories>

The actor which we are going to test looks like this:

import akka.actor.UntypedActor;
import com.honeysoft.business.service.IBusinessService;
import org.springframework.beans.factory.annotation.Autowired;

public class BusinessActor extends UntypedActor {

    @Autowired
    private IBusinessService businessService;

    @Override
    public void onReceive(Object message) {
        businessService.doBusiness(message);
    }
}

As you can see, I’m using Spring framework for dependency injection within the actor, but you are free to choose whatever approach you want to instantiate and assign the business service.

Our goal is quite simple, we have to mock the businessService within our BusinessActor. The difficulty comes from the fact that in a test case we are going to have an ActorRef reference variable to our actor and not a plain Actor reference. This means that our actor is actually nested within an ActorRef reference. As you might already guessed this brings further complications for mocking our businessService reference within the actor instance itself. Or in other words, our businessService is deeply nested and the exact path is actorRef.actorCell.actor.businessService. Luckily for us, since version 1.4 of FEST-Reflect we can play with (deeply) nested fields with a single line of code (not counting the static import). Here is how:

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import com.honeysoft.business.service.IBusinessService;
import com.typesafe.config.ConfigFactory;
import org.fest.reflect.core.Reflection;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import static org.fest.reflect.core.Reflection.*;
import static org.mockito.Matchers.eq;

@RunWith(MockitoJUnitRunner.class)
public class TestBusinessActor {

    static ActorSystem akkaSystem = ActorSystem.create("honeysoft-test-system", ConfigFactory.load().getConfig("honeysoft-test-system"));

    @Mock
    private IBusinessService businessServiceMock;

    private ActorRef businessActor;

    @Before
    public void setup() {
        businessActor = akkaSystem.actorOf(new Props(BusinessActor.class));
    }

    @After
    public void clean() {
        akkaSystem.stop(businessActor);
    }

    @Test
    public void shouldExecuteBusinessMethod() {
        //GIVEN
        String businessMessage = "Some business message";

        field("actorCell.actor.businessService").ofType(IBusinessService.class)//
            .in(businessActor).set(businessServiceMock);

        //WHEN
        businessActor.tell(businessMessage);

        //THEN
        Mockito.verify(businessServiceMock, Mockito.times(1))//
            .doBusiness(eq(businessMessage));
    }
}

As you can see from the highlighted line 43 setting a deeply nested variable is as simple as specifying the path to it. Note that the exact path is businessActor.actorCell.actor.businessService, but the businessActor is already passed to the .in() method and thus as an argument to field() we have only “actorCell.actor.businessService”.

To run the test you are also going to need a configuration file named application.conf which I have placed in my src/test/resources directory. The content of application.conf is:

honeysoft-test-system {
	akka {
	    mode = test
	  	event-handlers = ["akka.testkit.TestEventListener"]
		loglevel = DEBUG
		actor {
			debug {
			 receive = on
			 autoreceive = on
			 lifecycle = on
			}
		}
  	}
}

Well that was all for this post. Don’t hesitate to leave a comment!

 
2 Comments

Posted by on July 1, 2012 in AKKA, FEST-Reflect, Java, JUnit, Maven, Mockito

 

Tags: , , , ,

Sunday’s recipe

Today’s software recipe includes the following main ingredients (as Maven artifacts) Guice, Hibernate, JPA, HSQLDB, FEST-Assert, Apache Commons Lang, JUnit which we are going to put and mix in Eclipse 3.7.2 pot. Our goal would be a simple one: persist something in HSQLDB using JPA and Hibernate as underlying implementation; take advantage of Guice for Context and Dependency Injection (CDI) and test what we’ve done with JUnit, FEST-Assert and HSQLDB Manager. For a start here is the pom.xml relevant portion which you may want to include:

<dependencies>
 <dependency>
  <groupId>com.google.inject</groupId>
  <artifactId>guice</artifactId>
  <version>3.0</version>
 </dependency>
 <dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-entitymanager</artifactId>
  <version>4.1.1.Final</version>
 </dependency>
 <dependency>
  <groupId>org.hibernate.javax.persistence</groupId>
  <artifactId>hibernate-jpa-2.0-api</artifactId>
  <version>1.0.1.Final</version>
 </dependency>
 <dependency>
  <groupId>org.hsqldb</groupId>
  <artifactId>hsqldb</artifactId>
  <version>2.2.8</version>
 </dependency>
 <dependency>
  <groupId>commons-lang</groupId>
  <artifactId>commons-lang</artifactId>
  <version>2.6</version>
 </dependency>
 <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.10</version>
  <scope>test</scope>
 </dependency>
 <dependency>
  <groupId>org.easytesting</groupId>
  <artifactId>fest-assert</artifactId>
  <version>1.4</version>
  <scope>test</scope>
 </dependency>
</dependencies>

Since we need something to persist in our database let’s create a very simple Project entity having only an identifier and a name fields. Here is how it looks like:

package org.honeysoft.hsqldbtest.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import org.apache.commons.lang.builder.EqualsBuilder;

@Entity(name = "project")
public class Project {

    @Id
    @GeneratedValue
    private Long id;
    
    @Column(name="name")
    private String name;

    public Project() {
	// JPA needs empty constructor
    }
    
    public Project(String projectName) {
	this.name = projectName;
    }

    public Long getId() {
	return id;
    }

    public void setId(Long id) {
	this.id = id;
    }

    public String getName() {
	return name;
    }

    public void setName(String name) {
	this.name = name;
    }
    @Override
    public boolean equals(Object obj) {
	if (obj == this) {
	    return true;
	}

	if (!(obj instanceof Project)) {
	    return false;
	}
	Project theOtherObject = (Project) obj;
	EqualsBuilder equalsBuilder = new EqualsBuilder();
	equalsBuilder.append(theOtherObject.name, this.name);

	return equalsBuilder.isEquals();
    }
}

Next we need a data access objet (DAO) :

package org.honeysoft.hsqldbtest.dao;

import org.honeysoft.hsqldbtest.entity.Project;

public interface IProjectDao {
    void save(Project project);
}

… and its implementation (which we are going to keep short and simple):

package org.honeysoft.hsqldbtest.service.impl;

import javax.inject.Inject;
import javax.persistence.EntityManager;

import org.honeysoft.hsqldbtest.dao.IProjectDao;
import org.honeysoft.hsqldbtest.entity.Project;

public class ProjectDao implements IProjectDao {

    @Inject
    private EntityManager entityManager;

    @Override
    public void save(Project project) {
	entityManager.getTransaction().begin();
	entityManager.persist(project);
	entityManager.getTransaction().commit();
    }
}

Here comes our service which will do the business logic:

package org.honeysoft.hsqldbtest.service;

public interface IProjectService {
    void create(String projectName);
}

… and its implementation:

package org.honeysoft.hsqldbtest.service.impl;

import javax.inject.Inject;

import org.honeysoft.hsqldbtest.dao.IProjectDao;
import org.honeysoft.hsqldbtest.entity.Project;
import org.honeysoft.hsqldbtest.service.IProjectService;

public class ProjectService implements IProjectService {

    @Inject
    private IProjectDao projectDao;

    /** @{inheritDocs */
    @Override
    public void create(String projectName) {
	projectDao.save(new Project(projectName));
    }
}

So far so good. We do need some plumbing though. Within the scope of our small recipe we have two dimensions of plumbing we have to consider. One of them is the CDI and since we use Guice in our example we have to create a module keeping the dependency bindings, here it goes:

package org.honeysoft.hsqldbtest;

import javax.inject.Singleton;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.honeysoft.hsqldbtest.dao.IProjectDao;
import org.honeysoft.hsqldbtest.dao.impl.ProjectDao;
import org.honeysoft.hsqldbtest.service.IProjectService;
import org.honeysoft.hsqldbtest.service.impl.ProjectService;

import com.google.inject.AbstractModule;
import com.google.inject.Provides;

public class DBModule extends AbstractModule {

    private static final ThreadLocal<EntityManager> ENTITY_MANAGER_CACHE = new ThreadLocal<EntityManager>();

    @Override
    public void configure() {
	bind(IProjectDao.class).to(ProjectDao.class);
	bind(IProjectService.class).to(ProjectService.class);
    }

    @Provides
    @Singleton
    public EntityManagerFactory createEntityManagerFactory() {
	return Persistence.createEntityManagerFactory("db-manager");
    }

    @Provides
    public EntityManager createEntityManager(
	    EntityManagerFactory entityManagerFactory) {
	EntityManager entityManager = ENTITY_MANAGER_CACHE.get();
	if (entityManager == null) {
	    ENTITY_MANAGER_CACHE.set(entityManager = entityManagerFactory
		    .createEntityManager());
	}
	return entityManager;
    }
}

The second aspect of our concerns is the JPA. In order for JPA to be glued with its implementation (in our case Hibernate), we need a persistence.xml file located within META-INF directory in the classpath of our Java application. Since we use Maven, I’m going to put the persistence.xml file in src/main/resources/META-INF/persistence.xml. The actual content of the file is:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
	version="1.0">

	<persistence-unit name="db-manager" transaction-type="RESOURCE_LOCAL">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>

		<properties>
			<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
			<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:hsql://localhost/tmp/testdb" />
			<property name="javax.persistence.jdbc.user" value="sa" />
			<property name="javax.persistence.jdbc.password" value="" />
			<!-- if this is true, hibernate will print (to stdout) the SQL it executes, 
				so you can check it to ensure it's not doing anything crazy -->

			<property name="hibernate.show_sql" value="true" />
			<!-- since most database servers have slightly different versions of the 
				SQL, Hibernate needs you to choose a dialect so it knows the subtleties of 
				talking to that server -->
			<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
			<!-- this tell Hibernate to update the DDL when it starts, very useful 
				for development, dangerous in production -->
			<property name="hibernate.hbm2ddl.auto" value="update" />
		</properties>
	</persistence-unit>
</persistence>

Here are some important stuff:

  1. On line 7 we use the db-manager which is the same as in our DBModule.createEntityManagerFactory()
  2. On line 12, we’ve specified the URL of our database : jdbc:hsqldb:hsql://localhost/tmp/testdb which is important since we have to actually have a HSQLDB running. I’ve used an Eclipse launcher to run HSQLDB with the following program arguments: –database.0 file:/tmp/testdb –dbname.0 testdb. In case you don’t know how to create such an Eclipse launcher you can follow this link

And last but not least our test:

package org.honeysoft.hsqldbtest.service.impl;

import static org.fest.assertions.Assertions.assertThat;

import java.util.List;

import javax.persistence.EntityManager;

import org.honeysoft.hsqldbtest.DBModule;
import org.honeysoft.hsqldbtest.entity.Project;
import org.honeysoft.hsqldbtest.service.IProjectService;
import org.junit.Test;

import com.google.inject.Guice;
import com.google.inject.Injector;

public class TestProjectService {

    @Test
    public void shouldPersistProject() throws Exception {
	// GIVEN
	Injector injector = Guice.createInjector(new DBModule());
	IProjectService projectService = injector
		.getInstance(IProjectService.class);
	EntityManager entityManager = injector.getInstance(EntityManager.class);

	String projectName = "honeysoft";

	// WHEN
	projectService.create(projectName);
	
	// THEN
	List<Project> resultList = entityManager.createNamedQuery(
		Project.GET_ALL, Project.class).getResultList();

	assertThat(resultList.contains(new Project(projectName))).isTrue();
    }
}

Well, more or less that’s it. If you want you can take advantage of HSQLDB Database Manager. One way to run it is via Eclipse launcher. You don’t need any other dependency within you project in order to run it (except for hsqldb). Create a new Java Application Eclipse launcher where as a Project you choose the one which have the hsqldb as a Maven dependency and in Main class you provide org.hsqldb.util.DatabaseManagerSwing.

Acknowledgements goes to these guys for their work and to the open-source community without which this post would not take place:

 
Leave a comment

Posted by on April 29, 2012 in Eclipse, FEST-Assert, Guice, Hibernate, HSQLDB, Java, JPA, Maven

 

Tags:

Decoration with FEST-Reflect 1.3

Yesterday (3rd of April, 2012) the FEST-Reflect 1.3 (part of FEST libraries) was published on maven central. Since this fact requires a celebration, I’ve prepared a short tutorial of the new field decorator functionality.

Add the following dependency in your pom.xml to take advantage of FEST-Reflect.

		<dependency>
			<groupId>org.easytesting</groupId>
			<artifactId>fest-reflect</artifactId>
			<version>1.3</version>
		</dependency>

Example 1: Using FEST-Reflect decorator for testing logging functionality

Let’s say we have a simple business service:

package org.honey.service.impl;

import org.honey.service.IBusinessService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BusinessService implements IBusinessService {

  private final Logger logger = LoggerFactory.getLogger(this.getClass());

  /**
   * {@inheritDoc}
   */
  @Override
  public void doBusiness() {
    logger.info("Hi! This is a business service ...");
  }
}

Our goal would be to test that the logger field is called and still be able to see the printed message on our screen. With the help of FEST-Reflect we can pre-decorate or post-decorate the logger with another object implementing the org.slf4j.Logger (I’m going to use a mock object via Mockito). Here is the test:

package org.honey.service.impl;

import static org.fest.reflect.core.Reflection.field;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import org.honey.service.IBusinessService;
import org.junit.Test;
import org.slf4j.Logger;

public class TestBusinessService {

  @Test
  public void shouldCallLogger() {
    //GIVEN
    Logger loggerMock = mock(Logger.class);

    IBusinessService service = new BusinessService();

    field("logger").ofType(Logger.class).in(service).preDecorateWith(loggerMock);

    //WHEN
    service.doBusiness();

    //THEN
    verify(loggerMock, times(1)).info(eq("Hi! This is a business service ..."));
  }
}

With the above code, our loggerMock is going to be called first and then the actual logger. We can also post-decorate the targeted field (in which case the logger will be called first and then the loggerMock):

field("logger").ofType(Logger.class).in(service).postDecorateWith(loggerMock);

In fact we can have both pre-decoration and post-decoration:

field("logger").ofType(Logger.class).in(service)//
.preDecorateWith(loggerMock).postDecorateWith(loggerMock);

However, don’t forget to change the verification step (for the test to work) to:

verify(loggerMock, times(2)).info(eq("Hi! This is a business service ..."));

Example 2: Complex decoration

Now let’s take a look at what kind of arsenal we have for tackling some more complex scenarios:

Let’s upgrade our BusinessService implementation:

public class BusinessService implements IBusinessService {

  private final Logger logger = LoggerFactory.getLogger(this.getClass());

  private final INotificationService notificationService;

  public BusinessService() {
    this.notificationService = new INotificationService() {

      @Override
      public boolean notify(String recipient) {
        // some logic
        return false;
      }
    };
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void doBusiness() {

    String recipient = "some@one.com";

    if (notificationService.notify(recipient)) {
      logger.info("Successfully notified {}", recipient);
    } else {
      logger.error("Could not notifiy {}", recipient);
    }

  }

}

So what happened? I’ve added an INotificationService interface and a simple implementation in the constructor of our BusinessService. As we can see the implementation of INotificationService.notify() always returns false, so we can expect that the else branch in the doBusiness() method is always taken. What if we want to change this behaviour but without changing the actual INotificationService.notify() implementation? Here is a test that does exactly that:

public class TestBusinessService {

  @Test
  public void shouldCallInfoLogger() {
    //GIVEN
    Logger loggerMock = mock(Logger.class);

    INotificationService notificationServiceMock = mock(INotificationService.class);
    when(notificationServiceMock.sendNotification(anyString())).thenReturn(true);

    IBusinessService service = new BusinessService();

    field("logger").ofType(Logger.class).in(service).postDecorateWith(loggerMock);

    field("notificationService").ofType(INotificationService.class) //
          .in(service).postDecorateWith(notificationServiceMock)//
          .returningDecoratorResult();

    //WHEN
    service.doBusiness();

    //THEN
    verify(loggerMock, times(1)).info(eq("Successfully notified {}"), eq("some@one.com"));
  }
}

Again, we can pre-decorate instead of post-decorate, or we can pre- and post-decorate. Here is how:

field("notificationService").ofType(INotificationService.class) //
.in(service).preDecorateWith(notificationServiceMock1)//
.returningDecoratorResult()// taking the result from notificationServiceMock1
.postDecorateWith(notificationServiceMock2);

Ok, but what if our notificationServiceMock throws an exception? Well, we can silence any RuntimeException like this:

field("notificationService").ofType(INotificationService.class) //
.in(service).postDecorateWith(notificationServiceMock).returningDecoratorResult()//
.ignoringDecoratorExceptions(); // ignores any RuntimeException

Or a specific exception like this:

field("notificationService").ofType(INotificationService.class) //
.in(service).postDecorateWith(notificationServiceMock).returningDecoratorResult()//
.ignoringDecoratorExceptionsOfType(MyCoolException.class); // ignores only MyCoolException
 
1 Comment

Posted by on April 4, 2012 in FEST-Reflect, Java, Uncategorized