001: /*
002: * Created by IntelliJ IDEA.
003: * User: Matt
004: * Date: Jun 10, 2002
005: * Time: 9:22:36 PM
006: * To change template for new class use
007: * Code Style | Class Templates options (Tools | IDE Options).
008: */
009: package org.apache.ojb.odmg;
010:
011: import java.util.Collection;
012: import java.util.Iterator;
013: import java.util.List;
014:
015: import org.apache.ojb.broker.Article;
016: import org.apache.ojb.broker.InterfaceArticle;
017: import org.apache.ojb.broker.InterfaceProductGroup;
018: import org.apache.ojb.broker.Mammal;
019: import org.apache.ojb.broker.ProductGroup;
020: import org.apache.ojb.broker.Reptile;
021: import org.apache.ojb.broker.metadata.ClassDescriptor;
022: import org.apache.ojb.broker.metadata.CollectionDescriptor;
023: import org.apache.ojb.broker.metadata.MetadataManager;
024: import org.apache.ojb.junit.ODMGTestCase;
025: import org.apache.ojb.odmg.shared.ODMGZoo;
026: import org.odmg.ODMGException;
027: import org.odmg.OQLQuery;
028: import org.odmg.Transaction;
029:
030: public class OneToManyTest extends ODMGTestCase {
031: private static final int COUNT = 10;
032: int oldValue;
033:
034: public static void main(String[] args) {
035: String[] arr = { OneToManyTest.class.getName() };
036: junit.textui.TestRunner.main(arr);
037: }
038:
039: public OneToManyTest(String name) {
040: super (name);
041: }
042:
043: public void setUp() throws Exception {
044: super .setUp();
045: ClassDescriptor cld = MetadataManager.getInstance()
046: .getRepository().getDescriptorFor(ProductGroup.class);
047: CollectionDescriptor cod = cld
048: .getCollectionDescriptorByName("allArticlesInGroup");
049: oldValue = cod.getCascadingStore();
050: // odmg-api need false
051: cod.setCascadeStore(false);
052: }
053:
054: public void tearDown() throws Exception {
055: ClassDescriptor cld = MetadataManager.getInstance()
056: .getRepository().getDescriptorFor(ProductGroup.class);
057: cld.getCollectionDescriptorByName("allArticlesInGroup")
058: .setCascadingStore(oldValue);
059: super .tearDown();
060: }
061:
062: /**
063: * tests creation of new object that has a one to many relationship
064: *
065: * @throws Exception
066: */
067: public void testCreate() throws Exception {
068: String name = "testCreate_" + System.currentTimeMillis();
069: /**
070: * 1. create the article.
071: */
072: Transaction tx = odmg.newTransaction();
073: tx.begin();
074: ProductGroup group = new ProductGroup();
075: group.setGroupName(name);
076: tx.lock(group, Transaction.WRITE);
077: for (int i = 0; i < COUNT; i++) {
078: Article article = createArticle(name);
079: group.add(article);
080: }
081: tx.commit();
082: /**
083: * 2. query on the article to make sure it is everything we set it up to be.
084: */
085: tx.begin();
086: OQLQuery query = odmg.newOQLQuery();
087: query.create("select productGroup from "
088: + ProductGroup.class.getName() + " where groupName=$1");
089: query.bind(name);
090: Collection results = (Collection) query.execute();
091: tx.commit();
092: Iterator it = results.iterator();
093: assertTrue(it.hasNext());
094: InterfaceProductGroup pg = (InterfaceProductGroup) it.next();
095: assertFalse(it.hasNext());
096: assertNotNull(pg.getAllArticles());
097: assertEquals(COUNT, pg.getAllArticles().size());
098: }
099:
100: /**
101: * tests creation of new object that has a one to many relationship.
102: * thma: this test will not work, because ODMG is no able to track
103: * modifictations to normal collections.
104: * Only Odmg Collections like DList will be treated properly.
105: *
106: * @throws Exception
107: */
108: public void testUpdateWithProxy() throws Exception {
109: // arminw: fixed
110: // if(ojbSkipKnownIssueProblem()) return;
111: String name = "testUpdateWithProxy_"
112: + System.currentTimeMillis();
113:
114: ProductGroup pg1 = new ProductGroup(null, name + "_1",
115: "a group");
116: ProductGroup pg2 = new ProductGroup(null, name + "_2",
117: "a group");
118: Article a1 = createArticle(name);
119: a1.setProductGroup(pg1);
120: TransactionExt tx = (TransactionExt) odmg.newTransaction();
121: tx.begin();
122: database.makePersistent(a1);
123: database.makePersistent(pg1);
124: database.makePersistent(pg2);
125: tx.commit();
126:
127: tx.begin();
128: tx.getBroker().clearCache();
129: /**
130: * 1. get all articles from groups and add a new article
131: */
132: OQLQuery query = odmg.newOQLQuery();
133: query.create("select productGroup from "
134: + ProductGroup.class.getName()
135: + " where groupName like $1");
136: query.bind(name + "%");
137: Collection results = (Collection) query.execute();
138: assertEquals(2, results.size());
139: Iterator it = results.iterator();
140: InterfaceProductGroup temp = null;
141: /**
142: * to each productgroup add an article with a pre-determined name.
143: */
144: while (it.hasNext()) {
145: Article article = createArticle(name);
146: temp = (InterfaceProductGroup) it.next();
147: tx.lock(temp, Transaction.WRITE);
148: temp.add(article);
149: article.setProductGroup(temp);
150: }
151: tx.commit();
152:
153: /**
154: * 2. requery and find the articles we added.
155: */
156: tx.begin();
157: query = odmg.newOQLQuery();
158: query.create("select productGroup from "
159: + ProductGroup.class.getName()
160: + " where groupName like $1");
161: query.bind(name + "%");
162: results = (Collection) query.execute();
163: tx.commit();
164:
165: assertEquals(2, results.size());
166: it = results.iterator();
167: int counter = 0;
168: temp = null;
169: while (it.hasNext()) {
170: temp = (InterfaceProductGroup) it.next();
171: Collection articles = temp.getAllArticles();
172: counter = counter + articles.size();
173: Iterator it2 = articles.iterator();
174: while (it2.hasNext()) {
175: InterfaceArticle art = (InterfaceArticle) it2.next();
176: if (art.getArticleName() != null) {
177: assertEquals(name, art.getArticleName());
178: }
179: }
180: }
181: assertEquals(3, counter);
182: }
183:
184: /**
185: * this tests if polymorph collections (i.e. collections of objects
186: * implementing a common interface) are treated correctly
187: */
188: public void testPolymorphOneToMany() {
189:
190: ODMGZoo myZoo = new ODMGZoo("London");
191: Mammal elephant = new Mammal(37, "Jumbo", 4);
192: Mammal cat = new Mammal(11, "Silvester", 4);
193: Reptile snake = new Reptile(3, "Kaa", "green");
194:
195: myZoo.addAnimal(snake);
196: myZoo.addAnimal(elephant);
197: myZoo.addAnimal(cat);
198:
199: try {
200: Transaction tx = odmg.newTransaction();
201: tx.begin();
202: database.makePersistent(myZoo);
203: tx.commit();
204:
205: int id = myZoo.getZooId();
206:
207: tx = odmg.newTransaction();
208: tx.begin();
209: OQLQuery query = odmg.newOQLQuery();
210: query.create("select zoos from " + ODMGZoo.class.getName()
211: + " where zooId=$1");
212: query.bind(new Integer(id));
213: List zoos = (List) query.execute();
214: assertEquals(1, zoos.size());
215: ODMGZoo zoo = (ODMGZoo) zoos.get(0);
216: tx.commit();
217: assertEquals(3, zoo.getAnimals().size());
218:
219: } catch (ODMGException e) {
220: e.printStackTrace();
221: fail("ODMGException thrown " + e.getMessage());
222: }
223: }
224:
225: /**
226: * Create an article with 4 product groups related to it in a 1-N relationship
227: */
228: protected Article createArticle(String name) {
229: Article a = Article.createInstance();
230: a.setArticleName(name);
231: a.setIsSelloutArticle(true);
232: a.setMinimumStock(100);
233: a.setOrderedUnits(17);
234: a.setPrice(0.45);
235: //a.setProductGroupId(1);
236: a.setStock(234);
237: a.setSupplierId(4);
238: a.setUnit("bottle");
239: return a;
240: }
241: }
|