KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > junit > ResultCodec


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Forums JBoss Portlet *
6  * *
7  * Distributable under LGPL license. *
8  * See terms of license at gnu.org. *
9  * *
10  *****************************************/

11 package org.jboss.portal.junit;
12
13 import org.jboss.portal.common.util.Tools;
14
15 import java.io.ObjectOutputStream JavaDoc;
16 import java.io.ByteArrayOutputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.ByteArrayInputStream JavaDoc;
19 import java.io.ObjectInputStream JavaDoc;
20
21 /**
22  * Perform marshalling and unmarshalling of results objects.
23  * For now it is plain java serialization.
24  *
25  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
26  * @version $Revision: 1.1 $
27  */

28 public class ResultCodec
29 {
30
31    public static String JavaDoc marshallResult(Result result)
32    {
33       try
34       {
35          if (result == null)
36          {
37             throw new IllegalArgumentException JavaDoc("No null result accepted");
38          }
39          ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
40          ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
41          oos.writeObject(result);
42          oos.close();
43          return Tools.toHexString(baos.toByteArray());
44       }
45       catch (IOException JavaDoc e)
46       {
47          throw new Error JavaDoc("Unexpected", e);
48       }
49    }
50
51    public static Result unmarshallResult(String JavaDoc hex)
52    {
53       try
54       {
55          if (hex == null)
56          {
57             throw new IllegalArgumentException JavaDoc("No null string accepted");
58          }
59          byte[] bytes = Tools.fromHexString(hex);
60          ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(bytes);
61          ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
62          Result result = (Result)ois.readObject();
63          ois.close();
64          return result;
65       }
66       catch (IllegalArgumentException JavaDoc e)
67       {
68         throw new Error JavaDoc("Unexpected", e);
69       }
70       catch (IOException JavaDoc e)
71       {
72          throw new Error JavaDoc("Unexpected", e);
73       }
74       catch (ClassNotFoundException JavaDoc e)
75       {
76          throw new Error JavaDoc("Unexpected", e);
77       }
78    }
79 }
80
Popular Tags