001    /*
002     *  Copyright 2001-2007 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.TimeOfDay;
025    
026    public class TestPersistentTimeOfDay extends HibernateTestCase
027    {
028        private TimeOfDay[] writeReadTimes = new TimeOfDay[]
029        {
030            new TimeOfDay(12, 10, 31),
031            new TimeOfDay(23,  7, 43, 120)
032        };
033    
034        public void testSimpleStore() throws SQLException
035        {
036            SessionFactory factory = getSessionFactory();
037    
038            Session session = factory.openSession();
039    
040            for (int i = 0; i<writeReadTimes.length; i++)
041            {
042                TimeOfDay writeReadTime = writeReadTimes[i];
043    
044                Schedule event = new Schedule();
045                event.setId(i);
046                event.setNextTime(writeReadTime);
047    
048                session.save(event);
049            }
050    
051            session.flush();
052            session.connection().commit();
053            session.close();
054    
055            for (int i = 0; i<writeReadTimes.length; i++)
056            {
057                TimeOfDay writeReadTime = writeReadTimes[i];
058    
059                session = factory.openSession();
060                Schedule eventReread = (Schedule) session.get(Schedule.class, new Integer(i));
061    
062                assertNotNull("get failed - event#'" + i + "'not found", eventReread);
063                assertNotNull("get failed - returned null", eventReread.getNextTime());
064    
065                            TimeOfDay reReadTime = eventReread.getNextTime();
066                            if (writeReadTime.getHourOfDay() != reReadTime.getHourOfDay() ||
067                                    writeReadTime.getMinuteOfHour() != reReadTime.getMinuteOfHour() ||
068                                    writeReadTime.getSecondOfMinute() != reReadTime.getSecondOfMinute())
069                            {
070                                    fail("get failed - returned different date. expected " + writeReadTime + " was " + eventReread.getNextTime());
071                            }
072    
073                            if (writeReadTime.getMillisOfSecond() != reReadTime.getMillisOfSecond())
074                            {
075                                    System.out.println("millis different, might happen?");
076                            }
077                    }
078    
079                    session.close();
080        }
081    
082            protected void setupConfiguration(Configuration cfg)
083            {
084                    cfg.addFile(new File("src/test/java/org/joda/time/contrib/hibernate/schedule.hbm.xml"));
085            }
086    }