001: package org.apache.ojb.odmg;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005:
006: import org.apache.ojb.junit.ODMGTestCase;
007: import org.apache.ojb.odmg.shared.Article;
008: import org.apache.ojb.odmg.shared.ProductGroup;
009: import org.odmg.DList;
010: import org.odmg.ObjectNameNotFoundException;
011: import org.odmg.ObjectNameNotUniqueException;
012: import org.odmg.Transaction;
013:
014: /**
015: * Demo Application that shows basic concepts for Applications using the OJB ODMG
016: * implementation as an transactional object server.
017: *
018: * @version $Id: NamedRootsTest.java,v 1.1.2.4 2005/08/16 15:04:38 aclute Exp $
019: */
020: public class NamedRootsTest extends ODMGTestCase {
021: private ProductGroup testProductGroup;
022:
023: public static void main(String[] args) {
024: String[] arr = { NamedRootsTest.class.getName() };
025: junit.textui.TestRunner.main(arr);
026: }
027:
028: public NamedRootsTest(String name) {
029: super (name);
030: }
031:
032: protected void setUp() throws Exception {
033: super .setUp();
034: Transaction tx = odmg.newTransaction();
035: tx.begin();
036: testProductGroup = new ProductGroup();
037: testProductGroup.setGroupName("NamedRootsTest_"
038: + System.currentTimeMillis());
039: database.makePersistent(testProductGroup);
040: tx.commit();
041: }
042:
043: private Article createArticle() {
044: Article example = new Article();
045: example.setArticleName(testProductGroup.getName());
046: example.setProductGroupId(testProductGroup.getId());
047: return example;
048: }
049:
050: public void testBindPersistentCapableObjectCollection()
051: throws Exception {
052: String bindingName = "testBindPersistentCapableObjectCollection_"
053: + System.currentTimeMillis();
054:
055: TransactionExt tx = (TransactionExt) odmg.newTransaction();
056: //bind object to name
057: tx.begin();
058:
059: // get new DList instance
060: DList dlist = odmg.newDList();
061: Article a1 = createArticle();
062: Article a2 = createArticle();
063: Article a3 = createArticle();
064: dlist.add(a1);
065: dlist.add(a2);
066: dlist.add(a3);
067: database.bind(dlist, bindingName);
068: // lookup the named object - DList
069: List value = (List) database.lookup(bindingName);
070: assertNotNull("Could not lookup object for binding name: "
071: + bindingName, value);
072: tx.commit();
073:
074: try {
075: tx.begin();
076: database.bind(dlist, bindingName);
077: tx.commit();
078: fail("We expected a ObjectNameNotUniqueException, but was not thrown");
079: } catch (ObjectNameNotUniqueException ex) {
080: // we wait for this exception
081: assertTrue(true);
082: tx.abort();
083: }
084:
085: try {
086: tx.begin();
087: tx.getBroker().clearCache();
088: database.bind(dlist, bindingName);
089: tx.commit();
090: fail("We expected a ObjectNameNotUniqueException, but was not thrown");
091: } catch (ObjectNameNotUniqueException ex) {
092: // we wait for this exception
093: assertTrue(true);
094: tx.abort();
095: }
096:
097: tx.begin();
098: List result = (List) database.lookup(bindingName);
099: assertNotNull(result);
100: assertEquals(3, result.size());
101: Article newA1 = (Article) result.get(0);
102: assertNotNull(newA1);
103: assertEquals(a1.getArticleName(), newA1.getArticleName());
104: tx.commit();
105:
106: tx.begin();
107: // we want to completely remove the named object
108: // the persisted DList with all DList entries,
109: // but the Article objects itself shouldn't be deleted:
110: // 1. mandatory, clear the list to remove all entries
111: result.clear();
112: // 2. unbind named object
113: database.unbind(bindingName);
114:
115: // alternative can be used
116: //tx.setCascadingDelete(DListImpl.class, true);
117: //database.unbind(bindingName);
118: tx.commit();
119:
120: tx.begin();
121: try {
122: database.lookup(bindingName);
123: } catch (ObjectNameNotFoundException e) {
124: // expected exception
125: assertTrue(true);
126: }
127: tx.commit();
128: }
129:
130: public void testBindPersistentCapableObject() throws Exception {
131: String bindingName = "testBindPersistentCapableObject_"
132: + System.currentTimeMillis();
133: TransactionImpl tx = (TransactionImpl) odmg.newTransaction();
134: //bind object to name
135: tx.begin();
136: Article example = createArticle();
137: database.bind(example, bindingName);
138: Article value = (Article) database.lookup(bindingName);
139: assertTrue("Could not lookup object for binding name: "
140: + bindingName, value != null);
141: tx.commit();
142:
143: try {
144: tx.begin();
145: database.bind(example, bindingName);
146: tx.commit();
147: fail("We expected a ObjectNameNotUniqueException, but was not thrown");
148: } catch (ObjectNameNotUniqueException ex) {
149: // we wait for this exception
150: assertTrue(true);
151: tx.abort();
152: }
153:
154: tx.begin();
155: // this only remove the named object link, the Article object
156: // itself will not be touched
157: database.unbind(bindingName);
158: tx.commit();
159: }
160:
161: public void testBindPersistentSerialzableObject() throws Exception {
162: String bindingName = "testBindPersistentSerialzableObject_"
163: + System.currentTimeMillis();
164: TransactionImpl tx = (TransactionImpl) odmg.newTransaction();
165: //bind object to name
166: tx.begin();
167: List example = new ArrayList();
168: example.add("Merkur");
169: example.add("Venus");
170: database.bind(example, bindingName);
171: List value = (List) database.lookup(bindingName);
172: assertNotNull("Could not lookup object for binding name: "
173: + bindingName, value);
174: assertEquals(2, value.size());
175: tx.commit();
176:
177: try {
178: tx.begin();
179: database.bind(example, bindingName);
180: tx.commit();
181: fail("We expected a ObjectNameNotUniqueException, but was not thrown");
182: } catch (ObjectNameNotUniqueException ex) {
183: // we wait for this exception
184: assertTrue(true);
185: tx.abort();
186: }
187:
188: tx.begin();
189: database.unbind(bindingName);
190: tx.commit();
191:
192: tx.begin();
193: example.add("earth");
194: example.add("mars");
195: database.bind(example, bindingName);
196: value = (List) database.lookup(bindingName);
197: assertNotNull("Could not lookup object for binding name: "
198: + bindingName, value);
199: assertEquals(4, value.size());
200: tx.commit();
201:
202: tx.begin();
203: database.unbind(bindingName);
204: tx.commit();
205: }
206:
207: public void testDoubleBindInOneTx() throws Exception {
208: String bindingName = "testDoubleBindInOneTx_"
209: + System.currentTimeMillis();
210:
211: Article article = createArticle();
212: Article foundArticle = null;
213:
214: Transaction tx = odmg.newTransaction();
215: tx.begin();
216: database.bind(article, bindingName);
217:
218: foundArticle = (Article) database.lookup(bindingName);
219: assertNotNull(foundArticle);
220:
221: foundArticle = null;
222: database.unbind(bindingName);
223: try {
224: foundArticle = (Article) database.lookup(bindingName);
225: fail("Found unbound DList");
226: } catch (ObjectNameNotFoundException ex) {
227: // expected exception
228: assertTrue(true);
229: }
230:
231: database.bind(article, bindingName);
232: foundArticle = (Article) database.lookup(bindingName);
233:
234: foundArticle = null;
235: tx.commit();
236:
237: tx = odmg.newTransaction();
238: tx.begin();
239: foundArticle = (Article) database.lookup(bindingName);
240: assertNotNull(foundArticle);
241: database.unbind(bindingName);
242: tx.commit();
243: }
244:
245: public void testLookup() throws Exception {
246: String bindingName = "testLookup_" + System.currentTimeMillis();
247: // clear named roots.
248: TransactionImpl tx = (TransactionImpl) odmg.newTransaction();
249: //bind object to name
250: tx.begin();
251: Article example = createArticle();
252: database.makePersistent(example);
253: tx.commit();
254:
255: tx.begin();
256: database.bind(example, bindingName);
257: tx.commit();
258:
259: // TestThreadsNLocks look up
260: Article lookedUp1 = null;
261: tx = (TransactionImpl) odmg.newTransaction();
262: tx.begin();
263: // lookup by name binding
264: lookedUp1 = (Article) database.lookup(bindingName);
265: tx.commit();
266:
267: // looking up object by OID should return same Object as by name
268: assertEquals("lookups should return identical object", example,
269: lookedUp1);
270:
271: tx.begin();
272: database.unbind(bindingName);
273: tx.commit();
274: }
275:
276: public void testUnBind() throws Exception {
277: String name = "testUnBind_" + System.currentTimeMillis();
278:
279: Transaction tx = odmg.newTransaction();
280: //bind object to name
281: tx.begin();
282: Article example = createArticle();
283: database.makePersistent(example);
284: tx.commit();
285:
286: // 1. perform binding
287: tx.begin();
288: try {
289: database.bind(example, name);
290: tx.commit();
291: } catch (ObjectNameNotUniqueException ex) {
292: tx.abort();
293: fail(ex.getMessage());
294: }
295:
296: // 2. perform unbind
297: tx = odmg.newTransaction();
298: tx.begin();
299: try {
300: database.unbind(name);
301: tx.commit();
302: } catch (ObjectNameNotFoundException ex) {
303: tx.abort();
304: fail("name " + name + "should be known");
305: }
306:
307: // 3. check if name is really unknown now
308: tx = odmg.newTransaction();
309: tx.begin();
310: try {
311: Article value = (Article) database.lookup(name);
312: assertNotNull("Should not find unbind name '" + name + "'",
313: value);
314: fail("name " + name + " should not be known after unbind");
315: } catch (ObjectNameNotFoundException ex) {
316: // OK, expected
317: assertTrue(true);
318: }
319: tx.abort();
320: }
321: }
|