1 package org.apache.ojb.broker; 2 3 import java.io.File ; 4 import java.io.FileInputStream ; 5 import java.io.FileOutputStream ; 6 import java.io.ObjectInputStream ; 7 import java.io.ObjectOutputStream ; 8 import java.util.Iterator ; 9 import java.util.Vector ; 10 11 import junit.framework.TestCase; 12 import org.apache.commons.lang.SerializationUtils; 13 import org.apache.ojb.broker.metadata.ClassDescriptor; 14 import org.apache.ojb.broker.metadata.DescriptorRepository; 15 import org.apache.ojb.broker.metadata.MetadataManager; 16 17 22 public class MetaDataSerializationTest extends TestCase 23 { 24 private static Class CLASS = MetaDataSerializationTest.class; 25 26 30 public MetaDataSerializationTest(String name) 31 { 32 super(name); 33 } 34 35 40 public static void main(String [] args) 41 { 42 String [] arr = {CLASS.getName()}; 43 junit.textui.TestRunner.main(arr); 44 } 45 46 49 public void testToDisk() throws Exception 50 { 51 DescriptorRepository repository = MetadataManager.getInstance().getRepository(); 52 Iterator iter = repository.iterator(); 53 Vector vec = new Vector (); 54 while (iter.hasNext()) 55 { 56 vec.add(iter.next()); 57 } 58 File outfile = new File ("repository.serialized"); 59 FileOutputStream fos = new FileOutputStream (outfile); 60 ObjectOutputStream oos = new ObjectOutputStream (fos); 61 oos.writeObject(vec); 62 oos.close(); 63 fos.close(); 64 } 65 66 69 public void testFromDisk() throws Exception 70 { 71 File infile = new File ("repository.serialized"); 72 FileInputStream fis = new FileInputStream (infile); 73 ObjectInputStream ois = new ObjectInputStream (fis); 74 Vector vec = (Vector ) ois.readObject(); 75 ois.close(); 76 fis.close(); 77 78 Iterator iter = vec.iterator(); 79 while (iter.hasNext()) 80 { 81 ClassDescriptor cld = (ClassDescriptor) iter.next(); 82 } 83 } 84 85 86 90 public void testRemote() throws Exception 91 { 92 ClassDescriptor cld = null; 93 PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 94 for (int i = 0; i < 5; i++) 95 { 96 DescriptorRepository repository = MetadataManager.getInstance().getRepository(); 97 Iterator iter = repository.iterator(); 98 while (iter.hasNext()) 99 { 100 cld = (ClassDescriptor) iter.next(); 101 103 ClassDescriptor cldRemote = broker.getClassDescriptor(cld.getClassOfObject()); 104 } 105 } 106 broker.close(); 107 } 108 109 113 public void testIdentityStuff() throws Exception 114 { 115 PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 116 DescriptorRepository repository = MetadataManager.getInstance().getRepository(); 117 Iterator iter = repository.iterator(); 118 while (iter.hasNext()) 119 { 120 ClassDescriptor cld = (ClassDescriptor) iter.next(); 121 Class c = cld.getClassOfObject(); 123 if (!c.isInterface()) 124 { 125 126 Object o = null; 127 try 128 { 129 o = c.newInstance(); 130 Identity oid = new Identity(o, broker); 131 } 133 catch (InstantiationException e) 134 { 135 } 136 catch (IllegalAccessException e) 137 { 138 } 139 } 140 } 141 broker.close(); 142 } 143 144 148 public void XXXtestClassDescriptorSerialization() throws Exception 149 { 150 DescriptorRepository repository = MetadataManager.getInstance().getRepository(); 151 Iterator iter = repository.iterator(); 152 while (iter.hasNext()) 153 { 154 ClassDescriptor cld = (ClassDescriptor) iter.next(); 155 160 byte[] arr = SerializationUtils.serialize(cld); 161 ClassDescriptor cld1 = (ClassDescriptor) SerializationUtils.deserialize(arr); 162 } 163 } 164 } 165 | Popular Tags |