001: package org.apache.ojb.broker;
002:
003: import org.apache.ojb.broker.metadata.ClassDescriptor;
004: import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
005: import org.apache.ojb.junit.PBTestCase;
006:
007: /**
008: * This TestClass tests OJB facilities to work with polymorphism.
009: */
010: public class AutomaticForeignKeys extends PBTestCase {
011: public static void main(String[] args) {
012: String[] arr = { AutomaticForeignKeys.class.getName() };
013: junit.textui.TestRunner.main(arr);
014: }
015:
016: public AutomaticForeignKeys(String name)
017:
018: {
019: super (name);
020: }
021:
022: public void setUp() throws Exception {
023: super .setUp();
024: ClassDescriptor cld = broker.getClassDescriptor(Article.class);
025: ObjectReferenceDescriptor ord = (ObjectReferenceDescriptor) cld
026: .getObjectReferenceDescriptors().get(0);
027: ord.setCascadeStore(true);
028: }
029:
030: /** test automatic assignment of foreign keys for 1:1 reference. */
031: public void testOneOneReference() {
032: try {
033: Article art = new Article();
034: art.setArticleName("OJB O/R mapping power");
035: ProductGroup pg = new ProductGroup();
036: pg.setName("Software");
037: art.setProductGroup(pg);
038: Identity artOID = new Identity(art, broker);
039: broker.beginTransaction();
040: broker.store(art);
041: broker.store(pg);
042: broker.commitTransaction();
043: broker.clearCache();
044: InterfaceArticle readInArt = (Article) broker
045: .getObjectByIdentity(artOID);
046: InterfaceProductGroup readInPg = readInArt
047: .getProductGroup();
048: assertEquals(art.getArticleName(), readInArt
049: .getArticleName());
050: assertEquals(pg.getName(), readInPg.getName());
051: } catch (Throwable t) {
052: System.out.println(t.getMessage());
053: t.printStackTrace();
054: fail(t.getMessage());
055: }
056: }
057:
058: /** test automatic assignment of foreign keys for 1:n reference. */
059: public void testOneManyReference() {
060: try {
061: ProductGroup pg = new ProductGroup();
062: pg.setName("O/R mapping tools");
063:
064: Article art1 = new Article();
065: art1.setArticleName("TOPLink");
066: art1.setProductGroup(pg);
067:
068: Article art2 = new Article();
069: art2.setArticleName("OJB");
070: art2.setProductGroup(pg);
071:
072: Article art3 = new Article();
073: art3.setArticleName("CASTOR");
074: art3.setProductGroup(pg);
075:
076: pg.add(art1);
077: pg.add(art2);
078: pg.add(art3);
079:
080: Identity pgOID = new Identity(pg, broker);
081: broker.beginTransaction();
082: broker.store(pg);
083: broker.commitTransaction();
084: broker.clearCache();
085: InterfaceProductGroup readInPG = (InterfaceProductGroup) broker
086: .getObjectByIdentity(pgOID);
087: assertEquals(pg.getName(), readInPG.getName());
088: assertEquals(pg.getAllArticles().size(), readInPG
089: .getAllArticles().size());
090: InterfaceArticle art1a = (InterfaceArticle) pg
091: .getAllArticles().get(0);
092: InterfaceArticle art2a = (InterfaceArticle) pg
093: .getAllArticles().get(1);
094: InterfaceArticle art3a = (InterfaceArticle) pg
095: .getAllArticles().get(2);
096: assertEquals(art1.getArticleName(), art1a.getArticleName());
097: assertEquals(art2.getArticleName(), art2a.getArticleName());
098: assertEquals(art3.getArticleName(), art3a.getArticleName());
099: } catch (Throwable t) {
100: System.out.println(t.getMessage());
101: t.printStackTrace();
102: fail(t.getMessage());
103: }
104: }
105: }
|