コード例 #1
0
  @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 (?");
    }
  }
コード例 #2
0
ファイル: TestEncrypt.java プロジェクト: ebean-orm/ebean
  @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());
    }
  }
コード例 #3
0
ファイル: ResetBasicData.java プロジェクト: ebean-orm/ebean
  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();
        });
  }
コード例 #4
0
ファイル: ResetBasicData.java プロジェクト: ebean-orm/ebean
  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);
  }
コード例 #5
0
ファイル: ResetBasicData.java プロジェクト: ebean-orm/ebean
  private void createOrder5(Customer customer) {

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

    Ebean.save(order);
  }
コード例 #6
0
ファイル: ResetBasicData.java プロジェクト: ebean-orm/ebean
  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;
  }
コード例 #7
0
ファイル: ResetBasicData.java プロジェクト: ebean-orm/ebean
  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);
  }
コード例 #8
0
ファイル: ResetBasicData.java プロジェクト: ebean-orm/ebean
  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;
  }
コード例 #9
0
ファイル: ResetBasicData.java プロジェクト: ebean-orm/ebean
  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;
  }
コード例 #10
0
ファイル: ResetBasicData.java プロジェクト: ebean-orm/ebean
  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;
  }
コード例 #11
0
 @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());
 }
コード例 #12
0
  @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());
  }
コード例 #13
0
ファイル: ResetBasicData.java プロジェクト: ebean-orm/ebean
  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;
  }
コード例 #14
0
  @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);
  }
コード例 #15
0
ファイル: TestEncrypt.java プロジェクト: ebean-orm/ebean
  @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%)");
  }
コード例 #16
0
  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);
  }
コード例 #17
0
ファイル: ResetBasicData.java プロジェクト: ebean-orm/ebean
  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);
        });
  }
コード例 #18
0
ファイル: ResetBasicData.java プロジェクト: ebean-orm/ebean
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);
  }
}
コード例 #19
0
  @Test
  public void fetchWithInvalidPropertyName_expect_allowed() {

    Ebean.find(Customer.class).fetch("billingAddress", "invalidPropertyName").findList();
  }
コード例 #20
0
  @Test(expected = PersistenceException.class)
  public void invalidFetchPath_expect_error() {

    Ebean.find(Customer.class).fetch("notValidPath").findList();
  }
コード例 #21
0
  @Test(expected = PersistenceException.class)
  public void invalidFetchPathAndProperties_expect_error() {

    Ebean.find(Customer.class).fetch("notValidPath", "notHaveProps").findList();
  }
コード例 #22
0
ファイル: ResetBasicData.java プロジェクト: ebean-orm/ebean
 private static Customer insertCustomer(String name) {
   Customer c = createCustomer(name, "1 Banana St", "P.O.Box 1234", 1, null);
   Ebean.save(c);
   return c;
 }
コード例 #23
0
 public void create(User i) {
   Ebean.save(i);
   Ebean.save(new User(1L, "Peter")); // make it throw exception and rollback
 }