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.DateTime; 025 import org.joda.time.DateTimeZone; 026 027 public class TestPersistentDateTime extends HibernateTestCase 028 { 029 private DateTime[] writeReadTimes = new DateTime[] 030 { 031 new DateTime(2004, 2, 25, 17, 3, 45, 760), 032 new DateTime(1980, 3, 11, 2, 3, 45, 0, DateTimeZone.forOffsetHours(2)) 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 DateTime writeReadTime = writeReadTimes[i]; 044 045 Event event = new Event(); 046 event.setId(i); 047 event.setDateTime(writeReadTime); 048 049 session.save(event); 050 } 051 052 session.flush(); 053 session.connection().commit(); 054 session.close(); 055 056 for (int i = 0; i<writeReadTimes.length; i++) 057 { 058 DateTime writeReadTime = writeReadTimes[i]; 059 060 session = factory.openSession(); 061 Event eventReread = (Event) session.get(Event.class, new Integer(i)); 062 063 assertNotNull("get failed - event#'" + i + "'not found", eventReread); 064 assertNotNull("get failed - returned null", eventReread.getDateTime()); 065 066 // we loose the timezone, so we have to normalize both to offset=0 067 assertEquals("get failed - returned different time", 068 writeReadTime.toDateTime(DateTimeZone.forOffsetHours(0)), 069 eventReread.getDateTime().toDateTime(DateTimeZone.forOffsetHours(0))); 070 071 session.close(); 072 } 073 } 074 075 public void testStoreWithTimezone() throws SQLException 076 { 077 SessionFactory factory = getSessionFactory(); 078 079 Session session = factory.openSession(); 080 081 for (int i = 0; i<writeReadTimes.length; i++) 082 { 083 DateTime writeReadTime = writeReadTimes[i]; 084 085 EventTZ event = new EventTZ(); 086 event.setId(i); 087 event.setDateTime(writeReadTime); 088 089 session.save(event); 090 } 091 092 session.flush(); 093 session.connection().commit(); 094 session.close(); 095 096 for (int i = 0; i<writeReadTimes.length; i++) 097 { 098 DateTime writeReadTime = writeReadTimes[i]; 099 100 session = factory.openSession(); 101 EventTZ eventReread = (EventTZ) session.get(EventTZ.class, new Integer(i)); 102 103 assertNotNull("get failed - event#'" + i + "'not found", eventReread); 104 assertNotNull("get failed - returned null", eventReread.getDateTime()); 105 106 assertEquals("get failed - returned different time", 107 writeReadTime, eventReread.getDateTime()); 108 } 109 110 session.close(); 111 } 112 113 protected void setupConfiguration(Configuration cfg) 114 { 115 cfg.addFile(new File("src/test/java/org/joda/time/contrib/hibernate/event.hbm.xml")); 116 cfg.addFile(new File("src/test/java/org/joda/time/contrib/hibernate/eventTZ.hbm.xml")); 117 } 118 }