package com.github.cstroe.spendhawk.util;

import org.dbunit.DefaultDatabaseTester;
import org.dbunit.DefaultOperationListener;
import org.dbunit.IDatabaseTester;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.After;
import org.junit.Before;

import java.io.InputStream;
import java.sql.SQLException;

/**
 * A base class for all integration tests.  Every test run, it will reset the
 * database with the seed data.
 */
public class BaseIT {

    protected Session currentSession;

    @Before
    public void setUp() {
        currentSession = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction transaction = currentSession.beginTransaction();
        currentSession.doWork(connection -> {
            try {
                // http://stackoverflow.com/questions/3526556/session-connection-deprecated-on-hibernate
                IDatabaseConnection dbConnection = new DatabaseConnection(connection);
                IDatabaseTester dbTester = new DefaultDatabaseTester(dbConnection);

                // http://stackoverflow.com/questions/2653322/getresourceasstream-not-loading-resource-in-webapp
                InputStream seed = Thread.currentThread().getContextClassLoader().getResourceAsStream("db/seed.xml");
                IDataSet seedDataset = new FlatXmlDataSetBuilder().build(seed);

                dbTester.setDataSet(seedDataset);
                // don't close the connection after we setup
                dbTester.setOperationListener(new DefaultOperationListener(){
                    @Override
                    public void operationSetUpFinished(IDatabaseConnection connection) {}

                    @Override
                    public void operationTearDownFinished(IDatabaseConnection connection) {}
                });
                dbTester.onSetup();
            } catch (Exception ex) {
                throw new SQLException(ex);
            }
        });
        transaction.commit();
    }

    @After
    public void tearDown() {
        if(currentSession.isOpen() && currentSession.getTransaction().isActive()) {
            currentSession.getTransaction().rollback();
        }

        currentSession = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction transaction = currentSession.beginTransaction();
        currentSession.doWork(connection -> {
            try {
                // http://stackoverflow.com/questions/3526556/session-connection-deprecated-on-hibernate
                IDatabaseConnection dbConnection = new DatabaseConnection(connection);
                IDatabaseTester dbTester = new DefaultDatabaseTester(dbConnection);

                // http://stackoverflow.com/questions/2653322/getresourceasstream-not-loading-resource-in-webapp
                InputStream seed = Thread.currentThread().getContextClassLoader().getResourceAsStream("db/seed.xml");
                IDataSet seedDataset = new FlatXmlDataSetBuilder().build(seed);

                dbTester.setDataSet(seedDataset);
                // don't close the connection after we tear down
                dbTester.setOperationListener(new DefaultOperationListener(){
                    @Override
                    public void operationSetUpFinished(IDatabaseConnection connection) {}

                    @Override
                    public void operationTearDownFinished(IDatabaseConnection connection) {}
                });
                dbTester.onTearDown();
            } catch (Exception ex) {
                throw new SQLException(ex);
            }
        });
        transaction.commit();
    }

    public void startTransaction() {
        currentSession = HibernateUtil.getSessionFactory().getCurrentSession();
        currentSession.beginTransaction();
    }

    public void commitTransaction() {
        currentSession.getTransaction().commit();
    }
}

@@ -454,16 +454,233 @@
 efore;%0A%0A
+import javax.enterprise.context.spi.CreationalContext;%0Aimport javax.enterprise.inject.spi.Bean;%0Aimport javax.enterprise.inject.spi.BeanManager;%0Aimport javax.naming.InitialContext;%0Aimport javax.naming.NamingException;%0A
 import j
@@ -4276,11 +4276,816 @@
 );%0A    %7D
+%0A%0A    public BeanManager getBeanManager()%0A    %7B%0A        try %7B%0A            InitialContext initialContext = new InitialContext();%0A            return (BeanManager) initialContext.lookup(%22java:comp/BeanManager%22);%0A        %7D catch (NamingException e) %7B%0A            return null;%0A        %7D%0A    %7D%0A%0A    // CDI-based lookup of the bean.%0A    // See: http://dominikdorn.com/2010/04/cdi-weld-manual-bean-lookup/%0A    @SuppressWarnings(%7B%22unused%22, %22unchecked%22%7D)%0A    public %3CT%3E T manualInject(Class%3CT%3E beanClass) %7B%0A        BeanManager bm = getBeanManager();%0A        Bean%3CT%3E bean = (Bean%3CT%3E) bm.getBeans(beanClass).iterator().next();%0A        CreationalContext%3CT%3E ctx = bm.createCreationalContext(bean);%0A        return (T) bm.getReference(bean, beanClass, ctx); // this could be inlined, but intentionally left this way%0A    %7D
 %0A%7D%0A

Add manual injection method to BaseIT. Not used because @Inject works, but leaving it in for reference.