001: package com.mockrunner.mock.jms;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.ByteArrayOutputStream;
005: import java.io.DataInputStream;
006: import java.io.DataOutputStream;
007: import java.io.EOFException;
008: import java.io.IOException;
009: import java.util.Arrays;
010:
011: import javax.jms.BytesMessage;
012: import javax.jms.JMSException;
013: import javax.jms.MessageEOFException;
014: import javax.jms.MessageFormatException;
015: import javax.jms.MessageNotReadableException;
016: import javax.jms.MessageNotWriteableException;
017:
018: import com.mockrunner.base.NestedApplicationException;
019:
020: /**
021: * Mock implementation of JMS <code>BytesMessage</code>.
022: */
023: public class MockBytesMessage extends MockMessage implements
024: BytesMessage {
025: private DataOutputStream outStream;
026: private ByteArrayOutputStream byteOutStream;
027: private DataInputStream inStream;
028:
029: public MockBytesMessage() {
030: try {
031: clearBody();
032: } catch (JMSException exc) {
033: throw new NestedApplicationException(exc);
034: }
035: }
036:
037: public long getBodyLength() throws JMSException {
038: if (isInWriteMode()) {
039: throw new MessageNotReadableException(
040: "Message is in write mode");
041: }
042: return outStream.size();
043: }
044:
045: public boolean readBoolean() throws JMSException {
046: if (isInWriteMode()) {
047: throw new MessageNotReadableException(
048: "Message is in write mode");
049: }
050: try {
051: return inStream.readBoolean();
052: } catch (EOFException exc) {
053: throw new MessageEOFException(exc.getMessage());
054: } catch (IOException exc) {
055: throw new JMSException(exc.getMessage());
056: }
057: }
058:
059: public byte readByte() throws JMSException {
060: if (isInWriteMode()) {
061: throw new MessageNotReadableException(
062: "Message is in write mode");
063: }
064: try {
065: return inStream.readByte();
066: } catch (EOFException exc) {
067: throw new MessageEOFException(exc.getMessage());
068: } catch (IOException exc) {
069: throw new JMSException(exc.getMessage());
070: }
071: }
072:
073: public int readUnsignedByte() throws JMSException {
074: if (isInWriteMode()) {
075: throw new MessageNotReadableException(
076: "Message is in write mode");
077: }
078: try {
079: return inStream.readByte();
080: } catch (EOFException exc) {
081: throw new MessageEOFException(exc.getMessage());
082: } catch (IOException exc) {
083: throw new JMSException(exc.getMessage());
084: }
085: }
086:
087: public short readShort() throws JMSException {
088: if (isInWriteMode()) {
089: throw new MessageNotReadableException(
090: "Message is in write mode");
091: }
092: try {
093: return inStream.readShort();
094: } catch (EOFException exc) {
095: throw new MessageEOFException(exc.getMessage());
096: } catch (IOException exc) {
097: throw new JMSException(exc.getMessage());
098: }
099: }
100:
101: public int readUnsignedShort() throws JMSException {
102: if (isInWriteMode()) {
103: throw new MessageNotReadableException(
104: "Message is in write mode");
105: }
106: try {
107: return inStream.readShort();
108: } catch (EOFException exc) {
109: throw new MessageEOFException(exc.getMessage());
110: } catch (IOException exc) {
111: throw new JMSException(exc.getMessage());
112: }
113: }
114:
115: public char readChar() throws JMSException {
116: if (isInWriteMode()) {
117: throw new MessageNotReadableException(
118: "Message is in write mode");
119: }
120: try {
121: return inStream.readChar();
122: } catch (EOFException exc) {
123: throw new MessageEOFException(exc.getMessage());
124: } catch (IOException exc) {
125: throw new JMSException(exc.getMessage());
126: }
127: }
128:
129: public int readInt() throws JMSException {
130: if (isInWriteMode()) {
131: throw new MessageNotReadableException(
132: "Message is in write mode");
133: }
134: try {
135: return inStream.readInt();
136: } catch (EOFException exc) {
137: throw new MessageEOFException(exc.getMessage());
138: } catch (IOException exc) {
139: throw new JMSException(exc.getMessage());
140: }
141: }
142:
143: public long readLong() throws JMSException {
144: if (isInWriteMode()) {
145: throw new MessageNotReadableException(
146: "Message is in write mode");
147: }
148: try {
149: return inStream.readLong();
150: } catch (EOFException exc) {
151: throw new MessageEOFException(exc.getMessage());
152: } catch (IOException exc) {
153: throw new JMSException(exc.getMessage());
154: }
155: }
156:
157: public float readFloat() throws JMSException {
158: if (isInWriteMode()) {
159: throw new MessageNotReadableException(
160: "Message is in write mode");
161: }
162: try {
163: return inStream.readFloat();
164: } catch (EOFException exc) {
165: throw new MessageEOFException(exc.getMessage());
166: } catch (IOException exc) {
167: throw new JMSException(exc.getMessage());
168: }
169: }
170:
171: public double readDouble() throws JMSException {
172: if (isInWriteMode()) {
173: throw new MessageNotReadableException(
174: "Message is in write mode");
175: }
176: try {
177: return inStream.readDouble();
178: } catch (EOFException exc) {
179: throw new MessageEOFException(exc.getMessage());
180: } catch (IOException exc) {
181: throw new JMSException(exc.getMessage());
182: }
183: }
184:
185: public String readUTF() throws JMSException {
186: if (isInWriteMode()) {
187: throw new MessageNotReadableException(
188: "Message is in write mode");
189: }
190: try {
191: return inStream.readUTF();
192: } catch (EOFException exc) {
193: throw new MessageEOFException(exc.getMessage());
194: } catch (IOException exc) {
195: throw new JMSException(exc.getMessage());
196: }
197: }
198:
199: public int readBytes(byte[] data) throws JMSException {
200: if (isInWriteMode()) {
201: throw new MessageNotReadableException(
202: "Message is in write mode");
203: }
204: try {
205: return inStream.read(data);
206: } catch (EOFException exc) {
207: throw new MessageEOFException(exc.getMessage());
208: } catch (IOException exc) {
209: throw new JMSException(exc.getMessage());
210: }
211: }
212:
213: public int readBytes(byte[] data, int length) throws JMSException {
214: if (isInWriteMode()) {
215: throw new MessageNotReadableException(
216: "Message is in write mode");
217: }
218: try {
219: return inStream.read(data, 0, length);
220: } catch (EOFException exc) {
221: throw new MessageEOFException(exc.getMessage());
222: } catch (IOException exc) {
223: throw new JMSException(exc.getMessage());
224: }
225: }
226:
227: public void writeBoolean(boolean value) throws JMSException {
228: if (!isInWriteMode()) {
229: throw new MessageNotWriteableException(
230: "Message is in read mode");
231: }
232: try {
233: outStream.writeBoolean(value);
234: } catch (IOException exc) {
235: throw new JMSException(exc.getMessage());
236: }
237: }
238:
239: public void writeByte(byte value) throws JMSException {
240: if (!isInWriteMode()) {
241: throw new MessageNotWriteableException(
242: "Message is in read mode");
243: }
244: try {
245: outStream.writeByte(value);
246: } catch (IOException exc) {
247: throw new JMSException(exc.getMessage());
248: }
249: }
250:
251: public void writeShort(short value) throws JMSException {
252: if (!isInWriteMode()) {
253: throw new MessageNotWriteableException(
254: "Message is in read mode");
255: }
256: try {
257: outStream.writeShort(value);
258: } catch (IOException exc) {
259: throw new JMSException(exc.getMessage());
260: }
261: }
262:
263: public void writeChar(char value) throws JMSException {
264: if (!isInWriteMode()) {
265: throw new MessageNotWriteableException(
266: "Message is in read mode");
267: }
268: try {
269: outStream.writeChar(value);
270: } catch (IOException exc) {
271: throw new JMSException(exc.getMessage());
272: }
273: }
274:
275: public void writeInt(int value) throws JMSException {
276: if (!isInWriteMode()) {
277: throw new MessageNotWriteableException(
278: "Message is in read mode");
279: }
280: try {
281: outStream.writeInt(value);
282: } catch (IOException exc) {
283: throw new JMSException(exc.getMessage());
284: }
285: }
286:
287: public void writeLong(long value) throws JMSException {
288: if (!isInWriteMode()) {
289: throw new MessageNotWriteableException(
290: "Message is in read mode");
291: }
292: try {
293: outStream.writeLong(value);
294: } catch (IOException exc) {
295: throw new JMSException(exc.getMessage());
296: }
297: }
298:
299: public void writeFloat(float value) throws JMSException {
300: if (!isInWriteMode()) {
301: throw new MessageNotWriteableException(
302: "Message is in read mode");
303: }
304: try {
305: outStream.writeFloat(value);
306: } catch (IOException exc) {
307: throw new JMSException(exc.getMessage());
308: }
309: }
310:
311: public void writeDouble(double value) throws JMSException {
312: if (!isInWriteMode()) {
313: throw new MessageNotWriteableException(
314: "Message is in read mode");
315: }
316: try {
317: outStream.writeDouble(value);
318: } catch (IOException exc) {
319: throw new JMSException(exc.getMessage());
320: }
321: }
322:
323: public void writeUTF(String value) throws JMSException {
324: if (!isInWriteMode()) {
325: throw new MessageNotWriteableException(
326: "Message is in read mode");
327: }
328: try {
329: outStream.writeUTF(value);
330: } catch (IOException exc) {
331: throw new JMSException(exc.getMessage());
332: }
333: }
334:
335: public void writeBytes(byte[] data) throws JMSException {
336: if (!isInWriteMode()) {
337: throw new MessageNotWriteableException(
338: "Message is in read mode");
339: }
340: try {
341: outStream.write(data);
342: } catch (IOException exc) {
343: throw new JMSException(exc.getMessage());
344: }
345: }
346:
347: public void writeBytes(byte[] data, int offset, int length)
348: throws JMSException {
349: if (!isInWriteMode()) {
350: throw new MessageNotWriteableException(
351: "Message is in read mode");
352: }
353: try {
354: outStream.write(data, offset, length);
355: } catch (IOException exc) {
356: throw new JMSException(exc.getMessage());
357: }
358: }
359:
360: public void writeObject(Object object) throws JMSException {
361: if (!isInWriteMode()) {
362: throw new MessageNotWriteableException(
363: "Message is in read mode");
364: }
365: if (object instanceof Byte) {
366: writeByte(((Byte) object).byteValue());
367: return;
368: }
369: if (object instanceof Short) {
370: writeShort(((Short) object).shortValue());
371: return;
372: }
373: if (object instanceof Integer) {
374: writeInt(((Integer) object).intValue());
375: return;
376: }
377: if (object instanceof Long) {
378: writeLong(((Long) object).longValue());
379: return;
380: }
381: if (object instanceof Float) {
382: writeFloat(((Float) object).floatValue());
383: return;
384: }
385: if (object instanceof Double) {
386: writeDouble(((Double) object).doubleValue());
387: return;
388: }
389: if (object instanceof Character) {
390: writeChar(((Character) object).charValue());
391: return;
392: }
393: if (object instanceof Boolean) {
394: writeBoolean(((Boolean) object).booleanValue());
395: return;
396: }
397: if (object instanceof String) {
398: writeUTF((String) object);
399: return;
400: }
401: if (object instanceof byte[]) {
402: writeBytes((byte[]) object);
403: return;
404: }
405: throw new MessageFormatException(object.getClass().getName()
406: + " is an invalid type");
407: }
408:
409: public void reset() throws JMSException {
410: setReadOnly(true);
411: try {
412: outStream.flush();
413: } catch (IOException exc) {
414: throw new JMSException(exc.getMessage());
415: }
416: inStream = new DataInputStream(new ByteArrayInputStream(
417: byteOutStream.toByteArray()));
418: }
419:
420: public void clearBody() throws JMSException {
421: super .clearBody();
422: byteOutStream = new ByteArrayOutputStream();
423: outStream = new DataOutputStream(byteOutStream);
424: }
425:
426: /**
427: * Returns a copy of the underlying byte data regardless if the message
428: * is in read or write mode.
429: * @return the byte data
430: */
431: public byte[] getBytes() {
432: try {
433: outStream.flush();
434: } catch (IOException exc) {
435: throw new RuntimeException(exc.getMessage());
436: }
437: return byteOutStream.toByteArray();
438: }
439:
440: /**
441: * Compares the underlying byte data.
442: */
443: public boolean equals(Object otherObject) {
444: if (null == otherObject)
445: return false;
446: if (!(otherObject instanceof MockBytesMessage))
447: return false;
448: MockBytesMessage otherMessage = (MockBytesMessage) otherObject;
449: byte[] firstData = getBytes();
450: byte[] secondData = otherMessage.getBytes();
451: return Arrays.equals(firstData, secondData);
452: }
453:
454: public int hashCode() {
455: int value = 17;
456: byte[] data = getBytes();
457: for (int ii = 0; ii < data.length; ii++) {
458: value = (31 * value) + data[ii];
459: }
460: return value;
461: }
462:
463: public Object clone() {
464: MockBytesMessage message = (MockBytesMessage) super .clone();
465: try {
466: message.clearBody();
467: message.outStream.write(getBytes());
468: return message;
469: } catch (Exception exc) {
470: throw new NestedApplicationException(exc);
471: }
472: }
473:
474: public String toString() {
475: StringBuffer buffer = new StringBuffer();
476: buffer.append(this .getClass().getName() + ": [");
477: byte[] data = getBytes();
478: for (int ii = 0; ii < data.length; ii++) {
479: buffer.append(data[ii]);
480: if (ii < data.length - 1) {
481: buffer.append(", ");
482: }
483: }
484: buffer.append("]");
485: return buffer.toString();
486: }
487: }
|