KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.ByteArrayInputStream JavaDoc;
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.DataInputStream JavaDoc;
23 import java.io.DataOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 import junit.framework.AssertionFailedError;
27 import junit.framework.TestCase;
28
29 /**
30  *
31  * @version $Revision: 426366 $
32  */

33 public class BooleanStreamTest extends TestCase {
34
35     protected OpenWireFormat openWireformat;
36     protected int endOfStreamMarker = 0x12345678;
37     int numberOfBytes = 8 * 200;
38
39     interface BooleanValueSet {
40         public boolean getBooleanValueFor(int index, int count);
41     }
42
43     public void testBooleanMarshallingUsingAllTrue() throws Exception JavaDoc {
44         testBooleanStream(numberOfBytes, new BooleanValueSet() {
45             public boolean getBooleanValueFor(int index, int count) {
46                 return true;
47             }
48         });
49     }
50
51     public void testBooleanMarshallingUsingAllFalse() throws Exception JavaDoc {
52         testBooleanStream(numberOfBytes, new BooleanValueSet() {
53             public boolean getBooleanValueFor(int index, int count) {
54                 return false;
55             }
56         });
57     }
58
59     public void testBooleanMarshallingUsingOddAlternateTrueFalse() throws Exception JavaDoc {
60         testBooleanStream(numberOfBytes, new BooleanValueSet() {
61             public boolean getBooleanValueFor(int index, int count) {
62                 return (index & 1) == 0;
63             }
64         });
65     }
66     
67     public void testBooleanMarshallingUsingEvenAlternateTrueFalse() throws Exception JavaDoc {
68         testBooleanStream(numberOfBytes, new BooleanValueSet() {
69             public boolean getBooleanValueFor(int index, int count) {
70                 return (index & 1) != 0;
71             }
72         });
73     }
74
75     protected void testBooleanStream(int numberOfBytes, BooleanValueSet valueSet) throws Exception JavaDoc {
76         for (int i = 0; i < numberOfBytes; i++) {
77             try {
78                 assertMarshalBooleans(i, valueSet);
79             } catch (Throwable JavaDoc e) {
80                 throw (AssertionFailedError) new AssertionFailedError("Iteration failed at: "+i).initCause(e);
81             }
82         }
83     }
84
85     protected void assertMarshalBooleans(int count, BooleanValueSet valueSet) throws Exception JavaDoc {
86         BooleanStream bs = new BooleanStream();
87         for (int i = 0; i < count; i++) {
88             bs.writeBoolean(valueSet.getBooleanValueFor(i, count));
89         }
90         ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
91         DataOutputStream JavaDoc ds = new DataOutputStream JavaDoc(buffer);
92         bs.marshal(ds);
93         ds.writeInt(endOfStreamMarker);
94
95         // now lets read from the stream
96
ds.close();
97
98         ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(buffer.toByteArray());
99         DataInputStream JavaDoc dis = new DataInputStream JavaDoc(in);
100         bs = new BooleanStream();
101         try {
102             bs.unmarshal(dis);
103         }
104         catch (Exception JavaDoc e) {
105             e.printStackTrace();
106             fail("Failed to unmarshal: " + count + " booleans: " + e);
107         }
108
109         for (int i = 0; i < count; i++) {
110             boolean expected = valueSet.getBooleanValueFor(i, count);
111             // /System.out.println("Unmarshaling value: " + i + " = " + expected
112
// + " out of: " + count);
113

114             try {
115                 boolean actual = bs.readBoolean();
116                 assertEquals("value of object: " + i + " was: " + actual, expected, actual);
117             }
118             catch (IOException JavaDoc e) {
119                 e.printStackTrace();
120                 fail("Failed to parse boolean: " + i + " out of: " + count + " due to: " + e);
121             }
122         }
123         int marker = dis.readInt();
124         assertEquals("Marker int when unmarshalling: " + count + " booleans", Integer.toHexString(endOfStreamMarker), Integer.toHexString(marker));
125
126         // lets try read and we should get an exception
127
try {
128             byte value = dis.readByte();
129             fail("Should have reached the end of the stream");
130         }
131         catch (IOException JavaDoc e) {
132             // worked!
133
}
134     }
135
136     protected void setUp() throws Exception JavaDoc {
137         super.setUp();
138         openWireformat = createOpenWireFormat();
139     }
140
141     protected OpenWireFormat createOpenWireFormat() {
142         OpenWireFormat wf = new OpenWireFormat();
143         wf.setCacheEnabled(true);
144         wf.setStackTraceEnabled(false);
145         wf.setVersion(1);
146         return wf;
147     }
148
149 }
150
Popular Tags