Java tutorial
/* * Copyright 2013 Stanley Shyiko * * 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.github.shyiko.rook.target.hibernate.cache; import com.github.shyiko.rook.target.hibernate4.cache.SynchronizationContext; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import java.sql.SQLException; /** * @author <a href="mailto:stanley.shyiko@gmail.com">Stanley Shyiko</a> */ public abstract class AbstractHibernateTest { protected SynchronizationContext synchronizationContext; @BeforeClass public void setUp() throws SQLException { Configuration configuration = new Configuration().configure("hibernate.cfg.xml"); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()) .buildServiceRegistry(); SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); synchronizationContext = new SynchronizationContext(configuration, sessionFactory); } @AfterClass public void tearDown() { synchronizationContext.getSessionFactory().close(); } }