Atmosphere Framework is a most popular push framework for Java EE applications. I integrated framework in Spring Framework. In this simple application you can play a tic-tac-toe with real-time data sharing using push technology.
Git repository :
https://bitbucket.org/masal/tictactoe
Any comments are welcome!
Montag, 14. Januar 2013
Mittwoch, 5. Oktober 2011
Generic DAO and JPA
Generic DAO is a design pattern to reduce and prevent code replacing on basic CRUD operations. Hibernate-Generic-DAO (http://code.google.com/p/hibernate-generic-dao/) provides exactly this pattern solution for Hibernate or JPA.
UML Overview of this example:
BaseDAO is an extension of GenericDAO and it must be extended by each DAO class of the project.
JPAGenericDAO is a generic, abstract class to be extended by each concrete DAO class.
Sample code:
UserDAO extends the BaseDAO :
and a sample reference implementation of DAOs :
We keep basic CRUD operations in our abstract class but we have also seperate interfaces to provide 'Model' specific method and keep them seperate from generic DAO solutions.
UML Overview of this example:
BaseDAO is an extension of GenericDAO and it must be extended by each DAO class of the project.
JPAGenericDAO is a generic, abstract class to be extended by each concrete DAO class.
Sample code:
- /**
- * @author Hasan Oezdemir
- * @since 01.10.2011
- *
- */
- public abstract class JPAGenericDAO<T, ID extends Serializable> extends GenericDAOImpl<T, ID> implements
- BaseDAO<T, ID> {
- private static final Logger logger = LoggerFactory.getLogger(JPAGenericDAO.class);
- @PersistenceContext(unitName="scheduling")
- private EntityManager entityManager;
- public JPAGenericDAO(){
- super();
- }
- @PostConstruct
- public void initialized(){
- logger.info( "initializing generic dao, setting entity manager: {}", entityManager);
- this.setEntityManager(entityManager);
- JPASearchProcessor searchProcessor = new JPASearchProcessor(new JPAAnnotationMetadataUtil());
- this.setSearchProcessor(searchProcessor);
- }
- public T findByExampleUnique(T example) {
- Search s = this.createSearch(example);
- return searchUnique(s);
- }
- public Collection<T> findByExample(T example) {
- Search s = this.createSearch(example);
- return search(s);
- }
- protected Search createSearch(T example) {
- Search s = new Search(example.getClass());
- s.addFilter(this._getFilterFromExample(example));
- return s;
- }
- }
UserDAO extends the BaseDAO :
- /**
- * @author Hasan Oezdemir
- * @since 01.10.2011
- *
- */
- public interface UserDAO extends BaseDAO<User, Long> {
- }
and a sample reference implementation of DAOs :
- /**
- * @author Hasan Oezdemir
- * @since 01.10.2011
- *
- */
- @Singleton
- public class JPAUserDAO extends JPAGenericDAO<User, Long> implements UserDAO {
- public JPAUserDAO(){
- super();
- }
- }
We keep basic CRUD operations in our abstract class but we have also seperate interfaces to provide 'Model' specific method and keep them seperate from generic DAO solutions.
Dienstag, 4. Oktober 2011
Testing with Arquillian and Embedded Glassfish 3.1 - Part 2
We are configured our project to create and run tests with Arquillian. Here i will explain how to create a simple test using Arquillian and JPA.
We have created a data source with JNDI-name "jdbc/sample" in part 1, we will use it for our JPA test, i have used an API to make generic DAOs and hibernate-entity-manager as an entitity manager, needed dependencies therefore :
I've created a BaseDAO interface and then the a specific interface to provide CRUD operations for users.
And an abstract class for JPA implementations :
As you remember we are created a persistence-unit named "sample" while injecting the entity-manager at this point, we are used this unit for our DAOs :
We are now ready to implement an Arquillian Test for our JPA class.The most important section to create a static method annotated with @Deployment to build a ShrinkWrap, here is a sample method:
We must add our arquillian configuration too, otherwise ShrinkWrap reads the default configuration and ignores our configuration.
I've created an abstract class which is extended by all test-classes :
Transaction management may be better but this is enought for now :)
We must run our tests with Arquillian.class therefore we should add @RunWith(Arquillian.class) for each our tests-classes, here is a sample :
So our test class is ready to start, you start it either with maven or in eclipse.
We have created a data source with JNDI-name "jdbc/sample" in part 1, we will use it for our JPA test, i have used an API to make generic DAOs and hibernate-entity-manager as an entitity manager, needed dependencies therefore :
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-entitymanager</artifactId>
- <version>3.6.7.Final</version>
- <exclusions>
- <exclusion>
- <artifactId>slf4j-api</artifactId>
- <groupId>org.slf4j</groupId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-core</artifactId>
- <version>3.6.7.Final</version>
- <exclusions>
- <exclusion>
- <artifactId>slf4j-api</artifactId>
- <groupId>org.slf4j</groupId>
- </exclusion>
- </exclusions>
- </dependency>
- <!-- generic DAO dependencies -->
- <!-- This first dependency includes all the JPA implementations for the DAOs -->
- <dependency>
- <groupId>com.googlecode.genericdao</groupId>
- <artifactId>dao</artifactId>
- <version>1.0.0</version>
- </dependency>
- <!-- This second one includes the Hibernate Entity Manager plugin for the framework -->
- <dependency>
- <groupId>com.googlecode.genericdao</groupId>
- <artifactId>search-jpa-hibernate</artifactId>
- <version>1.0.0</version>
- </dependency>
- <!-- end of generic DAO dependencies -->
I've created a BaseDAO interface and then the a specific interface to provide CRUD operations for users.
- /**
- * @author Hasan Oezdemir
- * @since 01.10.2011
- *
- */
- public interface BaseDAO<T, ID extends Serializable> extends GenericDAO<T, ID> {
- public Collection<T> findByExample(T example);
- public T findByExampleUnique(T example);
- }
- /**
- * @author Hasan Oezdemir
- * @since 01.10.2011
- *
- */
- public interface UserDAO extends BaseDAO<User, Long> {
- }
And an abstract class for JPA implementations :
- /**
- * @author Hasan Oezdemir
- * @since 01.10.2011
- *
- */
- public abstract class JPAGenericDAO<T, ID extends Serializable> extends GenericDAOImpl<T, ID> implements
- BaseDAO<T, ID> {
- private static final Logger logger = LoggerFactory.getLogger(JPAGenericDAO.class);
- @PersistenceContext(unitName="sample")
- private EntityManager entityManager;
- public JPAGenericDAO(){
- super();
- }
- @PostConstruct
- public void initialized(){
- this.setEntityManager(entityManager);
- JPASearchProcessor searchProcessor = new JPASearchProcessor(new JPAAnnotationMetadataUtil());
- this.setSearchProcessor(searchProcessor);
- }
- public T findByExampleUnique(T example) {
- Search s = this.createSearch(example);
- return searchUnique(s);
- }
- public Collection<T> findByExample(T example) {
- Search s = this.createSearch(example);
- return search(s);
- }
- protected Search createSearch(T example) {
- Search s = new Search(example.getClass());
- s.addFilter(this._getFilterFromExample(example));
- return s;
- }
- }
As you remember we are created a persistence-unit named "sample" while injecting the entity-manager at this point, we are used this unit for our DAOs :
- @PersistenceContext(unitName="sample")
- private EntityManager entityManager
- /**
- * @author Hasan Oezdemir
- * @since 01.10.2011
- *
- */
- @Singleton
- public class JPAUserDAO extends JPAGenericDAO<User, Long> implements UserDAO {
- public JPAUserDAO(){
- super();
- }
- }
We are now ready to implement an Arquillian Test for our JPA class.The most important section to create a static method annotated with @Deployment to build a ShrinkWrap, here is a sample method:
- /**
- * Since Arquillian actually creates JAR files under the covers with
- * ShrinkWrap, the @Deployment is your way of controlling what is included
- * in that Archive. Note, each class utilized in your test case - whether
- * directly or indirectly - must be added to the deployment archive.
- */
- @Deployment
- public static Archive<?> createTestArchive() {
- return ShrinkWrap
- .create(JavaArchive.class, "test.jar")
- .addAsResource("arquillian.xml")
- .addPackages(true, UserDAO.class.getPackage())
- .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
- .addAsManifestResource("META-INF/persistence.xml",
- ArchivePaths.create("persistence.xml"));
- }
We must add our arquillian configuration too, otherwise ShrinkWrap reads the default configuration and ignores our configuration.
I've created an abstract class which is extended by all test-classes :
- /**
- *
- * @author Hasan Oezdemir
- * @since 02.10.2011
- *
- */
- public class AbstractJPATest {
- /**
- * Since Arquillian actually creates JAR files under the covers with
- * ShrinkWrap, the @Deployment is your way of controlling what is included
- * in that Archive. Note, each class utilized in your test case - whether
- * directly or indirectly - must be added to the deployment archive.
- */
- @Deployment
- public static Archive<?> createTestArchive() {
- return ShrinkWrap
- .create(JavaArchive.class, "test.jar")
- .addAsResource("arquillian.xml")
- .addAsResource("log4j.xml")
- .addPackages(true, UserDAO.class.getPackage())
- .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
- .addAsManifestResource("META-INF/persistence.xml",
- ArchivePaths.create("persistence.xml"));
- }
- @Inject
- protected UserTransaction userTransaction;
- public AbstractJPATest() {
- super();
- }
- /**
- * @throws java.lang.Exception
- */
- @Before
- public void setUp() throws Exception {
- this.userTransaction.begin();
- }
- /**
- * @throws java.lang.Exception
- */
- @After
- public void tearDown() throws Exception {
- try {
- this.userTransaction.rollback();
- } catch (Exception e) {
- throw new Exception(e);
- }
- }
- /**
- * @return a test user.
- */
- protected User createUser() {
- User user = new User();
- user.setEmail("hasan@hotmail.com");
- user.setName("hasan");
- user.setPassword("12345");
- return user;
- }
- }
Transaction management may be better but this is enought for now :)
We must run our tests with Arquillian.class therefore we should add @RunWith(Arquillian.class) for each our tests-classes, here is a sample :
- /**
- * @author Hasan Oezdemir
- * @since 01.10.2011
- *
- */
- @RunWith(Arquillian.class)
- public class JPAUserDAOTest extends AbstractJPATest {
- @Inject
- private JPAUserDAO jpaUserDAO;
- /**
- * Test method for
- * {@link com.citecompany.dao.jpa.JPAGenericDAO#findByExampleUnique(java.lang.Object)}
- * .
- */
- @Test
- public void testFindByExampleUnique() {
- User user = this.createUser();
- this.jpaUserDAO.save(user);
- User found = this.jpaUserDAO.findByExampleUnique(user);
- assertNotNull("Expected user " + user + " is not found in data source.", found);
- assertEquals(user.getName(), found.getName());
- }
- }
So our test class is ready to start, you start it either with maven or in eclipse.
Abonnieren
Posts (Atom)
