Click here to Skip to main content
15,881,852 members
Articles / Programming Languages / Java / Java SE / J2EE

Modular J2EE, Using Spring DM, Zk and EclipseLink

Rate me:
Please Sign up or sign in to vote.
4.92/5 (9 votes)
21 Apr 2011CPOL15 min read 52.6K   8  
This article describes how to create a basic modular Java Enterprise Application, by using these Open Source frameworks
package com.cp.adrabi.jln.todo.tests.integration;

import java.util.ArrayList;
import java.util.List;

import net.sf.ehcache.CacheManager;

import org.osgi.framework.ServiceReference;
import org.springframework.core.io.Resource;
import org.springframework.osgi.test.AbstractConfigurableBundleCreatorTests;

import com.cp.adrabi.jln.model.Todo;
import com.cp.adrabi.jln.todo.services.TodoService;
import com.cp.adrabi.jln.todo.tests.utils.TestUtil;

/**
 * Integration test for Todo service
 * 
 * @author ADRABI Abderrahim (z3vil)
 *
 */
public class TodoServiceIntegrationTest extends AbstractConfigurableBundleCreatorTests
{
	private static final String TODO_CACHE = "todoCache";
	
	/**
	 * Set up bundles/fragments needed by Todo service
	 */
	@Override
	protected Resource[] getTestFrameworkBundles()
	{
		// *** change path ***
		List<Resource> resources = new ArrayList<Resource>();
		for(Resource r : TestUtil.makeResources("/base-test.txt", "/mnt/dev/java/platform/"))
		{
			resources.add(r);
		}
		for(Resource r : TestUtil.makeResources("/todo-test.txt", "/mnt/dev/java/jeenightmare/plugins/"))
		{
			resources.add(r);
		}
		
		return resources.toArray(new Resource[]{});
	}
	
	/**
	 * Override root path for classes
	 */
	@Override
	public String getRootPath() 
	{
		   return"file:./bin";
	}
	
	/**
	 * test bundle context, work only on JUnit 3
	 */
	public void testBundleContext()
	{
		assertNotNull( this.bundleContext );
	}
	
	/**
	 * Test todo service
	 */
	public void testTodoService()
	{
		assertNotNull( makeService() );
	}
	
	/**
	 * Test cache service
	 */
	public void testCacheService()
	{
		assertNotNull(makeCache());
	}
	
	/**
	 * Test list of Todo objects
	 */
	public void testListTodo()
	{
		final int cache_size = 1;
		
		cacheEmpty();
		
		List<Todo> list = makeService().getListTodo();
		
		assertNotNull(list);
		
		cacheNonEmpty();
		
		cacheSize( cache_size );
		
		list = makeService().getListTodo();
		
		cacheNonEmpty();
		
		cacheSize( cache_size );
		
	}
	
	/**
	 * Test save new Todo object
	 */
	public void testSaveTodo()
	{
		cacheNonEmpty();
		
		Todo todo = new Todo();
		todo.setSummary("#Homework n1");
		todo.setSummary("Create a test integration for service Todo");
		
		makeService().saveTodo(todo);
		
		// after any persist operation need cache be empty
		cacheEmpty();
	}
	
	/**
	 * Test get user by ID
	 */
	public void testGetTodoById()
	{
		final int cache_size = 1;
		
		final Long id = 1l;
		
		cacheEmpty();
		
		Todo todo = makeService().getTodoById(id);
		
		assertNotNull(todo);
		
		cacheNonEmpty();
		cacheSize(cache_size);
	}
	
	/**
	 * Test list todo after insert
	 */
	public void testListTodoAfterInsert()
	{
		final int cache_size = 2;
		
		cacheNonEmpty();
		
		List<Todo> list = makeService().getListTodo();
		
		assertEquals(1, list.size());
		
		cacheNonEmpty();
		cacheSize(cache_size);
	}
	
	/**
	 * Test update todo
	 */
	public void testUpdateTodo()
	{
		final int cache_size = 1;
		
		final Long id = 1l;
		
		cacheNonEmpty();
		
		Todo todo = makeService().getTodoById(id);
		String old_val = todo.getSummary();
		
		assertSame(old_val, todo.getSummary());
		
		todo.setSummary("#Update");
		
		assertNotSame(old_val, todo.getSummary());
		
		makeService().updateTodo(todo);
		
		cacheEmpty();
		
		todo = makeService().getTodoById(id);
		
		assertNotSame(old_val, todo.getSummary());
		
		cacheNonEmpty();
		
		cacheSize(cache_size);
	}
	
	/**
	 * Test remove todo
	 */
	public void testRemoveTodoById()
	{
		cacheNonEmpty();
		
		final Long id = 1l;
		
		makeService().removeTodoById(id);
		
		cacheEmpty();
	}
	
	/**
	 * Test list todo list after delete
	 */
	public void testListTodoAfterDelete()
	{
		cacheEmpty();
		
		List<Todo> list = makeService().getListTodo();
		
		assertEquals(0, list.size());
		
		cacheNonEmpty();
	}
	
	/*
	 *
	 * Private stuff
	 * 
	 */
	
	private TodoService makeService()
	{
		ServiceReference ref = this.bundleContext.getServiceReference("com.cp.adrabi.jln.todo.services.TodoService");
		return (TodoService) this.bundleContext.getService(ref);
	}
	
	private CacheManager makeCache()
	{
		ServiceReference ref = this.bundleContext.getServiceReference("net.sf.ehcache.CacheManager");
		return (CacheManager) this.bundleContext.getService(ref);
	}
	
	private void cacheEmpty()
	{
		CacheManager cache = makeCache();
		assertTrue( cache.getCache(TODO_CACHE).getSize() == 0 );
	}
	
	private void cacheNonEmpty()
	{
		CacheManager cache = makeCache();
		assertTrue( cache.getCache(TODO_CACHE).getSize() > 0 );
	}
	
	private void cacheSize(int size)
	{
		CacheManager cache = makeCache();
		assertTrue( cache.getCache(TODO_CACHE).getSize() == size );
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
Morocco Morocco
Adrabi!, Just another Ghost in the Shell =)

Comments and Discussions