Package org.conserve

Examples of org.conserve.PersistenceManager


    leagues.addKeyWord("submarine");
    leagues.addKeyWord("ocean");
    verne.addBook(leagues);

    // save everything
    PersistenceManager persist = new PersistenceManager(driver, database, login, password);
    persist.saveObject(dickens);
    persist.saveObject(asimov);
    persist.saveObject(verne);
    persist.close();
  }
View Full Code Here


  @Test
  public void testAuthorBookExample1() throws Exception
  {
    this.createAuthors();
    // open a new connection
    PersistenceManager persist = new PersistenceManager(driver, database, login, password);

    // find all science fiction books
    Book seekBook = new Book();
    seekBook.addKeyWord("science fiction");
    List<Book> scifiBooks = persist.getObjects(Book.class, new Equal(seekBook));
    assertEquals(4, scifiBooks.size());
    // print the title of the scifi books
    for (Book book : scifiBooks)
    {
      System.out.println(book.getTitle());
    }

    // find all authors with the first name 'Isaac'.
    Author asimov = new Author();
    asimov.setFirstName("Isaac");
    List<Author> asimovAuthors = persist.getObjects(Author.class, new Equal(asimov));
    assertEquals(1, asimovAuthors.size());

    persist.close();
  }
View Full Code Here

   * Test saving/searching/loading an object that contains itself.
   */
  @Test
  public void testSelfContaining() throws Exception
  {
    PersistenceManager persist = new PersistenceManager(driver, database, login, password);
    SelfContainingObject sco = new SelfContainingObject();
    sco.setSelf(sco);
    persist.saveObject(sco);

    persist = new PersistenceManager(driver, database, login, password);
    List<SelfContainingObject> scos = persist.getObjects(SelfContainingObject.class, new All());
    assertEquals(1, scos.size());
    assertNotNull(scos.get(0).getSelf());

    Layer1 l1 = new Layer1();
    l1.setLayer2(new Layer2());
    l1.getLayer2().setLayer3(new Layer3());
    l1.getLayer2().getLayer3().setLayer1(l1);
    persist.saveObject(l1);
    persist.close();

    persist = new PersistenceManager(driver, database, login, password);

    // it should not be possible to delete l2, since it is part of l1.
    List<Layer2> l2s = persist.getObjects(Layer2.class, new All());
    assertEquals(1, l2s.size());
    persist.deleteObjects(Layer2.class, new All());
    l2s = persist.getObjects(Layer2.class, new All());
    assertEquals(1, l2s.size());
    // it should not be possible to delete l3, since it is part of l2.
    List<Layer3> l3s = persist.getObjects(Layer3.class, new All());
    assertEquals(1, l3s.size());
    persist.deleteObjects(Layer3.class, new All());
    l3s = persist.getObjects(Layer3.class, new All());
    assertEquals(1, l3s.size());
    // it should be possible to delete l1.
    List<Layer1> l1s = persist.getObjects(Layer1.class, new All());
    assertEquals(1, l1s.size());
    persist.deleteObjects(Layer1.class, new All());
    l1s = persist.getObjects(Layer1.class, new All());
    assertEquals(0, l1s.size());
    // deleting l1 should remove l2 and l3.
    l3s = persist.getObjects(Layer3.class, new All());
    assertEquals(0, l3s.size());
    l2s = persist.getObjects(Layer2.class, new All());
    assertEquals(0, l2s.size());

    persist.close();

  }
View Full Code Here

   */
  @Test
  public void testDeleteSelfContaining() throws Exception
  {

    PersistenceManager persist = new PersistenceManager(driver, database, login, password);

    Layer1 l1 = new Layer1();
    l1.setName("foo bar");
    l1.setLayer2(new Layer2());
    l1.getLayer2().setLayer3(new Layer3());
    l1.getLayer2().getLayer3().setLayer1(l1);
    persist.saveObject(l1);
    persist.saveObject(l1.getLayer2());
    persist.close();

    persist = new PersistenceManager(driver, database, login, password);
    // delete all self-containing objects
    persist.deleteObjects(Layer1.class, new All());
    persist.deleteObjects(Layer2.class, new All());
    List<Layer2> objects = persist.getObjects(Layer2.class, new All());

    // make sure no Layer2 objects remain
    assertEquals(0, objects.size());
  }
View Full Code Here

   *
   */
  @Test
  public void testAbstractMemberSearch() throws Exception
  {
    PersistenceManager persist = new PersistenceManager(driver, database, login, password);
    Foo foo = new Foo();
    ConcreteBar1 bar = new ConcreteBar1();
    bar.setAbstractName("abstract");
    bar.setConcreteName("concrete");
    foo.setBar(bar);
    persist.saveObject(foo);
    ConcreteBar2 bar2 = new ConcreteBar2();
    bar2.setAbstractName("f");
    bar2.setConcreteName("C");
    foo.setBar(bar2);
    persist.saveObject(foo);
    persist.close();

    persist = new PersistenceManager(driver, database, login, password);

    // assert that there are no ConcreteBar1 objects
    List<ConcreteBar1> cbar1s = persist.getObjects(ConcreteBar1.class, new All());
    assertEquals(0, cbar1s.size());

    // assert that there is one ConcreteBar2 object
    List<ConcreteBar2> cbar2s = persist.getObjects(ConcreteBar2.class, new All());
    assertEquals(1, cbar2s.size());

    // assert that there is one AbstractBar object
    List<AbstractBar> bars = persist.getObjects(AbstractBar.class, new All());
    assertEquals(1, bars.size());

    // create a Foo search object
    Foo searchFoo = new Foo();

    // assert that the Foo object can be retrieved with a ConcreteBar2
    // object.
    ConcreteBar2 cbar2 = new ConcreteBar2();
    cbar2.setAbstractName("f");
    cbar2.setConcreteName("C");
    searchFoo.setBar(cbar2);
    List<Foo> foos = persist.getObjects(Foo.class, new Equal(searchFoo));
    assertEquals(1, foos.size());

    // assert that the Foo object can be retrieved with a ConcreteBar1
    // object.
    ConcreteBar1 cbar1 = new ConcreteBar1();
    cbar1.setAbstractName("f");
    cbar1.setConcreteName("C");
    cbar1.setFooProperty("foo");
    searchFoo.setBar(cbar1);
    foos = persist.getObjects(Foo.class, new Equal(searchFoo, false));
    assertEquals(1, foos.size());

    // assert that the Foo object can not be retrieved with incorrect values
    cbar1.setAbstractName("fail");
    searchFoo.setBar(cbar1);
    foos = persist.getObjects(Foo.class, new Equal(searchFoo, false));
    assertEquals(0, foos.size());

    persist.close();
  }
View Full Code Here

   * @throws Exception
   */
  @Test
  public void testComplexArrayMatching() throws Exception
  {
    PersistenceManager persist = new PersistenceManager(driver, database, login, password);
    // create a complex object with an Integer array as object
    SimplestObject[] array = new SimplestObject[] { new SimplestObject(), new SimplestObject(),
        new SimplestObject() };
    for (int x = 0; x < array.length; x++)
    {
      array[x].setFoo((double) (x + 1));
    }
    ComplexObject co = new ComplexObject();
    co.setObject(array);
    persist.saveObject(co);
    // close the persistence object
    persist.close();

    // re-open the persistence object
    persist = new PersistenceManager(driver, database, login, password);
    // create an integer array where all entries but one is null
    SimplestObject[] searchArray = new SimplestObject[3];
    searchArray[2] = new SimplestObject();
    searchArray[2].setFoo(2.0);
    // make sure this matches no results
    ComplexObject search = new ComplexObject();
    search.setObject(searchArray);
    List<ComplexObject> none = persist.getObjectsMatching(search);
    assertEquals(0, none.size());
    // change the search so that one object is returned
    searchArray[1] = new SimplestObject();
    searchArray[1].setFoo(2.0);
    searchArray[2].setFoo(3.0);
    List<ComplexObject> one = persist.getObjectsMatching(search);
    assertEquals(1, one.size());

    persist.close();
  }
View Full Code Here

   * @throws Exception
   */
  @Test
  public void testArrayOfArraysMatching() throws Exception
  {
    PersistenceManager persist = new PersistenceManager(driver, database, login, password);
    // create a complex object with an Integer array as object
    SimplestObject[][] array = new SimplestObject[][] { {},
        { new SimplestObject(), new SimplestObject(), new SimplestObject() }, {} };
    for (int x = 0; x < array[1].length; x++)
    {
      array[1][x].setFoo((double) (x + 1));
    }
    ComplexObject co = new ComplexObject();
    co.setObject(array);
    persist.saveObject(co);
    // close the persistence object
    persist.close();

    // re-open the persistence object
    persist = new PersistenceManager(driver, database, login, password);
    // create an integer array where all entries but one is null
    SimplestObject[][] searchArray = new SimplestObject[3][3];
    searchArray[1][2] = new SimplestObject();
    searchArray[1][2].setFoo(2.0);
    // make sure this matches no results
    ComplexObject search = new ComplexObject();
    search.setObject(searchArray);
    List<ComplexObject> none = persist.getObjectsMatching(search);
    assertEquals(0, none.size());
    // change the search so that one object is returned
    searchArray[1][1] = new SimplestObject();
    searchArray[1][1].setFoo(2.0);
    searchArray[1][2].setFoo(3.0);
    List<ComplexObject> one = persist.getObjectsMatching(search);
    assertEquals(1, one.size());

    persist.close();

  }
View Full Code Here

  public void testCollectionSave() throws Exception
  {
    ArrayList<Double> foo = new ArrayList<Double>();
    foo.add(6.9);
    foo.add(1.1);
    PersistenceManager persist = new PersistenceManager(driver, database, login, password);
    persist.saveObject(foo);
    List<Object> all = persist.getObjectsMatching(new Object());
    assertEquals(1, all.size());
    persist.close();
  }
View Full Code Here

  public void testCollectionLoad() throws Exception
  {
    ArrayList<Double> foo = new ArrayList<Double>();
    foo.add(6.9);
    foo.add(1.1);
    PersistenceManager persist = new PersistenceManager(driver, database, login, password);
    persist.saveObject(foo);
    persist.close();

    persist = new PersistenceManager(driver, database, login, password);

    List<ArrayList<Object>> result = persist.getObjectsMatching(new ArrayList<Object>());
    assertEquals(1, result.size());
    ArrayList<?> first = result.get(0);
    assertEquals(2, first.size());
    assertEquals(6.9, first.get(0));
    assertEquals(1.1, first.get(1));
    List<Object> all = persist.getObjectsMatching(new Object());
    assertEquals(1, all.size());
    persist.close();

  }
View Full Code Here

  public void testMapSave() throws Exception
  {
    Map<String, Double> map = new HashMap<String, Double>();
    map.put("KEY", 3.0);
    map.put("ANOTHERKEY", 4.0);
    PersistenceManager persist = new PersistenceManager(driver, database, login, password);
    persist.saveObject(map);
    List<Object> all = persist.getObjectsMatching(new Object());
    assertEquals(1, all.size());
    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.