Package com.mysema.testutil

Source Code of com.mysema.testutil.HibernateTestRunner

/*
* Copyright 2011, Mysema Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mysema.testutil;

import com.mysema.query.Mode;
import com.mysema.query.jpa.domain.Domain;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.rules.MethodRule;
import org.junit.runner.Description;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;

import java.io.InputStream;
import java.lang.reflect.Method;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;

/**
* @author tiwe
*
*/
public class HibernateTestRunner extends BlockJUnit4ClassRunner {

    private SessionFactory sessionFactory;

    private Session session;

    private Method setter;

    private boolean isDerby = false;

    public HibernateTestRunner(Class<?> klass) throws InitializationError {
        super(klass);
    }

    @Override
    protected List<MethodRule> rules(Object test) {
        List<MethodRule> rules = super.rules(test);
        rules.add(new MethodRule() {
            @Override
            public Statement apply(final Statement base, FrameworkMethod method, final Object target) {
                return new Statement() {
                    @Override
                    public void evaluate() throws Throwable {
                        if (setter == null) {
                            setter = target.getClass().getMethod("setSession", Session.class);
                        }
                        setter.invoke(target, session);
                        base.evaluate();
                    }
                };
            }

        });
        return rules;
    }

    @Override
    public void run(final RunNotifier notifier) {
        try {
            start();
            super.run(notifier);
        } catch (Exception e) {
            e.printStackTrace();
            Failure failure = new Failure(Description.createSuiteDescription(getTestClass().getJavaClass()), e);
            notifier.fireTestFailure(failure);
        } finally {
            shutdown();
        }
    }

    private void start() throws Exception {
        Configuration cfg = new Configuration();
        for (Class<?> cl : Domain.classes) {
            cfg.addAnnotatedClass(cl);
        }
        String mode = Mode.mode.get() + ".properties";
        isDerby = mode.contains("derby");
        if (isDerby) {
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
        }
        Properties props = new Properties();
        InputStream is = HibernateTestRunner.class.getResourceAsStream(mode);
        if (is == null) {
            throw new IllegalArgumentException("No configuration available at classpath:" + mode);
        }
        props.load(is);
        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
            .applySettings(props)
            .build();
        cfg.setProperties(props);
        sessionFactory = cfg.buildSessionFactory(serviceRegistry);
        session = sessionFactory.openSession();
        session.beginTransaction();
    }

    private void shutdown() {
        if (session != null) {
            try {
                if (session.getTransaction().isActive()) {
                    session.getTransaction().rollback();
                }
            } finally {
                session.close();
                session = null;
            }
        }

        if (sessionFactory != null) {
            sessionFactory.getCache().evictEntityRegions();
            sessionFactory.close();
            sessionFactory = null;
        }

        // clean shutdown of derby
        if (isDerby) {
            try {
                DriverManager.getConnection("jdbc:derby:;shutdown=true");
            } catch (SQLException e) {
                if (!e.getMessage().equals("Derby system shutdown.")) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

}
TOP

Related Classes of com.mysema.testutil.HibernateTestRunner

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.