01: /*
02: * Copyright 2007 The Kuali Foundation
03: *
04: * Licensed under the Educational Community License, Version 1.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.opensource.org/licenses/ecl1.php
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package edu.iu.uis.eden.messaging;
17:
18: import java.io.ByteArrayInputStream;
19: import java.io.ByteArrayOutputStream;
20: import java.io.IOException;
21: import java.io.ObjectInputStream;
22: import java.io.ObjectOutput;
23: import java.io.ObjectOutputStream;
24: import java.io.Serializable;
25:
26: import javax.xml.namespace.QName;
27:
28: import junit.framework.TestCase;
29:
30: import org.apache.commons.codec.binary.Base64;
31: import org.apache.log4j.Logger;
32: import org.junit.Test;
33: import org.kuali.rice.exceptions.RiceRuntimeException;
34:
35: public class QNameSerializationTest extends TestCase {
36:
37: private static final Logger LOG = Logger
38: .getLogger(QNameSerializationTest.class);
39:
40: @Test
41: public void testQNameSerializaion() throws Exception {
42:
43: QName qname = new QName("hi", "HI");
44: String qnameSerialized = serializeObject(qname);
45: deserializeObject(qnameSerialized);
46:
47: assertTrue(true);
48: }
49:
50: public String serializeObject(Serializable object) {
51: ByteArrayOutputStream bos = new ByteArrayOutputStream();
52: ObjectOutput out = null;
53: try {
54: out = new ObjectOutputStream(bos);
55: out.writeObject(object);
56: } catch (IOException e) {
57: throw new RiceRuntimeException(e);
58: } finally {
59: try {
60: out.close();
61: } catch (IOException e) {
62: LOG.error("Failed to close ObjectOutputStream", e);
63: }
64: }
65: byte[] buf = bos.toByteArray();
66: Base64 b64 = new Base64();
67: byte[] encodedObj = b64.encode(buf);
68: return new String(encodedObj);
69: }
70:
71: public Object deserializeObject(String serializedObject) {
72: Base64 b64 = new Base64();
73: byte[] result = b64.decode(serializedObject.getBytes());
74: Object payload = null;
75: ObjectInputStream ois = null;
76: try {
77: ois = new ObjectInputStream(
78: new ByteArrayInputStream(result));
79: payload = ois.readObject();
80: } catch (Exception e) {
81: // may want to move this loggging up
82: LOG.error("Caught Error de-serializing message payload", e);
83: // throw new RiceRuntimeException(e);
84: } finally {
85: try {
86: ois.close();
87: } catch (IOException e) {
88: LOG.error("Failed to close de-serialization stream", e);
89: }
90: }
91: return payload;
92: }
93:
94: }
|