1 package org.apache.ojb.broker.metadata; 2 3 import java.io.FileOutputStream ; 4 import java.util.ArrayList ; 5 import java.util.Iterator ; 6 import java.util.List ; 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 17 public class RepositoryPersistorTest extends OJBTestCase 18 { 19 public static void main(String [] args) 20 { 21 String [] arr = { RepositoryPersistorTest.class.getName()}; 22 junit.textui.TestRunner.main(arr); 23 } 24 25 public RepositoryPersistorTest(String name) 26 { 27 super(name); 28 } 29 30 31 public void testStoreReadRepository() throws Exception 32 { 33 String fileNew = "test_repository_rewritten.xml"; 34 String file = "repository.xml"; 35 RepositoryPersistor persistor = new RepositoryPersistor(); 36 DescriptorRepository repository = persistor.readDescriptorRepository(file); 37 ConnectionRepository conRepository = MetadataManager.getInstance().connectionRepository(); 38 Iterator iter = repository.iterator(); 39 int numClasses = 0; 40 List list = new ArrayList (); 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 fos = new FileOutputStream (fileNew); 57 persistor.writeToFile(repository, conRepository, fos); 58 59 DescriptorRepository second = persistor.readDescriptorRepository(fileNew); 60 iter = second.iterator(); 61 int numClasses2 = 0; 62 List list2 = new ArrayList (); 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 75 public void testStoreReadConnectionRepository() throws Exception 76 { 77 String 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 fos = new FileOutputStream (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 |