#919 - io.ebean package initial

This commit is contained in:
Rob Bygrave committed 2016-12-11 23:23:22 +13:00
1 parent 5821dc96b9
commit 971e2dc91b
2134 files changed
+10721 -8652

No files matched your search

@@ -0,0 +1,100 @@
package org.tests.timezone;
import io.ebean.Ebean;
import io.ebean.Transaction;
import org.junit.Ignore;
import org.junit.Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
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 Timestamp nowTs = new Timestamp(1460000000000L);
public TimezoneTests() {
}
@Ignore
@Test
public void rawJdbc() throws SQLException {
// insert("Local");
// insert("UTC");
// insert("America/Los_Angeles");
// System.out.println("Local");
// fetch();
// System.out.println("UTC");
// setZone("UTC");
// fetch();
System.out.println("LA");
setZone("America/Los_Angeles");
fetch();
}
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", cal())));
System.out.println("tstz1:" + tsof(resultSet.getTimestamp("tstz1", cal())));
}
System.out.println("");
resultSet.close();
statement.close();
transaction.end();
}
private String tsof(Timestamp timestamp) {
return "" + timestamp.getTime() + ", " + timestamp.toString() + ", " + timestamp.toInstant();
}
private void setZone(String zone) {
TimeZone.setDefault(TimeZone.getTimeZone(zone));
}
private void insert(String zone) throws SQLException {
String insert = "insert into tztest (zone, ts, tstz, ts1, tstz1) values (?,?,?,?,?)";
if (!zone.equalsIgnoreCase("local")) {
setZone(zone);
}
Transaction transaction = Ebean.beginTransaction();
Connection connection = transaction.getConnection();
PreparedStatement statement = connection.prepareStatement(insert);
statement.setString(1, zone);
statement.setTimestamp(2, nowTs);
statement.setTimestamp(3, nowTs);
statement.setTimestamp(4, nowTs, cal());
statement.setTimestamp(5, nowTs, cal());
statement.executeUpdate();
transaction.commit();
}
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 org.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 = "moda")
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,93 @@
package org.tests.timezone;
import io.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();
}
}