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(
62                  baos);
63  
64          osos.writeObject(expected);
65          osos.flush();
66  
67          testDecoderAndInputStream(expected, IoBuffer.wrap(baos.toByteArray()));
68      }
69  
70      private void testDecoderAndInputStream(String expected, IoBuffer in)
71              throws Exception {
72          // Test InputStream
73          ObjectSerializationInputStream osis = new ObjectSerializationInputStream(
74                  in.duplicate().asInputStream());
75  
76          Object actual = osis.readObject();
77          assertEquals(expected, actual);
78  
79          // Test ProtocolDecoder
80          ProtocolDecoder decoder = new ObjectSerializationDecoder();
81          ProtocolCodecSession session = new ProtocolCodecSession();
82          ProtocolDecoderOutput decoderOut = session.getDecoderOutput();
83          decoder.decode(session, in.duplicate(), decoderOut);
84  
85          assertEquals(1, session.getDecoderOutputQueue().size());
86          assertEquals(expected, session.getDecoderOutputQueue().poll());
87      }
88  }