1 package org.apache.ojb.odmg; 2 3 4 import org.apache.ojb.junit.ODMGTestCase; 5 import org.apache.ojb.odmg.shared.Article; 6 import org.apache.ojb.odmg.shared.ProductGroup; 7 import org.odmg.DMap; 8 import org.odmg.Transaction; 9 10 13 public class DMapTest extends ODMGTestCase 14 { 15 private ProductGroup productGroup; 16 17 public static void main(String [] args) 18 { 19 String [] arr = {DMapTest.class.getName()}; 20 junit.textui.TestRunner.main(arr); 21 } 22 23 public DMapTest(String name) 24 { 25 super(name); 26 } 27 28 protected void setUp() throws Exception 29 { 30 super.setUp(); 31 Transaction tx = odmg.newTransaction(); 32 tx.begin(); 33 productGroup = new ProductGroup(); 34 productGroup.setName("DMapTest_" + System.currentTimeMillis() + "_"); 35 database.makePersistent(productGroup); 36 tx.commit(); 37 } 38 39 protected Article createArticle(String name) throws Exception 40 { 41 42 Article a = new Article(); 43 a.setArticleName(productGroup.getName() + name); 44 a.setStock(234); 45 a.setProductGroup(productGroup); 46 return a; 47 } 48 49 public void testAdding() throws Exception 50 { 51 String name = "testAdding"; 52 String namedObject = "testAdding_" + System.currentTimeMillis(); 53 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 54 55 tx.begin(); 56 DMap map = odmg.newDMap(); 57 58 database.bind(map, namedObject); 59 Article key1 = createArticle(name + "_key1"); 60 Article val1 = createArticle(name + "_val1"); 61 Article key2 = createArticle(name + "_key2"); 62 Article val2 = createArticle(name + "_val2"); 63 64 map.put(key1, val1); 65 map.put(key2, val2); 66 tx.commit(); 67 68 69 tx = (TransactionExt) odmg.newTransaction(); 70 tx.begin(); 71 tx.getBroker().clearCache(); 72 73 DMap mapA = (DMap) database.lookup(namedObject); 74 assertNotNull(mapA); 75 Article val1A = (Article) mapA.get(key1); 76 assertNotNull(val1A); 77 assertEquals(val1.getArticleId(), val1A.getArticleId()); 78 Article val2A = (Article) mapA.get(key2); 79 assertNotNull(val2A); 80 assertEquals(val2.getArticleId(), val2A.getArticleId()); 81 tx.commit(); 82 } 83 84 public void testRemove() throws Exception 85 { 86 String name = "testAdding"; 87 String namedObject = "testAdding_" + System.currentTimeMillis(); 88 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 89 90 tx.begin(); 91 DMap map = odmg.newDMap(); 92 93 database.bind(map, namedObject); 94 Article key1 = createArticle(name + "_key1"); 95 Article val1 = createArticle(name + "_val1"); 96 Article key2 = createArticle(name + "_key2"); 97 Article val2 = createArticle(name + "_val2"); 98 99 map.put(key1, val1); 100 map.put(key2, val2); 101 tx.commit(); 102 103 104 tx = (TransactionExt) odmg.newTransaction(); 105 tx.begin(); 106 tx.getBroker().clearCache(); 107 108 DMap mapA = (DMap) database.lookup(namedObject); 109 assertNotNull(mapA); 110 Article val1A = (Article) mapA.get(key1); 111 assertNotNull(val1A); 112 assertEquals(val1.getArticleId(), val1A.getArticleId()); 113 Article val2A = (Article) mapA.get(key2); 114 assertNotNull(val2A); 115 assertEquals(val2.getArticleId(), val2A.getArticleId()); 116 tx.commit(); 117 118 tx.begin(); 119 mapA.remove(key1); 120 121 tx.checkpoint(); 122 123 mapA = (DMap) database.lookup(namedObject); 124 assertNotNull(mapA); 125 val1A = (Article) mapA.get(key1); 126 assertNull(val1A); 127 val2A = (Article) mapA.get(key2); 128 assertNotNull(val2A); 129 assertEquals(val2.getArticleId(), val2A.getArticleId()); 130 tx.commit(); 131 132 tx.begin(); 133 mapA.remove(key2); 134 mapA.put(key2, val2); 135 136 tx.checkpoint(); 137 138 mapA = (DMap) database.lookup(namedObject); 139 assertNotNull(mapA); 140 val1A = (Article) mapA.get(key1); 141 assertNull(val1A); 142 val2A = (Article) mapA.get(key2); 143 assertNotNull(val2A); 144 assertEquals(val2.getArticleId(), val2A.getArticleId()); 145 tx.commit(); 146 147 tx.begin(); 148 mapA.remove(key2); 149 tx.commit(); 150 151 tx.begin(); 152 mapA = (DMap) database.lookup(namedObject); 153 assertNotNull(mapA); 154 val1A = (Article) mapA.get(key1); 155 assertNull(val1A); 156 val2A = (Article) mapA.get(key2); 157 assertNull(val2A); 158 tx.commit(); 159 } 160 } 161 | Popular Tags |