KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > metadata > RepositoryPersistorTest


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