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  
21  import org.hibernate.Session;
22  import org.hibernate.SessionFactory;
23  import org.hibernate.cfg.Configuration;
24  import org.joda.time.DateTime;
25  import org.joda.time.DateTimeZone;
26  
27  public class TestPersistentDateTime extends HibernateTestCase
28  {
29      private DateTime[] writeReadTimes = new DateTime[]
30      {
31          new DateTime(2004, 2, 25, 17, 3, 45, 760),
32          new DateTime(1980, 3, 11,  2, 3, 45,   0, DateTimeZone.forOffsetHours(2))
33      };
34  
35      public void testSimpleStore() throws SQLException
36      {
37          SessionFactory factory = getSessionFactory();
38  
39          Session session = factory.openSession();
40  
41          for (int i = 0; i<writeReadTimes.length; i++)
42          {
43              DateTime writeReadTime = writeReadTimes[i];
44  
45              Event event = new Event();
46              event.setId(i);
47              event.setDateTime(writeReadTime);
48  
49              session.save(event);
50          }
51  
52          session.flush();
53          session.connection().commit();
54          session.close();
55  
56          for (int i = 0; i<writeReadTimes.length; i++)
57          {
58              DateTime writeReadTime = writeReadTimes[i];
59  
60              session = factory.openSession();
61              Event eventReread = (Event) session.get(Event.class, new Integer(i));
62  
63              assertNotNull("get failed - event#'" + i + "'not found", eventReread);
64              assertNotNull("get failed - returned null", eventReread.getDateTime());
65  
66              // we loose the timezone, so we have to normalize both to offset=0
67              assertEquals("get failed - returned different time",
68                  writeReadTime.toDateTime(DateTimeZone.forOffsetHours(0)),
69                  eventReread.getDateTime().toDateTime(DateTimeZone.forOffsetHours(0)));
70  
71              session.close();
72          }
73      }
74  
75      public void testStoreWithTimezone() throws SQLException
76      {
77          SessionFactory factory = getSessionFactory();
78  
79          Session session = factory.openSession();
80  
81          for (int i = 0; i<writeReadTimes.length; i++)
82          {
83              DateTime writeReadTime = writeReadTimes[i];
84  
85              EventTZ event = new EventTZ();
86              event.setId(i);
87              event.setDateTime(writeReadTime);
88  
89              session.save(event);
90          }
91  
92          session.flush();
93          session.connection().commit();
94          session.close();
95  
96          for (int i = 0; i<writeReadTimes.length; i++)
97          {
98              DateTime writeReadTime = writeReadTimes[i];
99  
100             session = factory.openSession();
101             EventTZ eventReread = (EventTZ) session.get(EventTZ.class, new Integer(i));
102 
103             assertNotNull("get failed - event#'" + i + "'not found", eventReread);
104             assertNotNull("get failed - returned null", eventReread.getDateTime());
105 
106             assertEquals("get failed - returned different time",
107                 writeReadTime, eventReread.getDateTime());
108         }
109 		
110 		session.close();
111     }
112 
113 	protected void setupConfiguration(Configuration cfg)
114 	{
115 		cfg.addFile(new File("src/test/java/org/joda/time/contrib/hibernate/event.hbm.xml"));
116 		cfg.addFile(new File("src/test/java/org/joda/time/contrib/hibernate/eventTZ.hbm.xml"));
117 	}
118 }