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 java.net.SocketAddress;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import junit.framework.Assert;
27  import junit.framework.TestCase;
28  
29  import org.apache.mina.common.ByteBuffer;
30  import org.apache.mina.common.CloseFuture;
31  import org.apache.mina.common.IoFilterChain;
32  import org.apache.mina.common.IoHandler;
33  import org.apache.mina.common.IoServiceConfig;
34  import org.apache.mina.common.IoSession;
35  import org.apache.mina.common.IoService;
36  import org.apache.mina.common.IoSessionConfig;
37  import org.apache.mina.common.TransportType;
38  import org.apache.mina.common.support.BaseIoSession;
39  
40  /**
41   * Tests {@link CumulativeProtocolDecoder}.
42   * 
43   * @author The Apache Directory Project (mina-dev@directory.apache.org)
44   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (Fri, 13 Jul 2007) $ 
45   */
46  public class CumulativeProtocolDecoderTest extends TestCase {
47      private final IoSession session = new IoSessionImpl();
48  
49      private ByteBuffer buf;
50  
51      private IntegerDecoder decoder;
52  
53      private IntegerDecoderOutput output;
54  
55      public static void main(String[] args) {
56          junit.textui.TestRunner.run(CumulativeProtocolDecoderTest.class);
57      }
58  
59      protected void setUp() throws Exception {
60          buf = ByteBuffer.allocate(16);
61          decoder = new IntegerDecoder();
62          output = new IntegerDecoderOutput();
63      }
64  
65      protected void tearDown() throws Exception {
66          decoder.dispose(session);
67      }
68  
69      public void testCumulation() throws Exception {
70          buf.put((byte) 0);
71          buf.flip();
72  
73          decoder.decode(session, buf, output);
74          Assert.assertEquals(0, output.getValues().size());
75          Assert.assertEquals(buf.limit(), buf.position());
76  
77          buf.clear();
78          buf.put((byte) 0);
79          buf.put((byte) 0);
80          buf.put((byte) 1);
81          buf.flip();
82  
83          decoder.decode(session, buf, output);
84          Assert.assertEquals(1, output.getValues().size());
85          Assert.assertEquals(new Integer(1), output.getValues().get(0));
86          Assert.assertEquals(buf.limit(), buf.position());
87      }
88  
89      public void testRepeatitiveDecode() throws Exception {
90          for (int i = 0; i < 4; i++) {
91              buf.putInt(i);
92          }
93          buf.flip();
94  
95          decoder.decode(session, buf, output);
96          Assert.assertEquals(4, output.getValues().size());
97          Assert.assertEquals(buf.limit(), buf.position());
98  
99          List<Integer> expected = new ArrayList<Integer>();
100         for (int i = 0; i < 4; i++) {
101             expected.add(new Integer(i));
102         }
103         Assert.assertEquals(expected, output.getValues());
104     }
105 
106     public void testWrongImplementationDetection() throws Exception {
107         try {
108             new WrongDecoder().decode(session, buf, output);
109             Assert.fail();
110         } catch (IllegalStateException e) {
111             // OK
112         }
113     }
114 
115     private static class IntegerDecoder extends CumulativeProtocolDecoder {
116 
117         protected boolean doDecode(IoSession session, ByteBuffer in,
118                 ProtocolDecoderOutput out) throws Exception {
119             Assert.assertTrue(in.hasRemaining());
120             if (in.remaining() < 4)
121                 return false;
122 
123             out.write(new Integer(in.getInt()));
124             return true;
125         }
126 
127         public void dispose() throws Exception {
128         }
129 
130     }
131 
132     private static class IntegerDecoderOutput implements ProtocolDecoderOutput {
133         private List<Object> values = new ArrayList<Object>();
134 
135         public void write(Object message) {
136             values.add(message);
137         }
138 
139         public List getValues() {
140             return values;
141         }
142 
143         public void clear() {
144             values.clear();
145         }
146 
147         public void flush() {
148         }
149     }
150 
151     private static class WrongDecoder extends CumulativeProtocolDecoder {
152 
153         protected boolean doDecode(IoSession session, ByteBuffer in,
154                 ProtocolDecoderOutput out) throws Exception {
155             return true;
156         }
157 
158         public void dispose() throws Exception {
159         }
160     }
161 
162     private static class IoSessionImpl extends BaseIoSession implements
163             IoSession {
164 
165         public IoHandler getHandler() {
166             return null;
167         }
168 
169         public ProtocolEncoder getEncoder() {
170             return null;
171         }
172 
173         public ProtocolDecoder getDecoder() {
174             return null;
175         }
176 
177         public CloseFuture close() {
178             return null;
179         }
180 
181         public TransportType getTransportType() {
182             return TransportType.SOCKET;
183         }
184 
185         public SocketAddress getRemoteAddress() {
186             return null;
187         }
188 
189         public SocketAddress getLocalAddress() {
190             return null;
191         }
192 
193         public IoFilterChain getFilterChain() {
194             return null;
195         }
196 
197         public int getScheduledWriteRequests() {
198             return 0;
199         }
200 
201         protected void updateTrafficMask() {
202         }
203 
204         public boolean isClosing() {
205             return false;
206         }
207 
208         public IoService getService() {
209             return null;
210         }
211 
212         public IoServiceConfig getServiceConfig() {
213             return null;
214         }
215 
216         public IoSessionConfig getConfig() {
217             return null;
218         }
219 
220         public SocketAddress getServiceAddress() {
221             return null;
222         }
223 
224         public int getScheduledWriteBytes() {
225             return 0;
226         }
227     }
228 }