directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r159006 - in directory/network/trunk/src: java/org/apache/mina/io/ java/org/apache/mina/io/datagram/ java/org/apache/mina/io/socket/ java/org/apache/mina/protocol/ java/org/apache/mina/protocol/io/ java/org/apache/mina/protocol/vmpipe/ test/org/apache/mina/io/ test/org/apache/mina/protocol/
Date Fri, 25 Mar 2005 12:31:28 GMT
Author: trustin
Date: Fri Mar 25 04:31:24 2005
New Revision: 159006

URL: http://svn.apache.org/viewcvs?view=rev&rev=159006
Log:
* Added chain hierarchy integrity check code
* Added test cases for chains

Added:
    directory/network/trunk/src/test/org/apache/mina/protocol/
    directory/network/trunk/src/test/org/apache/mina/protocol/ProtocolHandlerFilterChainTest.java
  (with props)
Modified:
    directory/network/trunk/src/java/org/apache/mina/io/AbstractIoHandlerFilterChain.java
    directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java
    directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramConnector.java
    directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramFilterChain.java
    directory/network/trunk/src/java/org/apache/mina/io/socket/SocketAcceptor.java
    directory/network/trunk/src/java/org/apache/mina/io/socket/SocketConnector.java
    directory/network/trunk/src/java/org/apache/mina/io/socket/SocketFilterChain.java
    directory/network/trunk/src/java/org/apache/mina/protocol/AbstractProtocolHandlerFilterChain.java
    directory/network/trunk/src/java/org/apache/mina/protocol/io/IoAdapter.java
    directory/network/trunk/src/java/org/apache/mina/protocol/io/IoProtocolFilterChain.java
    directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeAcceptor.java
    directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeConnector.java
    directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeFilterChain.java
    directory/network/trunk/src/test/org/apache/mina/io/IoHandlerFilterChainTest.java

Modified: directory/network/trunk/src/java/org/apache/mina/io/AbstractIoHandlerFilterChain.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/io/AbstractIoHandlerFilterChain.java?view=diff&r1=159005&r2=159006
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/io/AbstractIoHandlerFilterChain.java
(original)
+++ directory/network/trunk/src/java/org/apache/mina/io/AbstractIoHandlerFilterChain.java
Fri Mar 25 04:31:24 2005
@@ -200,6 +200,8 @@
             nextFilter.filterWrite( session, buf, marker );
         }
     };
+    
+    private final boolean root;
 
     private AbstractIoHandlerFilterChain parent;
     
@@ -211,13 +213,18 @@
     
     private final Entry tail;
 
-    protected AbstractIoHandlerFilterChain()
+    protected AbstractIoHandlerFilterChain( boolean root )
     {
+        this.root = root;
+
         head = new Entry( null, null, "head", HEAD_FILTER );
         tail = new Entry( head, null, "tail", TAIL_FILTER );
         head.nextEntry = tail;
         
-        register( head, IoHandlerFilterChain.NEXT_FILTER, NEXT_FILTER );
+        if( !root )
+        {
+            register( head, IoHandlerFilterChain.NEXT_FILTER, NEXT_FILTER );
+        }
     }
     
     public IoHandlerFilterChain getRoot()
@@ -330,15 +337,29 @@
 
     private void register( Entry prevEntry, String name, IoHandlerFilter filter )
     {
+        if ( filter instanceof AbstractIoHandlerFilterChain )
+        {
+            if( !this.getClass().isAssignableFrom( filter.getClass() ) )
+            {
+                throw new IllegalArgumentException( "Incompatible chain" );
+            }
+            if( ( ( AbstractIoHandlerFilterChain ) filter ).root )
+            {
+                throw new IllegalArgumentException( "Root chain cannot be added." );
+            }
+            if( ( ( AbstractIoHandlerFilterChain ) filter ).parent != null )
+            {
+                throw new IllegalArgumentException( "Already added to other parent chain."
);
+            }
+
+            ( ( AbstractIoHandlerFilterChain ) filter ).parent = this;
+        }
+        
         Entry newEntry = new Entry( prevEntry, prevEntry.nextEntry, name, filter );
         prevEntry.nextEntry.prevEntry = newEntry;
         prevEntry.nextEntry = newEntry;
         name2entry.put( name, newEntry );
         filter2entry.put( filter, newEntry );
-        if ( filter instanceof AbstractIoHandlerFilterChain )
-        {
-            ( ( AbstractIoHandlerFilterChain ) filter ).parent = this;
-        }
     }
 
     /**

Modified: directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java?view=diff&r1=159005&r2=159006
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java Fri
Mar 25 04:31:24 2005
@@ -47,7 +47,7 @@
 {
     private static volatile int nextId = 0;
 
-    private final DatagramFilterChain filters = new DatagramFilterChain( this );
+    private final DatagramFilterChain filters = new DatagramFilterChain( true, this );
 
     private final int id = nextId ++ ;
 
@@ -515,7 +515,7 @@
     
     public IoHandlerFilterChain newFilterChain()
     {
-        return new DatagramFilterChain( this );
+        return new DatagramFilterChain( false, this );
     }
     
     public IoHandlerFilterChain getFilterChain()

Modified: directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramConnector.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramConnector.java?view=diff&r1=159005&r2=159006
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramConnector.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramConnector.java Fri
Mar 25 04:31:24 2005
@@ -48,7 +48,7 @@
 {
     private static volatile int nextId = 0;
 
-    private final DatagramFilterChain filters = new DatagramFilterChain( this );
+    private final DatagramFilterChain filters = new DatagramFilterChain( true, this );
 
     private final int id = nextId ++ ;
 
@@ -428,7 +428,7 @@
 
     public IoHandlerFilterChain newFilterChain()
     {
-        return new DatagramFilterChain( this );
+        return new DatagramFilterChain( false, this );
     }
     
     public IoHandlerFilterChain getFilterChain()

Modified: directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramFilterChain.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramFilterChain.java?view=diff&r1=159005&r2=159006
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramFilterChain.java
(original)
+++ directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramFilterChain.java
Fri Mar 25 04:31:24 2005
@@ -9,8 +9,9 @@
 
     private final DatagramProcessor processor;
 
-    DatagramFilterChain( DatagramProcessor processor )
+    DatagramFilterChain( boolean root, DatagramProcessor processor )
     {
+        super( root );
         this.processor = processor;
     }
 

Modified: directory/network/trunk/src/java/org/apache/mina/io/socket/SocketAcceptor.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/io/socket/SocketAcceptor.java?view=diff&r1=159005&r2=159006
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/io/socket/SocketAcceptor.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/io/socket/SocketAcceptor.java Fri Mar
25 04:31:24 2005
@@ -49,7 +49,7 @@
 {
     private static volatile int nextId = 0;
 
-    private final SocketFilterChain filters = new SocketFilterChain();
+    private final SocketFilterChain filters = new SocketFilterChain( true );
 
     private final int id = nextId ++ ;
 
@@ -425,7 +425,7 @@
 
     public IoHandlerFilterChain newFilterChain()
     {
-        return new SocketFilterChain();
+        return new SocketFilterChain( false );
     }
 
 

Modified: directory/network/trunk/src/java/org/apache/mina/io/socket/SocketConnector.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/io/socket/SocketConnector.java?view=diff&r1=159005&r2=159006
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/io/socket/SocketConnector.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/io/socket/SocketConnector.java Fri Mar
25 04:31:24 2005
@@ -48,7 +48,7 @@
 
     private final int id = nextId++;
 
-    private final SocketFilterChain filters = new SocketFilterChain();
+    private final SocketFilterChain filters = new SocketFilterChain( true );
 
     private final Selector selector;
 
@@ -330,7 +330,7 @@
 
     public IoHandlerFilterChain newFilterChain()
     {
-        return new SocketFilterChain();
+        return new SocketFilterChain( false );
     }
     
     public IoHandlerFilterChain getFilterChain()

Modified: directory/network/trunk/src/java/org/apache/mina/io/socket/SocketFilterChain.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/io/socket/SocketFilterChain.java?view=diff&r1=159005&r2=159006
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/io/socket/SocketFilterChain.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/io/socket/SocketFilterChain.java Fri
Mar 25 04:31:24 2005
@@ -7,8 +7,9 @@
 
 class SocketFilterChain extends AbstractIoHandlerFilterChain {
 
-    SocketFilterChain()
+    SocketFilterChain( boolean root )
     {
+        super( root );
     }
 
     protected void doWrite( IoSession session, ByteBuffer buf, Object marker )

Modified: directory/network/trunk/src/java/org/apache/mina/protocol/AbstractProtocolHandlerFilterChain.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/protocol/AbstractProtocolHandlerFilterChain.java?view=diff&r1=159005&r2=159006
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/protocol/AbstractProtocolHandlerFilterChain.java
(original)
+++ directory/network/trunk/src/java/org/apache/mina/protocol/AbstractProtocolHandlerFilterChain.java
Fri Mar 25 04:31:24 2005
@@ -198,6 +198,8 @@
         }
     };
 
+    private final boolean root;
+    
     private AbstractProtocolHandlerFilterChain parent;
     
     private final Map name2entry = new HashMap();
@@ -208,13 +210,18 @@
 
     private final Entry tail;
 
-    protected AbstractProtocolHandlerFilterChain()
+    protected AbstractProtocolHandlerFilterChain( boolean root )
     {
+        this.root = root;
+        
         head = new Entry( null, null, "head", HEAD_FILTER );
         tail = new Entry( head, null, "tail", TAIL_FILTER );
         head.nextEntry = tail;
         
-        register( head, ProtocolHandlerFilterChain.NEXT_FILTER, NEXT_FILTER );
+        if( !root )
+        {
+            register( head, ProtocolHandlerFilterChain.NEXT_FILTER, NEXT_FILTER );
+        }
     }
     
     public ProtocolHandlerFilterChain getRoot()
@@ -327,15 +334,28 @@
 
     private void register( Entry prevEntry, String name, ProtocolHandlerFilter filter )
     {
+        if ( filter instanceof AbstractProtocolHandlerFilterChain )
+        {
+            if( !this.getClass().isAssignableFrom( filter.getClass() ) )
+            {
+                throw new IllegalArgumentException( "Incompatible chain" );
+            }
+            if( ( ( AbstractProtocolHandlerFilterChain ) filter ).root )
+            {
+                throw new IllegalArgumentException( "Root chain cannot be added." );
+            }
+            if( ( ( AbstractProtocolHandlerFilterChain ) filter ).parent != null )
+            {
+                throw new IllegalArgumentException( "Already added to other parent chain."
);
+            }
+
+            ( ( AbstractProtocolHandlerFilterChain ) filter ).parent = this;
+        }
         Entry newEntry = new Entry( prevEntry, prevEntry.nextEntry, name, filter );
         prevEntry.nextEntry.prevEntry = newEntry;
         prevEntry.nextEntry = newEntry;
         name2entry.put( name, newEntry );
         filter2entry.put( filter, newEntry );
-        if ( filter instanceof AbstractProtocolHandlerFilterChain )
-        {
-            ( ( AbstractProtocolHandlerFilterChain ) filter ).parent = this;
-        }
     }
 
     /**

Modified: directory/network/trunk/src/java/org/apache/mina/protocol/io/IoAdapter.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/protocol/io/IoAdapter.java?view=diff&r1=159005&r2=159006
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/protocol/io/IoAdapter.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/protocol/io/IoAdapter.java Fri Mar 25
04:31:24 2005
@@ -52,7 +52,7 @@
  */
 public class IoAdapter
 {
-    private final IoProtocolFilterChain filters = new IoProtocolFilterChain();
+    private final IoProtocolFilterChain filters = new IoProtocolFilterChain( true );
 
     IoAdapter()
     {
@@ -60,7 +60,7 @@
     
     public ProtocolHandlerFilterChain newFilterChain()
     {
-        return new IoProtocolFilterChain();
+        return new IoProtocolFilterChain( false );
     }
     
     public ProtocolHandlerFilterChain getFilterChain()

Modified: directory/network/trunk/src/java/org/apache/mina/protocol/io/IoProtocolFilterChain.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/protocol/io/IoProtocolFilterChain.java?view=diff&r1=159005&r2=159006
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/protocol/io/IoProtocolFilterChain.java
(original)
+++ directory/network/trunk/src/java/org/apache/mina/protocol/io/IoProtocolFilterChain.java
Fri Mar 25 04:31:24 2005
@@ -7,7 +7,8 @@
 
 class IoProtocolFilterChain extends AbstractProtocolHandlerFilterChain {
 
-    IoProtocolFilterChain() {
+    IoProtocolFilterChain( boolean root ) {
+        super( root );
     }
 
     protected void doWrite(ProtocolSession session, Object message) {

Modified: directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeAcceptor.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeAcceptor.java?view=diff&r1=159005&r2=159006
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeAcceptor.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeAcceptor.java Fri
Mar 25 04:31:24 2005
@@ -24,7 +24,7 @@
 {
     static final Map boundHandlers = new HashMap();
 
-    private final VmPipeFilterChain filters = new VmPipeFilterChain();
+    private final VmPipeFilterChain filters = new VmPipeFilterChain( true );
 
     /**
      * Creates a new instance.
@@ -70,7 +70,7 @@
     
     public ProtocolHandlerFilterChain newFilterChain()
     {
-        return new VmPipeFilterChain();
+        return new VmPipeFilterChain( false );
     }
     
     public ProtocolHandlerFilterChain getFilterChain()

Modified: directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeConnector.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeConnector.java?view=diff&r1=159005&r2=159006
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeConnector.java
(original)
+++ directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeConnector.java
Fri Mar 25 04:31:24 2005
@@ -21,7 +21,7 @@
  */
 public class VmPipeConnector implements ProtocolConnector
 {
-    private final VmPipeFilterChain filters = new VmPipeFilterChain();
+    private final VmPipeFilterChain filters = new VmPipeFilterChain( true );
 
     /**
      * Creates a new instance.
@@ -33,7 +33,7 @@
     
     public ProtocolHandlerFilterChain newFilterChain()
     {
-        return new VmPipeFilterChain();
+        return new VmPipeFilterChain( false );
     }
     
     public ProtocolHandlerFilterChain getFilterChain()

Modified: directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeFilterChain.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeFilterChain.java?view=diff&r1=159005&r2=159006
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeFilterChain.java
(original)
+++ directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeFilterChain.java
Fri Mar 25 04:31:24 2005
@@ -5,8 +5,9 @@
 
 class VmPipeFilterChain extends AbstractProtocolHandlerFilterChain {
 
-    VmPipeFilterChain()
+    VmPipeFilterChain( boolean root )
     {
+        super( root );
     }
 
     protected void doWrite(ProtocolSession session, Object message)

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=159005&r2=159006
==============================================================================
--- directory/network/trunk/src/test/org/apache/mina/io/IoHandlerFilterChainTest.java (original)
+++ directory/network/trunk/src/test/org/apache/mina/io/IoHandlerFilterChainTest.java Fri
Mar 25 04:31:24 2005
@@ -2,96 +2,265 @@
 
 import java.net.SocketAddress;
 
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
 import org.apache.mina.common.ByteBuffer;
 import org.apache.mina.common.IdleStatus;
 import org.apache.mina.common.SessionConfig;
 import org.apache.mina.common.TransportType;
 
-import junit.framework.TestCase;
+public class IoHandlerFilterChainTest extends TestCase
+{
+    private IoHandlerFilterChain chain;
+    private IoSession session;
+    private String result;
 
-// I'll use easymock to test AbstractIoHandlerFilterChain
-public class IoHandlerFilterChainTest extends TestCase {
+    public void setUp()
+    {
+        chain = new IoHandlerFilterChainImpl( true );
+        session = new TestSession();
+        result = "";
+    }
+    
+    public void tearDown()
+    {
+    }
+    
+    public void testDefault()
+    {
+        run( "HSO HDR HDW HSI HEC HSC" );
+    }
+    
+    public void testSimpleChain()
+    {
+        chain.addLast( "A", new TestFilter( 'A' ) );
+        chain.addLast( "B", new TestFilter( 'B' ) );
+        run( "ASO BSO HSO" +
+             "ADR BDR HDR" +
+             "BFW AFW ADW BDW HDW" +
+             "ASI BSI HSI" +
+             "AEC BEC HEC" +
+             "ASC BSC HSC" );
+    }
+    
+    public void testNestedChain()
+    {
+        IoHandlerFilterChainImpl childChain = new IoHandlerFilterChainImpl( false );
 
-    private static class TestSession implements IoSession
+        chain.addLast( "A", new TestFilter( 'A' ) );
+        chain.addLast( "child", childChain );
+        chain.addLast( "B", new TestFilter( 'B' ) );
+        childChain.addFirst( "C", new TestFilter( 'C' ) );
+        childChain.addLast( "D", new TestFilter( 'D' ) );
+        
+        run( "ASO CSO BSO HSO DSO" +
+             "ADR CDR BDR HDR DDR" +
+             "BFW DFW AFW ADW CDW BDW HDW DDW CFW" +
+             "ASI CSI BSI HSI DSI" +
+             "AEC CEC BEC HEC DEC" +
+             "ASC CSC BSC HSC DSC" );
+    }
+    
+    private void run( String expectedResult )
     {
-        private final IoHandlerFilterChain chain;
-        private final IoHandler handler;
+        chain.sessionOpened( null, session );
+        chain.dataRead( null, session, ByteBuffer.allocate( 16 ) );
+        chain.filterWrite( null, session, ByteBuffer.allocate( 16 ), null );
+        chain.sessionIdle( null, session, IdleStatus.READER_IDLE );
+        chain.exceptionCaught( null, session, new Exception() );
+        chain.sessionClosed( null, session );
+        
+        result = formatResult( result );
+        expectedResult = formatResult( expectedResult );
         
-        private TestSession( IoHandlerFilterChain chain, IoHandler handler )
+        System.out.println( "Expected: " + expectedResult );
+        System.out.println( "Actual:   " + result );
+        Assert.assertEquals( expectedResult, result );
+    }
+    
+    private String formatResult( String result )
+    {
+        result = result.replaceAll( "\\s", "" );
+        StringBuffer buf = new StringBuffer( result.length() * 4 / 3 );
+        for( int i = 0; i < result.length(); i++ )
         {
-            this.chain = chain;
-            this.handler = handler;
+            buf.append( result.charAt( i ) );
+            if( i % 3 == 2 )
+            {
+                buf.append(' ');
+            }
         }
         
+        return buf.toString();
+    }
+
+    private class TestSession implements IoSession
+    {
+        private IoHandler handler = new IoHandler()
+        {
+            public void sessionOpened(IoSession session) {
+                result += "HSO ";
+            }
+
+            public void dataRead(IoSession session, ByteBuffer buf) {
+                result += "HDR ";
+            }
+
+            public void dataWritten(IoSession session, Object marker) {
+                result += "HDW ";
+            }
+            
+            public void sessionIdle(IoSession session, IdleStatus status) {
+                result += "HSI ";
+            }
+
+            public void exceptionCaught(IoSession session, Throwable cause) {
+                result += "HEC ";
+                if( cause.getClass() != Exception.class )
+                {
+                    cause.printStackTrace( System.out );
+                }
+            }
+
+            public void sessionClosed(IoSession session) {
+                result += "HSC ";
+            }
+        };
+
         public IoHandler getHandler()
         {
             return handler;
         }
 
-        public void close() {
+        public void close()
+        {
         }
 
-        public void write(ByteBuffer buf, Object marker) {
-            chain.filterWrite( null, this, buf, marker );
+        public void write(ByteBuffer buf, Object marker)
+        {
         }
 
-        public Object getAttachment() {
+        public Object getAttachment()
+        {
             return null;
         }
 
-        public void setAttachment(Object attachment) {
+        public void setAttachment(Object attachment)
+        {
         }
 
-        public TransportType getTransportType() {
+        public TransportType getTransportType()
+        {
             return null;
         }
 
-        public boolean isConnected() {
+        public boolean isConnected()
+        {
             return false;
         }
 
-        public SessionConfig getConfig() {
+        public SessionConfig getConfig()
+        {
             return null;
         }
 
-        public SocketAddress getRemoteAddress() {
+        public SocketAddress getRemoteAddress()
+        {
             return null;
         }
 
-        public SocketAddress getLocalAddress() {
+        public SocketAddress getLocalAddress()
+        {
             return null;
         }
 
-        public long getReadBytes() {
+        public long getReadBytes()
+        {
             return 0;
         }
 
-        public long getWrittenBytes() {
+        public long getWrittenBytes()
+        {
             return 0;
         }
 
-        public long getLastIoTime() {
+        public long getLastIoTime()
+        {
             return 0;
         }
 
-        public long getLastReadTime() {
+        public long getLastReadTime()
+        {
             return 0;
         }
 
-        public long getLastWriteTime() {
+        public long getLastWriteTime()
+        {
             return 0;
         }
 
-        public boolean isIdle(IdleStatus status) {
+        public boolean isIdle(IdleStatus status)
+        {
             return false;
         }
     }
 
+    private class TestFilter implements IoHandlerFilter
+    {
+        private final char id;
+
+        private TestFilter( char id )
+        {
+            this.id = id;
+        }
+        
+        public void sessionOpened(NextFilter nextFilter, IoSession session) {
+            result += id + "SO ";
+            nextFilter.sessionOpened( session );
+        }
+
+        public void sessionClosed(NextFilter nextFilter, IoSession session) {
+            result += id + "SC ";
+            nextFilter.sessionClosed( session );
+        }
+
+        public void sessionIdle(NextFilter nextFilter, IoSession session, IdleStatus status)
{
+            result += id + "SI ";
+            nextFilter.sessionIdle( session, status );
+        }
+
+        public void exceptionCaught(NextFilter nextFilter, IoSession session, Throwable cause)
{
+            result += id + "EC ";
+            nextFilter.exceptionCaught( session, cause );
+        }
+
+        public void dataRead(NextFilter nextFilter, IoSession session, ByteBuffer buf) {
+            result += id + "DR ";
+            nextFilter.dataRead( session, buf );
+        }
+
+        public void dataWritten(NextFilter nextFilter, IoSession session, Object marker)
{
+            result += id + "DW ";
+            nextFilter.dataWritten( session, marker );
+        }
+
+        public void filterWrite(NextFilter nextFilter, IoSession session, ByteBuffer buf,
Object marker) {
+            result += id + "FW ";
+            nextFilter.filterWrite( session, buf, marker );
+        }
+    }
+
     private static class IoHandlerFilterChainImpl extends AbstractIoHandlerFilterChain
     {
+        protected IoHandlerFilterChainImpl(boolean root) {
+            super( root );
+        }
+
         protected void doWrite(IoSession session, ByteBuffer buffer, Object marker)
         {
-            session.getHandler().dataWritten( session, marker );
+            getRoot().dataWritten( null, session, marker );
         }
     }
+    
 }

Added: 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=auto&rev=159006
==============================================================================
--- directory/network/trunk/src/test/org/apache/mina/protocol/ProtocolHandlerFilterChainTest.java
(added)
+++ directory/network/trunk/src/test/org/apache/mina/protocol/ProtocolHandlerFilterChainTest.java
Fri Mar 25 04:31:24 2005
@@ -0,0 +1,258 @@
+package org.apache.mina.protocol;
+
+import java.net.SocketAddress;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.apache.mina.common.IdleStatus;
+import org.apache.mina.common.SessionConfig;
+import org.apache.mina.common.TransportType;
+
+public class ProtocolHandlerFilterChainTest extends TestCase
+{
+    private ProtocolHandlerFilterChain chain;
+    private ProtocolSession session;
+    private String result;
+
+    public void setUp()
+    {
+        chain = new ProtocolHandlerFilterChainImpl( true );
+        session = new TestSession();
+        result = "";
+    }
+    
+    public void tearDown()
+    {
+    }
+    
+    public void testDefault()
+    {
+        run( "HSO HMR HMS HSI HEC HSC" );
+    }
+    
+    public void testSimpleChain()
+    {
+        chain.addLast( "A", new TestFilter( 'A' ) );
+        chain.addLast( "B", new TestFilter( 'B' ) );
+        run( "ASO BSO HSO" +
+             "AMR BMR HMR" +
+             "BFW AFW AMS BMS HMS" +
+             "ASI BSI HSI" +
+             "AEC BEC HEC" +
+             "ASC BSC HSC" );
+    }
+    
+    public void testNestedChain()
+    {
+        ProtocolHandlerFilterChainImpl childChain = new ProtocolHandlerFilterChainImpl( false
);
+
+        chain.addLast( "A", new TestFilter( 'A' ) );
+        chain.addLast( "child", childChain );
+        chain.addLast( "B", new TestFilter( 'B' ) );
+        childChain.addFirst( "C", new TestFilter( 'C' ) );
+        childChain.addLast( "D", new TestFilter( 'D' ) );
+        
+        run( "ASO CSO BSO HSO DSO" +
+             "AMR CMR BMR HMR DMR" +
+             "BFW DFW AFW AMS CMS BMS HMS DMS CFW" +
+             "ASI CSI BSI HSI DSI" +
+             "AEC CEC BEC HEC DEC" +
+             "ASC CSC BSC HSC DSC" );
+    }
+    
+    private void run( String expectedResult )
+    {
+        chain.sessionOpened( null, session );
+        chain.messageReceived( null, session, new Object() );
+        chain.filterWrite( null, session, new Object() );
+        chain.sessionIdle( null, session, IdleStatus.READER_IDLE );
+        chain.exceptionCaught( null, session, new Exception() );
+        chain.sessionClosed( null, session );
+        
+        result = formatResult( result );
+        expectedResult = formatResult( expectedResult );
+        
+        System.out.println( "Expected: " + expectedResult );
+        System.out.println( "Actual:   " + result );
+        Assert.assertEquals( expectedResult, result );
+    }
+    
+    private String formatResult( String result )
+    {
+        result = result.replaceAll( "\\s", "" );
+        StringBuffer buf = new StringBuffer( result.length() * 4 / 3 );
+        for( int i = 0; i < result.length(); i++ )
+        {
+            buf.append( result.charAt( i ) );
+            if( i % 3 == 2 )
+            {
+                buf.append(' ');
+            }
+        }
+        
+        return buf.toString();
+    }
+
+    private class TestSession implements ProtocolSession
+    {
+        private ProtocolHandler handler = new ProtocolHandler()
+        {
+            public void sessionOpened(ProtocolSession session) {
+                result += "HSO";
+            }
+
+            public void sessionClosed(ProtocolSession session) {
+                result += "HSC";
+            }
+
+            public void sessionIdle(ProtocolSession session, IdleStatus status) {
+                result += "HSI";
+            }
+
+            public void exceptionCaught(ProtocolSession session, Throwable cause) {
+                result += "HEC";
+                if( cause.getClass() != Exception.class )
+                {
+                    cause.printStackTrace( System.out );
+                }
+            }
+
+            public void messageReceived(ProtocolSession session, Object message) {
+                result += "HMR";
+            }
+
+            public void messageSent(ProtocolSession session, Object message) {
+                result += "HMS";
+            }
+        };
+
+        public ProtocolHandler getHandler() {
+            return handler;
+        }
+
+        public ProtocolEncoder getEncoder() {
+            return null;
+        }
+
+        public ProtocolDecoder getDecoder() {
+            return null;
+        }
+
+        public void close() {
+        }
+
+        public Object getAttachment() {
+            return null;
+        }
+
+        public void setAttachment(Object attachment) {
+        }
+
+        public void write(Object message) {
+        }
+
+        public TransportType getTransportType() {
+            return null;
+        }
+
+        public boolean isConnected() {
+            return false;
+        }
+
+        public SessionConfig getConfig() {
+            return null;
+        }
+
+        public SocketAddress getRemoteAddress() {
+            return null;
+        }
+
+        public SocketAddress getLocalAddress() {
+            return null;
+        }
+
+        public long getReadBytes() {
+            return 0;
+        }
+
+        public long getWrittenBytes() {
+            return 0;
+        }
+
+        public long getLastIoTime() {
+            return 0;
+        }
+
+        public long getLastReadTime() {
+            return 0;
+        }
+
+        public long getLastWriteTime() {
+            return 0;
+        }
+
+        public boolean isIdle(IdleStatus status) {
+            return false;
+        }
+    }
+
+    private class TestFilter implements ProtocolHandlerFilter
+    {
+        private final char id;
+
+        private TestFilter( char id )
+        {
+            this.id = id;
+        }
+        
+        public void sessionOpened(NextFilter nextFilter, ProtocolSession session) {
+            result += id + "SO";
+            nextFilter.sessionOpened( session );
+        }
+
+        public void sessionClosed(NextFilter nextFilter, ProtocolSession session) {
+            result += id + "SC";
+            nextFilter.sessionClosed( session );
+        }
+
+        public void sessionIdle(NextFilter nextFilter, ProtocolSession session, IdleStatus
status) {
+            result += id + "SI";
+            nextFilter.sessionIdle( session, status );
+        }
+
+        public void exceptionCaught(NextFilter nextFilter, ProtocolSession session, Throwable
cause) {
+            result += id + "EC";
+            nextFilter.exceptionCaught( session, cause );
+        }
+
+        public void filterWrite(NextFilter nextFilter, ProtocolSession session, Object message)
{
+            result += id + "FW";
+            nextFilter.filterWrite( session, message );
+        }
+
+        public void messageReceived(NextFilter nextFilter, org.apache.mina.protocol.ProtocolSession
session, Object message) {
+            result += id + "MR";
+            nextFilter.messageReceived( session, message );
+        }
+
+        public void messageSent(NextFilter nextFilter, org.apache.mina.protocol.ProtocolSession
session, Object message) {
+            result += id + "MS";
+            nextFilter.messageSent( session, message );
+        }
+    }
+
+    private static class ProtocolHandlerFilterChainImpl extends AbstractProtocolHandlerFilterChain
+    {
+        protected ProtocolHandlerFilterChainImpl( boolean root )
+        {
+            super( root );
+        }
+
+        protected void doWrite( ProtocolSession session, Object message )
+        {
+            getRoot().messageSent( null, session, message );
+        }
+    }
+    
+}

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



Mime
View raw message