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