001    /*
002     *  Copyright 2001-2009 Stephen Colebourne
003     *
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package org.joda.time.contrib.hibernate;
017    
018    import java.io.File;
019    import java.sql.SQLException;
020    
021    import org.hibernate.Session;
022    import org.hibernate.SessionFactory;
023    import org.hibernate.cfg.Configuration;
024    import org.joda.time.Instant;
025    
026    public class TestPersistentInstant extends HibernateTestCase
027    {
028        private Instant[] writeReadTimes = new Instant[]
029        {
030            new Instant(0),
031                    new Instant(1000),
032                    new Instant(1000000)
033        };
034    
035        public void testSimpleStore() throws SQLException
036        {
037            SessionFactory factory = getSessionFactory();
038    
039            Session session = factory.openSession();
040    
041            for (int i = 0; i<writeReadTimes.length; i++)
042            {
043                Instant writeReadTime = writeReadTimes[i];
044    
045                ThingWithInstant thing = new ThingWithInstant();
046                thing.setId(i);
047                thing.setInstant(writeReadTime);
048    
049                session.save(thing);
050            }
051    
052            session.flush();
053            session.connection().commit();
054            session.close();
055    
056            for (int i = 0; i<writeReadTimes.length; i++)
057            {
058                Instant writeReadTime = writeReadTimes[i];
059    
060                session = factory.openSession();
061                ThingWithInstant thingReread = (ThingWithInstant)session.get(ThingWithInstant.class, new Integer(i));
062    
063                assertNotNull("get failed - thing#'" + i + "'not found", thingReread);
064                assertNotNull("get failed - returned null", thingReread.getInstant());
065    
066                            Instant reReadTime = thingReread.getInstant();
067                            if (writeReadTime.getMillis() != reReadTime.getMillis())
068                            {
069                                    fail("get failed - returned different date. expected " + writeReadTime + " was " + thingReread.getInstant());
070                            }
071                    }
072    
073                    session.close();
074        }
075    
076            protected void setupConfiguration(Configuration cfg)
077            {
078                    cfg.addFile(new File("src/test/java/org/joda/time/contrib/hibernate/thingWithInstant.hbm.xml"));
079            }
080    }