View Javadoc

1   package org.joda.time.contrib.hibernate;
2   
3   import junit.framework.Assert;
4   import org.hibernate.Session;
5   import org.hibernate.cfg.Configuration;
6   import org.joda.time.DateTime;
7   import org.joda.time.Duration;
8   import org.joda.time.Period;
9   import org.joda.time.contrib.hibernate.testmodel.SomethingThatLasts;
10  
11  import java.io.IOException;
12  import java.io.File;
13  import java.sql.SQLException;
14  
15  /**
16   * @author gjoseph
17   * @author $Author: $ (last edit)
18   * @version $Revision: $
19   */
20  public class TestPersistentDuration extends HibernateTestCase {
21      protected void setupConfiguration(Configuration cfg) {
22          cfg.addFile(new File("src/test/java/org/joda/time/contrib/hibernate/testmodel/SomethingThatLasts.hbm.xml"));
23      }
24  
25      private Duration[] durations = new Duration[]{
26              Duration.ZERO, new Duration(30), Period.seconds(30).toDurationTo(new DateTime()), Period.months(3).toDurationFrom(new DateTime())
27      };
28  
29      public void testSimpleStore() throws SQLException, IOException {
30          Session session = getSessionFactory().openSession();
31  
32          for (int i = 0; i < durations.length; i++) {
33              SomethingThatLasts thing = new SomethingThatLasts();
34              thing.setId(i);
35              thing.setName("test_" + i);
36              thing.setTheDuration(durations[i]);
37              session.save(thing);
38          }
39  
40          session.flush();
41          session.connection().commit();
42          session.close();
43  
44          for (int i = 0; i < durations.length; i++) {
45              session = getSessionFactory().openSession();
46              SomethingThatLasts lastingThing = (SomethingThatLasts) session.get(SomethingThatLasts.class, new Long(i));
47  
48              Assert.assertNotNull(lastingThing);
49              Assert.assertEquals(i, lastingThing.getId());
50              Assert.assertEquals("test_" + i, lastingThing.getName());
51              Assert.assertEquals(durations[i], lastingThing.getTheDuration());
52  
53              session.close();
54          }
55  
56          // printSqlQueryResults("SELECT * FROM lasting");
57      }
58  
59  }