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.cfg.Configuration;
24  import org.joda.time.LocalDateTime;
25  
26  public class TestPersistentLocalDateTime extends HibernateTestCase
27  {
28      private LocalDateTime[] writeReadTimes = new LocalDateTime[]
29      {
30          new LocalDateTime(2004, 2, 25, 12, 11, 10),
31          new LocalDateTime(1980, 3, 11, 13, 12, 11)
32      };
33  
34      public void testSimpleStore() throws SQLException
35  	{
36          SessionFactory factory = getSessionFactory();
37  
38          Session session = factory.openSession();
39  
40          for (int i = 0; i<writeReadTimes.length; i++)
41          {
42              LocalDateTime writeReadTime = writeReadTimes[i];
43  
44              Event event = new Event();
45              event.setId(i);
46              event.setLocalDateTime(writeReadTime);
47  
48              session.save(event);
49          }
50  
51          session.flush();
52          session.connection().commit();
53          session.close();
54  
55          for (int i = 0; i<writeReadTimes.length; i++)
56          {
57              LocalDateTime writeReadTime = writeReadTimes[i];
58  
59              session = factory.openSession();
60              Event eventReread = (Event) session.get(Event.class, new Integer(i));
61  
62              assertNotNull("get failed - event#'" + i + "'not found", eventReread);
63              assertNotNull("get failed - returned null", eventReread.getLocalDateTime());
64  
65              // we loose the timezone, so we have to normalize both to offset=0
66              assertEquals("get failed - returned different time",
67                  writeReadTime,
68                  eventReread.getLocalDateTime());
69  
70              session.close();
71          }
72      }
73  
74  	protected void setupConfiguration(Configuration cfg)
75  	{
76  		cfg.addFile(new File("src/test/java/org/joda/time/contrib/hibernate/event.hbm.xml"));
77  		cfg.addFile(new File("src/test/java/org/joda/time/contrib/hibernate/eventTZ.hbm.xml"));
78  	}
79  }