1   /*
2    *   @(#) $Id: CumulativeProtocolDecoderTest.java 165302 2005-04-29 12:53:46Z 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 Trustin Lee (trustin@apache.org)
44   * @version $Rev: 165302 $, $Date: 2005-04-29 21:53:46 +0900 (금, 29  4월 2005) $ 
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             if( in.remaining() < 4 )
134                 return false;
135             
136             out.write( new Integer( in.getInt() ) );
137             return true;
138         }
139         
140     }
141     
142     private static class IntegerDecoderOutput implements ProtocolDecoderOutput
143     {
144         private List values = new ArrayList();
145 
146         public void write( Object message )
147         {
148             values.add( message );
149         }
150         
151         public List getValues()
152         {
153             return values;
154         }
155         
156         public void clear()
157         {
158             values.clear();
159         }
160     }
161     
162     private static class WrongDecoder extends CumulativeProtocolDecoder
163     {
164         public WrongDecoder()
165         {
166             super( 4 );
167         }
168 
169         protected boolean doDecode( ProtocolSession session, ByteBuffer in,
170                                     ProtocolDecoderOutput out ) throws ProtocolViolationException {
171             return true;
172         }
173     }
174     
175     private static class ProtocolSessionImpl extends BaseSession implements ProtocolSession
176     {
177 
178         public ProtocolHandler getHandler() {
179             return null;
180         }
181 
182         public ProtocolEncoder getEncoder() {
183             return null;
184         }
185 
186         public ProtocolDecoder getDecoder() {
187             return null;
188         }
189 
190         public void close( boolean wait ) {
191         }
192 
193         public void write(Object message) {
194         }
195 
196         public TransportType getTransportType() {
197             return TransportType.SOCKET;
198         }
199 
200         public boolean isConnected() {
201             return false;
202         }
203 
204         public SessionConfig getConfig() {
205             return null;
206         }
207 
208         public SocketAddress getRemoteAddress() {
209             return null;
210         }
211 
212         public SocketAddress getLocalAddress() {
213             return null;
214         }
215 
216         public ProtocolFilterChain getFilterChain()
217         {
218             return null;
219         }
220     }
221 }