001: package org.apache.ojb.broker;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.io.FileOutputStream;
006: import java.io.ObjectInputStream;
007: import java.io.ObjectOutputStream;
008: import java.util.Iterator;
009: import java.util.Vector;
010:
011: import junit.framework.TestCase;
012: import org.apache.commons.lang.SerializationUtils;
013: import org.apache.ojb.broker.metadata.ClassDescriptor;
014: import org.apache.ojb.broker.metadata.DescriptorRepository;
015: import org.apache.ojb.broker.metadata.MetadataManager;
016:
017: /**
018: * Insert the type's description here.
019: * Creation date: (06.12.2000 21:47:56)
020: * @author Thomas Mahler
021: */
022: public class MetaDataSerializationTest extends TestCase {
023: private static Class CLASS = MetaDataSerializationTest.class;
024:
025: /**
026: * BrokerTests constructor comment.
027: * @param name java.lang.String
028: */
029: public MetaDataSerializationTest(String name) {
030: super (name);
031: }
032:
033: /**
034: * Insert the method's description here.
035: * Creation date: (23.12.2000 18:30:38)
036: * @param args java.lang.String[]
037: */
038: public static void main(String[] args) {
039: String[] arr = { CLASS.getName() };
040: junit.textui.TestRunner.main(arr);
041: }
042:
043: /**
044: * test serialization of ClassDescriptors to disk
045: */
046: public void testToDisk() throws Exception {
047: DescriptorRepository repository = MetadataManager.getInstance()
048: .getRepository();
049: Iterator iter = repository.iterator();
050: Vector vec = new Vector();
051: while (iter.hasNext()) {
052: vec.add(iter.next());
053: }
054: File outfile = new File("repository.serialized");
055: FileOutputStream fos = new FileOutputStream(outfile);
056: ObjectOutputStream oos = new ObjectOutputStream(fos);
057: oos.writeObject(vec);
058: oos.close();
059: fos.close();
060: }
061:
062: /**
063: * test deserialization of ClassDescriptors from disk
064: */
065: public void testFromDisk() throws Exception {
066: File infile = new File("repository.serialized");
067: FileInputStream fis = new FileInputStream(infile);
068: ObjectInputStream ois = new ObjectInputStream(fis);
069: Vector vec = (Vector) ois.readObject();
070: ois.close();
071: fis.close();
072:
073: Iterator iter = vec.iterator();
074: while (iter.hasNext()) {
075: ClassDescriptor cld = (ClassDescriptor) iter.next();
076: }
077: }
078:
079: /**
080: * test serialization and deserialisation of all ClassDescriptors in
081: * Descriptor repository
082: */
083: public void testRemote() throws Exception {
084: ClassDescriptor cld = null;
085: PersistenceBroker broker = PersistenceBrokerFactory
086: .defaultPersistenceBroker();
087: for (int i = 0; i < 5; i++) {
088: DescriptorRepository repository = MetadataManager
089: .getInstance().getRepository();
090: Iterator iter = repository.iterator();
091: while (iter.hasNext()) {
092: cld = (ClassDescriptor) iter.next();
093: //System.out.println("CLD remote: " + cld.getClassOfObject().getName());
094:
095: ClassDescriptor cldRemote = broker
096: .getClassDescriptor(cld.getClassOfObject());
097: }
098: }
099: broker.close();
100: }
101:
102: /**
103: * test serialization and deserialisation of all ClassDescriptors in
104: * Descriptor repository
105: */
106: public void testIdentityStuff() throws Exception {
107: PersistenceBroker broker = PersistenceBrokerFactory
108: .defaultPersistenceBroker();
109: DescriptorRepository repository = MetadataManager.getInstance()
110: .getRepository();
111: Iterator iter = repository.iterator();
112: while (iter.hasNext()) {
113: ClassDescriptor cld = (ClassDescriptor) iter.next();
114: //System.out.println("CLD: " + cld.getClassOfObject().getName());
115: Class c = cld.getClassOfObject();
116: if (!c.isInterface()) {
117:
118: Object o = null;
119: try {
120: o = c.newInstance();
121: Identity oid = new Identity(o, broker);
122: //System.out.println(oid.toString());
123: } catch (InstantiationException e) {
124: } catch (IllegalAccessException e) {
125: }
126: }
127: }
128: broker.close();
129: }
130:
131: /**
132: * test serialization and deserialisation of all ClassDescriptors in
133: * Descriptor repository
134: */
135: public void XXXtestClassDescriptorSerialization() throws Exception {
136: DescriptorRepository repository = MetadataManager.getInstance()
137: .getRepository();
138: Iterator iter = repository.iterator();
139: while (iter.hasNext()) {
140: ClassDescriptor cld = (ClassDescriptor) iter.next();
141: // System.out.println("CLD: " + cld.getClassOfObject().getName());
142: // byte[] arr = serialize(cld);
143: //
144: // ClassDescriptor cld1 = deserialize(arr);
145:
146: byte[] arr = SerializationUtils.serialize(cld);
147: ClassDescriptor cld1 = (ClassDescriptor) SerializationUtils
148: .deserialize(arr);
149: }
150: }
151: }
|