1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with 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,
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.core.buffer.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 (dev@mina.apache.org)
38   */
39  public class ObjectSerializationTest extends TestCase {
40      public void testEncoder() throws Exception {
41          final String expected = "1234";
42  
43          ProtocolCodecSession session = new ProtocolCodecSession();
44          ProtocolEncoderOutput out = session.getEncoderOutput();
45  
46          ProtocolEncoder encoder = new ObjectSerializationEncoder();
47          encoder.encode(session, expected, out);
48  
49          Assert.assertEquals(1, session.getEncoderOutputQueue().size());
50          IoBuffer buf = (IoBuffer) session.getEncoderOutputQueue().poll();
51  
52          testDecoderAndInputStream(expected, buf);
53      }
54  
55      public void testOutputStream() throws Exception {
56          final String expected = "1234";
57  
58          ByteArrayOutputStream baos = new ByteArrayOutputStream();
59          ObjectSerializationOutputStream osos = new ObjectSerializationOutputStream(
60                  baos);
61  
62          osos.writeObject(expected);
63          osos.flush();
64  
65          testDecoderAndInputStream(expected, IoBuffer.wrap(baos.toByteArray()));
66      }
67  
68      private void testDecoderAndInputStream(String expected, IoBuffer in)
69              throws Exception {
70          // Test InputStream
71          ObjectSerializationInputStream osis = new ObjectSerializationInputStream(
72                  in.duplicate().asInputStream());
73  
74          Object actual = osis.readObject();
75          assertEquals(expected, actual);
76  
77          // Test ProtocolDecoder
78          ProtocolDecoder decoder = new ObjectSerializationDecoder();
79          ProtocolCodecSession session = new ProtocolCodecSession();
80          ProtocolDecoderOutput decoderOut = session.getDecoderOutput();
81          decoder.decode(session, in.duplicate(), decoderOut);
82  
83          Assert.assertEquals(1, session.getDecoderOutputQueue().size());
84          Assert.assertEquals(expected, session.getDecoderOutputQueue().poll());
85      }
86  }