View Javadoc

1   /*
2    *  Copyright 2001-2008 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  
21  import org.hibernate.Session;
22  import org.hibernate.SessionFactory;
23  import org.hibernate.Transaction;
24  import org.hibernate.cfg.Configuration;
25  import org.joda.time.Interval;
26  
27  public class TestPersistentIntervalNull extends HibernateTestCase
28  {
29      private SessionFactory factory;
30      private Session session;
31      private Transaction transaction;
32      
33      protected void setUp() throws SQLException
34      {
35          factory = getSessionFactory();
36          store();
37      }
38      
39      private void store() throws SQLException
40      {
41          openAndBegin();
42          
43          Plan plan = new Plan(1);
44          plan.setPeriod(null);
45          
46          session.save(plan);
47          session.flush();
48          
49          commitAndClose();
50      }
51  
52      private void openAndBegin()
53      {
54          session = factory.openSession();
55          transaction = session.beginTransaction();
56      }
57      
58      private void commitAndClose()
59      {
60          transaction.commit();
61          session.close();
62      }
63      
64      public void testQueryById() throws SQLException
65      {
66          openAndBegin();
67          Interval persistedPeriod = queryPlan().getPeriod();
68          commitAndClose();
69          assertPlanPeriod(persistedPeriod);
70      }
71  
72      private void assertPlanPeriod(Interval period)
73      {
74          assertNull(period);
75      }
76  
77      private Plan queryPlan()
78      {
79          return (Plan) session.get(Plan.class, new Integer(1));
80      }
81      
82      protected void setupConfiguration(Configuration cfg)
83      {
84          cfg.addFile(new File("src/test/java/org/joda/time/contrib/hibernate/plan.hbm.xml"));
85      }
86      
87      protected void tearDown() throws Exception
88      {
89          remove();
90          super.tearDown();
91      }
92  
93      private void remove()
94      {
95          openAndBegin();
96          session.delete(queryPlan());
97          commitAndClose();
98      }
99  }