@Before
 public void before() {
   TestTools.createDb();
   TestTools.defineSchema(TestClass.class);
   pm = TestTools.openPM();
   pm.currentTransaction().begin();
 }
Exemple #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();
      }
    }
  }
  /**
   * 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();
  }
 @BeforeClass
 public static void beforeClass() {
   TestTools.removeDb();
   TestTools.createDb();
   TestTools.defineSchema(ComplexHolder0.class, ComplexHolder1.class, ComplexHolder2.class);
   TestTools.defineIndex(ComplexHolder0.class, "id", false);
 }
  /**
   * Tests extent across transaction boundaries. The problem here is that extents are pos-indices,
   * which are COWed. So during a commit, object may get rewritten, changing the index, meaning that
   * the positions in the extents are wrong.
   *
   * <p>In this test we simply check that commits do not affect the extent if the index is not
   * modified.
   */
  @Test
  public void testExtentAcrossCommit() {
    int N = 10000;
    PersistenceManager pm = TestTools.openPM();
    // pm.setIgnoreCache(false);
    pm.currentTransaction().begin();

    for (int i = 0; i < N; i++) {
      TestClass tc = new TestClass();
      tc.setInt(i);
      pm.makePersistent(tc);
    }

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

    // modify all --> create empty space in the beginning of the DB
    for (TestClass tc : pm.getExtent(TestClass.class)) {
      tc.setLong(12);
    }

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

    // start iterating
    Iterator<TestClass> it = pm.getExtent(TestClass.class).iterator();
    int n = 0;
    while (n < N / 2 && it.hasNext()) {
      n++;
      it.next();
    }

    // commit, this should invalidate the first extent
    pm.currentTransaction().commit();
    pm.currentTransaction().begin();

    try {
      it.hasNext();
      fail();
    } catch (JDOUserException e) {
      // good
    }

    try {
      it.next();
      fail();
    } catch (JDOUserException e) {
      // good
    }

    pm.currentTransaction().commit();
    TestTools.closePM();
  }
  @Test
  public void testEmptyDB() {
    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);
    im.readDB(TestTools.getDbName());
  }
  @BeforeClass
  public static void setUp() {
    TestTools.createDb(DB_NAME);
    TestTools.defineSchema(DB_NAME, JB0.class);
    TestTools.defineSchema(DB_NAME, JB1.class);
    TestTools.defineSchema(DB_NAME, JB2.class);
    TestTools.defineSchema(DB_NAME, JB3.class);
    TestTools.defineSchema(DB_NAME, JB4.class);

    PersistenceManager pm = TestTools.openPM();
    pm.currentTransaction().begin();

    for (int i = 1; i <= COUNT; i++) {
      JB0 jb;
      jb = new JB4(4, 4, i, 4, 4);
      pm.makePersistent(jb);
      jb = new JB3(3, 3, i, 3);
      pm.makePersistent(jb);
      jb = new JB2(2, 2, i);
      pm.makePersistent(jb);
      jb = new JB1(1, 2);
      pm.makePersistent(jb);
      jb = new JB0(0);
      pm.makePersistent(jb);
    }

    pm.currentTransaction().commit();
    TestTools.closePM(pm);
  }
  @Test
  public void testSimpleClassesToFile() {
    // populate
    populateSimple();
    String file = System.getProperty("user.home") + File.separator + FILE;
    File f = new File(file);
    if (f.exists()) {
      f.delete();
    }

    ZooXmlExport.main(new String[] {TestTools.getDbName(), file});

    ZooXmlImport.main(new String[] {TestTools.getDbName(), file});
  }
  private void populate() {
    PersistenceManager pm = TestTools.openPM();
    pm.currentTransaction().begin();
    for (int i = 0; i < N; i++) {
      ComplexHolder1 h = new ComplexHolder1();
      h.setId(YEARS_MIN + i / YEARS);
      pm.makePersistent(h);
      ComplexHolder2 h2 = new ComplexHolder2();
      h2.setId(YEARS_MIN + i / YEARS);
      pm.makePersistent(h2);
    }

    pm.currentTransaction().commit();
    TestTools.closePM();
  }
  @Test
  public void testBarcelonaQueryJB4Value() {
    // System.out.println("Testing Query()");
    PersistenceManager pm = TestTools.openPM();
    pm.currentTransaction().begin();

    // special:
    // - uses "this."
    // - uses param from superclass
    String filter = "this.b4 <= param";
    Query query = pm.newQuery(JB4.class, filter);
    query.declareParameters("int param");
    assertEquals(COUNT, doQuery(query, 4));

    TestTools.closePM(pm);
  }
  /** 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();
  }
  /** Test import of ZooDB 0.3 xml files. */
  @Test
  public void testImport0_3() {
    String path = Test_014_XmlImportExport.class.getResource("XmlComplexTest.xml").getPath();

    // import to DB
    TestTools.defineSchema(TestSerializer.class, TestSuper.class, DBLargeVector.class);
    ZooXmlImport.main(new String[] {TestTools.getDbName(), path});

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

    // Check for content in target
    Extent<TestSerializer> ext = pm2.getExtent(TestSerializer.class);
    TestSerializer ts2 = ext.iterator().next();
    ts2.check(false);
    pm2.currentTransaction().rollback();
    TestTools.closePM();
  }
  @Test
  public void testBarcelonaQueryJB4() {
    // System.out.println("Testing Query()");
    PersistenceManager pm = TestTools.openPM();
    pm.currentTransaction().begin();

    // special:
    // - uses "this."
    // - uses param from superclass
    // - asks for a field that is not present in all super classes
    String filter = "this.b2 == param";
    for (int i = 1; i <= COUNT; i++) {
      Query query = pm.newQuery(JB4.class, filter);
      query.declareParameters("int param");
      assertEquals(1, doQuery(query, i));
    }

    TestTools.closePM(pm);
  }
  @Test
  public void testCallBack() {
    PersistenceManager pm = TestTools.openPM();
    pm.currentTransaction().begin();

    TestPreStore ps = new TestPreStore();
    TestPostLoad pl = new TestPostLoad();
    TestPreClear pc = new TestPreClear();
    TestPreDelete pd = new TestPreDelete();

    pm.makePersistent(ps);
    pm.makePersistent(pl);
    pm.makePersistent(pc);
    pm.makePersistent(pd);

    assertFalse(TestPreStore.storeCalled);
    assertFalse(TestPostLoad.loadCalled);
    assertFalse(TestPreClear.clearCalled);
    assertFalse(TestPreDelete.deleteCalled);

    pm.currentTransaction().commit();

    assertTrue(TestPreStore.storeCalled);
    assertTrue(TestPreClear.clearCalled);
    assertFalse(TestPostLoad.loadCalled);
    assertFalse(TestPreDelete.deleteCalled);

    pm.currentTransaction().begin();

    pl.getI();
    assertTrue(TestPostLoad.loadCalled);

    pm.deletePersistent(pd);
    assertFalse(TestPreDelete.deleteCalled);

    pm.currentTransaction().commit();
    assertTrue(TestPreDelete.deleteCalled);

    TestTools.closePM();
  }
Exemple #15
0
  @Test
  public void testCountPerYear() {
    populate();

    PersistenceManager pm = TestTools.openPM();
    pm.currentTransaction().begin();

    int result = 0;

    long t0 = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
      for (int i2 = 1945; i2 < 1980; i2 += 5) {
        Map<?, ?> m = countPerYears(pm, (long) i2 - 5, (long) i2);
        result += m.size();
      }
    }
    long t1 = System.currentTimeMillis();

    System.out.println("t=" + (t1 - t0));
    System.out.println("result=" + result);

    pm.currentTransaction().commit();
    TestTools.closePM();
  }
 public void open() {
   pm = TestTools.openPM();
 }
 @After
 public void after() {
   TestTools.closePM();
   TestTools.removeDb();
 }
 @Before
 public void before() {
   TestTools.createDb();
   TestSerializer.resetStatic();
 }
  @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();
  }
 @AfterClass
 public static void afterClass() {
   TestTools.removeDb();
 }
 @Before
 public void beforeTest() {
   TestTools.createDb();
   TestTools.defineSchema(TestClass.class, TestClassTiny.class, TestClassTiny2.class);
 }
 @AfterClass
 public static void tearDown() {
   TestTools.removeDb();
 }
 @BeforeClass
 public static void setUp() {
   TestTools.createDb();
   TestTools.defineSchema(TestClass.class, TestClassTiny.class, TestClassTiny2.class);
 }
 @BeforeClass
 public static void setUp() {
   TestTools.createDb();
   TestTools.defineSchema(JB0.class, JB1.class, JB2.class, JB3.class, JB4.class, JdoTree.class);
 }
 @BeforeClass
 public static void beforeClass() {
   TestTools.createDb();
 }
  private void populateSimple() {
    TestTools.defineSchema(TestClass.class);
    PersistenceManager pm = TestTools.openPM();
    pm.currentTransaction().begin();

    pm.newQuery(TestClass.class).deletePersistentAll();

    TestClass tc1 = new TestClass();
    tc1.setData(
        1, false, 'c', (byte) 127, (short) 32000, 1234567890L, "xyz", new byte[] {1, 2}, -1.1f, 35);
    pm.makePersistent(tc1);
    tc1 = new TestClass();
    tc1.setData(
        12,
        false,
        'd',
        (byte) 127,
        (short) 32000,
        1234567890L,
        "xyz",
        new byte[] {1, 2},
        -0.1f,
        34);
    pm.makePersistent(tc1);
    tc1 = new TestClass();
    tc1.setData(
        123,
        false,
        'e',
        (byte) 127,
        (short) 32000,
        1234567890L,
        "xyz",
        new byte[] {1, 2},
        0.1f,
        3.0);
    pm.makePersistent(tc1);
    tc1 = new TestClass();
    tc1.setData(
        1234,
        false,
        'f',
        (byte) 127,
        (short) 32000,
        1234567890L,
        "xyz",
        new byte[] {1, 2},
        1.1f,
        -0.01);
    pm.makePersistent(tc1);
    tc1 = new TestClass();
    tc1.setData(
        12345,
        false,
        'g',
        (byte) 127,
        (short) 32000,
        1234567890L,
        "xyz",
        new byte[] {1, 2},
        11.1f,
        -35);
    pm.makePersistent(tc1);

    pm.currentTransaction().commit();
    TestTools.closePM();
    ;
  }
 @After
 public void afterTest() {
   TestTools.closePM();
 }
 public void close() {
   TestTools.closePM();
   pm = null;
 }
  /**
   * Tests extent across transaction boundaries. The problem here is that extents are pos-indices,
   * which are COWed. So during a commit, object may get rewritten, changing the index, meaning that
   * the positions in the extents are wrong.
   *
   * <p>In this test, we iterate over the extent and delete previous objects, but never ahead of the
   * extent. That should work fine.
   */
  @Test
  public void testExtentDeletionAcrossCommit() {
    int N = 10000;
    PersistenceManager pm = TestTools.openPM();
    // pm.setIgnoreCache(false);
    pm.currentTransaction().begin();

    for (int i = 0; i < N; i++) {
      TestClass tc = new TestClass();
      tc.setInt(i);
      pm.makePersistent(tc);
    }

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

    // modify all --> create empty space in the beginning of the DB
    for (TestClass tc : pm.getExtent(TestClass.class)) {
      tc.setLong(12);
    }

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

    // start iterating
    Iterator<TestClass> it = pm.getExtent(TestClass.class).iterator();
    int n = 0;
    int currentI = -1;
    while (it.hasNext()) {
      n++;
      TestClass tc = it.next();
      assertEquals(currentI + 1, tc.getInt());
      currentI = tc.getInt();
      pm.deletePersistent(tc);
      if (n % 1000 == 0) {
        pm.currentTransaction().commit();
        pm.currentTransaction().begin();
        break;
      }
    }

    try {
      it.hasNext();
      fail();
    } catch (JDOUserException e) {
      // good
    }

    try {
      it.next();
      fail();
    } catch (JDOUserException e) {
      // good
    }

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

    // Assert that not all have been removed
    it = pm.getExtent(TestClass.class).iterator();
    assertTrue(it.hasNext());
    while (it.hasNext()) {
      n++;
      TestClass tc = it.next();
      assertEquals(currentI + 1, tc.getInt());
      currentI = tc.getInt();
      pm.deletePersistent(tc);
    }
    assertFalse(pm.getExtent(TestClass.class).iterator().hasNext());

    assertEquals(N, n);
    assertEquals(N, currentI + 1);

    pm.currentTransaction().commit();
    TestTools.closePM();
  }
 @After
 public void after() {
   pm.currentTransaction().rollback();
   TestTools.closePM();
   pm = null;
 }