KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > MetaDataSerializationTest


1 package org.apache.ojb.broker;
2
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.FileOutputStream JavaDoc;
6 import java.io.ObjectInputStream JavaDoc;
7 import java.io.ObjectOutputStream JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.Vector JavaDoc;
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 /**
18  * Insert the type's description here.
19  * Creation date: (06.12.2000 21:47:56)
20  * @author Thomas Mahler
21  */

22 public class MetaDataSerializationTest extends TestCase
23 {
24     private static Class JavaDoc CLASS = MetaDataSerializationTest.class;
25
26     /**
27      * BrokerTests constructor comment.
28      * @param name java.lang.String
29      */

30     public MetaDataSerializationTest(String JavaDoc name)
31     {
32         super(name);
33     }
34
35     /**
36      * Insert the method's description here.
37      * Creation date: (23.12.2000 18:30:38)
38      * @param args java.lang.String[]
39      */

40     public static void main(String JavaDoc[] args)
41     {
42         String JavaDoc[] arr = {CLASS.getName()};
43         junit.textui.TestRunner.main(arr);
44     }
45
46     /**
47      * test serialization of ClassDescriptors to disk
48      */

49     public void testToDisk() throws Exception JavaDoc
50     {
51         DescriptorRepository repository = MetadataManager.getInstance().getRepository();
52         Iterator JavaDoc iter = repository.iterator();
53         Vector JavaDoc vec = new Vector JavaDoc();
54         while (iter.hasNext())
55         {
56             vec.add(iter.next());
57         }
58         File JavaDoc outfile = new File JavaDoc("repository.serialized");
59         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(outfile);
60         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(fos);
61         oos.writeObject(vec);
62         oos.close();
63         fos.close();
64     }
65
66     /**
67      * test deserialization of ClassDescriptors from disk
68      */

69     public void testFromDisk() throws Exception JavaDoc
70     {
71         File JavaDoc infile = new File JavaDoc("repository.serialized");
72         FileInputStream JavaDoc fis = new FileInputStream JavaDoc(infile);
73         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(fis);
74         Vector JavaDoc vec = (Vector JavaDoc) ois.readObject();
75         ois.close();
76         fis.close();
77
78         Iterator JavaDoc iter = vec.iterator();
79         while (iter.hasNext())
80         {
81             ClassDescriptor cld = (ClassDescriptor) iter.next();
82         }
83     }
84
85
86     /**
87      * test serialization and deserialisation of all ClassDescriptors in
88      * Descriptor repository
89      */

90     public void testRemote() throws Exception JavaDoc
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 JavaDoc iter = repository.iterator();
98             while (iter.hasNext())
99             {
100                 cld = (ClassDescriptor) iter.next();
101                 //System.out.println("CLD remote: " + cld.getClassOfObject().getName());
102

103                 ClassDescriptor cldRemote = broker.getClassDescriptor(cld.getClassOfObject());
104             }
105         }
106         broker.close();
107     }
108
109     /**
110      * test serialization and deserialisation of all ClassDescriptors in
111      * Descriptor repository
112      */

113     public void testIdentityStuff() throws Exception JavaDoc
114     {
115         PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
116         DescriptorRepository repository = MetadataManager.getInstance().getRepository();
117         Iterator JavaDoc iter = repository.iterator();
118         while (iter.hasNext())
119         {
120             ClassDescriptor cld = (ClassDescriptor) iter.next();
121             //System.out.println("CLD: " + cld.getClassOfObject().getName());
122
Class JavaDoc c = cld.getClassOfObject();
123             if (!c.isInterface())
124             {
125
126                 Object JavaDoc o = null;
127                 try
128                 {
129                     o = c.newInstance();
130                     Identity oid = new Identity(o, broker);
131                     //System.out.println(oid.toString());
132
}
133                 catch (InstantiationException JavaDoc e)
134                 {
135                 }
136                 catch (IllegalAccessException JavaDoc e)
137                 {
138                 }
139             }
140         }
141         broker.close();
142     }
143
144     /**
145      * test serialization and deserialisation of all ClassDescriptors in
146      * Descriptor repository
147      */

148     public void XXXtestClassDescriptorSerialization() throws Exception JavaDoc
149     {
150         DescriptorRepository repository = MetadataManager.getInstance().getRepository();
151         Iterator JavaDoc iter = repository.iterator();
152         while (iter.hasNext())
153         {
154             ClassDescriptor cld = (ClassDescriptor) iter.next();
155 // System.out.println("CLD: " + cld.getClassOfObject().getName());
156
// byte[] arr = serialize(cld);
157
//
158
// ClassDescriptor cld1 = deserialize(arr);
159

160             byte[] arr = SerializationUtils.serialize(cld);
161             ClassDescriptor cld1 = (ClassDescriptor) SerializationUtils.deserialize(arr);
162         }
163     }
164 }
165
Popular Tags