001    package org.joda.time.contrib.hibernate;
002    
003    import junit.framework.Assert;
004    import org.hibernate.Session;
005    import org.hibernate.cfg.Configuration;
006    import org.joda.time.Period;
007    import org.joda.time.contrib.hibernate.testmodel.SomethingThatHappens;
008    
009    import java.io.IOException;
010    import java.io.File;
011    import java.sql.SQLException;
012    
013    /**
014     * @author gjoseph
015     * @author $Author: $ (last edit)
016     * @version $Revision: $
017     */
018    public class TestPersistentPeriod extends HibernateTestCase {
019        protected void setupConfiguration(Configuration cfg) {
020            cfg.addFile(new File("src/test/java/org/joda/time/contrib/hibernate/testmodel/SomethingThatHappens.hbm.xml"));
021        }
022    
023        private Period[] periods = new Period[]{
024                Period.days(2), Period.seconds(30), Period.months(3),
025                new Period(30), new Period(4, 35, 40, 141),
026                new Period(28, 10, 2, 2, 4, 35, 40, 141), new Period(28, 10, 0, 16, 4, 35, 40, 141),
027                // new Period(new DateTime()),
028                // new Period(new YearMonthDay()),
029                // new Period(new YearMonthDay(2005, 12, 25)),
030                // new Period(new YearMonthDay(2005, 0, 25)), new Period(new YearMonthDay(2005, 13, 25)),
031                // new Period(new DateTime(CopticChronology.getInstance())            )
032        };
033    
034        public void testSimpleStore() throws SQLException, IOException {
035            Session session = getSessionFactory().openSession();
036    
037            for (int i = 0; i < periods.length; i++) {
038                SomethingThatHappens thing = new SomethingThatHappens();
039                thing.setId(i);
040                thing.setName("test_" + i);
041                thing.setThePeriod(periods[i]);
042                session.save(thing);
043            }
044    
045            session.flush();
046            session.connection().commit();
047            session.close();
048    
049            for (int i = 0; i < periods.length; i++) {
050                session = getSessionFactory().openSession();
051                SomethingThatHappens happeningThing = (SomethingThatHappens) session.get(SomethingThatHappens.class, new Long(i));
052    
053                Assert.assertNotNull(happeningThing);
054                Assert.assertEquals(i, happeningThing.getId());
055                Assert.assertEquals("test_" + i, happeningThing.getName());
056                Assert.assertEquals(periods[i], happeningThing.getThePeriod());
057    
058                session.close();
059            }
060    
061            // printSqlQueryResults("SELECT * FROM happening");
062        }
063    
064    }