From 61c789dbafbf174d3089d28c83646d0f56704c97 Mon Sep 17 00:00:00 2001 From: Marc Prud'hommeaux Date: Sat, 20 Oct 2007 00:49:37 +0000 Subject: [PATCH] OPENJPA-322 reverted change #567170 to restore previous behavior of TimeZones in Calendar fields; added test case to confirm that the Calendar fields behave as expected git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@586649 13f79535-47bb-0310-9956-ffa450edef68 --- .../openjpa/kernel/SingleFieldManager.java | 5 +- .../apache/openjpa/util/ProxyCalendar.java | 2 - .../openjpa/jdbc/meta/TestCalendarField.java | 57 +++++++++++++++++++ .../simple/TemporalFieldTypes.java | 13 +++++ .../persistence/test/SingleEMTestCase.java | 8 +++ 5 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/meta/TestCalendarField.java diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/SingleFieldManager.java b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/SingleFieldManager.java index d32c0737e..31afc40b2 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/SingleFieldManager.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/SingleFieldManager.java @@ -92,10 +92,7 @@ class SingleFieldManager proxy = checkProxy(); if (proxy == null) { proxy = (Proxy) _sm.newFieldProxy(field); - Calendar pcal = (Calendar) proxy; - Calendar ocal = (Calendar) objval; - pcal.setTime (ocal.getTime ()); - pcal.setTimeZone (ocal.getTimeZone ()); + ((Calendar) proxy).setTime(((Calendar) objval).getTime()); ret = true; } break; diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/util/ProxyCalendar.java b/openjpa-kernel/src/main/java/org/apache/openjpa/util/ProxyCalendar.java index 4376766f3..1cc377cbe 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/util/ProxyCalendar.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/util/ProxyCalendar.java @@ -18,8 +18,6 @@ */ package org.apache.openjpa.util; -import java.util.TimeZone; - /** * Interface implemented by all generated proxies on {@link java.util.Calendar} * types. diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/meta/TestCalendarField.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/meta/TestCalendarField.java new file mode 100644 index 000000000..0dfe9d826 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/meta/TestCalendarField.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.openjpa.jdbc.meta; + +import java.io.IOException; +import java.sql.SQLException; + +import java.util.Calendar; +import java.util.TimeZone; + +import org.apache.openjpa.persistence.OpenJPAPersistence; +import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI; +import org.apache.openjpa.persistence.simple.TemporalFieldTypes; +import org.apache.openjpa.persistence.test.SingleEMTestCase; + +public class TestCalendarField extends SingleEMTestCase { + + public void setUp() { + setUp(TemporalFieldTypes.class); + } + + public void testCalendarField() throws IOException, SQLException { + TimeZone tz = TimeZone.getTimeZone("Europe/Budapest"); + + for (TemporalFieldTypes t : find(TemporalFieldTypes.class)) + remove(t); + + TemporalFieldTypes tft; + + tft = new TemporalFieldTypes(); + assertEquals(tz, tft.getCalendarTimeZoneField().getTimeZone()); + + persist(tft); + + // get a fresh EM + reset(); + + tft = find(TemporalFieldTypes.class).get(0); + assertEquals(tz, tft.getCalendarTimeZoneField().getTimeZone()); + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TemporalFieldTypes.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TemporalFieldTypes.java index daa962b1a..bff656e38 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TemporalFieldTypes.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TemporalFieldTypes.java @@ -20,6 +20,7 @@ package org.apache.openjpa.persistence.simple; import java.util.Calendar; import java.util.Date; +import java.util.TimeZone; import javax.persistence.Entity; import javax.persistence.Temporal; import javax.persistence.TemporalType; @@ -51,6 +52,9 @@ public class TemporalFieldTypes { @Temporal(TemporalType.TIMESTAMP) private Calendar calendarTimestampField; + private Calendar calendarTimeZoneField = + Calendar.getInstance(TimeZone.getTimeZone("Europe/Budapest")); + public void setDateDefaultField(Date date) { this.dateDefaultField = date; } @@ -114,4 +118,13 @@ public class TemporalFieldTypes { public Calendar getCalendarTimeStampField() { return this.calendarTimestampField; } + + public void setCalendarTimeZoneField(Calendar calendarTimeZoneField) { + this.calendarTimeZoneField = calendarTimeZoneField; + } + + public Calendar getCalendarTimeZoneField() { + return this.calendarTimeZoneField; + } + } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMTestCase.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMTestCase.java index 08cdc9e90..74b621388 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMTestCase.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMTestCase.java @@ -47,6 +47,14 @@ public abstract class SingleEMTestCase em = emf.createEntityManager(); } + /** + * Clear the current EntityManager and re-initialize it. + */ + protected void reset() { + close(); + em = emf.createEntityManager(); + } + @Override public void tearDown() throws Exception { rollback();