001: package org.apache.ojb.odmg;
002:
003: import org.apache.ojb.junit.ODMGTestCase;
004: import org.apache.ojb.odmg.shared.Article;
005: import org.apache.ojb.odmg.shared.ProductGroup;
006: import org.odmg.DMap;
007: import org.odmg.Transaction;
008:
009: /**
010: * Tests for OJB {@link org.odmg.DSet} implementation.
011: */
012: public class DMapTest extends ODMGTestCase {
013: private ProductGroup productGroup;
014:
015: public static void main(String[] args) {
016: String[] arr = { DMapTest.class.getName() };
017: junit.textui.TestRunner.main(arr);
018: }
019:
020: public DMapTest(String name) {
021: super (name);
022: }
023:
024: protected void setUp() throws Exception {
025: super .setUp();
026: Transaction tx = odmg.newTransaction();
027: tx.begin();
028: productGroup = new ProductGroup();
029: productGroup.setName("DMapTest_" + System.currentTimeMillis()
030: + "_");
031: database.makePersistent(productGroup);
032: tx.commit();
033: }
034:
035: protected Article createArticle(String name) throws Exception {
036:
037: Article a = new Article();
038: a.setArticleName(productGroup.getName() + name);
039: a.setStock(234);
040: a.setProductGroup(productGroup);
041: return a;
042: }
043:
044: public void testAdding() throws Exception {
045: String name = "testAdding";
046: String namedObject = "testAdding_" + System.currentTimeMillis();
047: TransactionExt tx = (TransactionExt) odmg.newTransaction();
048:
049: tx.begin();
050: DMap map = odmg.newDMap();
051:
052: database.bind(map, namedObject);
053: Article key1 = createArticle(name + "_key1");
054: Article val1 = createArticle(name + "_val1");
055: Article key2 = createArticle(name + "_key2");
056: Article val2 = createArticle(name + "_val2");
057:
058: map.put(key1, val1);
059: map.put(key2, val2);
060: tx.commit();
061:
062: tx = (TransactionExt) odmg.newTransaction();
063: tx.begin();
064: tx.getBroker().clearCache();
065:
066: DMap mapA = (DMap) database.lookup(namedObject);
067: assertNotNull(mapA);
068: Article val1A = (Article) mapA.get(key1);
069: assertNotNull(val1A);
070: assertEquals(val1.getArticleId(), val1A.getArticleId());
071: Article val2A = (Article) mapA.get(key2);
072: assertNotNull(val2A);
073: assertEquals(val2.getArticleId(), val2A.getArticleId());
074: tx.commit();
075: }
076:
077: public void testRemove() throws Exception {
078: String name = "testAdding";
079: String namedObject = "testAdding_" + System.currentTimeMillis();
080: TransactionExt tx = (TransactionExt) odmg.newTransaction();
081:
082: tx.begin();
083: DMap map = odmg.newDMap();
084:
085: database.bind(map, namedObject);
086: Article key1 = createArticle(name + "_key1");
087: Article val1 = createArticle(name + "_val1");
088: Article key2 = createArticle(name + "_key2");
089: Article val2 = createArticle(name + "_val2");
090:
091: map.put(key1, val1);
092: map.put(key2, val2);
093: tx.commit();
094:
095: tx = (TransactionExt) odmg.newTransaction();
096: tx.begin();
097: tx.getBroker().clearCache();
098:
099: DMap mapA = (DMap) database.lookup(namedObject);
100: assertNotNull(mapA);
101: Article val1A = (Article) mapA.get(key1);
102: assertNotNull(val1A);
103: assertEquals(val1.getArticleId(), val1A.getArticleId());
104: Article val2A = (Article) mapA.get(key2);
105: assertNotNull(val2A);
106: assertEquals(val2.getArticleId(), val2A.getArticleId());
107: tx.commit();
108:
109: tx.begin();
110: mapA.remove(key1);
111:
112: tx.checkpoint();
113:
114: mapA = (DMap) database.lookup(namedObject);
115: assertNotNull(mapA);
116: val1A = (Article) mapA.get(key1);
117: assertNull(val1A);
118: val2A = (Article) mapA.get(key2);
119: assertNotNull(val2A);
120: assertEquals(val2.getArticleId(), val2A.getArticleId());
121: tx.commit();
122:
123: tx.begin();
124: mapA.remove(key2);
125: mapA.put(key2, val2);
126:
127: tx.checkpoint();
128:
129: mapA = (DMap) database.lookup(namedObject);
130: assertNotNull(mapA);
131: val1A = (Article) mapA.get(key1);
132: assertNull(val1A);
133: val2A = (Article) mapA.get(key2);
134: assertNotNull(val2A);
135: assertEquals(val2.getArticleId(), val2A.getArticleId());
136: tx.commit();
137:
138: tx.begin();
139: mapA.remove(key2);
140: tx.commit();
141:
142: tx.begin();
143: mapA = (DMap) database.lookup(namedObject);
144: assertNotNull(mapA);
145: val1A = (Article) mapA.get(key1);
146: assertNull(val1A);
147: val2A = (Article) mapA.get(key2);
148: assertNull(val2A);
149: tx.commit();
150: }
151: }
|