Esempio n. 1
0
  public void testArticleImageLink() {
    Session ssn = SessionManager.getSession();
    DataObject article = ssn.create(getModel() + ".Article");
    article.set("id", BigInteger.ZERO);
    String text = "This is the article text.";
    article.set("text", text);

    for (int i = 0; i < 10; i++) {
      DataObject image = ssn.create(getModel() + ".Image");
      image.set("id", new BigInteger(Integer.toString(i)));
      byte[] bytes = "This is the image.".getBytes();
      image.set("bytes", bytes);
      image.save();
    }

    DataAssociation links = (DataAssociation) article.get("images");
    DataCollection images = ssn.retrieve(getModel() + ".Image");
    while (images.next()) {
      DataObject image = images.getDataObject();
      DataObject link = ssn.create(getModel() + ".ArticleImageLink");
      link.set("article", article);
      link.set("image", image);
      link.set("caption", "The caption for: " + image.getOID());
      links.add(link);
    }

    article.save();

    DataAssociationCursor cursor = links.cursor();
    assertEquals(10, cursor.size());

    DataCollection aiLinks = ssn.retrieve(getModel() + ".ArticleImageLink");
    aiLinks.addEqualsFilter("image.id", new BigDecimal(5));
    if (aiLinks.next()) {
      DataObject linkArticle = (DataObject) aiLinks.get("article");
      DataObject linkImage = (DataObject) aiLinks.get("image");
      String caption = (String) aiLinks.get("caption");
      assertEquals(BigInteger.valueOf(0), linkArticle.get("id"));
      assertEquals(BigInteger.valueOf(5), linkImage.get("id"));

      if (aiLinks.next()) {
        fail("too many rows");
      }
    } else {
      fail("no rows returned");
    }

    article.delete();
  }