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.YearMonthDay;
25  
26  public class TestPersistentYearMonthDay extends HibernateTestCase
27  {
28      private YearMonthDay[] writeReadTimes = new YearMonthDay[]
29      {
30          new YearMonthDay(2004, 2, 25),
31          new YearMonthDay(1980, 3, 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              YearMonthDay writeReadTime = writeReadTimes[i];
43  
44              Schedule event = new Schedule();
45              event.setId(i);
46              event.setStartDate(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              YearMonthDay writeReadTime = writeReadTimes[i];
58  
59              session = factory.openSession();
60              Schedule eventReread = (Schedule) session.get(Schedule.class, new Integer(i));
61  
62              assertNotNull("get failed - event#'" + i + "'not found", eventReread);
63              assertNotNull("get failed - returned null", eventReread.getStartDate());
64  
65              assertEquals("get failed - returned different date", writeReadTime, eventReread.getStartDate());
66          }
67  		
68  		session.close();
69      }
70  
71  	protected void setupConfiguration(Configuration cfg)
72  	{
73  		cfg.addFile(new File("src/test/java/org/joda/time/contrib/hibernate/schedule.hbm.xml"));
74  	}
75  }