1   /*
2    *   @(#) $Id: CumulativeProtocolDecoderTest.java 332218 2005-11-10 03:52:42Z trustin $
3    *
4    *   Copyright 2004 The Apache Software Foundation
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   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, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
17   *
18   */
19  package org.apache.mina.protocol.codec;
20  
21  import java.net.SocketAddress;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import junit.framework.Assert;
26  import junit.framework.TestCase;
27  
28  import org.apache.mina.common.BaseSession;
29  import org.apache.mina.common.ByteBuffer;
30  import org.apache.mina.common.SessionConfig;
31  import org.apache.mina.common.TransportType;
32  import org.apache.mina.protocol.ProtocolDecoder;
33  import org.apache.mina.protocol.ProtocolDecoderOutput;
34  import org.apache.mina.protocol.ProtocolEncoder;
35  import org.apache.mina.protocol.ProtocolHandler;
36  import org.apache.mina.protocol.ProtocolFilterChain;
37  import org.apache.mina.protocol.ProtocolSession;
38  import org.apache.mina.protocol.ProtocolViolationException;
39  
40  /***
41   * Tests {@link CumulativeProtocolDecoder}.
42   * 
43   * @author The Apache Directory Project (dev@directory.apache.org)
44   * @version $Rev: 332218 $, $Date: 2005-11-10 12:52:42 +0900 $ 
45   */
46  public class CumulativeProtocolDecoderTest extends TestCase
47  {
48      private final ProtocolSession session = new ProtocolSessionImpl();
49      private ByteBuffer buf;
50      private IntegerDecoder decoder;
51      private IntegerDecoderOutput output;
52  
53      public static void main(String[] args)
54      {
55          junit.textui.TestRunner.run(CumulativeProtocolDecoderTest.class);
56      }
57  
58      protected void setUp() throws Exception
59      {
60          buf = ByteBuffer.allocate( 16 );
61          decoder = new IntegerDecoder();
62          output = new IntegerDecoderOutput();
63      }
64  
65      protected void tearDown() throws Exception
66      {
67      }
68      
69      public void testCumulation() throws Exception
70      {
71          buf.put( (byte) 0 );
72          buf.flip();
73          
74          decoder.decode( session, buf, output );
75          Assert.assertEquals( 0, output.getValues().size() );
76          Assert.assertEquals( buf.limit(), buf.position() );
77          
78          buf.clear();
79          buf.put( (byte) 0 );
80          buf.put( (byte) 0 );
81          buf.put( (byte) 1 );
82          buf.flip();
83  
84          decoder.decode( session, buf, output );
85          Assert.assertEquals( 1, output.getValues().size() );
86          Assert.assertEquals( new Integer( 1 ), output.getValues().get( 0 ) );
87          Assert.assertEquals( buf.limit(), buf.position() );
88      }
89      
90      public void testRepeatitiveDecode() throws Exception
91      {
92          for( int i = 0; i < 4; i ++ )
93          {
94              buf.putInt( i );
95          }
96          buf.flip();
97          
98          decoder.decode( session, buf, output );
99          Assert.assertEquals( 4, output.getValues().size() );
100         Assert.assertEquals( buf.limit(), buf.position() );
101         
102         List expected = new ArrayList();
103         for( int i = 0; i < 4; i ++ )
104         {
105             expected.add( new Integer( i ) );
106         }
107         Assert.assertEquals( expected, output.getValues() );
108         
109     }
110     
111     public void testWrongImplementationDetection() throws Exception {
112         try
113         {
114             new WrongDecoder().decode( session, buf, output );
115             Assert.fail();
116         }
117         catch( IllegalStateException e )
118         {
119             // OK
120         }
121     }
122     
123     private static class IntegerDecoder extends CumulativeProtocolDecoder
124     {
125         protected IntegerDecoder()
126         {
127             super( 4 );
128         }
129 
130         protected boolean doDecode( ProtocolSession session, ByteBuffer in,
131                                     ProtocolDecoderOutput out ) throws ProtocolViolationException
132         {
133             Assert.assertTrue( in.hasRemaining() );
134             if( in.remaining() < 4 )
135                 return false;
136             
137             out.write( new Integer( in.getInt() ) );
138             return true;
139         }
140         
141     }
142     
143     private static class IntegerDecoderOutput implements ProtocolDecoderOutput
144     {
145         private List values = new ArrayList();
146 
147         public void write( Object message )
148         {
149             values.add( message );
150         }
151         
152         public List getValues()
153         {
154             return values;
155         }
156         
157         public void clear()
158         {
159             values.clear();
160         }
161     }
162     
163     private static class WrongDecoder extends CumulativeProtocolDecoder
164     {
165         public WrongDecoder()
166         {
167             super( 4 );
168         }
169 
170         protected boolean doDecode( ProtocolSession session, ByteBuffer in,
171                                     ProtocolDecoderOutput out ) throws ProtocolViolationException {
172             return true;
173         }
174     }
175     
176     private static class ProtocolSessionImpl extends BaseSession implements ProtocolSession
177     {
178 
179         public ProtocolHandler getHandler() {
180             return null;
181         }
182 
183         public ProtocolEncoder getEncoder() {
184             return null;
185         }
186 
187         public ProtocolDecoder getDecoder() {
188             return null;
189         }
190 
191         public void close( boolean wait ) {
192         }
193 
194         public void write(Object message) {
195         }
196 
197         public int getScheduledWriteRequests()
198         {
199             return 0;
200         }
201 
202         public TransportType getTransportType() {
203             return TransportType.SOCKET;
204         }
205 
206         public boolean isConnected() {
207             return false;
208         }
209 
210         public SessionConfig getConfig() {
211             return null;
212         }
213 
214         public SocketAddress getRemoteAddress() {
215             return null;
216         }
217 
218         public SocketAddress getLocalAddress() {
219             return null;
220         }
221 
222         public ProtocolFilterChain getFilterChain()
223         {
224             return null;
225         }
226     }
227 }