Example #1
0
  public void testQueryLaptops() throws Exception {
    Database database = _category.getDatabase();

    database.begin();
    OQLQuery query =
        database.getOQLQuery("select l from " + Laptop.class.getName() + " as l order by l.id");
    QueryResults results = query.execute();

    if (results.hasMore()) {
      int counter = 1;
      Laptop laptop = null;
      while (results.hasMore()) {
        laptop = (Laptop) results.next();
        assertNotNull(laptop);
        assertEquals(counter, laptop.getId());

        counter += 1;
      }
    } else {
      fail("Query does not return any Laptop instances.");
    }
    database.commit();

    database.close();
  }
Example #2
0
 public static void testClone(int size) throws CloneNotSupportedException {
   long start = System.currentTimeMillis();
   Laptop t = new Laptop();
   for (int i = 0; i < size; i++) {
     Laptop temp = (Laptop) t.clone();
   }
   long end = System.currentTimeMillis();
   System.out.println("clone的方式创建耗时:" + (end - start));
 }
Example #3
0
  public void testCreateAndLoadLaptop() throws Exception {
    Database database = _category.getDatabase();

    database.begin();
    ProductDetail detail = new ProductDetail();
    detail.setId(10);
    detail.setCategory("category 10");
    detail.setLocation("location 10");
    database.create(detail);
    database.commit();

    database.begin();
    Laptop laptop = new Laptop();
    laptop.setId(10);
    laptop.setName("laptop 10");
    laptop.setCpu("centrino");
    laptop.setResolution("1600");
    laptop.setWeight(2750);
    laptop.setDetail((ProductDetail) database.load(ProductDetail.class, new Integer(10)));
    database.create(laptop);

    Owner owner = new Owner();
    owner.setId(new Integer(10));
    owner.setName("owner 10");
    owner.setProduct(laptop);
    database.commit();

    database.begin();
    laptop = (Laptop) database.load(Laptop.class, new Integer(10));
    database.commit();

    assertNotNull(laptop);
    assertEquals("ctf.jdo.tc9x.Laptop", laptop.getClass().getName());
    assertEquals(10, laptop.getId());
    assertEquals("laptop 10", laptop.getName());

    database.begin();
    database.remove(database.load(Laptop.class, new Integer(10)));
    database.remove(database.load(ProductDetail.class, new Integer(10)));
    database.commit();

    database.begin();
    try {
      laptop = (Laptop) database.load(Laptop.class, new Integer(10));
      fail("Laptop with id 10 still exists.");
    } catch (ObjectNotFoundException e) {
      assertEquals(
          "The object of type ctf.jdo.tc9x.Laptop with identity <10(10)> "
              + "was not found in persistent storage",
          e.getMessage());
    }
    database.commit();

    database.close();
  }
Example #4
0
  public void testLoadLaptop() throws Exception {
    Database database = _category.getDatabase();

    database.begin();
    Laptop laptop = (Laptop) database.load(Laptop.class, new Integer(1));
    database.commit();

    assertNotNull(laptop);
    assertEquals("ctf.jdo.tc9x.Laptop", laptop.getClass().getName());
    assertEquals(1, laptop.getId());
    assertEquals("laptop 1", laptop.getName());

    database.close();
  }
Example #5
0
  public void testCreateAndLoadComputer() throws Exception {
    Database database = _category.getDatabase();

    database.begin();
    Order order = new Order();
    order.setId(12);
    order.setName("order 12");
    database.create(order);
    database.commit();

    database.begin();
    ProductDetail detail = new ProductDetail();
    detail.setId(12);
    detail.setCategory("category 12");
    detail.setLocation("location 12");
    database.create(detail);
    database.commit();

    database.begin();
    Laptop laptop = new Laptop();
    laptop.setId(12);
    laptop.setName("laptop 12");
    laptop.setCpu("centrino");
    laptop.setResolution("1600");
    laptop.setWeight(2450);
    laptop.setDetail((ProductDetail) database.load(ProductDetail.class, new Integer(12)));
    database.create(laptop);
    Collection orders = new LinkedList();
    orders.add(database.load(Order.class, new Integer(12)));
    laptop.setOrders(orders);
    database.commit();

    database.begin();
    Computer computer = (Computer) database.load(Computer.class, new Integer(12));
    database.commit();

    assertNotNull(computer);
    assertEquals("ctf.jdo.tc9x.Laptop", computer.getClass().getName());
    assertEquals(12, computer.getId());
    assertEquals("laptop 12", computer.getName());

    database.begin();
    computer = (Computer) database.load(Computer.class, new Integer(12));
    database.remove(computer);
    database.remove(database.load(ProductDetail.class, new Integer(12)));
    database.remove(database.load(Order.class, new Integer(12)));
    database.commit();

    database.close();
  }