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(); }
public void testReferenceLinkAttribute() { Session ssn = SessionManager.getSession(); DataObject user = ssn.create(getModelName() + ".User"); user.set("id", BigInteger.valueOf(0)); user.set("email", "*****@*****.**"); user.set("firstName", "foo"); user.set("lastNames", "bar"); user.save(); DataObject[] images = new DataObject[2]; for (int i = 0; i < images.length; i++) { images[i] = ssn.create(getModelName() + ".Image"); images[i].set("id", BigInteger.valueOf(i)); byte[] bytes = "This is the image.".getBytes(); images[i].set("bytes", bytes); images[i].save(); } // set image user.set("image", images[0]); user.save(); // retrieve and then update caption DataAssociationCursor dac = ((DataAssociation) images[0].get("users")).cursor(); dac.next(); assertNull(dac.getLinkProperty("caption")); assertEquals(user, dac.getDataObject()); DataObject link = dac.getLink(); link.set("caption", "caption"); link.save(); dac = ((DataAssociation) images[0].get("users")).cursor(); dac.next(); assertEquals("caption", dac.getLinkProperty("caption")); assertEquals(1L, ((DataAssociation) images[0].get("users")).size()); // set new image as image user.set("image", images[1]); user.save(); // check that old image is no longer associated with user assertEquals(0L, ((DataAssociation) images[0].get("users")).size()); // check that new image is associated with user and has no caption dac = ((DataAssociation) images[1].get("users")).cursor(); dac.next(); assertNull(dac.getLinkProperty("caption")); assertEquals(1L, ((DataAssociation) images[1].get("users")).size()); }