1 package org.sapia.ubik.test; 2 3 import org.sapia.ubik.rmi.server.Stub; 4 5 import java.io.ByteArrayInputStream ; 6 import java.io.ByteArrayOutputStream ; 7 import java.io.IOException ; 8 import java.io.ObjectInputStream ; 9 import java.io.ObjectOutputStream ; 10 11 import java.util.HashSet ; 12 import java.util.Set ; 13 14 15 19 public class TestUtils { 20 23 public TestUtils() { 24 super(); 25 } 26 27 public static Object deserialize(byte[] bytes) 28 throws IOException , ClassNotFoundException { 29 ObjectInputStream ois = new ObjectInputStream (new ByteArrayInputStream ( 30 bytes)); 31 Object o = ois.readObject(); 32 33 return o; 34 } 35 36 public static byte[] serialize(Object o) throws IOException { 37 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 38 ObjectOutputStream ous = new ObjectOutputStream (bos); 39 ous.writeObject(o); 40 ous.flush(); 41 ous.close(); 42 43 return bos.toByteArray(); 44 } 45 46 public static Class [] getInterfacesFor(Class clazz) { 47 HashSet set = new HashSet (); 48 Class current = clazz; 49 appendInterfaces(current, set); 50 set.add(Stub.class); 51 52 return (Class []) set.toArray(new Class [set.size()]); 53 } 54 55 private static void appendInterfaces(Class current, Set interfaces) { 56 Class [] ifs = current.getInterfaces(); 57 58 for (int i = 0; i < ifs.length; i++) { 59 appendInterfaces(ifs[i], interfaces); 60 interfaces.add(ifs[i]); 61 } 62 63 current = current.getSuperclass(); 64 65 if (current != null) { 66 appendInterfaces(current, interfaces); 67 } 68 } 69 } 70 | Popular Tags |