You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by tr...@apache.org on 2005/05/09 11:37:53 UTC
svn commit: r169260 - in
/directory/network/branches/api_integration/src/test/org/apache/mina:
common/ filter/ filter/codec/ io/ io/datagram/ io/socket/ protocol/
protocol/codec/ transport/ transport/socket/ transport/socket/nio/
Author: trustin
Date: Mon May 9 02:37:51 2005
New Revision: 169260
URL: http://svn.apache.org/viewcvs?rev=169260&view=rev
Log:
* Reorganized test classes
* Removed unused packages
Added:
directory/network/branches/api_integration/src/test/org/apache/mina/common/IoFilterChainTest.java (with props)
directory/network/branches/api_integration/src/test/org/apache/mina/filter/
directory/network/branches/api_integration/src/test/org/apache/mina/filter/codec/
directory/network/branches/api_integration/src/test/org/apache/mina/filter/codec/CumulativeProtocolDecoderTest.java (with props)
directory/network/branches/api_integration/src/test/org/apache/mina/transport/
directory/network/branches/api_integration/src/test/org/apache/mina/transport/socket/
directory/network/branches/api_integration/src/test/org/apache/mina/transport/socket/nio/
directory/network/branches/api_integration/src/test/org/apache/mina/transport/socket/nio/AbstractBindTest.java (with props)
directory/network/branches/api_integration/src/test/org/apache/mina/transport/socket/nio/DatagramBindTest.java (with props)
directory/network/branches/api_integration/src/test/org/apache/mina/transport/socket/nio/SocketBindTest.java (with props)
Removed:
directory/network/branches/api_integration/src/test/org/apache/mina/io/AbstractBindTest.java
directory/network/branches/api_integration/src/test/org/apache/mina/io/datagram/
directory/network/branches/api_integration/src/test/org/apache/mina/io/socket/
directory/network/branches/api_integration/src/test/org/apache/mina/protocol/ProtocolFilterChainTest.java
directory/network/branches/api_integration/src/test/org/apache/mina/protocol/codec/
Added: directory/network/branches/api_integration/src/test/org/apache/mina/common/IoFilterChainTest.java
URL: http://svn.apache.org/viewcvs/directory/network/branches/api_integration/src/test/org/apache/mina/common/IoFilterChainTest.java?rev=169260&view=auto
==============================================================================
--- directory/network/branches/api_integration/src/test/org/apache/mina/common/IoFilterChainTest.java (added)
+++ directory/network/branches/api_integration/src/test/org/apache/mina/common/IoFilterChainTest.java Mon May 9 02:37:51 2005
@@ -0,0 +1,236 @@
+/*
+ * @(#) $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;
+
+import java.net.SocketAddress;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.apache.mina.filter.codec.ProtocolDecoder;
+import org.apache.mina.filter.codec.ProtocolEncoder;
+
+/**
+ * Tests {@link AbstractProtocolFilterChain}.
+ *
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public class IoFilterChainTest extends TestCase
+{
+ private IoFilterChainImpl chain;
+ private IoSession session;
+ private String result;
+
+ public void setUp()
+ {
+ chain = new IoFilterChainImpl();
+ session = new TestSession();
+ result = "";
+ }
+
+ public void tearDown()
+ {
+ }
+
+ public void testDefault()
+ {
+ run( "HSO HMR HMS HSI HEC HSC" );
+ }
+
+ public void testChained()
+ {
+ 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" );
+ }
+
+ private void run( String expectedResult )
+ {
+ chain.sessionOpened( session );
+ chain.messageReceived( session, new Object() );
+ chain.filterWrite( session, new Object() );
+ chain.sessionIdle( session, IdleStatus.READER_IDLE );
+ chain.exceptionCaught( session, new Exception() );
+ chain.sessionClosed( 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 extends BaseIoSession implements IoSession
+ {
+ private IoHandler handler = new IoHandlerAdapter()
+ {
+ public void sessionOpened(IoSession session) {
+ result += "HSO";
+ }
+
+ public void sessionClosed(IoSession session) {
+ result += "HSC";
+ }
+
+ 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 messageReceived(IoSession session, Object message) {
+ result += "HMR";
+ }
+
+ public void messageSent(IoSession session, Object message) {
+ result += "HMS";
+ }
+ };
+
+ public IoHandler getHandler() {
+ return handler;
+ }
+
+ public ProtocolEncoder getEncoder() {
+ return null;
+ }
+
+ public ProtocolDecoder getDecoder() {
+ return null;
+ }
+
+ public void close( boolean wait ) {
+ }
+
+ public void write(Object message) {
+ }
+
+ public TransportType getTransportType() {
+ return null;
+ }
+
+ public boolean isConnected() {
+ return false;
+ }
+
+ public IoSessionConfig getConfig() {
+ return null;
+ }
+
+ public SocketAddress getRemoteAddress() {
+ return null;
+ }
+
+ public SocketAddress getLocalAddress() {
+ return null;
+ }
+
+ public IoFilterChain getFilterChain()
+ {
+ return null;
+ }
+ }
+
+ private class TestFilter implements IoFilter
+ {
+ 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 filterWrite(NextFilter nextFilter, IoSession session, Object message) {
+ result += id + "FW";
+ nextFilter.filterWrite( session, message );
+ }
+
+ public void messageReceived(NextFilter nextFilter, IoSession session, Object message) {
+ result += id + "MR";
+ nextFilter.messageReceived( session, message );
+ }
+
+ public void messageSent(NextFilter nextFilter, IoSession session, Object message) {
+ result += id + "MS";
+ nextFilter.messageSent( session, message );
+ }
+ }
+
+ private static class IoFilterChainImpl extends AbstractIoFilterChain
+ {
+ protected IoFilterChainImpl()
+ {
+ }
+
+ protected void doWrite( IoSession session, Object message )
+ {
+ messageSent( session, message );
+ }
+ }
+
+}
Propchange: directory/network/branches/api_integration/src/test/org/apache/mina/common/IoFilterChainTest.java
------------------------------------------------------------------------------
svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision
Added: directory/network/branches/api_integration/src/test/org/apache/mina/filter/codec/CumulativeProtocolDecoderTest.java
URL: http://svn.apache.org/viewcvs/directory/network/branches/api_integration/src/test/org/apache/mina/filter/codec/CumulativeProtocolDecoderTest.java?rev=169260&view=auto
==============================================================================
--- directory/network/branches/api_integration/src/test/org/apache/mina/filter/codec/CumulativeProtocolDecoderTest.java (added)
+++ directory/network/branches/api_integration/src/test/org/apache/mina/filter/codec/CumulativeProtocolDecoderTest.java Mon May 9 02:37:51 2005
@@ -0,0 +1,222 @@
+/*
+ * @(#) $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.filter.codec;
+
+import java.net.SocketAddress;
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.apache.mina.common.BaseIoSession;
+import org.apache.mina.common.ByteBuffer;
+import org.apache.mina.common.IoFilterChain;
+import org.apache.mina.common.IoHandler;
+import org.apache.mina.common.IoSession;
+import org.apache.mina.common.IoSessionConfig;
+import org.apache.mina.common.TransportType;
+import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
+import org.apache.mina.filter.codec.ProtocolDecoder;
+import org.apache.mina.filter.codec.ProtocolDecoderOutput;
+import org.apache.mina.filter.codec.ProtocolEncoder;
+import org.apache.mina.filter.codec.ProtocolViolationException;
+
+/**
+ * Tests {@link CumulativeProtocolDecoder}.
+ *
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public class CumulativeProtocolDecoderTest extends TestCase
+{
+ private final IoSession session = new IoSessionImpl();
+ 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( session, 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( session, 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( session, 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( session, buf, output );
+ Assert.fail();
+ }
+ catch( IllegalStateException e )
+ {
+ // OK
+ }
+ }
+
+ private static class IntegerDecoder extends CumulativeProtocolDecoder
+ {
+ protected IntegerDecoder()
+ {
+ super( 4 );
+ }
+
+ protected boolean doDecode( IoSession 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( IoSession session, ByteBuffer in,
+ ProtocolDecoderOutput out ) throws ProtocolViolationException {
+ return true;
+ }
+ }
+
+ private static class IoSessionImpl extends BaseIoSession implements IoSession
+ {
+
+ public IoHandler getHandler() {
+ return null;
+ }
+
+ public ProtocolEncoder getEncoder() {
+ return null;
+ }
+
+ public ProtocolDecoder getDecoder() {
+ return null;
+ }
+
+ public void close( boolean wait ) {
+ }
+
+ public void write(Object message) {
+ }
+
+ public TransportType getTransportType() {
+ return TransportType.SOCKET;
+ }
+
+ public boolean isConnected() {
+ return false;
+ }
+
+ public IoSessionConfig getConfig() {
+ return null;
+ }
+
+ public SocketAddress getRemoteAddress() {
+ return null;
+ }
+
+ public SocketAddress getLocalAddress() {
+ return null;
+ }
+
+ public IoFilterChain getFilterChain()
+ {
+ return null;
+ }
+ }
+}
Propchange: directory/network/branches/api_integration/src/test/org/apache/mina/filter/codec/CumulativeProtocolDecoderTest.java
------------------------------------------------------------------------------
svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision
Added: directory/network/branches/api_integration/src/test/org/apache/mina/transport/socket/nio/AbstractBindTest.java
URL: http://svn.apache.org/viewcvs/directory/network/branches/api_integration/src/test/org/apache/mina/transport/socket/nio/AbstractBindTest.java?rev=169260&view=auto
==============================================================================
--- directory/network/branches/api_integration/src/test/org/apache/mina/transport/socket/nio/AbstractBindTest.java (added)
+++ directory/network/branches/api_integration/src/test/org/apache/mina/transport/socket/nio/AbstractBindTest.java Mon May 9 02:37:51 2005
@@ -0,0 +1,149 @@
+/*
+ * @(#) $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.transport.socket.nio;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.util.Date;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.apache.mina.common.IoAcceptor;
+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;
+ protected int port;
+
+ public AbstractBindTest( IoAcceptor acceptor )
+ {
+ this.acceptor = acceptor;
+ }
+
+ public void setUp() throws IOException
+ {
+ // Find an availble test port and bind to it.
+ boolean socketBound = false;
+
+ // Let's start from port #1 to detect possible resource leak
+ // because test will fail in port 1-1023 if user run this test
+ // as a normal user.
+ for( port = 1; port <= 65535; port ++ )
+ {
+ socketBound = false;
+ try
+ {
+ acceptor.bind( new InetSocketAddress( port ),
+ new EchoProtocolHandler() );
+ socketBound = true;
+ break;
+ }
+ catch( IOException e )
+ {
+ }
+ }
+
+ // If there is no port available, test fails.
+ if( !socketBound )
+ {
+ throw new IOException( "Cannot bind any test port." );
+ }
+
+ //System.out.println( "Using port " + port + " for testing." );
+ }
+
+ public void tearDown()
+ {
+ try
+ {
+ acceptor.unbind( new InetSocketAddress( port ) );
+ }
+ catch( Exception e )
+ {
+ // ignore
+ }
+ }
+
+ public void testDuplicateBind()
+ {
+ try
+ {
+ acceptor.bind( new InetSocketAddress( port ), new EchoProtocolHandler() );
+ Assert.fail( "IOException is not thrown" );
+ }
+ catch( IOException e )
+ {
+ }
+ }
+
+ public void testDuplicateUnbind()
+ {
+ // this should succeed
+ acceptor.unbind( new InetSocketAddress( port ) );
+
+ try
+ {
+ // this should fail
+ acceptor.unbind( new InetSocketAddress( port ) );
+ Assert.fail( "Exception is not thrown" );
+ }
+ catch( Exception e )
+ {
+ }
+ }
+
+ public void testManyTimes() throws IOException
+ {
+ InetSocketAddress addr = new InetSocketAddress( port );
+ EchoProtocolHandler handler = new EchoProtocolHandler();
+ for( int i = 0; i < 1024; i++ )
+ {
+ acceptor.unbind( addr );
+ acceptor.bind( addr, handler );
+ }
+ }
+
+ public void _testRegressively() throws IOException
+ {
+ tearDown();
+
+ InetSocketAddress addr = new InetSocketAddress( port );
+ EchoProtocolHandler handler = new EchoProtocolHandler();
+ for( int i = 0; i < 1048576; i++ )
+ {
+ acceptor.bind( addr, handler );
+ testDuplicateBind();
+ testDuplicateUnbind();
+ if( i % 100 == 0 )
+ {
+ System.out.println( i + " (" + new Date() + ")" );
+ }
+ }
+ setUp();
+ }
+
+}
Propchange: directory/network/branches/api_integration/src/test/org/apache/mina/transport/socket/nio/AbstractBindTest.java
------------------------------------------------------------------------------
svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision
Added: directory/network/branches/api_integration/src/test/org/apache/mina/transport/socket/nio/DatagramBindTest.java
URL: http://svn.apache.org/viewcvs/directory/network/branches/api_integration/src/test/org/apache/mina/transport/socket/nio/DatagramBindTest.java?rev=169260&view=auto
==============================================================================
--- directory/network/branches/api_integration/src/test/org/apache/mina/transport/socket/nio/DatagramBindTest.java (added)
+++ directory/network/branches/api_integration/src/test/org/apache/mina/transport/socket/nio/DatagramBindTest.java Mon May 9 02:37:51 2005
@@ -0,0 +1,37 @@
+/*
+ * @(#) $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.transport.socket.nio;
+
+import java.io.IOException;
+
+/**
+ * Tests {@link DatagramAcceptor} resource leakage.
+ *
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public class DatagramBindTest extends AbstractBindTest
+{
+
+ public DatagramBindTest() throws IOException
+ {
+ super( new DatagramAcceptor() );
+ }
+
+}
Propchange: directory/network/branches/api_integration/src/test/org/apache/mina/transport/socket/nio/DatagramBindTest.java
------------------------------------------------------------------------------
svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision
Added: directory/network/branches/api_integration/src/test/org/apache/mina/transport/socket/nio/SocketBindTest.java
URL: http://svn.apache.org/viewcvs/directory/network/branches/api_integration/src/test/org/apache/mina/transport/socket/nio/SocketBindTest.java?rev=169260&view=auto
==============================================================================
--- directory/network/branches/api_integration/src/test/org/apache/mina/transport/socket/nio/SocketBindTest.java (added)
+++ directory/network/branches/api_integration/src/test/org/apache/mina/transport/socket/nio/SocketBindTest.java Mon May 9 02:37:51 2005
@@ -0,0 +1,37 @@
+/*
+ * @(#) $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.transport.socket.nio;
+
+import java.io.IOException;
+
+/**
+ * Tests {@link SocketAcceptor} resource leakage.
+ *
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public class SocketBindTest extends AbstractBindTest
+{
+
+ public SocketBindTest() throws IOException
+ {
+ super( new SocketAcceptor() );
+ }
+
+}
Propchange: directory/network/branches/api_integration/src/test/org/apache/mina/transport/socket/nio/SocketBindTest.java
------------------------------------------------------------------------------
svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision