001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.filter.codec;
021:
022: import java.net.SocketAddress;
023: import java.util.ArrayList;
024: import java.util.List;
025:
026: import junit.framework.Assert;
027: import junit.framework.TestCase;
028:
029: import org.apache.mina.common.DefaultTransportMetadata;
030: import org.apache.mina.common.IoBuffer;
031: import org.apache.mina.common.IoSession;
032: import org.apache.mina.common.IoSessionConfig;
033:
034: /**
035: * Tests {@link CumulativeProtocolDecoder}.
036: *
037: * @author The Apache MINA Project (dev@mina.apache.org)
038: * @version $Rev: 604779 $, $Date: 2007-12-16 23:11:58 -0700 (Sun, 16 Dec 2007) $
039: */
040: public class CumulativeProtocolDecoderTest extends TestCase {
041: private final ProtocolCodecSession session = new ProtocolCodecSession();
042:
043: private IoBuffer buf;
044: private IntegerDecoder decoder;
045:
046: public static void main(String[] args) {
047: junit.textui.TestRunner
048: .run(CumulativeProtocolDecoderTest.class);
049: }
050:
051: @Override
052: protected void setUp() throws Exception {
053: buf = IoBuffer.allocate(16);
054: decoder = new IntegerDecoder();
055: session.setTransportMetadata(new DefaultTransportMetadata(
056: "mina", "dummy", false, true, SocketAddress.class,
057: IoSessionConfig.class, IoBuffer.class));
058: }
059:
060: @Override
061: protected void tearDown() throws Exception {
062: decoder.dispose(session);
063: }
064:
065: public void testCumulation() throws Exception {
066: buf.put((byte) 0);
067: buf.flip();
068:
069: decoder.decode(session, buf, session.getDecoderOutput());
070: Assert.assertEquals(0, session.getDecoderOutputQueue().size());
071: Assert.assertEquals(buf.limit(), buf.position());
072:
073: buf.clear();
074: buf.put((byte) 0);
075: buf.put((byte) 0);
076: buf.put((byte) 1);
077: buf.flip();
078:
079: decoder.decode(session, buf, session.getDecoderOutput());
080: Assert.assertEquals(1, session.getDecoderOutputQueue().size());
081: Assert.assertEquals(new Integer(1), session
082: .getDecoderOutputQueue().poll());
083: Assert.assertEquals(buf.limit(), buf.position());
084: }
085:
086: public void testRepeatitiveDecode() throws Exception {
087: for (int i = 0; i < 4; i++) {
088: buf.putInt(i);
089: }
090: buf.flip();
091:
092: decoder.decode(session, buf, session.getDecoderOutput());
093: Assert.assertEquals(4, session.getDecoderOutputQueue().size());
094: Assert.assertEquals(buf.limit(), buf.position());
095:
096: List<Object> expected = new ArrayList<Object>();
097: for (int i = 0; i < 4; i++) {
098: expected.add(new Integer(i));
099: }
100: Assert.assertEquals(expected, session.getDecoderOutputQueue());
101: }
102:
103: public void testWrongImplementationDetection() throws Exception {
104: try {
105: new WrongDecoder().decode(session, buf, session
106: .getDecoderOutput());
107: Assert.fail();
108: } catch (IllegalStateException e) {
109: // OK
110: }
111: }
112:
113: private static class IntegerDecoder extends
114: CumulativeProtocolDecoder {
115:
116: @Override
117: protected boolean doDecode(IoSession session, IoBuffer in,
118: ProtocolDecoderOutput out) throws Exception {
119: Assert.assertTrue(in.hasRemaining());
120: if (in.remaining() < 4) {
121: return false;
122: }
123:
124: out.write(new Integer(in.getInt()));
125: return true;
126: }
127:
128: public void dispose() throws Exception {
129: }
130:
131: }
132:
133: private static class WrongDecoder extends CumulativeProtocolDecoder {
134:
135: @Override
136: protected boolean doDecode(IoSession session, IoBuffer in,
137: ProtocolDecoderOutput out) throws Exception {
138: return true;
139: }
140:
141: public void dispose() throws Exception {
142: }
143: }
144: }
|