Package org.conserve

Examples of org.conserve.PersistenceManager


  public void testMapLoad() 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);
    persist.close();

    persist = new PersistenceManager(driver, database, login, password);
    List<HashMap<Object, Object>> results = persist.getObjectsMatching(new HashMap<Object, Object>());
    assertEquals(1, results.size());
    HashMap<?, ?> first = results.get(0);
    assertEquals(2, first.size());
    assertEquals(3.0, first.get("KEY"));
    assertEquals(4.0, first.get("ANOTHERKEY"));
    List<Object> all = persist.getObjectsMatching(new Object());
    assertEquals(1, all.size());
    persist.close();
  }
View Full Code Here


    List<String> list = new ArrayList<String>();
    list.add("VALUE");
    list.add("ANOTHER VALUE");
    list.add("THIRD VALUE");
    list.add("FOURTH VALUE");
    PersistenceManager persist = new PersistenceManager(driver, database, login, password);
    persist.saveObject(list);
    List<String> anotherList = new ArrayList<String>();
    anotherList.add("ANOTHER VALUE");
    anotherList.add("SECOND VALUE");
    anotherList.add("THIRD VALUE");
    persist.saveObject(anotherList);
    List<String> yetAnotherList = new ArrayList<String>();
    yetAnotherList.add("VALUE");
    yetAnotherList.add("SOME VALUE");
    persist.saveObject(yetAnotherList);
    persist.close();

    persist = new PersistenceManager(driver, database, login, password);
    // retrieve objects by a sorted object
    List<Collection> allObjects = persist.getObjects(Collection.class, new All());
    assertEquals(3, allObjects.size());

    // find all objects that has the first entry equal to 'VALUE'.
    ArrayList<String> searchList = new ArrayList<String>();
    searchList.add("VALUE");
    List<Collection> searchResults = persist.getObjects(Collection.class, new Equal(searchList));
    // ensure that there are two
    assertEquals(2, searchResults.size());

    // find all objects that has the first entry equal to 'ANOTHER VALUE'
    searchList.clear();
    searchList.add("ANOTHER VALUE");
    searchResults = persist.getObjects(Collection.class, new Equal(searchList));
    // ensure that there is one
    assertEquals(1, searchResults.size());

    // find all objects that has an entry equal to 'ANOTHER VALUE'
    HashSet<String> unorderedSearchSet = new HashSet<String>();
    unorderedSearchSet.add("ANOTHER VALUE");
    searchResults = persist.getObjects(Collection.class, new Equal(unorderedSearchSet));
    // ensure that there are two
    assertEquals(2, searchResults.size());

    persist.close();
  }
View Full Code Here

  {
    // store some hashmaps in the database
    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);
    Map<String, Double> anotherMap = new HashMap<String, Double>();
    anotherMap.put("ANOTHERKEY", 4.0);
    persist.saveObject(anotherMap);
    Map<String, Double> yetAnotherMap = new HashMap<String, Double>();
    yetAnotherMap.put("KEY", 1.0);
    persist.saveObject(yetAnotherMap);
    persist.close();

    persist = new PersistenceManager(driver, database, login, password);
    // get all objects
    List<Map> allObjects = persist.getObjects(Map.class, new All());
    assertEquals(3, allObjects.size());

    // search by map contents
    Map<String, Double> searchMap = new HashMap<String, Double>();
    searchMap.put("KEY", 3.0);
    List<Map> result = persist.getObjects(Map.class, new Equal(searchMap));
    assertEquals(1, result.size());
    searchMap.clear();

    // search for all hashmaps with a given key
    searchMap.put("KEY", null);
    result = persist.getObjects(Map.class, new Equal(searchMap));
    assertEquals(2, result.size());
    searchMap.clear();

    // search for all hashmaps with a given key-value pair
    searchMap.put("ANOTHERKEY", 4.0);
    result = persist.getObjects(Map.class, new Equal(searchMap));
    assertEquals(2, result.size());

    persist.close();

  }
View Full Code Here

   * @throws Exception
   */
  @Test
  public void testUpdateSimple() throws Exception
  {
    PersistenceManager persist = new PersistenceManager(driver, database, login, password);
    // create a simple object
    SimpleObject object = new SimpleObject();
    object.setAge(500L);
    // save it
    persist.saveObject(object);
    // make sure only one object exists
    List<SimpleObject> allObjects = persist.getObjectsMatching(new SimpleObject());
    assertEquals(1, allObjects.size());
    // change the object
    object.setAge(1000L);
    // save the object again
    persist.saveObject(object);
    // make sure there's still only one object
    allObjects = persist.getObjectsMatching(new SimpleObject());
    assertEquals(1, allObjects.size());
    // re-open the database connection
    persist.close();
    persist = new PersistenceManager(driver, database, login, password);
    // make sure the object has been changed
    allObjects = persist.getObjectsMatching(new SimpleObject());
    assertEquals(1, allObjects.size());
    SimpleObject first = allObjects.get(0);
    assertTrue(first.getAge().equals(1000L));

    // add another object
    SimpleObject nuObject = new SimpleObject();
    nuObject.setAge(1L);
    persist.saveObject(nuObject);

    // update the new object
    nuObject.setAge(2L);

    // make sure only one object has been modified
    allObjects = persist.getObjectsMatching(new SimpleObject());
    assertEquals(2, allObjects.size());
    first = allObjects.get(0);
    SimpleObject second = allObjects.get(1);
    assertTrue((first.getAge() == 2L && second.getAge() == 1000L)
        || (first.getAge() == 1000L && second.getAge() == 2L));

    persist.close();

  }
View Full Code Here

   *
   */
  @Test
  public void testUpdateComplex() throws Exception
  {
    PersistenceManager persist = new PersistenceManager(driver, database, login, password);
    // create a complex object
    ComplexObject object = new ComplexObject();
    Integer[] ages = new Integer[] { 1, 2, 3 };
    object.setObject(ages);

    // save it
    persist.saveObject(object);
    // make sure only one object exists
    List<ComplexObject> allObjects = persist.getObjectsMatching(new ComplexObject());
    assertEquals(1, allObjects.size());
    // change the object
    ages[1] = 20;
    // save the object again
    persist.saveObject(object);
    // make sure there's still only one object
    allObjects = persist.getObjectsMatching(new ComplexObject());
    assertEquals(1, allObjects.size());
    // re-open the database connection
    persist.close();
    persist = new PersistenceManager(driver, database, login, password);
    // make sure the object has been changed
    allObjects = persist.getObjectsMatching(new ComplexObject());
    assertEquals(1, allObjects.size());
    ComplexObject first = allObjects.get(0);
    Integer[] databaseAges = (Integer[]) first.getObject();
    assertEquals(1, (int) databaseAges[0]);
    assertEquals(20, (int) databaseAges[1]);
    assertEquals(3, (int) databaseAges[2]);
    assertTrue(databaseAges.length == 3);

    // add another object
    ComplexObject nuObject = new ComplexObject();
    Integer[] nuAges = new Integer[] { 5, 6, 7 };
    nuObject.setObject(nuAges);
    persist.saveObject(nuObject);

    // update the new object
    nuAges[1] = 60;

    // make sure only one object has been modified
    allObjects = persist.getObjectsMatching(new ComplexObject());
    assertEquals(2, allObjects.size());
    first = allObjects.get(0);
    ComplexObject second = allObjects.get(1);
    Integer[] a = (Integer[]) first.getObject();
    Integer[] b = (Integer[]) second.getObject();

    assertTrue((a[0] == 1 && b[0] == 5) || (b[0] == 1 && a[0] == 5));
    assertTrue((a[1] == 20 && b[1] == 60) || (b[1] == 20 && a[1] == 60));
    assertTrue((a[2] == 3 && b[2] == 7) || (b[2] == 3 && a[2] == 7));

    persist.close();
  }
View Full Code Here

   * @throws Exception
   */
  @Test
  public void testCount() 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 the right number of objects are returned
    long count = persist.getCount(new SimplestObject());
    assertEquals(200L, count);
    // select half of the objects
    SimplestObject selectObject = new SimplestObject();
    selectObject.setFoo(99.0);
    Greater g = new Greater(selectObject);
    count = persist.getCount(SimplestObject.class, g);
    assertEquals(100L, count);
    persist.close();
  }
View Full Code Here

   *
   */
  @Test
  public void testGetClasses() throws Exception
  {
    PersistenceManager persist = new PersistenceManager(driver, database, login, password);
    persist.saveObject(new SimpleObject());
    List<Class<?>> classList = persist.getClasses();
    for (Class<?> c : classList)
    {
      System.out.println(ObjectTools.getSystemicName(c));
    }
    // there should be at least 3 classes: SimpleObject, Object and
    // Serializable.
    assertTrue(classList.size() > 2);

    persist.close();
  }
View Full Code Here

   * @throws Exception
   */
  @Test
  public void testJavaDate() throws Exception
  {
    PersistenceManager persist = new PersistenceManager(driver, database, login, password);
    Date date = new Date();
    persist.saveObject(date);

    persist.close();

    persist = new PersistenceManager(driver, database, login, password);
    List<Date> dates = persist.getObjects(Date.class, new LessOrEqual(new Date()));
    assertEquals(1, dates.size());
    assertEquals(date.getTime(), dates.get(0).getTime());
    persist.close();
  }
View Full Code Here

   * @throws Exception
   */
  @Test
  public void testSqlDate() throws Exception
  {
    PersistenceManager persist = new PersistenceManager(driver, database, login, password);
    DateObject dateO = new DateObject();
    dateO.setDate(new java.sql.Date(System.currentTimeMillis()));
    dateO.setTime(new java.sql.Time(System.currentTimeMillis()));
    dateO.setTimeStamp(new java.sql.Timestamp(System.currentTimeMillis()));
    persist.saveObject(dateO);

    // make sure only one object exists
    List<Object> all = persist.getObjectsMatching(new Object());
    assertEquals(1, all.size());

    persist.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 string to long
    pm = new PersistenceManager(driver, database, login, password);
    pm.changeName(OriginalObject.class, StringToLong.class);
    pm.close();
    pm = new PersistenceManager(driver, database, login, password);
    pm.updateSchema(StringToLong.class);
    pm.close();
    //check that the long no longer exist
    pm = new PersistenceManager(driver, database, login, password);
    List<StringToLong>res1 = pm.getObjects(StringToLong.class, new All());
    assertEquals(1,res1.size());
    StringToLong obj1 = res1.get(0);
    assertNull(obj1.getName());
    //save a long
    obj1.setName(1);
    pm.saveObject(obj1);
    pm.close();

    // change long to string
    pm = new PersistenceManager(driver, database, login, password);
    pm.changeName(StringToLong.class, OriginalObject.class);
    pm.close();
    pm = new PersistenceManager(driver, database, login, password);
    pm.updateSchema(OriginalObject.class);
    pm.close();
    // check that the string no longer exist
    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.getName());
    pm.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.