Package org.conserve

Examples of org.conserve.PersistenceManager


  {
    String name = "foo";
    int value = 42;
    Object otherObject = new SimpleObject();
    Object redundantObject = new SimplestObject();
    PersistenceManager pm = new PersistenceManager(driver, database, login, password);
    // drop all tables
    pm.dropTable(Object.class);

    // create and store a new value
    OriginalObject oo = new OriginalObject();
    oo.setName(name);
    oo.setValue(value);
    oo.setOtherObject(otherObject);
    oo.setRedundantObject(redundantObject);
    pm.saveObject(oo);
    // rename the table
    pm.changeName(OriginalObject.class, RemovedColumn.class);
    // update schema
    pm.updateSchema(RemovedColumn.class);

    // get all RemovedColumn objects
    List<RemovedColumn> res1 = pm.getObjects(RemovedColumn.class, new All());
    assertEquals(res1.size(), 1);
    // make sure all properties match
    RemovedColumn nn = res1.get(0);
    assertEquals(value, nn.getValue());
    assertEquals(otherObject, nn.getOtherObject());
    assertEquals(name, nn.getName());

    // make sure no SimplestObject entries are left
    List<SimplestObject> simpleRes = pm.getObjects(SimplestObject.class, new All());
    assertEquals(0, simpleRes.size());

    // change everything back
    pm.changeName(RemovedColumn.class, OriginalObject.class);
    pm.updateSchema(OriginalObject.class);

    // get all OriginalObject objects
    List<OriginalObject> res2 = pm.getObjects(OriginalObject.class, new All());
    assertEquals(res2.size(), 1);
    // make sure all properties match
    oo = res2.get(0);
    assertEquals(value, oo.getValue());
    assertEquals(name, oo.getName());
    assertEquals(otherObject, oo.getOtherObject());
    // make sure the column that has been added/removed is now null
    assertNull(oo.getRedundantObject());

    pm.close();
  }
View Full Code Here


  {
    String name = "foo";
    int value = 1;
    Object otherObject = new SimpleObject();
    Object redundantObject = new SimplestObject();
    PersistenceManager pm = new PersistenceManager(driver, database, login, password);
    // drop all tables
    pm.dropTable(Object.class);

    // create original objects
    OriginalObject oo = new OriginalObject();
    oo.setName(name);
    oo.setValue(value);
    oo.setOtherObject(otherObject);
    oo.setRedundantObject(redundantObject);
    pm.saveObject(oo);
    pm.close();

    // change object to long
    pm = new PersistenceManager(driver, database, login, password);
    pm.changeName(OriginalObject.class, ObjectToLong.class);
    pm.close();
    pm = new PersistenceManager(driver, database, login, password);
    pm.updateSchema(ObjectToLong.class);
    pm.close();
    //check that objects exist/not exist
    pm = new PersistenceManager(driver, database, login, password);
    List<ObjectToLong>res = pm.getObjects(ObjectToLong.class, new All());
    assertEquals(1,res.size());
    ObjectToLong tmp = res.get(0);
    assertTrue(tmp.getName().equals(name));
    assertNull(tmp.getOtherObject());
    tmp.setOtherObject(42L);
    pm.saveObject(tmp);
    //check that the dependent object has been dropped
    List<SimpleObject>dependents = pm.getObjects(SimpleObject.class, new All());
    assertEquals(0,dependents.size());
    pm.close();
    // change long to object
    pm = new PersistenceManager(driver, database, login, password);
    pm.changeName(ObjectToLong.class, OriginalObject.class);
    pm.close();
    pm = new PersistenceManager(driver, database, login, password);
    pm.updateSchema(OriginalObject.class);
    pm.close();
    pm = new PersistenceManager(driver, database, login, password);
    List<OriginalObject>res1 = pm.getObjects(OriginalObject.class, new All());
    assertEquals(1,res1.size());
    OriginalObject tmp1 = res1.get(0);
    assertNull(tmp1.getOtherObject());
    tmp1.setOtherObject(new SimpleObject());
    pm.saveObject(tmp1);
    pm.close();
  }
View Full Code Here

  public void testChangeColumnTypeObjectAndSubclass() throws Exception
  {
    String name = "foo";
    Object object = new Object();
    Object subObject = new SimplestObject();
    PersistenceManager pm = new PersistenceManager(driver, database, login, password);
    // drop all tables
    pm.dropTable(Object.class);

    // create original objects
    OriginalObject oo1 = new OriginalObject();
    oo1.setName(name);
    oo1.setValue(1);
    oo1.setOtherObject(object);
    pm.saveObject(oo1);
    OriginalObject oo2 = new OriginalObject();
    oo2.setName(name);
    oo2.setValue(2);
    oo2.setOtherObject(subObject);
    pm.saveObject(oo2);
    pm.close();

    // change object to subclass
    pm = new PersistenceManager(driver, database, login, password);
    pm.changeName(OriginalObject.class, ObjectToSubclass.class);
    pm.close();
    pm = new PersistenceManager(driver, database, login, password);
    pm.updateSchema(ObjectToSubclass.class);
    pm.close();
    //check that objects exist/not exist
    pm = new PersistenceManager(driver, database, login, password);
    ObjectToSubclass src = new ObjectToSubclass();
    src.setValue(1);
    List<ObjectToSubclass>res = pm.getObjects(ObjectToSubclass.class, new Equal(src));
    assertEquals(1,res.size());
    ObjectToSubclass obj = res.get(0);
    assertTrue(obj.getName().equals(name));
    assertNull(obj.getOtherObject());
    src.setValue(2);
    res = pm.getObjects(ObjectToSubclass.class, new Equal(src));
    assertEquals(1,res.size());
    obj = res.get(0);
    assertTrue(obj.getName().equals(name));
    assertNotNull(obj.getOtherObject());
   
    //change subclass to superclass
    pm = new PersistenceManager(driver, database, login, password);
    pm.changeName(ObjectToSubclass.class, OriginalObject.class);
    pm.close();
    pm = new PersistenceManager(driver, database, login, password);
    pm.updateSchema(OriginalObject.class);
    List<OriginalObject>res2 = pm.getObjects(OriginalObject.class, new All());
    assertEquals(2,res2.size());
    pm.close();
  }
View Full Code Here

  {
    String name = "foo";
    int value = 1;
    Object otherObject = new SimpleObject();
    Object redundantObject = new SimplestObject();
    PersistenceManager pm = new PersistenceManager(driver, database, login, password);
    // drop all tables
    pm.dropTable(Object.class);

    // create original objects
    OriginalObject oo = new OriginalObject();
    oo.setName(name);
    oo.setValue(value);
    oo.setOtherObject(otherObject);
    oo.setRedundantObject(redundantObject);
    pm.saveObject(oo);
    pm.close();

    // change int to long
    pm = new PersistenceManager(driver, database, login, password);
    pm.changeName(OriginalObject.class, IntToLong.class);
    pm.close();
    pm = new PersistenceManager(driver, database, login, password);
    pm.updateSchema(IntToLong.class);
    pm.close();
    //check that the integer has been changed into a long
    pm = new PersistenceManager(driver, database, login, password);
    List<IntToLong>res1 = pm.getObjects(IntToLong.class,new All());
    assertEquals(1,res1.size());
    IntToLong obj1 = res1.get(0);
    assertTrue(obj1.getName().equals(name));
    assertEquals(1,obj1.getValue());
    pm.close();
   
    //change long to int
    pm = new PersistenceManager(driver, database, login, password);
    pm.changeName(IntToLong.class, OriginalObject.class);
    pm.close();
    pm = new PersistenceManager(driver, database, login, password);
    pm.updateSchema(OriginalObject.class);
    pm.close();
    //check that the long is now null, since long can not fit in int
    pm = new PersistenceManager(driver, database, login, password);
    List<OriginalObject>res2 = pm.getObjects(OriginalObject.class,new All());
    assertEquals(1,res2.size());
    OriginalObject obj2 = res2.get(0);
    assertNull(obj2.getValue());
    pm.close();
   
  }
View Full Code Here

   * @throws Exception
   */
  @Test
  public void testBlobClob() throws Exception
  {
    PersistenceManager persist = new PersistenceManager(driver, database, login, password);
    BlobClobObject bco = new BlobClobObject();
    bco.setBytes(new byte[] { 3, 54, 1, 9 });
    bco.setChars(new char[] { 'a', 'b', 'c' });
    persist.saveObject(bco);

    // re-open the persistence object
    persist.close();
    persist = new PersistenceManager(driver, database, login, password);
    // make sure countworks for CLOB/BLOB objects
    long clobCount = persist.getCount(new BlobClobObject());
    assertEquals(1, clobCount);
    // get the one and only bco object
    List<BlobClobObject> all = persist.getObjectsMatching(new BlobClobObject());
    assertEquals(1, all.size());
    BlobClobObject first = all.get(0);
    assertEquals(bco.getBytes().length, first.getBytes().length);
    assertEquals(bco.getChars().length, first.getChars().length);
    for (int x = 0; x < bco.getChars().length; x++)
    {
      assertEquals(bco.getChars()[x], first.getChars()[x]);
    }
    for (int x = 0; x < bco.getBytes().length; x++)
    {
      assertEquals(bco.getBytes()[x], first.getBytes()[x]);
    }
    persist.close();
  }
View Full Code Here

   * @throws Exception
   */
  @Test
  public void testSortingSimple() throws Exception
  {
    PersistenceManager persist = new PersistenceManager(driver, database, login, password);
    ConnectionWrapper cw = persist.getConnectionWrapper();
    // add a large number of entries
    for (int x = 0; x < 200; x++)
    {
      SimplestObject so = new SimplestObject();
      so.setFoo((double) x);
      persist.saveObject(cw, so);
    }
    cw.commitAndDiscard();
    // make sure all entries have been added
    List<SimplestObject> list = persist.getObjectsMatching(new SimplestObject());
    assertEquals(200, list.size());
    // select using sorting
    SimplestObject orderObject = new SimplestObject();
    orderObject.setFoo(0.0);
    list = persist.getObjects(SimplestObject.class, new Descending(orderObject));
    assertEquals(200, list.size());
    assertEquals((Double) 199.0, list.get(0).getFoo());
    list = persist.getObjects(SimplestObject.class, new Ascending(orderObject));
    assertEquals(200, list.size());
    assertEquals((Double) 0.0, list.get(0).getFoo());

    persist.close();
  }
View Full Code Here

   * @throws Exception
   */
  @Test
  public void testSortingComplex() throws Exception
  {
    PersistenceManager persist = new PersistenceManager(driver, database, login, password);
    ConnectionWrapper cw = persist.getConnectionWrapper();
    // add a large number of entries
    for (int x = 0; x < 200; x++)
    {
      SimplestObject so = new SimplestObject();
      so.setFoo((double) x);
      ComplexObject co = new ComplexObject();
      co.setSimplestObject(so);
      persist.saveObject(cw, co);
    }
    cw.commitAndDiscard();
    // make sure all entries have been added
    List<ComplexObject> list = persist.getObjectsMatching(new ComplexObject());
    assertEquals(200, list.size());
    // select using sorting
    SimplestObject simpleObject = new SimplestObject();
    simpleObject.setFoo(0.0);
    ComplexObject orderObject = new ComplexObject();
    orderObject.setSimplestObject(simpleObject);

    list = persist.getObjects(ComplexObject.class, new Descending(orderObject));
    assertEquals(200, list.size());
    assertEquals((Double) 199.0, list.get(0).getSimplestObject().getFoo());
    list = persist.getObjects(ComplexObject.class, new Ascending(orderObject));
    assertEquals(200, list.size());
    assertEquals((Double) 0.0, list.get(0).getSimplestObject().getFoo());

    persist.close();

  }
View Full Code Here

   * @throws Exception
   */
  @Test
  public void testLimitSimple() throws Exception
  {
    PersistenceManager persist = new PersistenceManager(driver, database, login, password);
    ConnectionWrapper cw = persist.getConnectionWrapper();
    // add a large number of entries
    for (int x = 0; x < 200; x++)
    {
      SimplestObject so = new SimplestObject();
      so.setFoo((double) x);
      persist.saveObject(cw, so);
    }
    cw.commitAndDiscard();
    // make sure all entries have been added
    List<SimplestObject> list = persist.getObjectsMatching(new SimplestObject());
    assertEquals(200, list.size());
    // select using sorting
    SimplestObject orderObject = new SimplestObject();
    orderObject.setFoo(0.0);
    list = persist.getObjects(SimplestObject.class, new Order(20, 10, new Ascending(orderObject)));
    assertEquals(20, list.size());
    assertEquals((Double) 10.0, list.get(0).getFoo());
    list = persist.getObjects(SimplestObject.class, new Order(45, new Ascending(orderObject)));
    assertEquals(45, list.size());
    assertEquals((Double) 0.0, list.get(0).getFoo());

    persist.close();
  }
View Full Code Here

   *
   */
  @Test
  public void testComplexQuery() throws Exception
  {
    PersistenceManager persist = new PersistenceManager(driver, database, login, password);
    ConnectionWrapper cw = persist.getConnectionWrapper();
    // add a large number of entries
    for (int x = 1; x <= 100; x++)
    {
      SimplestObject so = new SimplestObject();
      so.setFoo((double) x);
      persist.saveObject(cw, so);
    }
    cw.commitAndDiscard();
    persist.close();
    persist = new PersistenceManager(driver, database, login, password);
    // construct a query that returns the objects with where foo is 3,66 and
    // 99
    SimplestObject different = new SimplestObject(100.0);
    SimplestObject larger = new SimplestObject(98.0);
    SimplestObject equal = new SimplestObject(66.0);
    SimplestObject smallerOrEquals = new SimplestObject(3.0);
    SimplestObject largerOrEquals = new SimplestObject(3.0);

    And one = new And(new GreaterOrEqual(largerOrEquals), new LessOrEqual(smallerOrEquals));
    And two = new And(new Different(different), new Greater(larger));
    List<SimplestObject> list = persist.getObjects(SimplestObject.class, new Or(one, two, new Equal(equal)));
    assertEquals(3, list.size());
    list = persist.getObjects(SimplestObject.class, new Or(one, two, new Equal(equal)), new Ascending(different));
    assertEquals(3, list.size());
    assertEquals(3.0, (double) list.get(0).getFoo(), 0.0001);
    assertEquals(66.0, (double) list.get(1).getFoo(), 0.0001);
    assertEquals(99.0, (double) list.get(2).getFoo(), 0.0001);

    persist.close();
  }
View Full Code Here

    cavesofsteel.addKeyWord("robot");
    cavesofsteel.addKeyWord("crime");
    asimov.addBook(cavesofsteel);

    // save everything
    PersistenceManager persist = new PersistenceManager(driver, database, login, password);
    persist.saveObject(asimov);
    persist.close();

    // open a new connection
    persist = new PersistenceManager(driver, database, login, password);

    // find all books with the authors last name 'Asimov'.
    asimov = new Author();
    asimov.setLastName("Asimov");
    Book seekBook = new Book();
    seekBook.addAuthor(asimov);
    List<Book> asimovBooks = persist.getObjects(Book.class, new Equal(seekBook));
    assertEquals(2, asimovBooks.size());
    // print the title of the scifi books
    for (Book book : asimovBooks)
    {
      System.out.println(book.getTitle());
    }

    persist.close();
  }
View Full Code Here

TOP

Related Classes of org.conserve.PersistenceManager

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.