@Test
  public void test() {

    if (isSqlServer()) return;

    List<OCachedBean> list = new ArrayList();

    for (int i = 0; i < 3; i++) {
      OCachedBean bean = new OCachedBean();
      bean.setName("name " + i);
      list.add(bean);
    }

    LoggedSqlCollector.start();

    Transaction txn = Ebean.beginTransaction();
    try {
      txn.setBatch(PersistBatch.ALL);

      Ebean.saveAll(list);
      txn.commit();

    } finally {
      txn.end();
    }

    List<String> loggedSql = LoggedSqlCollector.stop();
    assertThat(loggedSql).hasSize(3);

    for (String sql : loggedSql) {
      assertThat(sql).contains("insert into o_cached_bean (");
      assertThat(sql).contains("name) values (?");
    }
  }
Beispiel #2
0
  @Ignore
  @Test
  public void test() {

    Update<EBasicEncrypt> deleteAll =
        Ebean.createUpdate(EBasicEncrypt.class, "delete from EBasicEncrypt");
    deleteAll.execute();

    EBasicEncrypt e = new EBasicEncrypt();
    e.setName("testname");
    e.setDescription("testdesc");
    e.setDob(new Date(System.currentTimeMillis() - 100000));

    Ebean.save(e);

    Date earlyDob = new Date(System.currentTimeMillis() - 500000);

    SqlQuery q = Ebean.createSqlQuery("select * from e_basicenc where id = :id");
    q.setParameter("id", e.getId());

    SqlRow row = q.findUnique();
    row.getString("name");
    row.get("description");

    EBasicEncrypt e1 = Ebean.find(EBasicEncrypt.class, e.getId());

    e1.getDescription();

    e1.setName("testmod");
    e1.setDescription("moddesc");

    Ebean.save(e1);

    EBasicEncrypt e2 = Ebean.find(EBasicEncrypt.class, e.getId());

    e2.getDescription();

    SpiEbeanServer server = (SpiEbeanServer) Ebean.getServer(null);
    DbEncrypt dbEncrypt = server.getDatabasePlatform().getDbEncrypt();

    if (dbEncrypt == null) {
      // can not test the where clause
      System.out.println("TestEncrypt: Not testing where clause as no DbEncrypt");

    } else {

      List<EBasicEncrypt> list =
          Ebean.find(EBasicEncrypt.class).where().eq("description", "moddesc").findList();

      Assert.assertEquals(1, list.size());

      list = Ebean.find(EBasicEncrypt.class).where().startsWith("description", "modde").findList();

      Assert.assertEquals(1, list.size());
    }
  }
Beispiel #3
0
  public void deleteAll() {
    Ebean.execute(
        () -> {

          // orm update use bean name and bean properties
          server.createSqlUpdate("delete from o_cached_bean_child").execute();
          server.createSqlUpdate("delete from o_cached_bean_country").execute();
          server.createSqlUpdate("delete from o_cached_bean").execute();

          server.createUpdate(OrderShipment.class, "delete from orderShipment").execute();

          server.createUpdate(OrderDetail.class, "delete from orderDetail").execute();

          server.createUpdate(Order.class, "delete from order").execute();

          server.createUpdate(Contact.class, "delete from contact").execute();

          server.createUpdate(Customer.class, "delete from Customer").execute();

          server.createUpdate(Address.class, "delete from address").execute();

          // sql update uses table and column names
          server.createSqlUpdate("delete from o_country").execute();
          server.createSqlUpdate("delete from o_product").execute();
        });
  }
Beispiel #4
0
  private void createOrder2(Customer customer) {

    Product product1 = Ebean.getReference(Product.class, 1);

    Order order = new Order();
    order.setStatus(Status.SHIPPED);
    order.setCustomer(customer);

    List<OrderDetail> details = new ArrayList<>();
    details.add(new OrderDetail(product1, 4, 10.50));
    order.setDetails(details);

    order.addShipment(new OrderShipment());

    Ebean.save(order);
  }
Beispiel #5
0
  private void createOrder5(Customer customer) {

    Order order = new Order();
    order.setCustomer(customer);
    order.addShipment(new OrderShipment());

    Ebean.save(order);
  }
Beispiel #6
0
  private Customer insertCustomerNoContacts(String name) {

    Customer c = createCustomer("Roger", "15 Kumera Way", "Bos town", 1, "2010-04-10");
    c.setName(name);
    c.setStatus(Customer.Status.ACTIVE);

    Ebean.save(c);
    return c;
  }
Beispiel #7
0
  private void createOrder3(Customer customer) {

    Product product1 = Ebean.getReference(Product.class, 1);
    Product product3 = Ebean.getReference(Product.class, 3);

    Order order = new Order();
    order.setStatus(Status.COMPLETE);
    order.setCustomer(customer);

    List<OrderDetail> details = new ArrayList<>();
    details.add(new OrderDetail(product1, 3, 10.50));
    details.add(new OrderDetail(product3, 40, 2.10));
    details.add(new OrderDetail(product1, 5, 10.00));
    order.setDetails(details);

    order.addShipment(new OrderShipment());

    Ebean.save(order);
  }
Beispiel #8
0
  private Customer insertCustomerNoAddress() {

    Customer c = new Customer();
    c.setName("Cust NoAddress");
    c.setStatus(Customer.Status.NEW);
    c.addContact(createContact("Jack", "Black"));

    Ebean.save(c);
    return c;
  }
Beispiel #9
0
  private Customer insertCustomerFiona() {

    Customer c = createCustomer("Fiona", "12 Apple St", "West Coast Rd", 1, "2009-08-31");
    c.setStatus(Customer.Status.ACTIVE);

    c.addContact(createContact("Fiona", "Black"));
    c.addContact(createContact("Tracy", "Red"));

    Ebean.save(c);
    return c;
  }
Beispiel #10
0
  private Order createOrder1(Customer customer) {

    Product product1 = Ebean.getReference(Product.class, 1);
    Product product2 = Ebean.getReference(Product.class, 2);
    Product product3 = Ebean.getReference(Product.class, 3);

    Order order = new Order();
    order.setCustomer(customer);

    List<OrderDetail> details = new ArrayList<>();
    details.add(new OrderDetail(product1, 5, 10.50));
    details.add(new OrderDetail(product2, 3, 1.10));
    details.add(new OrderDetail(product3, 1, 2.00));
    order.setDetails(details);

    order.addShipment(new OrderShipment());

    Ebean.save(order);
    return order;
  }
 @Test
 public void test() {
   try {
     new UserService().create(new User(1L, "David"));
     fail("Exception should be thrown");
   } catch (PersistenceException pe) {
     LOG.error("e: " + pe);
   }
   List<User> users = Ebean.find(User.class).findList();
   LOG.debug("users: {}", users);
   assertTrue("users should be empty", users.isEmpty());
 }
  @Test
  public void test() {

    Cat cat = new Cat();
    cat.setName("Puss");
    Ebean.save(cat);

    Dog dog = new Dog();
    dog.setRegistrationNumber("DOGGIE");
    Ebean.save(dog);

    BigDog bd = new BigDog();
    bd.setDogSize("large");
    bd.setRegistrationNumber("BG1");
    Ebean.save(bd);

    AnimalShelter shelter = new AnimalShelter();
    shelter.setName("My Animal Shelter");
    shelter.getAnimals().add(cat);
    shelter.getAnimals().add(dog);

    Ebean.save(shelter);

    AnimalShelter shelter2 = Ebean.find(AnimalShelter.class, shelter.getId());
    List<Animal> animals = shelter2.getAnimals();

    BeanList<?> beanList = (BeanList<?>) animals;
    ModifyListenMode modifyListenMode = beanList.getModifyListenMode();

    assertNotNull(modifyListenMode);

    assertNotNull(Ebean.find(Animal.class).findList());
  }
Beispiel #13
0
  public static Customer createCustomer(
      String name, String shippingStreet, String billingStreet, int contactSuffix, String annDate) {

    Customer c = new Customer();
    c.setName(name);
    c.setStatus(Customer.Status.NEW);
    if (annDate == null) {
      annDate = "2010-04-14";
    }
    c.setAnniversary(Date.valueOf(annDate));
    if (contactSuffix > 0) {
      Contact jim = new Contact("Jim" + contactSuffix, "Cricket");
      jim.getNotes().add(new ContactNote("ORM Lives", "And it is cool!"));
      c.addContact(jim);
      c.addContact(new Contact("Fred" + contactSuffix, "Blue"));
      c.addContact(new Contact("Bugs" + contactSuffix, "Bunny"));
    }

    if (shippingStreet != null) {
      Address shippingAddr = new Address();
      shippingAddr.setLine1(shippingStreet);
      shippingAddr.setLine2("Sandringham");
      shippingAddr.setCity("Auckland");
      shippingAddr.setCountry(Ebean.getReference(Country.class, "NZ"));

      c.setShippingAddress(shippingAddr);
    }

    if (billingStreet != null) {
      Address billingAddr = new Address();
      billingAddr.setLine1(billingStreet);
      billingAddr.setLine2("St Lukes");
      billingAddr.setCity("Auckland");
      billingAddr.setCountry(Ebean.getReference(Country.class, "NZ"));

      c.setBillingAddress(billingAddr);
    }

    return c;
  }
  @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);
  }
Beispiel #15
0
  @Test
  public void testQueryBind() {

    if (!isH2()) {
      // only run this on H2 - PGCrypto not happy on CI server
      return;
    }

    LoggedSqlCollector.start();
    Ebean.find(EBasicEncrypt.class).where().startsWith("description", "Rob").findList();

    List<String> loggedSql = LoggedSqlCollector.stop();
    assertThat(loggedSql).hasSize(1);
    assertThat(loggedSql.get(0)).contains("; --bind(****,Rob%)");
  }
  private static void test() {
    RawSql rawSql =
        RawSqlBuilder.unparsed(
                "select r.id, r.name from o_customer r where r.id >= :a and r.name like :b")
            .columnMapping("r.id", "id")
            .columnMapping("r.name", "name")
            .create();

    Query<Customer> query = Ebean.find(Customer.class);
    query.setRawSql(rawSql);
    query.setParameter("a", 1);
    query.setParameter("b", "R%");

    List<Customer> list = query.findList();
    Assert.assertNotNull(list);
  }
Beispiel #17
0
  public void insertTestCustAndOrders() {

    Ebean.execute(
        () -> {
          Customer cust1 = insertCustomer("Rob");
          Customer cust2 = insertCustomerNoAddress();
          insertCustomerFiona();
          insertCustomerNoContacts("NocCust");

          createOrder1(cust1);
          createOrder2(cust2);
          createOrder3(cust1);
          createOrder4(cust1);
          createOrder5(cust2);
        });
  }
Beispiel #18
0
public class ResetBasicData {

  private static boolean runOnce;

  private static EbeanServer server = Ebean.getServer(null);

  public static synchronized void reset() {

    if (runOnce) {
      return;
    }

    final ResetBasicData me = new ResetBasicData();

    server.execute(
        () -> {
          if (server.find(Product.class).findCount() > 0) {
            // we can't really delete this base data as
            // the test rely on the products being in there
            return;
          }
          // me.deleteAll();
          me.insertCountries();
          me.insertProducts();
          me.insertTestCustAndOrders();
        });
    runOnce = true;
  }

  public void deleteAll() {
    Ebean.execute(
        () -> {

          // orm update use bean name and bean properties
          server.createSqlUpdate("delete from o_cached_bean_child").execute();
          server.createSqlUpdate("delete from o_cached_bean_country").execute();
          server.createSqlUpdate("delete from o_cached_bean").execute();

          server.createUpdate(OrderShipment.class, "delete from orderShipment").execute();

          server.createUpdate(OrderDetail.class, "delete from orderDetail").execute();

          server.createUpdate(Order.class, "delete from order").execute();

          server.createUpdate(Contact.class, "delete from contact").execute();

          server.createUpdate(Customer.class, "delete from Customer").execute();

          server.createUpdate(Address.class, "delete from address").execute();

          // sql update uses table and column names
          server.createSqlUpdate("delete from o_country").execute();
          server.createSqlUpdate("delete from o_product").execute();
        });
  }

  public void insertCountries() {

    if (server.find(Country.class).findCount() > 0) {
      return;
    }

    server.execute(
        () -> {
          Country c = new Country();
          c.setCode("NZ");
          c.setName("New Zealand");
          server.save(c);

          Country au = new Country();
          au.setCode("AU");
          au.setName("Australia");
          server.save(au);
        });
  }

  public void insertProducts() {

    if (server.find(Product.class).findCount() > 0) {
      return;
    }
    server.execute(
        () -> {
          Product p = new Product();
          p.setName("Chair");
          p.setSku("C001");
          server.save(p);

          p = new Product();
          p.setName("Desk");
          p.setSku("DSK1");
          server.save(p);

          p = new Product();
          p.setName("Computer");
          p.setSku("C002");
          server.save(p);

          p = new Product();
          p.setName("Printer");
          p.setSku("C003");
          server.save(p);
        });
  }

  public void insertTestCustAndOrders() {

    Ebean.execute(
        () -> {
          Customer cust1 = insertCustomer("Rob");
          Customer cust2 = insertCustomerNoAddress();
          insertCustomerFiona();
          insertCustomerNoContacts("NocCust");

          createOrder1(cust1);
          createOrder2(cust2);
          createOrder3(cust1);
          createOrder4(cust1);
          createOrder5(cust2);
        });
  }

  public static Customer createCustAndOrder(String custName) {

    ResetBasicData me = new ResetBasicData();
    Customer cust1 = insertCustomer(custName);
    me.createOrder1(cust1);
    return cust1;
  }

  public static Order createOrderCustAndOrder(String custName) {

    ResetBasicData me = new ResetBasicData();
    Customer cust1 = insertCustomer(custName);
    return me.createOrder1(cust1);
  }

  private static int contactEmailNum = 1;

  private Customer insertCustomerFiona() {

    Customer c = createCustomer("Fiona", "12 Apple St", "West Coast Rd", 1, "2009-08-31");
    c.setStatus(Customer.Status.ACTIVE);

    c.addContact(createContact("Fiona", "Black"));
    c.addContact(createContact("Tracy", "Red"));

    Ebean.save(c);
    return c;
  }

  public static Contact createContact(String firstName, String lastName) {
    Contact contact = new Contact(firstName, lastName);
    String email = contact.getLastName() + (contactEmailNum++) + "@test.com";
    contact.setEmail(email.toLowerCase());
    return contact;
  }

  private Customer insertCustomerNoContacts(String name) {

    Customer c = createCustomer("Roger", "15 Kumera Way", "Bos town", 1, "2010-04-10");
    c.setName(name);
    c.setStatus(Customer.Status.ACTIVE);

    Ebean.save(c);
    return c;
  }

  private Customer insertCustomerNoAddress() {

    Customer c = new Customer();
    c.setName("Cust NoAddress");
    c.setStatus(Customer.Status.NEW);
    c.addContact(createContact("Jack", "Black"));

    Ebean.save(c);
    return c;
  }

  private static Customer insertCustomer(String name) {
    Customer c = createCustomer(name, "1 Banana St", "P.O.Box 1234", 1, null);
    Ebean.save(c);
    return c;
  }

  public static Customer createCustomer(
      String name, String shippingStreet, String billingStreet, int contactSuffix) {
    return createCustomer(name, shippingStreet, billingStreet, contactSuffix, null);
  }

  public static Customer createCustomer(
      String name, String shippingStreet, String billingStreet, int contactSuffix, String annDate) {

    Customer c = new Customer();
    c.setName(name);
    c.setStatus(Customer.Status.NEW);
    if (annDate == null) {
      annDate = "2010-04-14";
    }
    c.setAnniversary(Date.valueOf(annDate));
    if (contactSuffix > 0) {
      Contact jim = new Contact("Jim" + contactSuffix, "Cricket");
      jim.getNotes().add(new ContactNote("ORM Lives", "And it is cool!"));
      c.addContact(jim);
      c.addContact(new Contact("Fred" + contactSuffix, "Blue"));
      c.addContact(new Contact("Bugs" + contactSuffix, "Bunny"));
    }

    if (shippingStreet != null) {
      Address shippingAddr = new Address();
      shippingAddr.setLine1(shippingStreet);
      shippingAddr.setLine2("Sandringham");
      shippingAddr.setCity("Auckland");
      shippingAddr.setCountry(Ebean.getReference(Country.class, "NZ"));

      c.setShippingAddress(shippingAddr);
    }

    if (billingStreet != null) {
      Address billingAddr = new Address();
      billingAddr.setLine1(billingStreet);
      billingAddr.setLine2("St Lukes");
      billingAddr.setCity("Auckland");
      billingAddr.setCountry(Ebean.getReference(Country.class, "NZ"));

      c.setBillingAddress(billingAddr);
    }

    return c;
  }

  private Order createOrder1(Customer customer) {

    Product product1 = Ebean.getReference(Product.class, 1);
    Product product2 = Ebean.getReference(Product.class, 2);
    Product product3 = Ebean.getReference(Product.class, 3);

    Order order = new Order();
    order.setCustomer(customer);

    List<OrderDetail> details = new ArrayList<>();
    details.add(new OrderDetail(product1, 5, 10.50));
    details.add(new OrderDetail(product2, 3, 1.10));
    details.add(new OrderDetail(product3, 1, 2.00));
    order.setDetails(details);

    order.addShipment(new OrderShipment());

    Ebean.save(order);
    return order;
  }

  private void createOrder2(Customer customer) {

    Product product1 = Ebean.getReference(Product.class, 1);

    Order order = new Order();
    order.setStatus(Status.SHIPPED);
    order.setCustomer(customer);

    List<OrderDetail> details = new ArrayList<>();
    details.add(new OrderDetail(product1, 4, 10.50));
    order.setDetails(details);

    order.addShipment(new OrderShipment());

    Ebean.save(order);
  }

  private void createOrder3(Customer customer) {

    Product product1 = Ebean.getReference(Product.class, 1);
    Product product3 = Ebean.getReference(Product.class, 3);

    Order order = new Order();
    order.setStatus(Status.COMPLETE);
    order.setCustomer(customer);

    List<OrderDetail> details = new ArrayList<>();
    details.add(new OrderDetail(product1, 3, 10.50));
    details.add(new OrderDetail(product3, 40, 2.10));
    details.add(new OrderDetail(product1, 5, 10.00));
    order.setDetails(details);

    order.addShipment(new OrderShipment());

    Ebean.save(order);
  }

  private void createOrder4(Customer customer) {

    Order order = new Order();
    order.setCustomer(customer);

    order.addShipment(new OrderShipment());

    Ebean.save(order);
  }

  private void createOrder5(Customer customer) {

    Order order = new Order();
    order.setCustomer(customer);
    order.addShipment(new OrderShipment());

    Ebean.save(order);
  }
}
  @Test
  public void fetchWithInvalidPropertyName_expect_allowed() {

    Ebean.find(Customer.class).fetch("billingAddress", "invalidPropertyName").findList();
  }
  @Test(expected = PersistenceException.class)
  public void invalidFetchPath_expect_error() {

    Ebean.find(Customer.class).fetch("notValidPath").findList();
  }
  @Test(expected = PersistenceException.class)
  public void invalidFetchPathAndProperties_expect_error() {

    Ebean.find(Customer.class).fetch("notValidPath", "notHaveProps").findList();
  }
Beispiel #22
0
 private static Customer insertCustomer(String name) {
   Customer c = createCustomer(name, "1 Banana St", "P.O.Box 1234", 1, null);
   Ebean.save(c);
   return c;
 }
 public void create(User i) {
   Ebean.save(i);
   Ebean.save(new User(1L, "Peter")); // make it throw exception and rollback
 }