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  import java.net.SocketAddress;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import junit.framework.Assert;
28  import junit.framework.TestCase;
29  
30  import org.apache.mina.common.ByteBuffer;
31  import org.apache.mina.common.IoFilterChain;
32  import org.apache.mina.common.IoHandler;
33  import org.apache.mina.common.IoService;
34  import org.apache.mina.common.IoServiceConfig;
35  import org.apache.mina.common.IoSession;
36  import org.apache.mina.common.IoSessionConfig;
37  import org.apache.mina.common.TransportType;
38  import org.apache.mina.common.WriteFuture;
39  import org.apache.mina.common.support.BaseIoSession;
40  import org.apache.mina.filter.codec.ProtocolDecoder;
41  import org.apache.mina.filter.codec.ProtocolDecoderOutput;
42  import org.apache.mina.filter.codec.ProtocolEncoder;
43  import org.apache.mina.filter.codec.support.SimpleProtocolEncoderOutput;
44  
45  /**
46   * Tests object serialization codec and streams.
47   * 
48   * @author The Apache MINA Project Team (dev@mina.apache.org)
49   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (금, 13  7월 2007) $
50   */
51  public class ObjectSerializationTest extends TestCase {
52      public void testEncoder() throws Exception {
53          final String expected = "1234";
54  
55          IoSession session = new MockIoSession();
56          SimpleProtocolEncoderOutput out = new SimpleProtocolEncoderOutput() {
57              protected WriteFuture doFlush(ByteBuffer buf) {
58                  return null;
59              }
60          };
61  
62          ProtocolEncoder encoder = new ObjectSerializationEncoder();
63          encoder.encode(session, expected, out);
64  
65          Assert.assertEquals(1, out.getBufferQueue().size());
66          ByteBuffer buf = (ByteBuffer) out.getBufferQueue().poll();
67  
68          testDecoderAndInputStream(expected, buf);
69      }
70  
71      public void testOutputStream() throws Exception {
72          final String expected = "1234";
73  
74          ByteArrayOutputStream baos = new ByteArrayOutputStream();
75          ObjectSerializationOutputStream osos = new ObjectSerializationOutputStream(
76                  baos);
77  
78          osos.writeObject(expected);
79          osos.flush();
80  
81          testDecoderAndInputStream(expected, ByteBuffer.wrap(baos.toByteArray()));
82      }
83  
84      private void testDecoderAndInputStream(String expected, ByteBuffer in)
85              throws Exception {
86          // Test InputStream
87          ObjectSerializationInputStream osis = new ObjectSerializationInputStream(
88                  in.duplicate().asInputStream());
89  
90          Object actual = osis.readObject();
91          assertEquals(expected, actual);
92  
93          // Test ProtocolDecoder
94          ProtocolDecoder decoder = new ObjectSerializationDecoder();
95          MockProtocolDecoderOutput decoderOut = new MockProtocolDecoderOutput();
96          IoSession session = new MockIoSession();
97          decoder.decode(session, in.duplicate(), decoderOut);
98  
99          Assert.assertEquals(expected, decoderOut.result.get(0));
100         Assert.assertEquals(1, decoderOut.result.size());
101     }
102 
103     private static class MockIoSession extends BaseIoSession {
104 
105         protected void updateTrafficMask() {
106         }
107 
108         public IoSessionConfig getConfig() {
109             return null;
110         }
111 
112         public IoFilterChain getFilterChain() {
113             return null;
114         }
115 
116         public IoHandler getHandler() {
117             return null;
118         }
119 
120         public SocketAddress getLocalAddress() {
121             return null;
122         }
123 
124         public SocketAddress getRemoteAddress() {
125             return null;
126         }
127 
128         public int getScheduledWriteBytes() {
129             return 0;
130         }
131 
132         public int getScheduledWriteRequests() {
133             return 0;
134         }
135 
136         public IoService getService() {
137             return null;
138         }
139 
140         public SocketAddress getServiceAddress() {
141             return null;
142         }
143 
144         public IoServiceConfig getServiceConfig() {
145             return null;
146         }
147 
148         public TransportType getTransportType() {
149             return null;
150         }
151     }
152 
153     private static class MockProtocolDecoderOutput implements
154             ProtocolDecoderOutput {
155         private List result = new ArrayList();
156 
157         public void flush() {
158         }
159 
160         public void write(Object message) {
161             result.add(message);
162         }
163     }
164 }