mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#662 - Postgres Timestamp with time zone testing
This commit is contained in:
@@ -89,7 +89,7 @@ public class TestTypeManager extends BaseTestCase {
|
||||
String val;
|
||||
|
||||
public DummyDataReader(String val) {
|
||||
super(null);
|
||||
super(null, null);
|
||||
this.val = val;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ import com.avaje.ebean.text.json.JsonContext;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.ebeaninternal.server.query.CQuery;
|
||||
import com.avaje.ebeaninternal.server.transaction.RemoteTransactionEvent;
|
||||
import com.avaje.ebeaninternal.server.core.timezone.DataTimeZone;
|
||||
|
||||
import javax.persistence.OptimisticLockException;
|
||||
import javax.persistence.PersistenceException;
|
||||
@@ -44,6 +45,11 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataTimeZone getDataTimeZone() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SpiServer getPluginApi() {
|
||||
return null;
|
||||
|
||||
@@ -13,17 +13,14 @@ import java.sql.Timestamp;
|
||||
import java.util.Calendar;
|
||||
import java.util.TimeZone;
|
||||
|
||||
/**
|
||||
* Must run this manually with various JVM timezones for insert and fetch.
|
||||
*/
|
||||
public class TimezoneTests {
|
||||
|
||||
private final long now = 1460000000000L;
|
||||
|
||||
private final Timestamp nowTs = new Timestamp(now);
|
||||
|
||||
private final Calendar utcCalendar;
|
||||
private final Timestamp nowTs = new Timestamp(1460000000000L);
|
||||
|
||||
public TimezoneTests() {
|
||||
utcCalendar = Calendar.getInstance();
|
||||
utcCalendar.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@@ -34,12 +31,12 @@ public class TimezoneTests {
|
||||
// insert("UTC");
|
||||
// insert("America/Los_Angeles");
|
||||
|
||||
System.out.println("Local");
|
||||
fetch();
|
||||
// System.out.println("Local");
|
||||
// fetch();
|
||||
|
||||
System.out.println("UTC");
|
||||
setZone("UTC");
|
||||
fetch();
|
||||
// System.out.println("UTC");
|
||||
// setZone("UTC");
|
||||
// fetch();
|
||||
|
||||
System.out.println("LA");
|
||||
setZone("America/Los_Angeles");
|
||||
@@ -49,14 +46,15 @@ public class TimezoneTests {
|
||||
private void fetch() throws SQLException {
|
||||
Transaction transaction = Ebean.beginTransaction();
|
||||
Connection connection = transaction.getConnection();
|
||||
|
||||
PreparedStatement statement = connection.prepareStatement("select * from tztest");
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
while (resultSet.next()) {
|
||||
System.out.println(" zone:"+resultSet.getString("zone"));
|
||||
System.out.println(" ts:"+tsof(resultSet.getTimestamp("ts")));
|
||||
System.out.println(" tstz:"+tsof(resultSet.getTimestamp("tstz")));
|
||||
System.out.println(" ts1:"+tsof(resultSet.getTimestamp("ts1", utcCal())));
|
||||
System.out.println("tstz1:"+tsof(resultSet.getTimestamp("tstz1", utcCal())));
|
||||
System.out.println(" ts1:"+tsof(resultSet.getTimestamp("ts1", cal())));
|
||||
System.out.println("tstz1:"+tsof(resultSet.getTimestamp("tstz1", cal())));
|
||||
}
|
||||
System.out.println("");
|
||||
resultSet.close();
|
||||
@@ -65,7 +63,7 @@ public class TimezoneTests {
|
||||
}
|
||||
|
||||
private String tsof(Timestamp timestamp) {
|
||||
return ""+timestamp.getTime()+","+timestamp.toString();
|
||||
return ""+timestamp.getTime()+", "+timestamp.toString()+", "+timestamp.toInstant();
|
||||
}
|
||||
|
||||
private void setZone(String zone) {
|
||||
@@ -86,14 +84,17 @@ public class TimezoneTests {
|
||||
statement.setString(1, zone);
|
||||
statement.setTimestamp(2, nowTs);
|
||||
statement.setTimestamp(3, nowTs);
|
||||
statement.setTimestamp(4, nowTs, utcCal());
|
||||
statement.setTimestamp(5, nowTs, utcCal());
|
||||
statement.setTimestamp(4, nowTs, cal());
|
||||
statement.setTimestamp(5, nowTs, cal());
|
||||
statement.executeUpdate();
|
||||
|
||||
transaction.commit();
|
||||
}
|
||||
|
||||
private Calendar utcCal() {
|
||||
return (Calendar) utcCalendar.clone();
|
||||
private Calendar cal() {
|
||||
|
||||
Calendar instance = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
|
||||
//Calendar instance = Calendar.getInstance(TimeZone.getDefault());
|
||||
return (Calendar) instance.clone();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.avaje.tests.timezone;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
@Entity
|
||||
@Table(name="tz_bean")
|
||||
public class TzBean {
|
||||
|
||||
@Id
|
||||
Long id;
|
||||
|
||||
@Column(name = "mode")
|
||||
String mode;
|
||||
|
||||
Timestamp ts;
|
||||
Timestamp tstz;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getMode() {
|
||||
return mode;
|
||||
}
|
||||
|
||||
public void setMode(String mode) {
|
||||
this.mode = mode;
|
||||
}
|
||||
|
||||
public Timestamp getTs() {
|
||||
return ts;
|
||||
}
|
||||
|
||||
public void setTs(Timestamp ts) {
|
||||
this.ts = ts;
|
||||
}
|
||||
|
||||
public Timestamp getTstz() {
|
||||
return tstz;
|
||||
}
|
||||
|
||||
public void setTstz(Timestamp tstz) {
|
||||
this.tstz = tstz;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.avaje.tests.timezone;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.List;
|
||||
import java.util.TimeZone;
|
||||
//import java.util.TimeZone;
|
||||
|
||||
|
||||
|
||||
// Postgres
|
||||
// manually create table
|
||||
// create table tz_bean (id serial, mode varchar(40), ts timestamp, tstz timestamp with time zone);
|
||||
|
||||
// MySql
|
||||
// manually create table (note there is no timestamp with time zone type)
|
||||
// create table tz_bean (id serial, mode varchar(40), ts timestamp, tstz timestamp);
|
||||
|
||||
// Oracle
|
||||
// create table tz_bean (id integer, run_mode varchar(40), ts timestamp, tstz timestamp);
|
||||
|
||||
/**
|
||||
* Must run this test manually with various time zones for insert and fetch.
|
||||
*/
|
||||
public class TzBeanTests {
|
||||
|
||||
private final Timestamp nowTs = new Timestamp(1460000000000L);
|
||||
|
||||
public TzBeanTests() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Run this 5 times with different modes.
|
||||
*/
|
||||
@Ignore
|
||||
@Test
|
||||
public void insert_one_at_a_time() {
|
||||
|
||||
//String mode = "noCal + local tz";
|
||||
|
||||
// String mode = "noCal + LA";
|
||||
// TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
|
||||
|
||||
// String mode = "noCal + UTC";
|
||||
// TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
|
||||
|
||||
// System.setProperty("ebean.dataTimeZone", "UTC");
|
||||
// TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
|
||||
// String mode = "UTC + LA";
|
||||
|
||||
System.setProperty("ebean.dataTimeZone", "UTC");
|
||||
String mode = "UTC + NZST";
|
||||
|
||||
TzBean bean = new TzBean();
|
||||
bean.setMode(mode);
|
||||
bean.setTs(nowTs);
|
||||
bean.setTstz(nowTs);
|
||||
|
||||
Ebean.save(bean);
|
||||
}
|
||||
|
||||
/**
|
||||
* In separate JVM/execution fetch the beans with various timezones etc.
|
||||
*/
|
||||
@Ignore
|
||||
@Test
|
||||
public void fetch_beans() {
|
||||
|
||||
|
||||
// set the jvm timezone
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
|
||||
// TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
|
||||
|
||||
// set the Calendar time zone to use in JDBC calls
|
||||
System.setProperty("ebean.dataTimeZone", "UTC");
|
||||
|
||||
List<TzBean> list = Ebean.find(TzBean.class)
|
||||
.findList();
|
||||
|
||||
for (TzBean bean : list) {
|
||||
System.out.println(" mode:"+bean.getMode());
|
||||
System.out.println(" ts:"+tsof(bean.getTs()));
|
||||
System.out.println(" tstz:"+tsof(bean.getTstz()));
|
||||
}
|
||||
}
|
||||
|
||||
private String tsof(Timestamp timestamp) {
|
||||
return ""+timestamp.getTime()+", "+timestamp.toString()+", "+timestamp.toInstant();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user