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 java.io.File;
019    import java.sql.SQLException;
020    import java.util.List;
021    
022    import org.hibernate.Query;
023    import org.hibernate.Session;
024    import org.hibernate.SessionFactory;
025    import org.hibernate.Transaction;
026    import org.hibernate.cfg.Configuration;
027    import org.joda.time.DateTime;
028    import org.joda.time.Interval;
029    
030    public class TestPersistentInterval extends HibernateTestCase
031    {
032        private SessionFactory factory;
033        private Session session;
034        private DateTime beginDateTime;
035        private DateTime endDateTime;
036        private Transaction transaction;
037        private String intervalQuery;
038        
039        protected void setUp() throws SQLException
040        {
041            factory = getSessionFactory();
042            store();
043            intervalQuery = "from Plan where :aDate between period.start and period.end";
044        }
045        
046        private void store() throws SQLException
047        {
048            openAndBegin();
049            
050            Plan plan = new Plan(1);
051            beginDateTime = new DateTime(1980, 3, 11, 2, 3, 45, 0);
052            endDateTime = new DateTime(2004, 2, 25, 17, 3, 45, 760);
053            Interval period = new Interval(beginDateTime, endDateTime);
054            
055            plan.setPeriod(period);
056            
057            session.save(plan);
058            session.flush();
059            
060            commitAndClose();
061        }
062    
063        private void openAndBegin()
064        {
065            session = factory.openSession();
066            transaction = session.beginTransaction();
067        }
068        
069        private void commitAndClose()
070        {
071            transaction.commit();
072            session.close();
073        }
074        
075        public void testQueryById() throws SQLException
076        {
077            openAndBegin();
078            Interval persistedPeriod = queryPlan().getPeriod();
079            commitAndClose();
080            assertPlanPeriod(persistedPeriod);
081        }
082    
083        private void assertPlanPeriod(Interval period)
084        {
085            assertEquals(beginDateTime, period.getStart());
086            assertEquals(endDateTime, period.getEnd());
087        }
088    
089        private Plan queryPlan()
090        {
091            return (Plan) session.get(Plan.class, new Integer(1));
092        }
093        
094        public void testQueryInsideInterval() throws SQLException
095        {
096            openAndBegin();
097            DateTime includedDateTime = new DateTime(2004, 1, 10, 0, 0, 0, 0);
098            Query query = session.createQuery(intervalQuery)
099                                 .setParameter("aDate", includedDateTime.toDate());
100            List queriedPlans = query.list();
101            commitAndClose();
102            assertEquals(1, queriedPlans.size());
103            assertPlanPeriod(((Plan) queriedPlans.get(0)).getPeriod());
104        }
105        
106        public void testQueryOutsideInterval()
107        {
108            openAndBegin();
109            DateTime excludedDateTime = new DateTime(2007, 1, 10, 0, 0, 0, 0);
110            Query query = session.createQuery(intervalQuery)
111                                 .setParameter("aDate", excludedDateTime.toDate());
112            List queriedPlans = query.list();
113            commitAndClose();
114            assertEquals(0, queriedPlans.size());
115        }
116    
117        protected void setupConfiguration(Configuration cfg)
118        {
119            cfg.addFile(new File("src/test/java/org/joda/time/contrib/hibernate/plan.hbm.xml"));
120        }
121        
122        protected void tearDown() throws Exception
123        {
124            remove();
125            super.tearDown();
126        }
127    
128        private void remove()
129        {
130            openAndBegin();
131            session.delete(queryPlan());
132            commitAndClose();
133        }
134    }