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