KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > openwire > NumberRangesWhileMarshallingTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.openwire;
19
20 import org.apache.activemq.command.SessionId;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.DataInputStream JavaDoc;
27 import java.io.DataOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29
30 import junit.framework.TestCase;
31
32 /**
33  *
34  * @version $Revision: 426366 $
35  */

36 public class NumberRangesWhileMarshallingTest extends TestCase {
37
38     private static final Log log = LogFactory.getLog(NumberRangesWhileMarshallingTest.class);
39     
40     protected String JavaDoc connectionId = "Cheese";
41     protected ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
42     protected DataOutputStream JavaDoc ds = new DataOutputStream JavaDoc(buffer);
43     protected OpenWireFormat openWireformat;
44     protected int endOfStreamMarker = 0x12345678;
45
46     
47     public void testLongNumberRanges() throws Exception JavaDoc {
48         long[] numberValues = {
49                 // bytes
50
0, 1, 0x7e, 0x7f, 0x80, 0x81, 0xf0, 0xff,
51                 // shorts
52
0x7eff, 0x7fffL, 0x8001L, 0x8000L, 0xe000L, 0xe0001L, 0xff00L, 0xffffL,
53                 // ints
54
0x10000L,
55                 0x700000L,
56                 0x12345678L,
57                 0x72345678L,
58                 0x7fffffffL,
59                 0x80000000L,
60                 0x80000001L,
61                 0xE0000001L,
62                 0xFFFFFFFFL,
63                 
64                 // 3 byte longs
65
0x123456781L,
66                 0x1234567812L,
67                 0x12345678123L,
68                 0x123456781234L,
69                 0x1234567812345L,
70                 0x12345678123456L,
71                 0x7e345678123456L,
72                 0x7fffffffffffffL,
73                 0x80000000000000L,
74                 0x80000000000001L,
75                 0xe0000000000001L,
76                 0xffffffffffffffL,
77                 
78                 // 4 byte longs
79
0x1234567812345678L,
80                 0x7fffffffffffffffL,
81                 0x8000000000000000L,
82                 0x8000000000000001L,
83                 0xe000000000000001L,
84                 0xffffffffffffffffL,
85                 1 };
86         
87         for (int i = 0; i < numberValues.length; i++) {
88             long value = numberValues[i];
89             
90             SessionId object = new SessionId();
91             object.setConnectionId(connectionId);
92             object.setValue(value);
93             writeObject(object);
94         }
95         ds.writeInt(endOfStreamMarker);
96         
97         // now lets read from the stream
98
ds.close();
99         
100         ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(buffer.toByteArray());
101         DataInputStream JavaDoc dis = new DataInputStream JavaDoc(in);
102         for (int i = 0; i < numberValues.length; i++) {
103             long value = numberValues[i];
104             String JavaDoc expected = Long.toHexString(value);
105             log.info("Unmarshaling value: " + i + " = " + expected);
106             
107             SessionId command = (SessionId) openWireformat.unmarshal(dis);
108             assertEquals("connection ID in object: "+ i, connectionId, command.getConnectionId());
109             String JavaDoc actual = Long.toHexString(command.getValue());
110             assertEquals("value of object: "+ i + " was: " + actual, expected, actual);
111         }
112         int marker = dis.readInt();
113         assertEquals("Marker int", Integer.toHexString(endOfStreamMarker), Integer.toHexString(marker));
114         
115         // lets try read and we should get an exception
116
try {
117             byte value = dis.readByte();
118             fail("Should have reached the end of the stream");
119         }
120         catch (IOException JavaDoc e) {
121             // worked!
122
}
123     }
124
125     protected void setUp() throws Exception JavaDoc {
126         super.setUp();
127         openWireformat = createOpenWireFormat();
128     }
129
130     protected OpenWireFormat createOpenWireFormat() {
131         OpenWireFormat wf = new OpenWireFormat();
132         wf.setCacheEnabled(true);
133         wf.setStackTraceEnabled(false);
134         wf.setVersion(1);
135         return wf;
136     }
137
138     private void writeObject(Object JavaDoc object) throws IOException JavaDoc {
139         openWireformat.marshal(object, ds);
140     }
141 }
142
Popular Tags