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;
21  
22  import org.apache.mina.filter.codec.demux.MessageDecoderResult;
23  import org.apache.mina.filter.codec.demux.MessageDecoderAdapter;
24  import org.apache.mina.filter.codec.demux.DemuxingProtocolDecoder;
25  import org.apache.mina.filter.codec.ProtocolDecoderOutput;
26  import org.apache.mina.core.session.IoSession;
27  import org.apache.mina.core.session.DummySession;
28  import org.apache.mina.core.buffer.IoBuffer;
29  import org.apache.mina.core.service.DefaultTransportMetadata;
30  import org.apache.mina.core.file.FileRegion;
31  import org.apache.mina.transport.socket.SocketSessionConfig;
32  import org.easymock.EasyMock;
33  import org.junit.Test;
34  
35  import java.net.InetSocketAddress;
36  import java.nio.charset.Charset;
37  
38  /**
39   * Simple Unit Test showing that the DemuxingProtocolDecoder has
40   * inconsistent behavior if used with a non fragmented transport.
41   * 
42   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
43  */
44  public class DemuxingProtocolDecoderBugTest {
45  
46      private static void doTest(IoSession session) throws Exception {
47          ProtocolDecoderOutput mock = EasyMock.createMock(ProtocolDecoderOutput.class);
48          mock.write(Character.valueOf('A'));
49          mock.write(Character.valueOf('B'));
50          mock.write(Integer.valueOf(1));
51          mock.write(Integer.valueOf(2));
52          mock.write(Character.valueOf('C'));
53          EasyMock.replay(mock);
54  
55          IoBuffer buffer = IoBuffer.allocate(1000);
56          buffer.putString("AB12C", Charset.defaultCharset().newEncoder());
57          buffer.flip();
58  
59          DemuxingProtocolDecoder decoder = new DemuxingProtocolDecoder();
60          decoder.addMessageDecoder(CharacterMessageDecoder.class);
61          decoder.addMessageDecoder(IntegerMessageDecoder.class);
62  
63          decoder.decode(session, buffer, mock);
64  
65          EasyMock.verify(mock);
66      }
67  
68      public static class CharacterMessageDecoder extends MessageDecoderAdapter {
69          public MessageDecoderResult decodable(IoSession session, IoBuffer in) {
70              return Character.isDigit((char) in.get()) ? MessageDecoderResult.NOT_OK : MessageDecoderResult.OK;
71          }
72  
73          public MessageDecoderResult decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
74              out.write(Character.valueOf((char) in.get()));
75              return MessageDecoderResult.OK;
76          }
77      }
78  
79      public static class IntegerMessageDecoder extends MessageDecoderAdapter {
80          public MessageDecoderResult decodable(IoSession session, IoBuffer in) {
81              return Character.isDigit((char) in.get()) ? MessageDecoderResult.OK : MessageDecoderResult.NOT_OK;
82          }
83  
84          public MessageDecoderResult decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
85              out.write(Integer.parseInt("" + (char) in.get()));
86              return MessageDecoderResult.OK;
87          }
88      }
89  
90      private static class SessionStub extends DummySession {
91          public SessionStub(boolean fragmented) {
92              setTransportMetadata(new DefaultTransportMetadata("nio", "socket", false, fragmented,
93                      InetSocketAddress.class, SocketSessionConfig.class, IoBuffer.class, FileRegion.class));
94          }
95      }
96  
97      /**
98       * Test a decoding with fragmentation
99       * @throws Exception If the test failed
100      */
101     @Test
102     public void testFragmentedTransport() throws Exception {
103         doTest(new SessionStub(true));
104     }
105 
106     /**
107      * Test a decoding without fragmentation
108      * @throws Exception If the test failed
109      */
110     @Test
111     public void testNonFragmentedTransport() throws Exception {
112         doTest(new SessionStub(false));
113     }
114 }