directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r161424 - in directory/network/trunk/src: java/org/apache/mina/protocol/codec/ test/org/apache/mina/common/ test/org/apache/mina/examples/echoserver/ test/org/apache/mina/io/ test/org/apache/mina/io/datagram/ test/org/apache/mina/io/socket/ test/org/apache/mina/protocol/ test/org/apache/mina/protocol/codec/ test/org/apache/mina/util/
Date Fri, 15 Apr 2005 09:13:14 GMT
Author: trustin
Date: Fri Apr 15 02:13:10 2005
New Revision: 161424

URL: http://svn.apache.org/viewcvs?view=rev&rev=161424
Log:
* Added CumulativeProtocolDecoder and its test.
* Added some JavaDoc and copyright statements.

Added:
    directory/network/trunk/src/java/org/apache/mina/protocol/codec/CumulativeProtocolDecoder.java
  (with props)
    directory/network/trunk/src/test/org/apache/mina/protocol/codec/
    directory/network/trunk/src/test/org/apache/mina/protocol/codec/CumulativeProtocolDecoderTest.java
  (with props)
Modified:
    directory/network/trunk/src/test/org/apache/mina/common/ByteBufferTest.java
    directory/network/trunk/src/test/org/apache/mina/examples/echoserver/AbstractTest.java
    directory/network/trunk/src/test/org/apache/mina/examples/echoserver/AcceptorTest.java
    directory/network/trunk/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java
    directory/network/trunk/src/test/org/apache/mina/io/AbstractBindTest.java
    directory/network/trunk/src/test/org/apache/mina/io/IoHandlerFilterChainTest.java
    directory/network/trunk/src/test/org/apache/mina/io/datagram/BindTest.java
    directory/network/trunk/src/test/org/apache/mina/io/socket/BindTest.java
    directory/network/trunk/src/test/org/apache/mina/protocol/ProtocolHandlerFilterChainTest.java
    directory/network/trunk/src/test/org/apache/mina/util/IoHandlerFilterImpl.java
    directory/network/trunk/src/test/org/apache/mina/util/ProtocolHandlerFilterImpl.java
    directory/network/trunk/src/test/org/apache/mina/util/QueueTest.java

Added: directory/network/trunk/src/java/org/apache/mina/protocol/codec/CumulativeProtocolDecoder.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/protocol/codec/CumulativeProtocolDecoder.java?view=auto&rev=161424
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/protocol/codec/CumulativeProtocolDecoder.java
(added)
+++ directory/network/trunk/src/java/org/apache/mina/protocol/codec/CumulativeProtocolDecoder.java
Fri Apr 15 02:13:10 2005
@@ -0,0 +1,142 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.mina.protocol.codec;
+
+import org.apache.mina.common.ByteBuffer;
+import org.apache.mina.protocol.ProtocolDecoder;
+import org.apache.mina.protocol.ProtocolDecoderOutput;
+import org.apache.mina.protocol.ProtocolSession;
+import org.apache.mina.protocol.ProtocolViolationException;
+
+/**
+ * A {@link ProtocolDecoder} that cumulates the content of received
+ * buffers to a <em>cumulative buffer</em> to help users implement decoders.
+ * <p>
+ * If the received {@link ByteBuffer} is only a part of a message.
+ * decoders should cumulate received buffers to make a message complete or
+ * to postpone decoding until more buffers arrive.
+ * <p>
+ * Here is an example decoder that decodes a list of integers:
+ * <pre>
+ * public class IntegerDecoder extends CumulativeProtocolDecoder {
+ * 
+ *     public IntegerDecoder() {
+ *         super(4);
+ *     }
+ * 
+ *     protected boolean doDecode(ProtocolSession session, ByteBuffer in,
+ *                                ProtocolDecoderOutput out) throws ProtocolViolationException
{
+ *         if (in.remaining() < 4) {
+ *             return false; // Cumulate remainder to decode later.
+ *         }
+ *         
+ *         out.write(new Integer(in.getInt()));
+ * 
+ *         // Decoded one integer; CumulativeProtocolDecoder will call me again,
+ *         // so I can decode as many integers as possible.
+ *         return true;
+ *     }
+ * }
+ * </pre>
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public abstract class CumulativeProtocolDecoder implements ProtocolDecoder {
+    
+    /** Cumulation buffer */
+    private ByteBuffer buf;
+    
+    /**
+     * Creates a new instance with the specified default capacity of
+     * cumulative buffer.  Please note that the capacity increases
+     * automatically.
+     */
+    protected CumulativeProtocolDecoder( int defaultCapacity )
+    {
+        buf = ByteBuffer.allocate( defaultCapacity );
+    }
+    
+    /**
+     * Cumulates content of <tt>in</tt> into internal buffer and forwards
+     * decoding request to {@link #doDecode(ProtocolSession, ByteBuffer, ProtocolDecoderOutput)}.
+     * <tt>doDecode()</tt> is invoked repeatedly until it returns <tt>false</tt>
+     * and the cumulative buffer is compacted after decoding ends.
+     * 
+     * @throws IllegalStateException if your <tt>doDecode()</tt> returned
+     *                               <tt>true</tt> not consuming the cumulative
buffer.
+     */
+    public void decode( ProtocolSession session, ByteBuffer in,
+                        ProtocolDecoderOutput out ) throws ProtocolViolationException
+    {
+        put( in );
+        
+        ByteBuffer buf = this.buf;
+        buf.flip();
+
+        for( ;; )
+        {
+            int oldPos = buf.position();
+            if( !doDecode( session, buf, out ) )
+            {
+                break;
+            }
+            
+            if( buf.position() == oldPos )
+            {
+                throw new IllegalStateException(
+                        "doDecode() can't return true when buffer is not consumed." );
+            }
+        }
+
+        buf.compact();
+    }
+    
+    /**
+     * Implement this method to consume the specified cumulative buffer and
+     * decode its content into message(s). 
+     *  
+     * @param in the cumulative buffer
+     * @return <tt>true</tt> if and only if there's more to decode in the buffer
+     *         and you want to have <tt>doDecode</tt> method invoked again.
+     * @throws ProtocolViolationException if cannot decode <tt>in</tt>.
+     */
+    protected abstract boolean doDecode( ProtocolSession session, ByteBuffer in,
+                                         ProtocolDecoderOutput out ) throws ProtocolViolationException;
+
+
+    private void put( ByteBuffer in )
+    {
+        ByteBuffer buf = this.buf;
+        
+        // Check capacity
+        if( in.remaining() > buf.remaining() ) {
+            int newCapacity = buf.position() + in.remaining();
+            ByteBuffer newBuf = ByteBuffer.allocate( newCapacity );
+            buf.flip();
+            newBuf.put( buf );
+            buf.release(); // Release old buffer
+            buf = newBuf;
+        }
+        
+        // Copy to cumulative buffer
+        buf.put( in.buf() );
+        this.buf = buf;
+    }
+}

Propchange: directory/network/trunk/src/java/org/apache/mina/protocol/codec/CumulativeProtocolDecoder.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Modified: directory/network/trunk/src/test/org/apache/mina/common/ByteBufferTest.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/test/org/apache/mina/common/ByteBufferTest.java?view=diff&r1=161423&r2=161424
==============================================================================
--- directory/network/trunk/src/test/org/apache/mina/common/ByteBufferTest.java (original)
+++ directory/network/trunk/src/test/org/apache/mina/common/ByteBufferTest.java Fri Apr 15
02:13:10 2005
@@ -1,5 +1,20 @@
 /*
- * @(#) $Id$
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
  */
 package org.apache.mina.common;
 
@@ -7,10 +22,10 @@
 import junit.framework.TestCase;
 
 /**
- * TODO Document me.
+ * Tests {@link ByteBuffer}.
  * 
  * @author Trustin Lee (trustin@apache.org)
- * @version $Rev$, $Date$, 
+ * @version $Rev$, $Date$ 
  */
 public class ByteBufferTest extends TestCase
 {

Modified: directory/network/trunk/src/test/org/apache/mina/examples/echoserver/AbstractTest.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/test/org/apache/mina/examples/echoserver/AbstractTest.java?view=diff&r1=161423&r2=161424
==============================================================================
--- directory/network/trunk/src/test/org/apache/mina/examples/echoserver/AbstractTest.java
(original)
+++ directory/network/trunk/src/test/org/apache/mina/examples/echoserver/AbstractTest.java
Fri Apr 15 02:13:10 2005
@@ -1,5 +1,20 @@
 /*
- * @(#) $Id$
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
  */
 package org.apache.mina.examples.echoserver;
 

Modified: directory/network/trunk/src/test/org/apache/mina/examples/echoserver/AcceptorTest.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/test/org/apache/mina/examples/echoserver/AcceptorTest.java?view=diff&r1=161423&r2=161424
==============================================================================
--- directory/network/trunk/src/test/org/apache/mina/examples/echoserver/AcceptorTest.java
(original)
+++ directory/network/trunk/src/test/org/apache/mina/examples/echoserver/AcceptorTest.java
Fri Apr 15 02:13:10 2005
@@ -1,5 +1,20 @@
 /*
- * @(#) $Id$
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
  */
 package org.apache.mina.examples.echoserver;
 

Modified: directory/network/trunk/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java?view=diff&r1=161423&r2=161424
==============================================================================
--- directory/network/trunk/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java
(original)
+++ directory/network/trunk/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java
Fri Apr 15 02:13:10 2005
@@ -1,5 +1,20 @@
 /*
- * @(#) $Id$
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
  */
 package org.apache.mina.examples.echoserver;
 

Modified: directory/network/trunk/src/test/org/apache/mina/io/AbstractBindTest.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/test/org/apache/mina/io/AbstractBindTest.java?view=diff&r1=161423&r2=161424
==============================================================================
--- directory/network/trunk/src/test/org/apache/mina/io/AbstractBindTest.java (original)
+++ directory/network/trunk/src/test/org/apache/mina/io/AbstractBindTest.java Fri Apr 15 02:13:10
2005
@@ -1,5 +1,20 @@
 /*
- * @(#) $Id$
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
  */
 package org.apache.mina.io;
 
@@ -12,6 +27,12 @@
 
 import org.apache.mina.examples.echoserver.EchoProtocolHandler;
 
+/**
+ * Tests {@link IoAcceptor} resource leakage by repeating bind and unbind.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$ 
+ */
 public class AbstractBindTest extends TestCase
 {
     protected final IoAcceptor acceptor;

Modified: directory/network/trunk/src/test/org/apache/mina/io/IoHandlerFilterChainTest.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/test/org/apache/mina/io/IoHandlerFilterChainTest.java?view=diff&r1=161423&r2=161424
==============================================================================
--- directory/network/trunk/src/test/org/apache/mina/io/IoHandlerFilterChainTest.java (original)
+++ directory/network/trunk/src/test/org/apache/mina/io/IoHandlerFilterChainTest.java Fri
Apr 15 02:13:10 2005
@@ -1,3 +1,21 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
 package org.apache.mina.io;
 
 import java.net.SocketAddress;
@@ -10,6 +28,12 @@
 import org.apache.mina.common.SessionConfig;
 import org.apache.mina.common.TransportType;
 
+/**
+ * Tests {@link AbstractIoHandlerFilterChain}.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$ 
+ */
 public class IoHandlerFilterChainTest extends TestCase
 {
     private IoHandlerFilterChainImpl chain;

Modified: directory/network/trunk/src/test/org/apache/mina/io/datagram/BindTest.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/test/org/apache/mina/io/datagram/BindTest.java?view=diff&r1=161423&r2=161424
==============================================================================
--- directory/network/trunk/src/test/org/apache/mina/io/datagram/BindTest.java (original)
+++ directory/network/trunk/src/test/org/apache/mina/io/datagram/BindTest.java Fri Apr 15
02:13:10 2005
@@ -1,5 +1,20 @@
 /*
- * @(#) $Id$
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
  */
 package org.apache.mina.io.datagram;
 
@@ -7,6 +22,12 @@
 
 import org.apache.mina.io.AbstractBindTest;
 
+/**
+ * Tests {@link DatagramAcceptor} resource leakage.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$ 
+ */
 public class BindTest extends AbstractBindTest
 {
 

Modified: directory/network/trunk/src/test/org/apache/mina/io/socket/BindTest.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/test/org/apache/mina/io/socket/BindTest.java?view=diff&r1=161423&r2=161424
==============================================================================
--- directory/network/trunk/src/test/org/apache/mina/io/socket/BindTest.java (original)
+++ directory/network/trunk/src/test/org/apache/mina/io/socket/BindTest.java Fri Apr 15 02:13:10
2005
@@ -1,5 +1,20 @@
 /*
- * @(#) $Id$
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
  */
 package org.apache.mina.io.socket;
 
@@ -7,6 +22,12 @@
 
 import org.apache.mina.io.AbstractBindTest;
 
+/**
+ * Tests {@link SocketAcceptor} resource leakage.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$ 
+ */
 public class BindTest extends AbstractBindTest
 {
 

Modified: directory/network/trunk/src/test/org/apache/mina/protocol/ProtocolHandlerFilterChainTest.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/test/org/apache/mina/protocol/ProtocolHandlerFilterChainTest.java?view=diff&r1=161423&r2=161424
==============================================================================
--- directory/network/trunk/src/test/org/apache/mina/protocol/ProtocolHandlerFilterChainTest.java
(original)
+++ directory/network/trunk/src/test/org/apache/mina/protocol/ProtocolHandlerFilterChainTest.java
Fri Apr 15 02:13:10 2005
@@ -1,3 +1,21 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
 package org.apache.mina.protocol;
 
 import java.net.SocketAddress;
@@ -9,6 +27,12 @@
 import org.apache.mina.common.SessionConfig;
 import org.apache.mina.common.TransportType;
 
+/**
+ * Tests {@link AbstractProtocolHandlerFilterChain}.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$ 
+ */
 public class ProtocolHandlerFilterChainTest extends TestCase
 {
     private ProtocolHandlerFilterChainImpl chain;

Added: directory/network/trunk/src/test/org/apache/mina/protocol/codec/CumulativeProtocolDecoderTest.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/test/org/apache/mina/protocol/codec/CumulativeProtocolDecoderTest.java?view=auto&rev=161424
==============================================================================
--- directory/network/trunk/src/test/org/apache/mina/protocol/codec/CumulativeProtocolDecoderTest.java
(added)
+++ directory/network/trunk/src/test/org/apache/mina/protocol/codec/CumulativeProtocolDecoderTest.java
Fri Apr 15 02:13:10 2005
@@ -0,0 +1,165 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.mina.protocol.codec;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.apache.mina.common.ByteBuffer;
+import org.apache.mina.protocol.ProtocolDecoderOutput;
+import org.apache.mina.protocol.ProtocolSession;
+import org.apache.mina.protocol.ProtocolViolationException;
+
+/**
+ * Tests {@link CumulativeProtocolDecoder}.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$ 
+ */
+public class CumulativeProtocolDecoderTest extends TestCase
+{
+    private ByteBuffer buf;
+    private IntegerDecoder decoder;
+    private IntegerDecoderOutput output;
+
+    public static void main(String[] args)
+    {
+        junit.textui.TestRunner.run(CumulativeProtocolDecoderTest.class);
+    }
+
+    protected void setUp() throws Exception
+    {
+        buf = ByteBuffer.allocate( 16 );
+        decoder = new IntegerDecoder();
+        output = new IntegerDecoderOutput();
+    }
+
+    protected void tearDown() throws Exception
+    {
+    }
+    
+    public void testCumulation() throws Exception
+    {
+        buf.put( (byte) 0 );
+        buf.flip();
+        
+        decoder.decode( null, buf, output );
+        Assert.assertEquals( 0, output.getValues().size() );
+        Assert.assertEquals( buf.limit(), buf.position() );
+        
+        buf.clear();
+        buf.put( (byte) 0 );
+        buf.put( (byte) 0 );
+        buf.put( (byte) 1 );
+        buf.flip();
+
+        decoder.decode( null, buf, output );
+        Assert.assertEquals( 1, output.getValues().size() );
+        Assert.assertEquals( new Integer( 1 ), output.getValues().get( 0 ) );
+        Assert.assertEquals( buf.limit(), buf.position() );
+    }
+    
+    public void testRepeatitiveDecode() throws Exception
+    {
+        for( int i = 0; i < 4; i ++ )
+        {
+            buf.putInt( i );
+        }
+        buf.flip();
+        
+        decoder.decode( null, buf, output );
+        Assert.assertEquals( 4, output.getValues().size() );
+        Assert.assertEquals( buf.limit(), buf.position() );
+        
+        List expected = new ArrayList();
+        for( int i = 0; i < 4; i ++ )
+        {
+            expected.add( new Integer( i ) );
+        }
+        Assert.assertEquals( expected, output.getValues() );
+        
+    }
+    
+    public void testWrongImplementationDetection() throws Exception {
+        try
+        {
+            new WrongDecoder().decode( null, buf, output );
+            Assert.fail();
+        }
+        catch( IllegalStateException e )
+        {
+            // OK
+        }
+    }
+    
+    private static class IntegerDecoder extends CumulativeProtocolDecoder
+    {
+        protected IntegerDecoder()
+        {
+            super( 4 );
+        }
+
+        protected boolean doDecode( ProtocolSession session, ByteBuffer in,
+                                    ProtocolDecoderOutput out ) throws ProtocolViolationException
+        {
+            if( in.remaining() < 4 )
+                return false;
+            
+            out.write( new Integer( in.getInt() ) );
+            return true;
+        }
+        
+    }
+    
+    private static class IntegerDecoderOutput implements ProtocolDecoderOutput
+    {
+        private List values = new ArrayList();
+
+        public void write( Object message )
+        {
+            values.add( message );
+        }
+        
+        public List getValues()
+        {
+            return values;
+        }
+        
+        public void clear()
+        {
+            values.clear();
+        }
+    }
+    
+    private static class WrongDecoder extends CumulativeProtocolDecoder
+    {
+        public WrongDecoder()
+        {
+            super( 4 );
+        }
+
+        protected boolean doDecode( ProtocolSession session, ByteBuffer in,
+                                    ProtocolDecoderOutput out ) throws ProtocolViolationException
{
+            return true;
+        }
+    }
+}

Propchange: directory/network/trunk/src/test/org/apache/mina/protocol/codec/CumulativeProtocolDecoderTest.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Modified: directory/network/trunk/src/test/org/apache/mina/util/IoHandlerFilterImpl.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/test/org/apache/mina/util/IoHandlerFilterImpl.java?view=diff&r1=161423&r2=161424
==============================================================================
--- directory/network/trunk/src/test/org/apache/mina/util/IoHandlerFilterImpl.java (original)
+++ directory/network/trunk/src/test/org/apache/mina/util/IoHandlerFilterImpl.java Fri Apr
15 02:13:10 2005
@@ -1,12 +1,30 @@
 /*
- * @(#) $Id$
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
  */
 package org.apache.mina.util;
 
+import org.apache.mina.io.IoHandlerFilter;
 import org.apache.mina.io.IoHandlerFilterAdapter;
+import org.apache.mina.io.IoHandlerFilterChain;
 
 /**
- * TODO Document me.
+ * Bogus implementation of {@link IoHandlerFilter} to test
+ * {@link IoHandlerFilterChain}.
  * 
  * @author Trustin Lee (trustin@apache.org)
  * @version $Rev$, $Date$

Modified: directory/network/trunk/src/test/org/apache/mina/util/ProtocolHandlerFilterImpl.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/test/org/apache/mina/util/ProtocolHandlerFilterImpl.java?view=diff&r1=161423&r2=161424
==============================================================================
--- directory/network/trunk/src/test/org/apache/mina/util/ProtocolHandlerFilterImpl.java (original)
+++ directory/network/trunk/src/test/org/apache/mina/util/ProtocolHandlerFilterImpl.java Fri
Apr 15 02:13:10 2005
@@ -1,12 +1,30 @@
 /*
- * @(#) $Id$
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
  */
 package org.apache.mina.util;
 
+import org.apache.mina.protocol.ProtocolHandlerFilter;
 import org.apache.mina.protocol.ProtocolHandlerFilterAdapter;
+import org.apache.mina.protocol.ProtocolHandlerFilterChain;
 
 /**
- * TODO Document me.
+ * Bogus implementation of {@link ProtocolHandlerFilter} to test
+ * {@link ProtocolHandlerFilterChain}.
  * 
  * @author Trustin Lee (trustin@apache.org)
  * @version $Rev$, $Date$

Modified: directory/network/trunk/src/test/org/apache/mina/util/QueueTest.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/test/org/apache/mina/util/QueueTest.java?view=diff&r1=161423&r2=161424
==============================================================================
--- directory/network/trunk/src/test/org/apache/mina/util/QueueTest.java (original)
+++ directory/network/trunk/src/test/org/apache/mina/util/QueueTest.java Fri Apr 15 02:13:10
2005
@@ -1,11 +1,33 @@
 /*
- * @(#) $Id$
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
  */
 package org.apache.mina.util;
 
 import junit.framework.Assert;
 import junit.framework.TestCase;
 
+/**
+ * Tests {@link Queue}
+ * 
+ * @author The Apache Directory Project (dev@directory.apache.org)
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
 public class QueueTest extends TestCase
 {
     private int pushCount;
@@ -55,5 +77,4 @@
     {
         junit.textui.TestRunner.run( QueueTest.class );
     }
-
 }



Mime
View raw message