Source Code Cross Referenced for MockStreamMessage.java in  » Testing » mockrunner-0.4 » com » mockrunner » mock » jms » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » mockrunner 0.4 » com.mockrunner.mock.jms 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.