/**
   * Test serialisation.
   *
   * <p>If a transaction has only new objects (no modified objects) and if the new objects have
   * other persistent capable objects referenced (but no have called makePersistent() on them) and
   * if we are not in the first transaction in a session, then we get the following NPE. See issue
   * #57.
   */
  @Test
  public void testSerialization() {
    TestTools.defineSchema(ProjectPC.class, CoordinatePC.class);
    PersistenceManager pm = TestTools.openPM();
    pm.currentTransaction().begin();

    CoordinatePC x = new CoordinatePC();
    pm.makePersistent(x);

    pm.currentTransaction().commit();
    pm.currentTransaction().begin();

    ProjectPC s = new ProjectPC(new ProjectPC(null));
    pm.makePersistent(s);
    Object oid = pm.getObjectId(s);
    pm.currentTransaction().commit();
    TestTools.closePM();

    pm = TestTools.openPM();
    pm.currentTransaction().begin();
    ProjectPC s2 = (ProjectPC) pm.getObjectById(oid);
    assertNotNull(s2);
    pm.currentTransaction().commit();
    pm.close();
  }
Ejemplo n.º 2
0
  /** test batch loading. */
  @SuppressWarnings("unchecked")
  @Test
  public void testBatchLoading2() {
    System.out.println("Batch-test 2");
    PersistenceManager pm = null;
    Object oid = null;
    try {
      pm = TestTools.openPM();
      pm.currentTransaction().begin();
      DBArrayList<Object> dbv = new DBArrayList<Object>();
      for (int i = 0; i < 120; i++) {
        dbv.add(new PersistentDummyImpl());
      }
      pm.makePersistent(dbv);
      oid = pm.getObjectId(dbv);
      pm.currentTransaction().commit();
      pm.close();
      pm = null;

      pm = TestTools.openPM();
      pm.currentTransaction().begin();
      dbv = (DBArrayList<Object>) pm.getObjectById(oid);
      dbv.setBatchSize(110);
      for (Object o : dbv) {
        o.getClass();
      }
      pm.currentTransaction().commit();
    } finally {
      if (pm != null) {
        pm.close();
      }
    }
  }
  /** This used to fail because deserialized DbCollection were marked dirty. */
  @SuppressWarnings("unchecked")
  @Test
  public void testSerialization() {
    PersistenceManager pm = TestTools.openPM();
    pm.currentTransaction().begin();
    DBHashMap<TestClass, TestClass> h1 = new DBHashMap<TestClass, TestClass>();
    DBHashMap<TestClass, TestClass> h2 = new DBHashMap<TestClass, TestClass>();
    TestClass k = new TestClass();
    h2.put(k, new TestClass());
    DBArrayList<TestClass> l1 = new DBArrayList<TestClass>();
    DBArrayList<TestClass> l2 = new DBArrayList<TestClass>();
    l2.add(new TestClass());
    pm.makePersistent(h1);
    pm.makePersistent(h2);
    pm.makePersistent(k);
    pm.makePersistent(l1);
    pm.makePersistent(l2);
    Object oid1 = pm.getObjectId(h1);
    Object oid2 = pm.getObjectId(h2);
    Object oidk = pm.getObjectId(k);
    Object oid3 = pm.getObjectId(l1);
    Object oid4 = pm.getObjectId(l2);
    pm.currentTransaction().commit();
    TestTools.closePM();

    pm = TestTools.openPM();
    pm.currentTransaction().begin();
    h1 = (DBHashMap<TestClass, TestClass>) pm.getObjectById(oid1);
    assertEquals(false, JDOHelper.isDirty(h1));
    h2 = (DBHashMap<TestClass, TestClass>) pm.getObjectById(oid2);
    assertEquals(false, JDOHelper.isDirty(h2));
    k = (TestClass) pm.getObjectById(oidk);
    assertEquals(false, JDOHelper.isDirty(k));
    assertEquals(false, JDOHelper.isDirty(h2.get(k)));

    l1 = (DBArrayList<TestClass>) pm.getObjectById(oid3);
    assertEquals(false, JDOHelper.isDirty(l1));
    l2 = (DBArrayList<TestClass>) pm.getObjectById(oid4);
    assertEquals(false, JDOHelper.isDirty(l2));
    assertEquals(false, JDOHelper.isDirty(l2.get(0)));

    pm.currentTransaction().commit();
    pm.close();
  }
Ejemplo n.º 4
0
  @Test
  public void testComplexClass() {
    // populate
    Object oid = null;
    TestTools.defineSchema(TestSerializer.class, TestSuper.class, DBLargeVector.class);
    PersistenceManager pm = TestTools.openPM();
    pm.currentTransaction().begin();
    TestSerializer ts1 = new TestSerializer();
    ts1.init();
    ts1.check(true);
    pm.makePersistent(ts1);
    oid = pm.getObjectId(ts1);
    pm.currentTransaction().commit();
    TestTools.closePM();
    pm = null;

    TestSerializer.resetStatic();

    // export to XML
    StringWriter out = new StringWriter();
    ZooXmlExport ex = new ZooXmlExport(out);
    ex.writeDB(TestTools.getDbName());
    System.out.println(out.getBuffer());
    Scanner sc = new Scanner(new StringReader(out.getBuffer().toString()));
    ZooXmlImport im = new ZooXmlImport(sc);

    // import to new DB
    DataStoreManager dsm = ZooHelper.getDataStoreManager();
    if (dsm.dbExists(DB2)) {
      dsm.removeDb(DB2);
    }
    dsm.createDb(DB2);
    TestTools.defineSchema(DB2, TestSerializer.class, TestSuper.class, DBLargeVector.class);
    im.readDB(DB2);

    // check target
    PersistenceManager pm2 = TestTools.openPM(DB2);
    pm2.currentTransaction().begin();

    // Check for content in target
    TestSerializer ts2 = (TestSerializer) pm2.getObjectById(oid, true);
    ts2.check(false);
    pm2.currentTransaction().rollback();
    TestTools.closePM();

    TestSerializer.resetStatic();

    // Now try the same thing again, this time with an existing object.
    pm2 = TestTools.openPM(DB2);
    pm2.currentTransaction().begin();

    TestSerializer ts3 = (TestSerializer) pm2.getObjectById(oid);
    ts3.check(false);
    // mark dirty to enforce rewrite.
    ts3.markDirtyTS();
    pm2.currentTransaction().commit();
    TestTools.closePM();

    TestSerializer.resetStatic();
    // Check target
    pm2 = TestTools.openPM(DB2);
    pm2.currentTransaction().begin();

    TestSerializer ts4 = (TestSerializer) pm2.getObjectById(oid, true);
    ts4.check(false);
    pm2.currentTransaction().rollback();
    TestTools.closePM();
  }
Ejemplo n.º 5
0
  /** Test changing the state */
  public void testEvictAllAndWrites() {
    try {
      Properties multiProps = new Properties();
      multiProps.setProperty("datanucleus.Multithreaded", "true");
      pmf = TestHelper.getPMF(1, multiProps);

      int THREAD_SIZE = 1000;
      Thread[] threads = new Thread[THREAD_SIZE];
      Thread[] threads2 = new Thread[THREAD_SIZE];

      final PersistenceManager pm = pmf.getPersistenceManager();
      pm.currentTransaction().begin();
      final Employee woody =
          new Employee(
              1, "Woody", "Woodpecker", "*****@*****.**", 13, "serial 1", new Integer(10));
      Manager bart = new Manager(2, "Bart", "Simpson", "*****@*****.**", 2, "serial 2");
      final Manager boss = new Manager(3, "Boss", "WakesUp", "*****@*****.**", 4, "serial 3");
      woody.setManager(bart);
      pm.makePersistent(woody);
      final Object id = pm.getObjectId(woody);
      pm.currentTransaction().commit();
      pm.currentTransaction().begin();
      try {
        for (int i = 0; i < THREAD_SIZE; i++) {
          threads[i] =
              new Thread(
                  new Runnable() {
                    public void run() {
                      pm.getObjectById(id, true);
                      woody.setLastName("name");
                      woody.setManager(boss);
                    }
                  });
        }
        for (int i = 0; i < THREAD_SIZE; i++) {
          threads2[i] =
              new Thread(
                  new Runnable() {
                    public void run() {
                      pm.evictAll();
                    }
                  });
        }
        for (int i = 0; i < THREAD_SIZE; i++) {
          threads[i].start();
          threads2[i].start();
        }
        for (int i = 0; i < THREAD_SIZE; i++) {
          try {
            threads[i].join();
            threads2[i].join();
          } catch (InterruptedException e) {
            fail(e.getMessage());
          }
        }
      } finally {
        if (pm.currentTransaction().isActive()) {
          pm.currentTransaction().rollback();
        }
        pm.close();
      }
    } finally {
      CompanyHelper.clearCompanyData(pmf);
    }
  }
Ejemplo n.º 6
0
  /** test batch loading. */
  @SuppressWarnings("unchecked")
  @Test
  public void testBatchLoading() {
    System.out.println("Batch-test");
    PersistenceManager pm = null;
    Object oid = null;

    pm = TestTools.openPM();
    pm.currentTransaction().begin();
    DBArrayList<Object> dbv = new DBArrayList<Object>();
    dbv.add("TestString");
    for (int i = 0; i < 100; i++) {
      dbv.add(new PersistentDummyImpl());
    }
    dbv.add("TestString2");
    pm.makePersistent(dbv);
    oid = pm.getObjectId(dbv);
    pm.currentTransaction().commit();
    pm.close();
    pm = null;

    pm = TestTools.openPM();
    pm.currentTransaction().begin();
    dbv = (DBArrayList<Object>) pm.getObjectById(oid);
    long t1 = System.currentTimeMillis();
    for (Object o : dbv) {
      o.hashCode();
    }
    long t2 = System.currentTimeMillis();
    System.out.println("NORMAL: " + (t2 - t1));
    pm.currentTransaction().commit();
    pm.close();
    pm = null;

    pm = TestTools.openPM();
    pm.currentTransaction().begin();
    dbv = (DBArrayList<Object>) pm.getObjectById(oid);
    t1 = System.currentTimeMillis();
    dbv.setBatchSize(1000);
    for (Object o : dbv) {
      o.hashCode();
    }
    t2 = System.currentTimeMillis();
    System.out.println("BATCHED: " + (t2 - t1));
    pm.currentTransaction().commit();
    pm.close();
    pm = null;

    // Close the store and load the stuff
    pm = TestTools.openPM();
    pm.currentTransaction().begin();
    dbv = (DBArrayList<Object>) pm.getObjectById(oid);
    t1 = System.currentTimeMillis();
    dbv.setBatchSize(1);
    for (Object o : dbv) {
      o.hashCode();
    }
    t2 = System.currentTimeMillis();
    System.out.println("NORMAL: " + (t2 - t1));
    pm.currentTransaction().commit();
    pm.close();
    pm = null;

    // Close the store and load the stuff
    pm = TestTools.openPM();
    pm.currentTransaction().begin();
    dbv = (DBArrayList<Object>) pm.getObjectById(oid);
    t1 = System.currentTimeMillis();
    dbv.setBatchSize(0);
    for (Object o : dbv) {
      o.hashCode();
    }
    t2 = System.currentTimeMillis();
    System.out.println("BATCHED: " + (t2 - t1));
    pm.currentTransaction().commit();
    pm.close();
    pm = null;

    // Close the store, load the stuff and test with transient object
    pm = TestTools.openPM();
    pm.currentTransaction().begin();
    dbv = (DBArrayList<Object>) pm.getObjectById(oid);
    PersistentDummyImpl dummyTrans = new PersistentDummyImpl();
    dbv.add(13, dummyTrans);
    t1 = System.currentTimeMillis();
    dbv.setBatchSize(0);
    for (Object o : dbv) {
      o.hashCode();
    }
    t2 = System.currentTimeMillis();
    assertEquals(dummyTrans, dbv.get(13));
    System.out.println("BATCHED: " + (t2 - t1));
    pm.currentTransaction().commit();
    pm.close();
    pm = null;

    // Close the store, load the stuff and test with modified object
    pm = TestTools.openPM();
    pm.currentTransaction().begin();
    dbv = (DBArrayList<Object>) pm.getObjectById(oid);
    ((PersistentDummyImpl) dbv.get(18)).setData(new byte[] {15});
    t1 = System.currentTimeMillis();
    dbv.setBatchSize(0);
    for (Object o : dbv) {
      o.hashCode();
    }
    t2 = System.currentTimeMillis();
    assertEquals(15, ((PersistentDummyImpl) dbv.get(18)).getData()[0]);
    System.out.println("BATCHED but dirty: " + (t2 - t1));
    pm.currentTransaction().rollback();
    TestTools.closePM();

    // TODO use setBatch() also for all other tests to verify batch loading!
    // Or call these tests here again, outside the store.!
  }