001: package org.apache.ojb.broker.metadata;
002:
003: import java.util.Iterator;
004:
005: import junit.framework.TestCase;
006: import org.apache.ojb.broker.Identity;
007: import org.apache.ojb.broker.ObjectRepository;
008: import org.apache.ojb.broker.PBKey;
009: import org.apache.ojb.broker.PersistenceBroker;
010: import org.apache.ojb.broker.PersistenceBrokerException;
011: import org.apache.ojb.broker.PersistenceBrokerFactory;
012: import org.apache.ojb.broker.TestHelper;
013: import org.apache.ojb.broker.cache.ObjectCacheDefaultImpl;
014: import org.apache.ojb.broker.cache.ObjectCacheEmptyImpl;
015: import org.apache.ojb.broker.query.Criteria;
016: import org.apache.ojb.broker.query.Query;
017: import org.apache.ojb.broker.query.QueryFactory;
018: import org.apache.ojb.broker.sequence.Repository;
019: import org.apache.ojb.broker.util.sequence.SequenceManagerHighLowImpl;
020: import org.apache.ojb.odmg.OJB;
021: import org.odmg.Database;
022: import org.odmg.Implementation;
023: import org.odmg.OQLQuery;
024: import org.odmg.Transaction;
025:
026: /**
027: * This TestClass tests the RepositoryPersitors facilities for
028: * reading and writing a valid repository.
029: */
030: public class MetadataTest extends TestCase {
031: public static final String TEST_CLASS_DESCRIPTOR = "Test_ClassDescriptor.xml";
032: public static final String TEST_CONNECTION_DESCRIPTOR = "Test_ConnectionDescriptor.xml";
033: public static final String TEST_REPOSITORY = "Test_Repository.xml";
034:
035: /**
036: * A persistent object class
037: */
038: private static final Class TEST_CLASS = Repository.SMKey.class;
039:
040: public static void main(String[] args) {
041: String[] arr = { MetadataTest.class.getName() };
042: junit.textui.TestRunner.main(arr);
043: }
044:
045: public MetadataTest(String name) {
046: super (name);
047: }
048:
049: public void setUp() {
050: }
051:
052: public void tearDown() {
053: try {
054: MetadataManager.getInstance().setEnablePerThreadChanges(
055: false);
056: } catch (Exception e) {
057: }
058: }
059:
060: public void testFindFirstConcreteClassDescriptor() {
061: DescriptorRepository dr = MetadataManager.getInstance()
062: .getRepository();
063: ClassDescriptor cld = dr
064: .getDescriptorFor(Repository.SMInterface.class);
065: ClassDescriptor firstConcrete = dr.findFirstConcreteClass(cld);
066: assertFalse(firstConcrete.isInterface());
067: assertFalse(firstConcrete.isAbstract());
068: firstConcrete = dr.findFirstConcreteClass(cld);
069: firstConcrete = dr.findFirstConcreteClass(cld);
070: }
071:
072: public void testDescriptorRepository_1() {
073: MetadataManager mm = MetadataManager.getInstance();
074: DescriptorRepository dr = mm.copyOfGlobalRepository();
075: // get an class/interface with extents
076: ClassDescriptor cld = dr
077: .getDescriptorFor(Repository.SMMax.class);
078:
079: int extentSize = cld.getExtentClasses().size();
080: Class topLevelInterface = dr
081: .getTopLevelClass(Repository.SMMax.class);
082: Class topLevelExtent = dr
083: .getTopLevelClass(Repository.SMMaxA.class);
084: assertEquals(Repository.SMMax.class, topLevelInterface);
085: assertEquals(Repository.SMMax.class, topLevelExtent);
086: assertEquals(2, extentSize);
087:
088: dr.removeExtent(Repository.SMMaxA.class.getName());
089: int extentSizeNew = cld.getExtentClasses().size();
090: Class topLevelInterfaceNew = dr
091: .getTopLevelClass(Repository.SMMax.class);
092: Class topLevelExtentNew = dr
093: .getTopLevelClass(Repository.SMMaxA.class);
094: assertEquals(Repository.SMMax.class, topLevelInterfaceNew);
095: assertEquals(Repository.SMMaxA.class, topLevelExtentNew);
096: assertEquals(1, extentSizeNew);
097: }
098:
099: public void testDescriptorRepository_2() {
100: MetadataManager mm = MetadataManager.getInstance();
101: DescriptorRepository dr = mm.copyOfGlobalRepository();
102: // get an class/interface with extents
103: ClassDescriptor cld = dr
104: .getDescriptorFor(Repository.SMMax.class);
105: int allSubClasses = dr.getAllConcreteSubclassDescriptors(cld)
106: .size();
107: int allExtents = cld.getExtentClasses().size();
108: int allExtentNames = cld.getExtentClassNames().size();
109: assertEquals(allExtents, allExtentNames);
110:
111: dr.remove(Repository.SMMaxA.class);
112: // after removing SMMaxA, SMMax interface lost 4 concrete extents (sub-classes)
113: // be carefully in changing SMM*** metadata, could make fail this test
114: int allSubClassesNew = dr
115: .getAllConcreteSubclassDescriptors(cld).size();
116: int allExtentsNew = cld.getExtentClasses().size();
117: int allExtentNamesNew = cld.getExtentClassNames().size();
118: assertEquals(allExtentsNew, allExtentNamesNew);
119: assertEquals(allSubClasses - 4, allSubClassesNew);
120: }
121:
122: public void testClassDescriptor_1() {
123: MetadataManager mm = MetadataManager.getInstance();
124: DescriptorRepository dr = mm.copyOfGlobalRepository();
125: ClassDescriptor cld = dr
126: .getDescriptorFor(ObjectRepository.Component.class);
127:
128: FieldDescriptor[] a = cld.getAutoIncrementFields();
129: assertEquals("autoincrement field should be found", 1, a.length);
130: FieldDescriptor target = cld.getFieldDescriptorByName("id");
131: cld.removeFieldDescriptor(target);
132: a = cld.getAutoIncrementFields();
133: assertEquals("autoincrement PK should be deleted", 0, a.length);
134: assertNull(cld.getFieldDescriptorByName("id"));
135:
136: cld.addFieldDescriptor(target);
137: a = cld.getAutoIncrementFields();
138: assertEquals("autoincrement field should be found", 1, a.length);
139: assertNotNull(cld.getFieldDescriptorByName("id"));
140:
141: }
142:
143: public void testLoadingProfiles() throws Exception {
144: PersistenceBroker broker = null;
145: MetadataManager mm = MetadataManager.getInstance();
146: try {
147: mm.setEnablePerThreadChanges(true);
148: DescriptorRepository dr_1 = mm
149: .readDescriptorRepository(TEST_CLASS_DESCRIPTOR);
150: // add some profiles
151: mm.addProfile("global", mm.copyOfGlobalRepository());
152: mm.addProfile("test", dr_1);
153:
154: // now load a specific profile
155: mm.loadProfile("test");
156: broker = PersistenceBrokerFactory
157: .defaultPersistenceBroker();
158: CldTestObject obj = new CldTestObject();
159: obj.setName("testLoadingProfiles");
160: try {
161: broker.beginTransaction();
162: broker.store(obj);
163: broker.commitTransaction();
164: try {
165: // try to find persistent object, only available in global
166: // repository profile
167: Class clazz = broker.getClassDescriptor(TEST_CLASS)
168: .getClassOfObject();
169: assertNull(
170: "We should not found this class-descriptor in profile",
171: clazz);
172: } catch (PersistenceBrokerException e) {
173: assertTrue(true);
174: }
175: } finally {
176: broker.close();
177: }
178:
179: //***************************************
180: mm.removeProfile("test");
181: try {
182: mm.loadProfile("test");
183: fail("Loading of profile should fail, but doesn't");
184: } catch (Exception e) {
185: // we expect exception
186: }
187: // now we load copy of global DescriptorRepository
188: mm.loadProfile("global");
189: broker = PersistenceBrokerFactory
190: .defaultPersistenceBroker();
191: Class clazz = broker.getClassDescriptor(TEST_CLASS)
192: .getClassOfObject();
193:
194: ObjectRepository.Component compChild = new ObjectRepository.Component();
195: compChild.setName("MetadataTest_child");
196:
197: ObjectRepository.Component compParent = new ObjectRepository.Component();
198: compParent.setName("MetadataTest_parent");
199: compChild.setParentComponent(compParent);
200:
201: broker.beginTransaction();
202: broker.store(compChild);
203: broker.commitTransaction();
204:
205: Identity oid = new Identity(compChild, broker);
206: broker.clearCache();
207: compChild = (ObjectRepository.Component) broker
208: .getObjectByIdentity(oid);
209:
210: assertNotNull(compChild);
211: assertNotNull(compChild.getParentComponent());
212:
213: broker.close();
214: assertEquals(TEST_CLASS, clazz);
215: mm.removeAllProfiles();
216: try {
217: mm.loadProfile("global");
218: fail("Loading of profile should fail, but doesn't");
219: } catch (Exception e) {
220: }
221:
222: broker = PersistenceBrokerFactory
223: .defaultPersistenceBroker();
224: clazz = broker.getClassDescriptor(TEST_CLASS)
225: .getClassOfObject();
226: broker.close();
227: assertEquals(TEST_CLASS, clazz);
228: } finally {
229: mm.setEnablePerThreadChanges(false);
230: if (broker != null)
231: broker.close();
232: }
233:
234: }
235:
236: public void testRuntimeMergeConnectionDescriptor() throws Exception {
237:
238: MetadataManager mm = MetadataManager.getInstance();
239: ConnectionRepository cr = mm
240: .readConnectionRepository(TEST_CONNECTION_DESCRIPTOR);
241: mm.mergeConnectionRepository(cr);
242:
243: ConnectionRepository mergedCR = mm.connectionRepository();
244: JdbcConnectionDescriptor jcd = mergedCR
245: .getDescriptor(new PBKey("runtime"));
246: assertNotNull("Runtime merge of ConnectionRepository failed",
247: jcd);
248: }
249:
250: public void testRuntimeMergeDescriptorRepository() throws Exception {
251: MetadataManager mm = MetadataManager.getInstance();
252: DescriptorRepository dr = mm
253: .readDescriptorRepository(TEST_CLASS_DESCRIPTOR);
254: mm.mergeDescriptorRepository(dr);
255:
256: DescriptorRepository mergedDR = mm.getRepository();
257: ClassDescriptor cld = mergedDR
258: .getDescriptorFor(MetadataTest.CldTestObject.class);
259: assertNotNull("Runtime merge of DescriptorRepository failed",
260: cld);
261: }
262:
263: /**
264: * test to check PB create with PBKey
265: */
266: public void testLookupPB1() {
267: PBKey key1 = new PBKey(TestHelper.DEF_JCD_ALIAS);
268: PBKey key2 = new PBKey(TestHelper.DEF_JCD_ALIAS,
269: TestHelper.DEF_USER, TestHelper.DEF_PASSWORD);
270: PBKey key3 = new PBKey(TestHelper.FAR_AWAY_JCD_ALIAS);
271: Query query = QueryFactory.newQuery(TEST_CLASS, new Criteria());
272:
273: PersistenceBroker broker = PersistenceBrokerFactory
274: .createPersistenceBroker(key1);
275: broker.getCount(query);
276: broker.close();
277:
278: broker = PersistenceBrokerFactory.createPersistenceBroker(key2);
279: broker.getCount(query);
280: broker.close();
281:
282: broker = PersistenceBrokerFactory.createPersistenceBroker(key3);
283: broker.getCount(query);
284: broker.close();
285: }
286:
287: /**
288: * test to check PB create with PBKey
289: */
290: public void testLookupPB2() {
291: PBKey key1 = new PBKey(TestHelper.DEF_JCD_ALIAS,
292: "!!TestCase: This should fail!!", "nothing");
293: Query query = QueryFactory.newQuery(TEST_CLASS, new Criteria());
294: PersistenceBroker broker = PersistenceBrokerFactory
295: .createPersistenceBroker(key1);
296: // hsql is not very strict in user handling
297: try {
298: broker.getCount(query);
299: fail("We excect a exception, because we pass a PBKey with user and password that doesn't exist");
300: } catch (Exception e) {
301: assertTrue(true);
302: }
303: broker.close();
304: }
305:
306: /**
307: * test to check database open
308: */
309: public void testLookupDatabase() throws Exception {
310: String queryStr = "select allArticle from "
311: + TEST_CLASS.getName();
312: Implementation odmg = OJB.getInstance();
313: Transaction tx;
314: Database db = odmg.newDatabase();
315: db.open(TestHelper.DEF_JCD_ALIAS, Database.OPEN_READ_WRITE);
316: db.close();
317: db = odmg.newDatabase();
318: db.open(TestHelper.DEF_JCD_ALIAS + "#" + TestHelper.DEF_USER
319: + "#" + TestHelper.DEF_PASSWORD,
320: Database.OPEN_READ_WRITE);
321: tx = odmg.newTransaction();
322: tx.begin();
323: OQLQuery query = odmg.newOQLQuery();
324: query.create(queryStr);
325: query.execute();
326: tx.commit();
327: db.close();
328:
329: db = odmg.newDatabase();
330: db.open(TestHelper.DEF_JCD_ALIAS, Database.OPEN_READ_WRITE);
331: tx = odmg.newTransaction();
332: tx.begin();
333: OQLQuery query2 = odmg.newOQLQuery();
334: query2.create(queryStr);
335: query2.execute();
336: tx.commit();
337: db.close();
338: }
339:
340: public void testTimeToCopyRepository() {
341: DescriptorRepository dr = null;
342: int loop = 5;
343: long period = System.currentTimeMillis();
344: for (int i = 0; i < loop; i++) {
345: dr = MetadataManager.getInstance().copyOfGlobalRepository();
346: }
347: period = System.currentTimeMillis() - period;
348: int descriptors = 0;
349: Iterator it = dr.iterator();
350: while (it.hasNext()) {
351: it.next();
352: ++descriptors;
353: }
354: System.out.println("# Time to create a copy of " + descriptors
355: + " class-descriptors: " + period / loop + " ms #");
356: }
357:
358: public void testObjectCacheDeclarations() {
359: DescriptorRepository dr = MetadataManager.getInstance()
360: .readDescriptorRepository(TEST_REPOSITORY);
361: ConnectionRepository cr = MetadataManager.getInstance()
362: .readConnectionRepository(TEST_REPOSITORY);
363:
364: ObjectCacheDescriptor ocd;
365: ocd = ((JdbcConnectionDescriptor) cr.getAllDescriptor().get(0))
366: .getObjectCacheDescriptor();
367: assertNotNull(ocd);
368: assertNotNull(ocd.getObjectCache());
369: assertEquals(ObjectCacheEmptyImpl.class, ocd.getObjectCache());
370: assertNotNull(ocd.getAttribute("attr_con"));
371: assertNull("Wrong custom attribute found", ocd
372: .getAttribute("attr_class"));
373: assertEquals("555", ocd.getAttribute("attr_con"));
374:
375: ocd = dr.getDescriptorFor(CacheObject.class)
376: .getObjectCacheDescriptor();
377: assertNotNull(ocd);
378: assertNotNull(ocd.getObjectCache());
379: assertEquals(ObjectCacheDefaultImpl.class, ocd.getObjectCache());
380: assertNotNull(ocd.getAttribute("attr_class"));
381: assertNull("Wrong custom attribute found", ocd
382: .getAttribute("attr_con"));
383: assertEquals("444", ocd.getAttribute("attr_class"));
384: }
385:
386: public void testReadConnectionDescriptor() {
387: JdbcConnectionDescriptor jcd = MetadataManager.getInstance()
388: .connectionRepository().getDescriptor(
389: new PBKey("testConnection", "a user",
390: "a password"));
391: /* descriptor snip
392:
393: <jdbc-connection-descriptor
394: jcd-alias="testConnection"
395: default-connection="false"
396: platform="Oracle"
397: jdbc-level="1.0"
398: driver="a driver"
399: protocol="a protocol"
400: subprotocol="a subprotocol"
401: dbalias="myDbalias"
402: username="a user"
403: password="a password"
404: eager-release="true"
405: batch-mode="true"
406: useAutoCommit="0"
407: ignoreAutoCommitExceptions="true"
408: >
409:
410: <object-cache class="org.apache.ojb.broker.cache.ObjectCacheEmptyImpl">
411: <attribute attribute-name="cacheKey1" attribute-value="cacheValue1"/>
412: <attribute attribute-name="cacheKey2" attribute-value="cacheValue2"/>
413: </object-cache>
414:
415: <connection-pool
416: maxActive="1"
417: maxIdle="2"
418: maxWait="3"
419: minEvictableIdleTimeMillis="4"
420: numTestsPerEvictionRun="5"
421: testOnBorrow="true"
422: testOnReturn="true"
423: testWhileIdle="true"
424: timeBetweenEvictionRunsMillis="6"
425: whenExhaustedAction="2"
426: validationQuery="a query"
427: logAbandoned="true"
428: removeAbandoned="true"
429: removeAbandonedTimeout="8"
430: />
431:
432: <sequence-manager className="org.apache.ojb.broker.util.sequence.SequenceManagerHighLowImpl">
433: <attribute attribute-name="key1" attribute-value="value1"/>
434: <attribute attribute-name="key2" attribute-value="value2"/>
435: </sequence-manager>
436: </jdbc-connection-descriptor>
437: */
438: // don't set it to true!!! This may break everything (2 default connections)
439: assertEquals(false, jcd.isDefaultConnection());
440:
441: assertNotNull(jcd.getDbms());
442: assertEquals("Oracle", jcd.getDbms());
443: assertEquals(1.0d, jcd.getJdbcLevel(), 0.1);
444:
445: assertNotNull(jcd.getDriver());
446: assertEquals("a driver", jcd.getDriver());
447:
448: assertNotNull(jcd.getProtocol());
449: assertEquals("a protocol", jcd.getProtocol());
450:
451: assertNotNull(jcd.getSubProtocol());
452: assertEquals("a subprotocol", jcd.getSubProtocol());
453:
454: assertNotNull(jcd.getDbAlias());
455: assertEquals("myDbalias", jcd.getDbAlias());
456:
457: assertNotNull(jcd.getUserName());
458: assertEquals("a user", jcd.getUserName());
459:
460: assertNotNull(jcd.getPassWord());
461: assertEquals("a password", jcd.getPassWord());
462:
463: assertEquals(true, jcd.getEagerRelease());
464:
465: assertEquals(true, jcd.getBatchMode());
466:
467: assertEquals(0, jcd.getUseAutoCommit());
468: assertEquals(true, jcd.isIgnoreAutoCommitExceptions());
469:
470: ObjectCacheDescriptor ocd = jcd.getObjectCacheDescriptor();
471: assertNotNull(ocd);
472: assertNotNull(ocd.getObjectCache());
473: assertEquals(ocd.getObjectCache(), ObjectCacheEmptyImpl.class);
474: assertNotNull(ocd.getAttribute("cacheKey1"));
475: assertNotNull(ocd.getAttribute("cacheKey2"));
476: assertEquals("cacheValue1", ocd.getAttribute("cacheKey1"));
477: assertEquals("cacheValue2", ocd.getAttribute("cacheKey2"));
478:
479: ConnectionPoolDescriptor cpd = jcd
480: .getConnectionPoolDescriptor();
481: assertEquals(1, cpd.getMaxActive());
482: assertEquals(2, cpd.getMaxIdle());
483: assertEquals(3, cpd.getMaxWait());
484: assertEquals(4, cpd.getMinEvictableIdleTimeMillis());
485: assertEquals(5, cpd.getNumTestsPerEvictionRun());
486: assertEquals(true, cpd.isTestOnBorrow());
487: assertEquals(true, cpd.isTestOnReturn());
488: assertEquals(true, cpd.isTestWhileIdle());
489: assertEquals(6, cpd.getTimeBetweenEvictionRunsMillis());
490: assertEquals(2, cpd.getWhenExhaustedAction());
491:
492: assertNotNull(cpd.getValidationQuery());
493: assertEquals("a query", cpd.getValidationQuery());
494:
495: assertEquals(true, cpd.isLogAbandoned());
496: assertEquals(true, cpd.isRemoveAbandoned());
497: assertEquals(8, cpd.getRemoveAbandonedTimeout());
498:
499: SequenceDescriptor seq = jcd.getSequenceDescriptor();
500: assertEquals(SequenceManagerHighLowImpl.class.getName(), seq
501: .getSequenceManagerClass().getName());
502:
503: assertNotNull(seq.getAttribute("key1"));
504: assertEquals("value1", seq.getAttribute("key1"));
505:
506: assertNotNull(seq.getAttribute("key2"));
507: assertEquals("value2", seq.getAttribute("key2"));
508: }
509:
510: // ======================================================================
511: // inner test class
512: // ======================================================================
513: public static class CldTestObject {
514: int id;
515: String name;
516:
517: public CldTestObject() {
518: }
519:
520: public CldTestObject(int id, String name) {
521: this .id = id;
522: this .name = name;
523: }
524:
525: public int getId() {
526: return id;
527: }
528:
529: public void setId(int id) {
530: this .id = id;
531: }
532:
533: public String getName() {
534: return name;
535: }
536:
537: public void setName(String name) {
538: this .name = name;
539: }
540: }
541:
542: public static class CacheObject {
543: Integer objId;
544: String name;
545:
546: public Integer getObjId() {
547: return objId;
548: }
549:
550: public void setObjId(Integer objId) {
551: this .objId = objId;
552: }
553:
554: public String getName() {
555: return name;
556: }
557:
558: public void setName(String name) {
559: this.name = name;
560: }
561: }
562: }
|