001    /*
002     *  Copyright 2001-2007 Stephen Colebourne
003     *
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package org.joda.time.contrib.hibernate;
017    
018    import junit.framework.TestCase;
019    
020    import org.hibernate.SessionFactory;
021    import org.hibernate.cfg.Configuration;
022    import org.hibernate.dialect.HSQLDialect;
023    import org.hibernate.tool.hbm2ddl.SchemaUpdate;
024    
025    import java.sql.Connection;
026    import java.sql.Statement;
027    
028    public abstract class HibernateTestCase extends TestCase
029    {
030            private SessionFactory factory;
031        private Configuration cfg;
032    
033        protected SessionFactory getSessionFactory()
034            {
035                    if (this.factory == null)
036                    {
037                        cfg = new Configuration();
038    
039                        setupConfiguration(cfg);
040    
041                            cfg.setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver");
042                            cfg.setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:hbmtest" + getClass().getName());
043                            cfg.setProperty("hibernate.dialect", HSQLDialect.class.getName());
044    
045                            cfg.setProperty("hibernate.show_sql", "true");
046                            SessionFactory factory = cfg.buildSessionFactory();
047    
048                            SchemaUpdate update = new SchemaUpdate(cfg);
049                            update.execute(true, true);
050    
051                            this.factory = factory;
052                    }
053                    return factory;
054            }
055    
056            protected void tearDown() throws Exception
057            {
058                final String[] dropSQLs = cfg.generateDropSchemaScript(new HSQLDialect());
059                final Connection connection = getSessionFactory().openSession().connection();
060                try {
061                    Statement stmt = connection.createStatement();
062                    for (int i = 0; i < dropSQLs.length; i++) {
063                        //System.out.println("dropSQLs[i] = " + dropSQLs[i]);
064                        stmt.executeUpdate(dropSQLs[i]);
065                    }
066                } finally {
067                    connection.close();
068                }
069    
070                if (this.factory != null)
071                    {
072                            this.factory.close();
073                            this.factory = null;
074                    }
075            }
076    
077            protected abstract void setupConfiguration(Configuration cfg);
078    }