Exemplo n.º 1
0
  @Test
  public void testQuery() {

    Car car = new Car();
    car.setLicenseNumber("MARIOS_CAR_LICENSE");
    Ebean.save(car);

    VehicleDriver driver = new VehicleDriver();
    driver.setName("Mario");
    driver.setVehicle(car);
    Ebean.save(driver);

    Query<VehicleDriver> query = Ebean.find(VehicleDriver.class);
    query.where().eq("vehicle.licenseNumber", "MARIOS_CAR_LICENSE");
    List<VehicleDriver> drivers = query.findList();

    Assert.assertNotNull(drivers);
    Assert.assertEquals(1, drivers.size());
    Assert.assertNotNull(drivers.get(0));

    Assert.assertEquals("Mario", drivers.get(0).getName());
    Assert.assertEquals("MARIOS_CAR_LICENSE", drivers.get(0).getVehicle().getLicenseNumber());

    Vehicle car2 = Ebean.find(Vehicle.class, car.getId());

    car2.setLicenseNumber("test");
    Ebean.save(car);
  }
Exemplo n.º 2
0
  @Test
  public void testCasting() {

    Truck t = new Truck();
    t.setCapacity(10d);
    Ebean.save(t);

    Vehicle v = Ebean.find(Vehicle.class, t.getId());
    if (v instanceof Truck) {
      Truck t0 = (Truck) v;
      Assert.assertEquals(Double.valueOf(10d), t0.getCapacity());
      Assert.assertEquals(Double.valueOf(10d), ((Truck) v).getCapacity());
      Assert.assertNotNull(t0.getId());
    } else {
      Assert.assertTrue("v not a Truck?", false);
    }

    VehicleDriver driver = new VehicleDriver();
    driver.setName("Jim");
    driver.setVehicle(v);

    Ebean.save(driver);

    VehicleDriver d1 = Ebean.find(VehicleDriver.class, driver.getId());
    v = d1.getVehicle();
    if (v instanceof Truck) {
      Double capacity = ((Truck) v).getCapacity();
      Assert.assertEquals(Double.valueOf(10d), capacity);
      Assert.assertNotNull(v.getId());
    } else {
      Assert.assertTrue("v not a Truck?", false);
    }

    List<VehicleDriver> list = Ebean.find(VehicleDriver.class).findList();
    for (VehicleDriver vehicleDriver : list) {
      if (vehicleDriver.getVehicle() instanceof Truck) {
        Double capacity = ((Truck) vehicleDriver.getVehicle()).getCapacity();
        Assert.assertEquals(Double.valueOf(10d), capacity);
      }
    }
  }