View Javadoc

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 static org.junit.Assert.assertEquals;
23  
24  import java.io.ByteArrayOutputStream;
25  
26  import org.apache.mina.core.buffer.IoBuffer;
27  import org.apache.mina.filter.codec.ProtocolCodecSession;
28  import org.apache.mina.filter.codec.ProtocolDecoder;
29  import org.apache.mina.filter.codec.ProtocolDecoderOutput;
30  import org.apache.mina.filter.codec.ProtocolEncoder;
31  import org.apache.mina.filter.codec.ProtocolEncoderOutput;
32  import org.junit.Test;
33  
34  /**
35   * Tests object serialization codec and streams.
36   *
37   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
38   */
39  public class ObjectSerializationTest {
40      @Test
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          assertEquals(1, session.getEncoderOutputQueue().size());
51          IoBuffer buf = (IoBuffer) session.getEncoderOutputQueue().poll();
52  
53          testDecoderAndInputStream(expected, buf);
54      }
55  
56      @Test
57      public void testOutputStream() throws Exception {
58          final String expected = "1234";
59  
60          ByteArrayOutputStream baos = new ByteArrayOutputStream();
61          ObjectSerializationOutputStream osos = new ObjectSerializationOutputStream(baos);
62  
63          osos.writeObject(expected);
64          osos.flush();
65  
66          testDecoderAndInputStream(expected, IoBuffer.wrap(baos.toByteArray()));
67      }
68  
69      private void testDecoderAndInputStream(String expected, IoBuffer in) throws Exception {
70          // Test InputStream
71          ObjectSerializationInputStream osis = new ObjectSerializationInputStream(in.duplicate().asInputStream());
72  
73          Object actual = osis.readObject();
74          assertEquals(expected, actual);
75  
76          // Test ProtocolDecoder
77          ProtocolDecoder decoder = new ObjectSerializationDecoder();
78          ProtocolCodecSession session = new ProtocolCodecSession();
79          ProtocolDecoderOutput decoderOut = session.getDecoderOutput();
80          decoder.decode(session, in.duplicate(), decoderOut);
81  
82          assertEquals(1, session.getDecoderOutputQueue().size());
83          assertEquals(expected, session.getDecoderOutputQueue().poll());
84      }
85  }