All Downloads are FREE. Search and download functionalities are using the official Maven repository.

lugin-Junit.1.0.0.5.source-code.AbstractPersistentTest Maven / Gradle / Ivy

package com.evasion.plugin.junit.persistence;

import java.net.URL;
import java.util.Properties;
import javax.sql.DataSource;
import javax.annotation.Resource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.persistence.Cache;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;
import javax.transaction.UserTransaction;
import org.apache.openejb.api.LocalClient;
import org.dbunit.database.DatabaseConfig;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.DatabaseDataSet;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.filter.ExcludeTableFilter;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.dbunit.ext.hsqldb.HsqldbDataTypeFactory;
import org.dbunit.operation.DatabaseOperation;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author sebastien.glon
 */
@LocalClient
//@RunWith(Parameterized.class)
public abstract class AbstractPersistentTest {

    private enum Database {

        hsql, mysql;

    }

    private static final Logger LOGGER =
            LoggerFactory.getLogger(AbstractPersistentTest.class);

    public AbstractPersistentTest(String database) {
        this.database = Database.valueOf(database);
    }

    public AbstractPersistentTest() {
        this.database = Database.hsql;
    }
    
    @PersistenceUnit
    EntityManagerFactory emf;

    @PersistenceContext
    EntityManager em;

    private static IDatabaseConnection connection;

    @Resource
    public UserTransaction userTransaction;

    @Resource
    DataSource datasource;

    protected static Context ctx;

    private Database database;

    @BeforeClass
    public static void initEntityManager() throws Exception {
        Properties props = new Properties();
        props.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                "org.apache.openejb.client.LocalInitialContextFactory");
        props.setProperty("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true)");
        props.put("openejb.embedded.initialcontext.close", "DESTROY");
        props.put("myTransactionManager", "new://TransactionManager");
        props.put("mySecurityService", "new://SecurityService");
        props.put("myStatelessContainer", "new://Container?type=STATELESS");
        props.put("myStatefulContainer", "new://Container?type=STATEFUL");

        ctx = new InitialContext(props);
    }

    @Before
    public void setUp() throws Exception {
        ctx.bind("inject", this);
       
        // Initializes DBUnit
        String dateFile = this.getClass().getSimpleName() + "-dataset.xml";
        userTransaction.begin();
        try {
            if (connection == null) {
                connection = new DatabaseConnection(datasource.getConnection());
                if (database == Database.hsql) {
                    DatabaseConfig config = connection.getConfig();
                    config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
                            new HsqldbDataTypeFactory());
                }
                em.flush();
            }
        } finally {
            userTransaction.commit();
        }
        URL file = ClassLoader.getSystemResource(dateFile);
        if (file != null) {

            userTransaction.begin();
            try {
                IDataSet dataset = new FlatXmlDataSetBuilder().build(file);

                LOGGER.info("Dataset File: {}", dateFile);
                DatabaseOperation.CLEAN_INSERT.execute(connection, dataset);
                em.flush();
                em.clear();
            } finally {
                userTransaction.commit();
            }
        }
    }

    @After
    public void tearDown() throws Exception {
        clearDataBase();
    }

    private void clearDataBase() throws Exception {
        LOGGER.info("Clean Database except SEQUENCE table");
        userTransaction.begin();
        try {
            IDataSet dataset = new DatabaseDataSet(connection, false, new ExcludeTableFilter(new String[]{"SEQUENCE"}));
            disableReferentialIntegrity(connection);
            DatabaseOperation.DELETE_ALL.execute(connection, dataset);
            enableReferentialIntegrity(connection);
            em.flush();
            em.clear();
        } finally {
            userTransaction.commit();
        }
    }

    public EntityManager getEntityManager() {
        return em;
    }

    /**
     * Override this method if you aren't using HSQL DB.
     * 

* Execute whatever statement is necessary to either defer or disable foreign * key constraint checking on the given database connection, which is used by * DBUnit to import datasets. * * @param con A DBUnit connection wrapper, which is used afterwards for dataset operations */ protected void disableReferentialIntegrity(IDatabaseConnection con) { try { if (database.equals(Database.hsql)) { con.getConnection().prepareStatement( "set referential_integrity FALSE").execute(); // HSQL DB } else if (database.equals(Database.mysql)) { con.getConnection().prepareStatement( "set foreign_key_checks=0").execute(); // MySQL > 4.1.1 } } catch (Exception ex) { throw new RuntimeException(ex); } } /** * Override this method if you aren't using HSQL DB. *

* Execute whatever statement is necessary to enable integrity constraint checks after * dataset operations. * * @param con A DBUnit connection wrapper, before it is used by the application again */ protected void enableReferentialIntegrity(IDatabaseConnection con) { try { if (database.equals(Database.hsql)) { con.getConnection().prepareStatement( "set referential_integrity TRUE").execute(); // HSQL DB } else if (database.equals(Database.mysql)) { con.getConnection().prepareStatement( "set foreign_key_checks=1").execute(); // MySQL > 4.1.1 } } catch (Exception ex) { throw new RuntimeException(ex); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy