/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008-2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program 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 distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.events;
import java.util.Set;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.SessionFactoryObserver;
import org.hibernate.cfg.Configuration;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.DeleteEvent;
import org.hibernate.event.spi.DeleteEventListener;
import org.hibernate.event.spi.EventType;
import org.hibernate.integrator.spi.Integrator;
import org.hibernate.metamodel.source.MetadataImplementor;
import org.hibernate.service.BootstrapServiceRegistryBuilder;
import org.hibernate.service.spi.SessionFactoryServiceRegistry;
import org.junit.Test;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
/**
* CallbackTest implementation
*
* @author Steve Ebersole
*/
public class CallbackTest extends BaseCoreFunctionalTestCase {
private TestingObserver observer = new TestingObserver();
private TestingListener listener = new TestingListener();
@Override
public String[] getMappings() {
return NO_MAPPINGS;
}
@Override
public void configure(Configuration cfg) {
cfg.setSessionFactoryObserver( observer );
}
@Override
protected void prepareBootstrapRegistryBuilder(BootstrapServiceRegistryBuilder builder) {
super.prepareBootstrapRegistryBuilder( builder );
builder.with(
new Integrator() {
@Override
public void integrate(
Configuration configuration,
SessionFactoryImplementor sessionFactory,
SessionFactoryServiceRegistry serviceRegistry) {
integrate(serviceRegistry);
}
@Override
public void integrate( MetadataImplementor metadata,
SessionFactoryImplementor sessionFactory,
SessionFactoryServiceRegistry serviceRegistry ) {
integrate(serviceRegistry);
}
private void integrate( SessionFactoryServiceRegistry serviceRegistry ) {
serviceRegistry.getService( EventListenerRegistry.class ).setListeners(EventType.DELETE, listener);
listener.initialize();
}
@Override
public void disintegrate(
SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
listener.cleanup();
}
}
);
}
@Test
public void testCallbacks() {
assertEquals( "observer not notified of creation", 1, observer.creationCount );
assertEquals( "listener not notified of creation", 1, listener.initCount );
sessionFactory().close();
assertEquals( "observer not notified of close", 1, observer.closedCount );
assertEquals( "listener not notified of close", 1, listener.destoryCount );
}
private static class TestingObserver implements SessionFactoryObserver {
private int creationCount = 0;
private int closedCount = 0;
public void sessionFactoryCreated(SessionFactory factory) {
creationCount++;
}
public void sessionFactoryClosed(SessionFactory factory) {
closedCount++;
}
}
private static class TestingListener implements DeleteEventListener {
private int initCount = 0;
private int destoryCount = 0;
public void initialize() {
initCount++;
}
public void cleanup() {
destoryCount++;
}
public void onDelete(DeleteEvent event) throws HibernateException {
}
public void onDelete(DeleteEvent event, Set transientEntities) throws HibernateException {
}
}
}