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.LocalTime;
25  
26  public class TestPersistentLocalTime extends HibernateTestCase
27  {
28      private LocalTime[] writeReadTimes = new LocalTime[]
29      {
30          new LocalTime(14, 2, 25),
31          new LocalTime(23, 59, 59, 999),
32  		new LocalTime(0, 0, 0)
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              LocalTime writeReadTime = writeReadTimes[i];
44  
45              Event event = new Event();
46              event.setId(i);
47              event.setLocalTime(writeReadTime);
48  			event.setLocalTime2(writeReadTime);
49  			event.setLocalTime3(writeReadTime);
50  
51              session.save(event);
52          }
53  
54          session.flush();
55          session.connection().commit();
56          session.close();
57  
58          for (int i = 0; i<writeReadTimes.length; i++)
59          {
60              LocalTime writeReadTime = writeReadTimes[i];
61  
62              session = factory.openSession();
63              Event eventReread = (Event) session.get(Event.class, new Integer(i));
64  
65              assertNotNull("get failed - event#'" + i + "'not found", eventReread);
66              assertNotNull("get failed - returned null", eventReread.getLocalTime());
67  			assertNotNull("get failed - returned null", eventReread.getLocalTime2());
68  			assertNotNull("get failed - returned null", eventReread.getLocalTime3());
69  
70              // we might loose the millis, depends on database
71              assertEquals("get failed - returned different time (TIME)",
72                  writeReadTime.getMillisOfDay()/1000,
73                  eventReread.getLocalTime().getMillisOfDay()/1000);
74  
75  			assertEquals("get failed - returned different time (INT)",
76  				writeReadTime.getMillisOfDay(),
77  				eventReread.getLocalTime2().getMillisOfDay());
78  
79  			assertEquals("get failed - returned different time (STRING)",
80  				writeReadTime.getMillisOfDay(),
81  				eventReread.getLocalTime3().getMillisOfDay());
82  
83  			session.close();
84          }
85      }
86  
87  	protected void setupConfiguration(Configuration cfg)
88  	{
89  		cfg.addFile(new File("src/test/java/org/joda/time/contrib/hibernate/event.hbm.xml"));
90  		cfg.addFile(new File("src/test/java/org/joda/time/contrib/hibernate/eventTZ.hbm.xml"));
91  	}
92  }