Package org.jbpm.persistence.db

Source Code of org.jbpm.persistence.db.PersistenceServiceDbTest

/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jbpm.persistence.db;

import java.sql.Connection;
import java.util.List;

import javax.sql.DataSource;

import junit.framework.TestCase;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.jbpm.JbpmException;
import org.jbpm.mock.Invocation;
import org.jbpm.mock.Jdbc;
import org.jbpm.mock.Recorded;

public class PersistenceServiceDbTest extends TestCase {
 
  public void testDefaults() throws Exception {
    DbPersistenceServiceFactory persistenceServiceFactory = new DbPersistenceServiceFactory();
    DbPersistenceService persistenceService = (DbPersistenceService) persistenceServiceFactory.openService();
   
    assertNotNull(persistenceService);
    assertSame(persistenceServiceFactory, persistenceService.persistenceServiceFactory);
   
    assertNull(persistenceServiceFactory.configuration);
    assertNull(persistenceServiceFactory.dataSource);
    assertNull(persistenceServiceFactory.dataSourceJndiName);
    assertNull(persistenceServiceFactory.schemaExport);
    assertNull(persistenceServiceFactory.sessionFactory);
    assertNull(persistenceServiceFactory.sessionFactoryJndiName);
    assertNull(persistenceService.session);
    assertNull(persistenceService.transaction);
    assertNull(persistenceService.connection);
    assertNull(persistenceService.contextSession);
    assertNull(persistenceService.graphSession);
    assertNull(persistenceService.loggingSession);
    assertNull(persistenceService.messagingSession);
    assertNull(persistenceService.schedulerSession);
    assertNull(persistenceService.taskMgmtSession);
   
    Session session = persistenceService.getSession();

    assertSame(session, persistenceService.session);
    assertNull(persistenceService.connection);
   
    assertNotNull(persistenceServiceFactory.configuration);
    assertNotNull(persistenceServiceFactory.sessionFactory);
    assertNotNull(persistenceService.transaction);
    assertNull(persistenceService.connection);
    assertNotNull(persistenceService.session);

    assertNull(persistenceServiceFactory.dataSource);
    assertNull(persistenceServiceFactory.dataSourceJndiName);
    assertNull(persistenceServiceFactory.schemaExport);
    assertNull(persistenceServiceFactory.sessionFactoryJndiName);
    assertNull(persistenceService.contextSession);
    assertNull(persistenceService.graphSession);
    assertNull(persistenceService.loggingSession);
    assertNull(persistenceService.messagingSession);
    assertNull(persistenceService.schedulerSession);
    assertNull(persistenceService.taskMgmtSession);
   
    assertTrue(persistenceService.transaction.isActive());
    assertTrue(persistenceService.session.isOpen());
    assertFalse(persistenceService.transaction.wasCommitted());
    assertFalse(persistenceService.transaction.wasRolledBack());

    persistenceService.close();

    assertNotNull(persistenceServiceFactory.configuration);
    assertNotNull(persistenceServiceFactory.sessionFactory);

    assertTrue(persistenceService.transaction.wasCommitted());
    assertFalse(persistenceService.transaction.isActive());
    assertFalse(persistenceService.transaction.wasRolledBack());
    assertFalse(persistenceService.session.isOpen());
    assertFalse(persistenceService.session.isConnected());
  }

  public void testRollbackWithoutSessionCreation() throws Exception {
    DbPersistenceServiceFactory persistenceServiceFactory = new DbPersistenceServiceFactory();
    DbPersistenceService persistenceService = (DbPersistenceService) persistenceServiceFactory.openService();

    persistenceService.setRollbackOnly();

    persistenceService.close();

    assertNull(persistenceService.transaction);
  }

  public void testRollback() throws Exception {
    DbPersistenceServiceFactory persistenceServiceFactory = new DbPersistenceServiceFactory();
    DbPersistenceService persistenceService = (DbPersistenceService) persistenceServiceFactory.openService();
    persistenceService.setRollbackOnly();

    persistenceService.getSession();
   
    persistenceService.close();

    assertFalse(persistenceService.transaction.wasCommitted());
    assertFalse(persistenceService.transaction.isActive());
    assertTrue(persistenceService.transaction.wasRolledBack());
    assertFalse(persistenceService.session.isOpen());
  }
 
  public void testUserSuppliedConnection() throws Exception {
    DataSource dataSource = Jdbc.createRecordedDataSource();
    Connection connection = dataSource.getConnection();
   
    DbPersistenceServiceFactory persistenceServiceFactory = new DbPersistenceServiceFactory();
    DbPersistenceService persistenceService = (DbPersistenceService) persistenceServiceFactory.openService();
    persistenceService.setConnection(connection);

    Session session = persistenceService.getSession();

    assertNotNull(persistenceService.transaction);
    assertSame(session, persistenceService.session);
    Recorded recordedConnection = (Recorded) connection;
    List invocations = recordedConnection.getInvocations();
    assertNull(Invocation.getInvocation(invocations, "commit", 0));

    persistenceService.close();

    invocations = recordedConnection.getInvocations();
    assertNotNull(Invocation.getInvocation(invocations, "commit", 0));
    assertNull(Invocation.getInvocation(invocations, "close", 0));
  }
 
  public void testUserSuppliedConnectionWithRollback() throws Exception {
    DataSource dataSource = Jdbc.createRecordedDataSource();
    Connection connection = dataSource.getConnection();
   
    DbPersistenceServiceFactory persistenceServiceFactory = new DbPersistenceServiceFactory();
    DbPersistenceService persistenceService = (DbPersistenceService) persistenceServiceFactory.openService();
    persistenceService.setConnection(connection);

    Session session = persistenceService.getSession();
    persistenceService.setRollbackOnly();

    assertNotNull(persistenceService.transaction);
    assertSame(session, persistenceService.session);
    Recorded recordedConnection = (Recorded)connection;
    List invocations = recordedConnection.getInvocations();
    assertNull(Invocation.getInvocation(invocations, "commit", 0));
    assertNull(Invocation.getInvocation(invocations, "rollback", 0));

    persistenceService.close();

    invocations = recordedConnection.getInvocations();
    assertNull(Invocation.getInvocation(invocations, "commit", 0));
    assertNotNull(Invocation.getInvocation(invocations, "rollback", 0));
  }
 
  public void testUserSuppliedSession() throws Exception {
    DbPersistenceServiceFactory persistenceServiceFactory = new DbPersistenceServiceFactory();
    SessionFactory sessionFactory = persistenceServiceFactory.getSessionFactory();
    DbPersistenceService persistenceService = (DbPersistenceService) persistenceServiceFactory.openService();
   
    Session session = sessionFactory.openSession();
    persistenceService.setSession(session);
   
    persistenceService.getSession();
    assertNull(persistenceService.transaction);
    persistenceService.close();
    assertNull(persistenceService.transaction);
    assertNotNull(persistenceService.session);
    assertTrue(persistenceService.session.isOpen());
  }

  public void testUserSuppliedSessionWithRollback() throws Exception {
    DataSource dataSource = Jdbc.createRecordedDataSource();
    Connection connection = dataSource.getConnection();

    DbPersistenceServiceFactory persistenceServiceFactory = new DbPersistenceServiceFactory();
    SessionFactory sessionFactory = persistenceServiceFactory.getSessionFactory();
    DbPersistenceService persistenceService = (DbPersistenceService) persistenceServiceFactory.openService();
   
    Session session = sessionFactory.openSession(connection);
    persistenceService.setSession(session);
    persistenceService.setRollbackOnly();
   
    persistenceService.getSession();
    assertNull(persistenceService.transaction);
    try {
      persistenceService.close();
      fail("expected exception");
    } catch (JbpmException e) {
      // OK
    }
  }

  public void testTransferResponsibility() throws Exception {
    DbPersistenceServiceFactory persistenceServiceFactory = new DbPersistenceServiceFactory();
    DbPersistenceService persistenceService = (DbPersistenceService) persistenceServiceFactory.openService();
    // first make sure that hibernate creates a jdbc connection
    persistenceService.getSession();
    // then get the connection from the session
    Connection connection = persistenceService.getConnection();
    // then the responsibility has be transferred to the user to close the connection
    // that is the same behaviour as hibernate (since hibernate 3.1)
    persistenceService.close();

    assertFalse(persistenceService.session.isOpen());
    assertFalse(persistenceService.session.isConnected());
    assertFalse(connection.isClosed());
  }
}
TOP

Related Classes of org.jbpm.persistence.db.PersistenceServiceDbTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.