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.Period;
7   import org.joda.time.contrib.hibernate.testmodel.SomethingThatHappens;
8   
9   import java.io.IOException;
10  import java.io.File;
11  import java.sql.SQLException;
12  
13  /**
14   * @author gjoseph
15   * @author $Author: $ (last edit)
16   * @version $Revision: $
17   */
18  public class TestPersistentPeriod extends HibernateTestCase {
19      protected void setupConfiguration(Configuration cfg) {
20          cfg.addFile(new File("src/test/java/org/joda/time/contrib/hibernate/testmodel/SomethingThatHappens.hbm.xml"));
21      }
22  
23      private Period[] periods = new Period[]{
24              Period.days(2), Period.seconds(30), Period.months(3),
25              new Period(30), new Period(4, 35, 40, 141),
26              new Period(28, 10, 2, 2, 4, 35, 40, 141), new Period(28, 10, 0, 16, 4, 35, 40, 141),
27              // new Period(new DateTime()),
28              // new Period(new YearMonthDay()),
29              // new Period(new YearMonthDay(2005, 12, 25)),
30              // new Period(new YearMonthDay(2005, 0, 25)), new Period(new YearMonthDay(2005, 13, 25)),
31              // new Period(new DateTime(CopticChronology.getInstance())            )
32      };
33  
34      public void testSimpleStore() throws SQLException, IOException {
35          Session session = getSessionFactory().openSession();
36  
37          for (int i = 0; i < periods.length; i++) {
38              SomethingThatHappens thing = new SomethingThatHappens();
39              thing.setId(i);
40              thing.setName("test_" + i);
41              thing.setThePeriod(periods[i]);
42              session.save(thing);
43          }
44  
45          session.flush();
46          session.connection().commit();
47          session.close();
48  
49          for (int i = 0; i < periods.length; i++) {
50              session = getSessionFactory().openSession();
51              SomethingThatHappens happeningThing = (SomethingThatHappens) session.get(SomethingThatHappens.class, new Long(i));
52  
53              Assert.assertNotNull(happeningThing);
54              Assert.assertEquals(i, happeningThing.getId());
55              Assert.assertEquals("test_" + i, happeningThing.getName());
56              Assert.assertEquals(periods[i], happeningThing.getThePeriod());
57  
58              session.close();
59          }
60  
61          // printSqlQueryResults("SELECT * FROM happening");
62      }
63  
64  }