#919 - io.ebean package initial

This commit is contained in:
Rob Bygrave
2016-12-11 23:23:22 +13:00
parent 5821dc96b9
commit 971e2dc91b
2134 changed files with 10721 additions and 8652 deletions
@@ -0,0 +1,29 @@
package org.tests.insert;
import io.ebean.Ebean;
import io.ebean.EbeanServer;
import org.tests.model.basic.EBasic;
import org.tests.model.basic.EBasic.Status;
public class MainDbCopy {
public static void main(String[] args) {
EbeanServer defaultServer = Ebean.getServer(null);
EBasic e = new EBasic();
e.setName("blah");
e.setStatus(Status.NEW);
e.setDescription(null);
defaultServer.save(e);
EBasic e1 = defaultServer.find(EBasic.class, e.getId());
EbeanServer serverDest = Ebean.getServer("mysql");
serverDest.insert(e1);
}
}
@@ -0,0 +1,81 @@
package org.tests.insert;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import org.tests.model.basic.Customer;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class TestInsertCollection extends BaseTestCase {
@Test
public void test() {
Customer cust1 = new Customer();
cust1.setName("jim");
Customer cust2 = new Customer();
cust2.setName("bob");
List<Customer> customers = new ArrayList<>();
customers.add(cust1);
customers.add(cust2);
Ebean.insertAll(customers);
Assert.assertNotNull(cust1.getId());
Assert.assertNotNull(cust2.getId());
Customer cust1Check = Ebean.find(Customer.class, cust1.getId());
Assert.assertEquals(cust1.getName(), cust1Check.getName());
Customer cust2Check = Ebean.find(Customer.class, cust2.getId());
Assert.assertEquals(cust2.getName(), cust2Check.getName());
cust1.setName("jim-changed");
cust2.setName("bob-changed");
Ebean.updateAll(customers);
awaitL2Cache();
Customer cust1Check2 = Ebean.find(Customer.class, cust1.getId());
Assert.assertEquals("jim-changed", cust1Check2.getName());
Customer cust2Check2 = Ebean.find(Customer.class, cust2.getId());
Assert.assertEquals("bob-changed", cust2Check2.getName());
cust1Check2.setName("jim-updated");
Customer cust3 = new Customer();
cust3.setName("mac");
List<Customer> saveList = new ArrayList<>();
saveList.add(cust1Check2);
saveList.add(cust3);
Ebean.saveAll(saveList);
awaitL2Cache();
Customer cust1Check3 = Ebean.find(Customer.class, cust1.getId());
Assert.assertEquals("jim-updated", cust1Check3.getName());
Customer cust3Check = Ebean.find(Customer.class, cust3.getId());
Assert.assertEquals("mac", cust3Check.getName());
List<Customer> deleteList = new ArrayList<>();
deleteList.add(cust1Check3);
deleteList.add(cust3);
deleteList.add(cust2Check2);
Ebean.deleteAll(deleteList);
awaitL2Cache();
Assert.assertNull(Ebean.find(Customer.class, cust1Check3.getId()));
Assert.assertNull(Ebean.find(Customer.class, cust2Check2.getId()));
Assert.assertNull(Ebean.find(Customer.class, cust3.getId()));
}
}
@@ -0,0 +1,40 @@
package org.tests.insert;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import org.tests.model.orderentity.OrderEntity;
import org.tests.model.orderentity.OrderItemEntity;
import org.junit.Test;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class TestInsertOnStringKey extends BaseTestCase {
@Test
public void test() {
OrderEntity orderEntity = new OrderEntity();
orderEntity.setId("anyOrderId" + new Random().nextInt());
OrderItemEntity orderItemEntity = new OrderItemEntity();
orderItemEntity.setId("anyOrderItemId" + new Random().nextInt());
orderItemEntity.setVariantId("anyVariantId");
orderItemEntity.setAmount(BigDecimal.ONE);
orderEntity.setItems(toList(orderItemEntity));
Ebean.save(orderEntity);
}
private List<OrderItemEntity> toList(OrderItemEntity orderItemEntity) {
List<OrderItemEntity> list = new ArrayList<>();
list.add(orderItemEntity);
return list;
}
}
@@ -0,0 +1,110 @@
package org.tests.insert;
import io.ebean.BaseTestCase;
import org.junit.Test;
import java.sql.SQLException;
// CURRENTLY this test is failing with an upgrade of H2 so investigating that ...
public class TestSaveWithDaylightSavings extends BaseTestCase {
@Test
public void test() {
// // For it to fail, the time has to match the time at which the daylight saving changes
// // are applied in that time zone. Therefore specify it explicitly.
//
// TimeZone defaultTimeZone = TimeZone.getDefault();
// try {
//
// TimeZone.setDefault(TimeZone.getTimeZone("EET"));
//
// // Run the code and see how there is a 3600 second change
// Timestamp daylightSavingDate = new Timestamp(1351382400000l);
// // On a second run comment in the following date and see
// // how there is a 0 second change
// // daylightSavingDate = new Date(1361382400000l);
//
// EBasic e = new EBasic();
// e.setSomeDate(daylightSavingDate);
//
// Ebean.save(e);
// Assert.assertNotNull(e.getId());
//
// // Reload the entity from database
// EBasic e2 = Ebean.find(EBasic.class, e.getId());
//
// long diffMillis = e2.getSomeDate().getTime() - e.getSomeDate().getTime();
//
// System.out.println("The date I created " + daylightSavingDate);
// System.out.println(" --- the date i put in : " + e.getSomeDate());
// System.out.println(" as millis : " + e.getSomeDate().getTime());
// System.out.println(" --- the date i get back : " + e2.getSomeDate());
// System.out.println(" as millis : " + e2.getSomeDate().getTime());
// System.out.println("The difference is " + diffMillis / 1000 + " seconds");
//
// assertEquals(0L, diffMillis);
//
// } finally {
// TimeZone.setDefault(defaultTimeZone);
// }
}
@Test
public void testDirect() throws SQLException {
// // For it to fail, the time has to match the time at which the daylight saving changes
// // are applied in that time zone. Therefore specify it explicitly.
//
// EbeanServer server = Ebean.getServer(null);
//
// Transaction transaction = server.createTransaction();
// Connection connection = transaction.getConnection();
//
// PreparedStatement pstmt = connection.prepareStatement("create table dls_test (id bigint auto_increment not null, myts timestamp)");
// pstmt.execute();
// pstmt.close();
//
// TimeZone defaultTimeZone = TimeZone.getDefault();
// try {
//
// TimeZone.setDefault(TimeZone.getTimeZone("EET"));
//
// // Run the code and see how there is a 3600 second change
// Timestamp daylightSavingDate = new Timestamp(1351382400000l);
// // On a second run comment in the following date and see
// // how there is a 0 second change
// // daylightSavingDate = new Date(1361382400000l);
//
// pstmt = connection.prepareStatement("insert into dls_test (myts) values (?)");
// pstmt.setTimestamp(1, daylightSavingDate);
// assertEquals(1, pstmt.executeUpdate());
// pstmt.close();
//
// pstmt = connection.prepareStatement("select myts from dls_test ");
// ResultSet rset = pstmt.executeQuery();
// rset.next();
// Timestamp timestampBack = rset.getTimestamp(1);
// pstmt.close();
// rset.close();
//
//
// long diffMillis = daylightSavingDate.getTime() - timestampBack.getTime();
//
// System.out.println(" --- the date i put in : " + daylightSavingDate);
// System.out.println(" as millis : " + daylightSavingDate.getTime());
// System.out.println(" --- the date i get back : " + timestampBack);
// System.out.println(" as millis : " + timestampBack.getTime());
// System.out.println("The difference is " + diffMillis / 1000 + " seconds");
//
// assertEquals(0L, diffMillis);
//
// } finally {
// TimeZone.setDefault(defaultTimeZone);
// }
}
}