01: package com.mockrunner.mock.jms;
02:
03: import java.io.ByteArrayInputStream;
04: import java.io.ByteArrayOutputStream;
05: import java.io.ObjectInputStream;
06: import java.io.ObjectOutputStream;
07: import java.io.Serializable;
08:
09: import javax.jms.JMSException;
10: import javax.jms.MessageNotWriteableException;
11: import javax.jms.ObjectMessage;
12:
13: import com.mockrunner.base.NestedApplicationException;
14:
15: /**
16: * Mock implementation of JMS <code>ObjectMessage</code>.
17: */
18: public class MockObjectMessage extends MockMessage implements
19: ObjectMessage {
20: private Serializable object;
21:
22: public MockObjectMessage() {
23: this (null);
24: }
25:
26: public MockObjectMessage(Serializable object) {
27: this .object = object;
28: }
29:
30: public void setObject(Serializable object) throws JMSException {
31: if (!isInWriteMode()) {
32: throw new MessageNotWriteableException(
33: "Message is in read mode");
34: }
35: this .object = object;
36: }
37:
38: public Serializable getObject() throws JMSException {
39: return object;
40: }
41:
42: public void clearBody() throws JMSException {
43: super .clearBody();
44: object = null;
45: }
46:
47: /**
48: * Calls the <code>equals</code> method of the underlying
49: * object. If both objects are <code>null</code>, this
50: * method returns <code>true</code>.
51: */
52: public boolean equals(Object otherObject) {
53: if (null == otherObject)
54: return false;
55: if (!(otherObject instanceof MockObjectMessage))
56: return false;
57: MockObjectMessage otherMessage = (MockObjectMessage) otherObject;
58: if (null == object && null == otherMessage.object)
59: return true;
60: return object.equals(otherMessage.object);
61: }
62:
63: public int hashCode() {
64: if (null == object)
65: return 0;
66: return object.hashCode();
67: }
68:
69: public Object clone() {
70: MockObjectMessage message = (MockObjectMessage) super .clone();
71: try {
72: ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
73: ObjectOutputStream objectOutStream = new ObjectOutputStream(
74: byteOutStream);
75: objectOutStream.writeObject(object);
76: objectOutStream.flush();
77: ByteArrayInputStream byteInStream = new ByteArrayInputStream(
78: byteOutStream.toByteArray());
79: ObjectInputStream objectInStream = new ObjectInputStream(
80: byteInStream);
81: message.object = (Serializable) objectInStream.readObject();
82: return message;
83: } catch (Exception exc) {
84: throw new NestedApplicationException(exc);
85: }
86: }
87:
88: public String toString() {
89: return this .getClass().getName() + ": " + object;
90: }
91: }
|