1 package org.apache.ojb.odmg; 2 3 4 import java.util.ArrayList ; 5 import java.util.List ; 6 7 import org.apache.ojb.junit.ODMGTestCase; 8 import org.apache.ojb.odmg.shared.Article; 9 import org.apache.ojb.odmg.shared.ProductGroup; 10 import org.odmg.DList; 11 import org.odmg.ObjectNameNotFoundException; 12 import org.odmg.ObjectNameNotUniqueException; 13 import org.odmg.Transaction; 14 15 21 public class NamedRootsTest extends ODMGTestCase 22 { 23 private ProductGroup testProductGroup; 24 25 public static void main(String [] args) 26 { 27 String [] arr = {NamedRootsTest.class.getName()}; 28 junit.textui.TestRunner.main(arr); 29 } 30 31 public NamedRootsTest(String name) 32 { 33 super(name); 34 } 35 36 protected void setUp() throws Exception 37 { 38 super.setUp(); 39 Transaction tx = odmg.newTransaction(); 40 tx.begin(); 41 testProductGroup = new ProductGroup(); 42 testProductGroup.setGroupName("NamedRootsTest_" + System.currentTimeMillis()); 43 database.makePersistent(testProductGroup); 44 tx.commit(); 45 } 46 47 private Article createArticle() 48 { 49 Article example = new Article(); 50 example.setArticleName(testProductGroup.getName()); 51 example.setProductGroupId(testProductGroup.getId()); 52 return example; 53 } 54 55 public void testBindPersistentCapableObjectCollection() throws Exception 56 { 57 String bindingName = "testBindPersistentCapableObjectCollection_" + System.currentTimeMillis(); 58 59 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 60 tx.begin(); 62 63 DList dlist = odmg.newDList(); 65 Article a1 = createArticle(); 66 Article a2 = createArticle(); 67 Article a3 = createArticle(); 68 dlist.add(a1); 69 dlist.add(a2); 70 dlist.add(a3); 71 database.bind(dlist, bindingName); 72 List value = (List ) database.lookup(bindingName); 74 assertNotNull("Could not lookup object for binding name: "+bindingName, value); 75 tx.commit(); 76 77 try 78 { 79 tx.begin(); 80 database.bind(dlist, bindingName); 81 tx.commit(); 82 fail("We expected a ObjectNameNotUniqueException, but was not thrown"); 83 } 84 catch (ObjectNameNotUniqueException ex) 85 { 86 assertTrue(true); 88 tx.abort(); 89 } 90 91 try 92 { 93 tx.begin(); 94 tx.getBroker().clearCache(); 95 database.bind(dlist, bindingName); 96 tx.commit(); 97 fail("We expected a ObjectNameNotUniqueException, but was not thrown"); 98 } 99 catch (ObjectNameNotUniqueException ex) 100 { 101 assertTrue(true); 103 tx.abort(); 104 } 105 106 tx.begin(); 107 List result = (List ) database.lookup(bindingName); 108 assertNotNull(result); 109 assertEquals(3, result.size()); 110 Article newA1 = (Article) result.get(0); 111 assertNotNull(newA1); 112 assertEquals(a1.getArticleName(), newA1.getArticleName()); 113 tx.commit(); 114 115 tx.begin(); 116 result.clear(); 121 database.unbind(bindingName); 123 124 tx.commit(); 128 129 tx.begin(); 130 try 131 { 132 database.lookup(bindingName); 133 } 134 catch(ObjectNameNotFoundException e) 135 { 136 assertTrue(true); 138 } 139 tx.commit(); 140 } 141 142 public void testBindPersistentCapableObject() throws Exception 143 { 144 String bindingName = "testBindPersistentCapableObject_" + System.currentTimeMillis(); 145 TransactionImpl tx = (TransactionImpl) odmg.newTransaction(); 146 tx.begin(); 148 Article example = createArticle(); 149 database.bind(example, bindingName); 150 Article value = (Article) database.lookup(bindingName); 151 assertTrue("Could not lookup object for binding name: "+bindingName, value != null); 152 tx.commit(); 153 154 try 155 { 156 tx.begin(); 157 database.bind(example, bindingName); 158 tx.commit(); 159 fail("We expected a ObjectNameNotUniqueException, but was not thrown"); 160 } 161 catch (ObjectNameNotUniqueException ex) 162 { 163 assertTrue(true); 165 tx.abort(); 166 } 167 168 tx.begin(); 169 database.unbind(bindingName); 172 tx.commit(); 173 } 174 175 public void testBindPersistentSerialzableObject() throws Exception 176 { 177 String bindingName = "testBindPersistentSerialzableObject_" + System.currentTimeMillis(); 178 TransactionImpl tx = (TransactionImpl) odmg.newTransaction(); 179 tx.begin(); 181 List example = new ArrayList (); 182 example.add("Merkur"); 183 example.add("Venus"); 184 database.bind(example, bindingName); 185 List value = (List ) database.lookup(bindingName); 186 assertNotNull("Could not lookup object for binding name: "+bindingName, value); 187 assertEquals(2, value.size()); 188 tx.commit(); 189 190 try 191 { 192 tx.begin(); 193 database.bind(example, bindingName); 194 tx.commit(); 195 fail("We expected a ObjectNameNotUniqueException, but was not thrown"); 196 } 197 catch (ObjectNameNotUniqueException ex) 198 { 199 assertTrue(true); 201 tx.abort(); 202 } 203 204 tx.begin(); 205 database.unbind(bindingName); 206 tx.commit(); 207 208 tx.begin(); 209 example.add("earth"); 210 example.add("mars"); 211 database.bind(example, bindingName); 212 value = (List ) database.lookup(bindingName); 213 assertNotNull("Could not lookup object for binding name: "+bindingName, value); 214 assertEquals(4, value.size()); 215 tx.commit(); 216 217 tx.begin(); 218 database.unbind(bindingName); 219 tx.commit(); 220 } 221 222 public void testDoubleBindInOneTx() throws Exception 223 { 224 String bindingName = "testDoubleBindInOneTx_" + System.currentTimeMillis(); 225 226 Article article = createArticle(); 227 Article foundArticle = null; 228 229 Transaction tx = odmg.newTransaction(); 230 tx.begin(); 231 database.bind(article, bindingName); 232 233 foundArticle = (Article) database.lookup(bindingName); 234 assertNotNull(foundArticle); 235 236 foundArticle = null; 237 database.unbind(bindingName); 238 try 239 { 240 foundArticle = (Article) database.lookup(bindingName); 241 fail("Found unbound DList"); 242 } 243 catch (ObjectNameNotFoundException ex) 244 { 245 assertTrue(true); 247 } 248 249 database.bind(article, bindingName); 250 foundArticle = (Article) database.lookup(bindingName); 251 252 foundArticle = null; 253 tx.commit(); 254 255 tx = odmg.newTransaction(); 256 tx.begin(); 257 foundArticle = (Article) database.lookup(bindingName); 258 assertNotNull(foundArticle); 259 database.unbind(bindingName); 260 tx.commit(); 261 } 262 263 264 public void testLookup() throws Exception 265 { 266 String bindingName = "testLookup_" + System.currentTimeMillis(); 267 TransactionImpl tx = (TransactionImpl) odmg.newTransaction(); 269 tx.begin(); 271 Article example = createArticle(); 272 database.makePersistent(example); 273 tx.commit(); 274 275 tx.begin(); 276 database.bind(example, bindingName); 277 tx.commit(); 278 279 Article lookedUp1 = null; 281 tx = (TransactionImpl) odmg.newTransaction(); 282 tx.begin(); 283 lookedUp1 = (Article) database.lookup(bindingName); 285 tx.commit(); 286 287 assertEquals("lookups should return identical object", example, lookedUp1); 289 290 tx.begin(); 291 database.unbind(bindingName); 292 tx.commit(); 293 } 294 295 public void testUnBind() throws Exception 296 { 297 String name = "testUnBind_" + System.currentTimeMillis(); 298 299 Transaction tx = odmg.newTransaction(); 300 tx.begin(); 302 Article example = createArticle(); 303 database.makePersistent(example); 304 tx.commit(); 305 306 tx.begin(); 308 try 309 { 310 database.bind(example, name); 311 tx.commit(); 312 } 313 catch (ObjectNameNotUniqueException ex) 314 { 315 tx.abort(); 316 fail(ex.getMessage()); 317 } 318 319 tx = odmg.newTransaction(); 321 tx.begin(); 322 try 323 { 324 database.unbind(name); 325 tx.commit(); 326 } 327 catch (ObjectNameNotFoundException ex) 328 { 329 tx.abort(); 330 fail("name " + name + "should be known"); 331 } 332 333 tx = odmg.newTransaction(); 335 tx.begin(); 336 try 337 { 338 Article value = (Article) database.lookup(name); 339 assertNotNull("Should not find unbind name '" + name+"'", value); 340 fail("name " + name + " should not be known after unbind"); 341 } 342 catch (ObjectNameNotFoundException ex) 343 { 344 assertTrue(true); 346 } 347 tx.abort(); 348 } 349 } 350 | Popular Tags |