001    /*
002     *  Copyright 2001-2008 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 java.io.File;
019    import java.sql.SQLException;
020    
021    import org.hibernate.Session;
022    import org.hibernate.SessionFactory;
023    import org.hibernate.Transaction;
024    import org.hibernate.cfg.Configuration;
025    import org.joda.time.Interval;
026    
027    public class TestPersistentIntervalNull extends HibernateTestCase
028    {
029        private SessionFactory factory;
030        private Session session;
031        private Transaction transaction;
032        
033        protected void setUp() throws SQLException
034        {
035            factory = getSessionFactory();
036            store();
037        }
038        
039        private void store() throws SQLException
040        {
041            openAndBegin();
042            
043            Plan plan = new Plan(1);
044            plan.setPeriod(null);
045            
046            session.save(plan);
047            session.flush();
048            
049            commitAndClose();
050        }
051    
052        private void openAndBegin()
053        {
054            session = factory.openSession();
055            transaction = session.beginTransaction();
056        }
057        
058        private void commitAndClose()
059        {
060            transaction.commit();
061            session.close();
062        }
063        
064        public void testQueryById() throws SQLException
065        {
066            openAndBegin();
067            Interval persistedPeriod = queryPlan().getPeriod();
068            commitAndClose();
069            assertPlanPeriod(persistedPeriod);
070        }
071    
072        private void assertPlanPeriod(Interval period)
073        {
074            assertNull(period);
075        }
076    
077        private Plan queryPlan()
078        {
079            return (Plan) session.get(Plan.class, new Integer(1));
080        }
081        
082        protected void setupConfiguration(Configuration cfg)
083        {
084            cfg.addFile(new File("src/test/java/org/joda/time/contrib/hibernate/plan.hbm.xml"));
085        }
086        
087        protected void tearDown() throws Exception
088        {
089            remove();
090            super.tearDown();
091        }
092    
093        private void remove()
094        {
095            openAndBegin();
096            session.delete(queryPlan());
097            commitAndClose();
098        }
099    }