01: package org.apache.ojb.broker.metadata;
02:
03: import java.io.FileOutputStream;
04: import java.util.ArrayList;
05: import java.util.Iterator;
06: import java.util.List;
07:
08: import org.apache.commons.collections.CollectionUtils;
09: import org.apache.ojb.broker.PBKey;
10: import org.apache.ojb.broker.TestHelper;
11: import org.apache.ojb.junit.OJBTestCase;
12:
13: /**
14: * This TestClass tests the RepositoryPersitors facilities for
15: * reading and writing a valid repository.
16: */
17: public class RepositoryPersistorTest extends OJBTestCase {
18: public static void main(String[] args) {
19: String[] arr = { RepositoryPersistorTest.class.getName() };
20: junit.textui.TestRunner.main(arr);
21: }
22:
23: public RepositoryPersistorTest(String name) {
24: super (name);
25: }
26:
27: /** Test storing/read repository.*/
28: public void testStoreReadRepository() throws Exception {
29: String fileNew = "test_repository_rewritten.xml";
30: String file = "repository.xml";
31: RepositoryPersistor persistor = new RepositoryPersistor();
32: DescriptorRepository repository = persistor
33: .readDescriptorRepository(file);
34: ConnectionRepository conRepository = MetadataManager
35: .getInstance().connectionRepository();
36: Iterator iter = repository.iterator();
37: int numClasses = 0;
38: List list = new ArrayList();
39: while (iter.hasNext()) {
40: ClassDescriptor cld = ((ClassDescriptor) iter.next());
41: if (!list.contains(cld.getClassOfObject())) {
42: list.add(cld.getClassOfObject());
43: } else {
44: fail("## Duplicate cld: " + cld.toXML());
45: }
46: numClasses++;
47: }
48:
49: persistor = new RepositoryPersistor();
50: FileOutputStream fos = new FileOutputStream(fileNew);
51: persistor.writeToFile(repository, conRepository, fos);
52:
53: DescriptorRepository second = persistor
54: .readDescriptorRepository(fileNew);
55: iter = second.iterator();
56: int numClasses2 = 0;
57: List list2 = new ArrayList();
58: while (iter.hasNext()) {
59: list2.add(((ClassDescriptor) iter.next())
60: .getClassOfObject());
61: numClasses2++;
62: }
63: assertEquals(
64: "read in persisted repository should have same number of classes, the differences are "
65: + CollectionUtils.disjunction(list, list2),
66: numClasses, numClasses2);
67: assertTrue(numClasses2 > 0);
68: }
69:
70: /** Test storing/read repository.*/
71: public void testStoreReadConnectionRepository() throws Exception {
72: String filename = "test_repository_database.xml";
73: DescriptorRepository repository = MetadataManager.getInstance()
74: .getRepository();
75: ConnectionRepository conRepository = MetadataManager
76: .getInstance().connectionRepository();
77: int connectionCount = conRepository.getAllDescriptor().size();
78:
79: FileOutputStream fos = new FileOutputStream(filename);
80: RepositoryPersistor persistor = new RepositoryPersistor();
81: persistor.writeToFile(repository, conRepository, fos);
82:
83: ConnectionRepository second = persistor
84: .readConnectionRepository(filename);
85: int connectionCount2 = second.getAllDescriptor().size();
86:
87: PBKey defaultKey = second
88: .getStandardPBKeyForJcdAlias(TestHelper.DEF_JCD_ALIAS);
89: assertNotNull(defaultKey);
90: assertEquals(TestHelper.DEF_KEY, defaultKey);
91:
92: assertTrue(connectionCount2 > 0);
93: assertEquals(
94: "read in persisted connection repository should have same number of classes",
95: connectionCount, connectionCount2);
96: }
97: }
|