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.LocalDate;
025    
026    public class TestPersistentLocalDate extends HibernateTestCase
027    {
028        private LocalDate[] writeReadTimes = new LocalDate[]
029        {
030            new LocalDate(2004, 2, 25),
031            new LocalDate(1980, 3, 11)
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                LocalDate writeReadTime = writeReadTimes[i];
043    
044                Event event = new Event();
045                event.setId(i);
046                event.setLocalDate(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                LocalDate writeReadTime = writeReadTimes[i];
058    
059                session = factory.openSession();
060                Event eventReread = (Event) session.get(Event.class, new Integer(i));
061    
062                assertNotNull("get failed - event#'" + i + "'not found", eventReread);
063                assertNotNull("get failed - returned null", eventReread.getLocalDate());
064    
065                // we loose the timezone, so we have to normalize both to offset=0
066                assertEquals("get failed - returned different time",
067                    writeReadTime,
068                    eventReread.getLocalDate());
069    
070                session.close();
071            }
072        }
073    
074            protected void setupConfiguration(Configuration cfg)
075            {
076                    cfg.addFile(new File("src/test/java/org/joda/time/contrib/hibernate/event.hbm.xml"));
077                    cfg.addFile(new File("src/test/java/org/joda/time/contrib/hibernate/eventTZ.hbm.xml"));
078            }
079    }