View Javadoc

1   /*
2    *  Copyright 2001-2007 Stephen Colebourne
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.joda.time.contrib.hibernate;
17  
18  import java.io.File;
19  import java.sql.SQLException;
20  import java.util.List;
21  
22  import org.hibernate.Query;
23  import org.hibernate.Session;
24  import org.hibernate.SessionFactory;
25  import org.hibernate.Transaction;
26  import org.hibernate.cfg.Configuration;
27  import org.joda.time.DateTime;
28  import org.joda.time.Interval;
29  
30  public class TestPersistentInterval extends HibernateTestCase
31  {
32      private SessionFactory factory;
33      private Session session;
34      private DateTime beginDateTime;
35      private DateTime endDateTime;
36      private Transaction transaction;
37      private String intervalQuery;
38      
39      protected void setUp() throws SQLException
40      {
41          factory = getSessionFactory();
42          store();
43          intervalQuery = "from Plan where :aDate between period.start and period.end";
44      }
45      
46      private void store() throws SQLException
47      {
48          openAndBegin();
49          
50          Plan plan = new Plan(1);
51          beginDateTime = new DateTime(1980, 3, 11, 2, 3, 45, 0);
52          endDateTime = new DateTime(2004, 2, 25, 17, 3, 45, 760);
53          Interval period = new Interval(beginDateTime, endDateTime);
54          
55          plan.setPeriod(period);
56          
57          session.save(plan);
58          session.flush();
59          
60          commitAndClose();
61      }
62  
63      private void openAndBegin()
64      {
65          session = factory.openSession();
66          transaction = session.beginTransaction();
67      }
68      
69      private void commitAndClose()
70      {
71          transaction.commit();
72          session.close();
73      }
74      
75      public void testQueryById() throws SQLException
76      {
77          openAndBegin();
78          Interval persistedPeriod = queryPlan().getPeriod();
79          commitAndClose();
80          assertPlanPeriod(persistedPeriod);
81      }
82  
83      private void assertPlanPeriod(Interval period)
84      {
85          assertEquals(beginDateTime, period.getStart());
86          assertEquals(endDateTime, period.getEnd());
87      }
88  
89      private Plan queryPlan()
90      {
91          return (Plan) session.get(Plan.class, new Integer(1));
92      }
93      
94      public void testQueryInsideInterval() throws SQLException
95      {
96          openAndBegin();
97          DateTime includedDateTime = new DateTime(2004, 1, 10, 0, 0, 0, 0);
98          Query query = session.createQuery(intervalQuery)
99                               .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 }