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  /**
40   * Simple Unit Test showing that the DemuxingProtocolDecoder has
41   * inconsistent behavior if used with a non fragmented transport.
42   * 
43   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
44  */
45  public class DemuxingProtocolDecoderBugTest
46  {
47  
48      private static void doTest(IoSession session) throws Exception
49      {
50          ProtocolDecoderOutput mock = EasyMock.createMock(ProtocolDecoderOutput.class);
51          mock.write(Character.valueOf('A'));
52          mock.write(Character.valueOf('B'));
53          mock.write(Integer.valueOf(1));
54          mock.write(Integer.valueOf(2));
55          mock.write(Character.valueOf('C'));
56          EasyMock.replay(mock);
57  
58          IoBuffer buffer = IoBuffer.allocate(1000);
59          buffer.putString("AB12C", Charset.defaultCharset().newEncoder());
60          buffer.flip();
61  
62          DemuxingProtocolDecoder decoder = new DemuxingProtocolDecoder();
63          decoder.addMessageDecoder(CharacterMessageDecoder.class);
64          decoder.addMessageDecoder(IntegerMessageDecoder.class);
65  
66          decoder.decode(session,buffer,mock);
67  
68          EasyMock.verify(mock);
69      }
70  
71      public static class CharacterMessageDecoder extends MessageDecoderAdapter
72      {
73          public MessageDecoderResult decodable(IoSession session, IoBuffer in)
74          {
75              return Character.isDigit((char)in.get())
76                      ? MessageDecoderResult.NOT_OK
77                      : MessageDecoderResult.OK;
78          }
79  
80          public MessageDecoderResult decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception
81          {
82              out.write(Character.valueOf((char)in.get()));
83              return MessageDecoderResult.OK;
84          }
85      }
86  
87      public static class IntegerMessageDecoder extends MessageDecoderAdapter
88      {
89          public MessageDecoderResult decodable(IoSession session, IoBuffer in)
90          {
91              return Character.isDigit((char)in.get())
92                      ? MessageDecoderResult.OK
93                      : MessageDecoderResult.NOT_OK;
94          }
95  
96          public MessageDecoderResult decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception
97          {
98              out.write(Integer.parseInt("" + (char)in.get()));
99              return MessageDecoderResult.OK;
100         }
101     }
102 
103     private static class SessionStub extends DummySession
104     {
105         public SessionStub(boolean fragmented)
106         {
107             setTransportMetadata(
108                 new DefaultTransportMetadata(
109                         "nio", "socket", false, fragmented,
110                         InetSocketAddress.class,
111                         SocketSessionConfig.class,
112                         IoBuffer.class, FileRegion.class)
113             );
114         }
115     }
116 
117     /**
118      * Test a decoding with fragmentation
119      */
120     @Test
121     public void testFragmentedTransport() throws Exception
122     {
123         doTest(new SessionStub(true));
124     }
125 
126     /**
127      * Test a decoding without fragmentation
128      */
129     @Test
130     public void testNonFragmentedTransport() throws Exception
131     {
132         doTest(new SessionStub(false));
133     }
134 }