KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > AutomaticForeignKeys


1 package org.apache.ojb.broker;
2 import org.apache.ojb.broker.metadata.ClassDescriptor;
3 import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
4 import org.apache.ojb.junit.PBTestCase;
5
6 /**
7  * This TestClass tests OJB facilities to work with polymorphism.
8  */

9 public class AutomaticForeignKeys extends PBTestCase
10 {
11     public static void main(String JavaDoc[] args)
12     {
13         String JavaDoc[] arr = {AutomaticForeignKeys.class.getName()};
14         junit.textui.TestRunner.main(arr);
15     }
16
17     public AutomaticForeignKeys(String JavaDoc name)
18
19     {
20         super(name);
21     }
22
23     public void setUp() throws Exception JavaDoc
24     {
25         super.setUp();
26         ClassDescriptor cld = broker.getClassDescriptor(Article.class);
27         ObjectReferenceDescriptor ord =
28             (ObjectReferenceDescriptor) cld.getObjectReferenceDescriptors().get(0);
29         ord.setCascadeStore(true);
30     }
31
32     /** test automatic assignment of foreign keys for 1:1 reference. */
33     public void testOneOneReference()
34     {
35         try
36         {
37             Article art = new Article();
38             art.setArticleName("OJB O/R mapping power");
39             ProductGroup pg = new ProductGroup();
40             pg.setName("Software");
41             art.setProductGroup(pg);
42             Identity artOID = new Identity(art, broker);
43             broker.beginTransaction();
44             broker.store(art);
45             broker.store(pg);
46             broker.commitTransaction();
47             broker.clearCache();
48             InterfaceArticle readInArt = (Article) broker.getObjectByIdentity(artOID);
49             InterfaceProductGroup readInPg = readInArt.getProductGroup();
50             assertEquals(art.getArticleName(), readInArt.getArticleName());
51             assertEquals(pg.getName(), readInPg.getName());
52         }
53         catch (Throwable JavaDoc t)
54         {
55             System.out.println(t.getMessage());
56             t.printStackTrace();
57             fail(t.getMessage());
58         }
59     }
60
61     /** test automatic assignment of foreign keys for 1:n reference. */
62     public void testOneManyReference()
63     {
64         try
65         {
66             ProductGroup pg = new ProductGroup();
67             pg.setName("O/R mapping tools");
68
69             Article art1 = new Article();
70             art1.setArticleName("TOPLink");
71             art1.setProductGroup(pg);
72
73             Article art2 = new Article();
74             art2.setArticleName("OJB");
75             art2.setProductGroup(pg);
76
77             Article art3 = new Article();
78             art3.setArticleName("CASTOR");
79             art3.setProductGroup(pg);
80
81             pg.add(art1);
82             pg.add(art2);
83             pg.add(art3);
84
85             Identity pgOID = new Identity(pg, broker);
86             broker.beginTransaction();
87             broker.store(pg);
88             broker.commitTransaction();
89             broker.clearCache();
90             InterfaceProductGroup readInPG =
91                 (InterfaceProductGroup) broker.getObjectByIdentity(pgOID);
92             assertEquals(pg.getName(), readInPG.getName());
93             assertEquals(pg.getAllArticles().size(), readInPG.getAllArticles().size());
94             InterfaceArticle art1a = (InterfaceArticle) pg.getAllArticles().get(0);
95             InterfaceArticle art2a = (InterfaceArticle) pg.getAllArticles().get(1);
96             InterfaceArticle art3a = (InterfaceArticle) pg.getAllArticles().get(2);
97             assertEquals(art1.getArticleName(), art1a.getArticleName());
98             assertEquals(art2.getArticleName(), art2a.getArticleName());
99             assertEquals(art3.getArticleName(), art3a.getArticleName());
100         }
101         catch (Throwable JavaDoc t)
102         {
103             System.out.println(t.getMessage());
104             t.printStackTrace();
105             fail(t.getMessage());
106         }
107     }
108 }
109
Popular Tags