1 9 package org.apache.ojb.odmg; 10 11 import java.util.Collection ; 12 import java.util.Iterator ; 13 import java.util.List ; 14 15 import org.apache.ojb.broker.Article; 16 import org.apache.ojb.broker.InterfaceArticle; 17 import org.apache.ojb.broker.InterfaceProductGroup; 18 import org.apache.ojb.broker.Mammal; 19 import org.apache.ojb.broker.ProductGroup; 20 import org.apache.ojb.broker.Reptile; 21 import org.apache.ojb.broker.metadata.ClassDescriptor; 22 import org.apache.ojb.broker.metadata.CollectionDescriptor; 23 import org.apache.ojb.broker.metadata.MetadataManager; 24 import org.apache.ojb.junit.ODMGTestCase; 25 import org.apache.ojb.odmg.shared.ODMGZoo; 26 import org.odmg.ODMGException; 27 import org.odmg.OQLQuery; 28 import org.odmg.Transaction; 29 30 public class OneToManyTest extends ODMGTestCase 31 { 32 private static final int COUNT = 10; 33 int oldValue; 34 35 public static void main(String [] args) 36 { 37 String [] arr = {OneToManyTest.class.getName()}; 38 junit.textui.TestRunner.main(arr); 39 } 40 41 public OneToManyTest(String name) 42 { 43 super(name); 44 } 45 46 public void setUp() throws Exception 47 { 48 super.setUp(); 49 ClassDescriptor cld = MetadataManager.getInstance().getRepository().getDescriptorFor(ProductGroup.class); 50 CollectionDescriptor cod = cld.getCollectionDescriptorByName("allArticlesInGroup"); 51 oldValue = cod.getCascadingStore(); 52 cod.setCascadeStore(false); 54 } 55 56 public void tearDown() throws Exception 57 { 58 ClassDescriptor cld = MetadataManager.getInstance().getRepository().getDescriptorFor(ProductGroup.class); 59 cld.getCollectionDescriptorByName("allArticlesInGroup").setCascadingStore(oldValue); 60 super.tearDown(); 61 } 62 63 68 public void testCreate() throws Exception 69 { 70 String name = "testCreate_" + System.currentTimeMillis(); 71 74 Transaction tx = odmg.newTransaction(); 75 tx.begin(); 76 ProductGroup group = new ProductGroup(); 77 group.setGroupName(name); 78 tx.lock(group, Transaction.WRITE); 79 for (int i = 0; i < COUNT; i++) 80 { 81 Article article = createArticle(name); 82 group.add(article); 83 } 84 tx.commit(); 85 88 tx.begin(); 89 OQLQuery query = odmg.newOQLQuery(); 90 query.create("select productGroup from " + ProductGroup.class.getName() + " where groupName=$1"); 91 query.bind(name); 92 Collection results = (Collection ) query.execute(); 93 tx.commit(); 94 Iterator it = results.iterator(); 95 assertTrue(it.hasNext()); 96 InterfaceProductGroup pg = (InterfaceProductGroup) it.next(); 97 assertFalse(it.hasNext()); 98 assertNotNull(pg.getAllArticles()); 99 assertEquals(COUNT, pg.getAllArticles().size()); 100 } 101 102 110 public void testUpdateWithProxy() throws Exception 111 { 112 String name = "testUpdateWithProxy_" + System.currentTimeMillis(); 115 116 ProductGroup pg1 = new ProductGroup(null, name + "_1", "a group"); 117 ProductGroup pg2 = new ProductGroup(null, name + "_2", "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 132 OQLQuery query = odmg.newOQLQuery(); 133 query.create("select productGroup from " + ProductGroup.class.getName() + " where groupName like $1"); 134 query.bind(name + "%"); 135 Collection results = (Collection ) query.execute(); 136 assertEquals(2, results.size()); 137 Iterator it = results.iterator(); 138 InterfaceProductGroup temp = null; 139 142 while (it.hasNext()) 143 { 144 Article article = createArticle(name); 145 temp = (InterfaceProductGroup) it.next(); 146 tx.lock(temp, Transaction.WRITE); 147 temp.add(article); 148 article.setProductGroup(temp); 149 } 150 tx.commit(); 151 152 155 tx.begin(); 156 query = odmg.newOQLQuery(); 157 query.create("select productGroup from " + ProductGroup.class.getName() + " where groupName like $1"); 158 query.bind(name + "%"); 159 results = (Collection ) query.execute(); 160 tx.commit(); 161 162 assertEquals(2, results.size()); 163 it = results.iterator(); 164 int counter = 0; 165 temp = null; 166 while (it.hasNext()) 167 { 168 temp = (InterfaceProductGroup) it.next(); 169 Collection articles = temp.getAllArticles(); 170 counter = counter + articles.size(); 171 Iterator it2 = articles.iterator(); 172 while (it2.hasNext()) 173 { 174 InterfaceArticle art = (InterfaceArticle) it2.next(); 175 if (art.getArticleName() != null) 176 { 177 assertEquals(name, art.getArticleName()); 178 } 179 } 180 } 181 assertEquals(3, counter); 182 } 183 184 188 public void testPolymorphOneToMany() 189 { 190 191 ODMGZoo myZoo = new ODMGZoo("London"); 192 Mammal elephant = new Mammal(37, "Jumbo", 4); 193 Mammal cat = new Mammal(11, "Silvester", 4); 194 Reptile snake = new Reptile(3, "Kaa", "green"); 195 196 myZoo.addAnimal(snake); 197 myZoo.addAnimal(elephant); 198 myZoo.addAnimal(cat); 199 200 try 201 { 202 Transaction tx = odmg.newTransaction(); 203 tx.begin(); 204 database.makePersistent(myZoo); 205 tx.commit(); 206 207 int id = myZoo.getZooId(); 208 209 tx = odmg.newTransaction(); 210 tx.begin(); 211 OQLQuery query = odmg.newOQLQuery(); 212 query.create("select zoos from " + ODMGZoo.class.getName() + 213 " where zooId=$1"); 214 query.bind(new Integer (id)); 215 List zoos = (List ) query.execute(); 216 assertEquals(1, zoos.size()); 217 ODMGZoo zoo = (ODMGZoo) zoos.get(0); 218 tx.commit(); 219 assertEquals(3, zoo.getAnimals().size()); 220 221 222 } 223 catch (ODMGException e) 224 { 225 e.printStackTrace(); 226 fail("ODMGException thrown " + e.getMessage()); 227 } 228 } 229 230 233 protected Article createArticle(String name) 234 { 235 Article a = Article.createInstance(); 236 a.setArticleName(name); 237 a.setIsSelloutArticle(true); 238 a.setMinimumStock(100); 239 a.setOrderedUnits(17); 240 a.setPrice(0.45); 241 a.setStock(234); 243 a.setSupplierId(4); 244 a.setUnit("bottle"); 245 return a; 246 } 247 } 248 | Popular Tags |