01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: *
19: */
20: package org.apache.mina.filter.codec.serialization;
21:
22: import java.io.ByteArrayOutputStream;
23:
24: import junit.framework.Assert;
25: import junit.framework.TestCase;
26:
27: import org.apache.mina.common.IoBuffer;
28: import org.apache.mina.filter.codec.ProtocolCodecSession;
29: import org.apache.mina.filter.codec.ProtocolDecoder;
30: import org.apache.mina.filter.codec.ProtocolDecoderOutput;
31: import org.apache.mina.filter.codec.ProtocolEncoder;
32: import org.apache.mina.filter.codec.ProtocolEncoderOutput;
33:
34: /**
35: * Tests object serialization codec and streams.
36: *
37: * @author The Apache MINA Project Team (dev@mina.apache.org)
38: * @version $Rev: 607166 $, $Date: 2007-12-27 20:43:04 -0700 (Thu, 27 Dec 2007) $
39: */
40: public class ObjectSerializationTest extends TestCase {
41: public void testEncoder() throws Exception {
42: final String expected = "1234";
43:
44: ProtocolCodecSession session = new ProtocolCodecSession();
45: ProtocolEncoderOutput out = session.getEncoderOutput();
46:
47: ProtocolEncoder encoder = new ObjectSerializationEncoder();
48: encoder.encode(session, expected, out);
49:
50: Assert.assertEquals(1, session.getEncoderOutputQueue().size());
51: IoBuffer buf = (IoBuffer) session.getEncoderOutputQueue()
52: .poll();
53:
54: testDecoderAndInputStream(expected, buf);
55: }
56:
57: public void testOutputStream() throws Exception {
58: final String expected = "1234";
59:
60: ByteArrayOutputStream baos = new ByteArrayOutputStream();
61: ObjectSerializationOutputStream osos = new ObjectSerializationOutputStream(
62: baos);
63:
64: osos.writeObject(expected);
65: osos.flush();
66:
67: testDecoderAndInputStream(expected, IoBuffer.wrap(baos
68: .toByteArray()));
69: }
70:
71: private void testDecoderAndInputStream(String expected, IoBuffer in)
72: throws Exception {
73: // Test InputStream
74: ObjectSerializationInputStream osis = new ObjectSerializationInputStream(
75: in.duplicate().asInputStream());
76:
77: Object actual = osis.readObject();
78: assertEquals(expected, actual);
79:
80: // Test ProtocolDecoder
81: ProtocolDecoder decoder = new ObjectSerializationDecoder();
82: ProtocolCodecSession session = new ProtocolCodecSession();
83: ProtocolDecoderOutput decoderOut = session.getDecoderOutput();
84: decoder.decode(session, in.duplicate(), decoderOut);
85:
86: Assert.assertEquals(1, session.getDecoderOutputQueue().size());
87: Assert.assertEquals(expected, session.getDecoderOutputQueue()
88: .poll());
89: }
90: }
|