001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.filter.codec.serialization;
021
022import static org.junit.Assert.assertEquals;
023
024import java.io.ByteArrayOutputStream;
025
026import org.apache.mina.core.buffer.IoBuffer;
027import org.apache.mina.filter.codec.ProtocolCodecSession;
028import org.apache.mina.filter.codec.ProtocolDecoder;
029import org.apache.mina.filter.codec.ProtocolDecoderOutput;
030import org.apache.mina.filter.codec.ProtocolEncoder;
031import org.apache.mina.filter.codec.ProtocolEncoderOutput;
032import org.junit.Test;
033
034/**
035 * Tests object serialization codec and streams.
036 *
037 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
038 */
039public class ObjectSerializationTest {
040    @Test
041    public void testEncoder() throws Exception {
042        final String expected = "1234";
043
044        ProtocolCodecSession session = new ProtocolCodecSession();
045        ProtocolEncoderOutput out = session.getEncoderOutput();
046
047        ProtocolEncoder encoder = new ObjectSerializationEncoder();
048        encoder.encode(session, expected, out);
049
050        assertEquals(1, session.getEncoderOutputQueue().size());
051        IoBuffer buf = (IoBuffer) session.getEncoderOutputQueue().poll();
052
053        testDecoderAndInputStream(expected, buf);
054    }
055
056    @Test
057    public void testOutputStream() throws Exception {
058        final String expected = "1234";
059
060        ByteArrayOutputStream baos = new ByteArrayOutputStream();
061        ObjectSerializationOutputStream osos = new ObjectSerializationOutputStream(baos);
062
063        osos.writeObject(expected);
064        osos.flush();
065
066        testDecoderAndInputStream(expected, IoBuffer.wrap(baos.toByteArray()));
067        osos.close();
068    }
069
070    private void testDecoderAndInputStream(String expected, IoBuffer in) throws Exception {
071        // Test InputStream
072        ObjectSerializationInputStream osis = new ObjectSerializationInputStream(in.duplicate().asInputStream());
073
074        Object actual = osis.readObject();
075        assertEquals(expected, actual);
076
077        // Test ProtocolDecoder
078        ProtocolDecoder decoder = new ObjectSerializationDecoder();
079        ProtocolCodecSession session = new ProtocolCodecSession();
080        ProtocolDecoderOutput decoderOut = session.getDecoderOutput();
081        decoder.decode(session, in.duplicate(), decoderOut);
082
083        assertEquals(1, session.getDecoderOutputQueue().size());
084        assertEquals(expected, session.getDecoderOutputQueue().poll());
085        osis.close();
086    }
087}