View Javadoc

1   /*
2    *  Copyright 2001-2009 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.Instant;
25  
26  public class TestPersistentInstantAsBigInt extends HibernateTestCase
27  {
28      private Instant[] writeReadTimes = new Instant[]
29      {
30          new Instant(0),
31  		new Instant(1000),
32  		new Instant(1000000)
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              Instant writeReadTime = writeReadTimes[i];
44  
45              ThingWithInstant thing = new ThingWithInstant();
46              thing.setId(i);
47              thing.setInstant(writeReadTime);
48  
49              session.save(thing);
50          }
51  
52          session.flush();
53          session.connection().commit();
54          session.close();
55  
56          for (int i = 0; i<writeReadTimes.length; i++)
57          {
58              Instant writeReadTime = writeReadTimes[i];
59  
60              session = factory.openSession();
61              ThingWithInstant thingReread = (ThingWithInstant)session.get(ThingWithInstant.class, new Integer(i));
62  
63              assertNotNull("get failed - thing#'" + i + "'not found", thingReread);
64              assertNotNull("get failed - returned null", thingReread.getInstant());
65  
66  			Instant reReadTime = thingReread.getInstant();
67  			if (writeReadTime.getMillis() != reReadTime.getMillis())
68  			{
69  				fail("get failed - returned different date. expected " + writeReadTime + " was " + thingReread.getInstant());
70  			}
71  		}
72  
73  		session.close();
74      }
75  
76  	protected void setupConfiguration(Configuration cfg)
77  	{
78  		cfg.addFile(new File("src/test/java/org/joda/time/contrib/hibernate/thingWithInstantAsBigInt.hbm.xml"));
79  	}
80  }