harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mloe...@apache.org
Subject svn commit: r417163 - /incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AbstractSelectableChannelTest.java
Date Mon, 26 Jun 2006 10:42:48 GMT
Author: mloenko
Date: Mon Jun 26 03:42:47 2006
New Revision: 417163

URL: http://svn.apache.org/viewvc?rev=417163&view=rev
Log:
applied patch from HARMONY-659
[classlib][nio]java.nio.channels.spi.AbstractSelectableChannel needs more tests

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AbstractSelectableChannelTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AbstractSelectableChannelTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AbstractSelectableChannelTest.java?rev=417163&r1=417162&r2=417163&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AbstractSelectableChannelTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AbstractSelectableChannelTest.java
Mon Jun 26 03:42:47 2006
@@ -16,12 +16,21 @@
 package org.apache.harmony.tests.java.nio.channels.spi;
 
 import java.io.IOException;
+import java.nio.channels.ClosedChannelException;
+import java.nio.channels.IllegalBlockingModeException;
+import java.nio.channels.IllegalSelectorException;
+import java.nio.channels.SelectableChannel;
 import java.nio.channels.SelectionKey;
+import java.nio.channels.Selector;
+import java.nio.channels.SocketChannel;
 import java.nio.channels.spi.AbstractSelectableChannel;
 import java.nio.channels.spi.SelectorProvider;
 
 import junit.framework.TestCase;
 
+/**
+ * Tests for AbstractSelectableChannel 
+ */
 public class AbstractSelectableChannelTest extends TestCase {
 
     private MockSelectableChannel testChannel;
@@ -91,27 +100,199 @@
         assertNotNull(gotObj);
     }
 
+    /**
+     * @tests AbstractSelectableChannel#register(Selector, int, Object)
+     */
+    public void test_register_LSelectorILObject() throws IOException {
+        assertFalse(testChannel.isRegistered());
+        Selector acceptSelector1 = SelectorProvider.provider().openSelector();
+        Selector acceptSelector2 = new MockAbstractSelector(SelectorProvider
+                .provider());
+        SocketChannel sc = SocketChannel.open();
+        sc.configureBlocking(false);
+        SelectionKey acceptKey = sc.register(acceptSelector1,
+                SelectionKey.OP_READ, null);
+        assertNotNull(acceptKey);
+        assertTrue(acceptKey.isValid());
+        assertSame(sc, acceptKey.channel());
+
+        //test that sc.register invokes Selector.register()
+        acceptKey = sc.register(acceptSelector2, SelectionKey.OP_READ, null);
+        assertNull(acceptKey);
+    }
+
+    /**
+     * @tests AbstractSelectableChannel#register(Selector, int, Object)
+     */
+    public void test_register_LSelectorILObject_IllegalArgument()
+            throws IOException {
+        Selector acceptSelector = SelectorProvider.provider().openSelector();
+        assertTrue(acceptSelector.isOpen());
+        MockSelectableChannel msc = new MockSelectableChannel(SelectorProvider
+                .provider());
+        msc.configureBlocking(false);
+        // in nonblocking mode
+        try {
+            //different SelectionKey with validOps
+            msc.register(acceptSelector, SelectionKey.OP_READ, null);
+            fail("Should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+        try {
+            msc.register(null, 0, null);
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        // in nonblocking mode, if selector closed
+        acceptSelector.close();
+        try {
+            msc.register(acceptSelector, SelectionKey.OP_READ, null);
+            fail("Should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+        try {
+            msc.register(null, 0, null);
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        try {
+            msc.register(acceptSelector, 0, null);
+            fail("Should throw IllegalSelectorException");
+        } catch (IllegalSelectorException e) {
+            // expected
+        }
+
+        acceptSelector = SelectorProvider.provider().openSelector();
+        // test in blocking mode
+        msc.configureBlocking(true);
+        try {
+            msc.register(acceptSelector, SelectionKey.OP_READ, null);
+            fail("Should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+        try {
+            msc.register(null, 0, null);
+            fail("Should throw IllegalBlockingModeException");
+        } catch (IllegalBlockingModeException e) {
+            // expected
+        }
+        acceptSelector.close();
+        // in blocking mode, if selector closed
+        try {
+            msc.register(acceptSelector, SelectionKey.OP_READ, null);
+            fail("Should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+        try {
+            msc.register(null, 0, null);
+            fail("Should throw IllegalBlockingModeException");
+        } catch (IllegalBlockingModeException e) {
+            // expected
+        }
+
+        // register with an object
+        Object argObj = new Object();
+        SocketChannel sc = SocketChannel.open();
+        sc.configureBlocking(false);
+        try {
+            sc.register(null, SelectionKey.OP_READ, argObj);
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+
+        // if channel closed
+        msc.close();
+        try {
+            msc.register(acceptSelector, SelectionKey.OP_READ, null);
+            fail("Should throw ClosedChannelException");
+        } catch (ClosedChannelException e) {
+            // expected
+        }
+
+    }
+
+    /**
+     * @tests AbstractSelectableChannel#keyFor(Selector)
+     */
+    public void test_keyfor_LSelector() throws Exception {
+        SocketChannel sc = SocketChannel.open();
+        Object argObj = new Object();
+        sc.configureBlocking(false);
+        Selector acceptSelector = SelectorProvider.provider().openSelector();
+        Selector acceptSelectorOther = SelectorProvider.provider()
+                .openSelector();
+        SelectionKey acceptKey = sc.register(acceptSelector,
+                SelectionKey.OP_READ, argObj);
+        assertEquals(sc.keyFor(acceptSelector), acceptKey);
+        SelectionKey acceptKeyObjNull = sc.register(acceptSelector,
+                SelectionKey.OP_READ, null);
+        assertSame(sc.keyFor(acceptSelector), acceptKeyObjNull);
+        assertSame(acceptKeyObjNull, acceptKey);
+        SelectionKey acceptKeyOther = sc.register(acceptSelectorOther,
+                SelectionKey.OP_READ, null);
+        assertSame(sc.keyFor(acceptSelectorOther), acceptKeyOther);
+    }
+
+    /**
+     * @tests AbstractSelectableChannel#configureBlocking(boolean)
+     */
+    public void test_configureBlocking_Z_IllegalBlockingMode() throws Exception {
+        SocketChannel sc = SocketChannel.open();
+        sc.configureBlocking(false);
+        Selector acceptSelector = SelectorProvider.provider().openSelector();
+        SelectionKey acceptKey = sc.register(acceptSelector,
+                SelectionKey.OP_READ, null);
+        assertEquals(sc.keyFor(acceptSelector), acceptKey);
+        SelectableChannel getChannel = sc.configureBlocking(false);
+        assertEquals(getChannel, sc);
+        try {
+            sc.configureBlocking(true);
+            fail("Should throw IllegalBlockingModeException");
+        } catch (IllegalBlockingModeException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests AbstractSelectableChannel#configureBlocking(boolean)
+     */
+    public void test_configureBlocking_Z() throws Exception {
+        MockSelectableChannel mock = new MockSelectableChannel(SelectorProvider
+                .provider());
+        //default blocking mode is true
+        //the implConfigureBlocking is only invoked if the given mode is different with current
one
+        mock.configureBlocking(true);
+        assertFalse(mock.implConfigureBlockingCalled);
+        mock.configureBlocking(false);
+        assertTrue(mock.implConfigureBlockingCalled);
+    }
+
     private class MockSelectableChannel extends AbstractSelectableChannel {
 
         private boolean isImplCloseSelectableChannelCalled = false;
 
         private int implCloseSelectableChannelCount = 0;
-        
+
+        private boolean implConfigureBlockingCalled = false;
+
         public MockSelectableChannel(SelectorProvider arg0) {
             super(arg0);
         }
 
-        public void implClose() throws IOException {
-            super.implCloseChannel();
-        }
-
         protected void implCloseSelectableChannel() throws IOException {
             isImplCloseSelectableChannelCalled = true;
             ++implCloseSelectableChannelCount;
         }
 
         protected void implConfigureBlocking(boolean arg0) throws IOException {
-            // for test only, do nothing
+            implConfigureBlockingCalled = true;
         }
 
         public int validOps() {



Mime
View raw message