You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by gh...@apache.org on 2006/06/05 23:17:57 UTC

svn commit: r411920 - in /incubator/harmony/enhanced/classlib/trunk/modules/nio/src: main/java/java/nio/channels/spi/ test/java/org/apache/harmony/tests/java/nio/channels/spi/

Author: gharley
Date: Mon Jun  5 14:17:56 2006
New Revision: 411920

URL: http://svn.apache.org/viewvc?rev=411920&view=rev
Log:
HARMONY-41 : java.nio.channels.spi and java.nio.channels.Channels, Pipe, Selector have not been implemented

Added:
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractSelectionKey.java   (with props)
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AbstractInterruptibleChannelTest.java   (with props)
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AbstractSelectableChannelTest.java   (with props)
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AbstractSelectionKeyTest.java   (with props)
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AbstractSelectorTest.java   (with props)
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AllTests.java   (with props)
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/MockAbstractSelector.java   (with props)
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/SelectorProviderTest.java   (with props)
Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractInterruptibleChannel.java
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractSelectableChannel.java
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractSelector.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractInterruptibleChannel.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractInterruptibleChannel.java?rev=411920&r1=411919&r2=411920&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractInterruptibleChannel.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractInterruptibleChannel.java Mon Jun  5 14:17:56 2006
@@ -18,6 +18,7 @@
 import java.io.IOException;
 import java.nio.channels.AsynchronousCloseException;
 import java.nio.channels.Channel;
+import java.nio.channels.ClosedByInterruptException;
 import java.nio.channels.InterruptibleChannel;
 import java.util.LinkedList;
 import java.util.List;
@@ -36,9 +37,7 @@
 public abstract class AbstractInterruptibleChannel implements Channel,
 		InterruptibleChannel {
 
-	private static List<Thread> blockingThreads = new LinkedList();
-
-	private volatile boolean closed = false;
+    private volatile boolean closed = false;
 
 	/**
 	 * Default constructor.
@@ -83,11 +82,10 @@
 	 * Once the operation is completed the application should invoke a
 	 * corresponding <code>end(boolean)</code>.
 	 */
-	protected final void begin() {
-		// FIXME: not implemented yet
-		blockingThreads.add(Thread.currentThread());
-		// throw new NotYetImplementedException();
-	}
+    protected final void begin() {
+        // FIXME: not implemented yet, need kernel class Thread's support
+        // idea is to indicate current thread that a blocking I/O begins
+    }
 
 	/**
 	 * End an IO operation that was previously started with <code>begin()</code>.
@@ -101,15 +99,16 @@
 	 * @throws java.nio.channels.ClosedByInterruptException
 	 *             the thread conducting the IO operation was interrupted.
 	 */
-	protected final void end(boolean success) throws AsynchronousCloseException {
-		blockingThreads.remove(Thread.currentThread());
-		if (success) {
-			return;
-		}
-
-		// FIXME: not implemented yet
-		// throw new NotYetImplementedException();
-	}
+    protected final void end(boolean success) throws AsynchronousCloseException {
+        if(Thread.currentThread().isInterrupted()){
+            throw new ClosedByInterruptException();
+        }
+        if (!success && closed){
+            throw new AsynchronousCloseException();
+        }
+        // FIXME: not fully implemented yet, need kernel class Thread's support
+        // idea is to indicate current thread that a blocking I/O ends        
+    }
 
 	/**
 	 * Implements the close channel behavior.

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractSelectableChannel.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractSelectableChannel.java?rev=411920&r1=411919&r2=411920&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractSelectableChannel.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractSelectableChannel.java Mon Jun  5 14:17:56 2006
@@ -224,7 +224,7 @@
     /*
      * package private for deregister method in AbstractSelector.
      */
-    synchronized void deRegister(SelectionKey k) {
+    synchronized void deregister(SelectionKey k) {
         if (null != keyList) {
             keyList.remove(k);
         }

Added: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractSelectionKey.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractSelectionKey.java?rev=411920&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractSelectionKey.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractSelectionKey.java Mon Jun  5 14:17:56 2006
@@ -0,0 +1,59 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * 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 java.nio.channels.spi;
+
+import java.nio.channels.SelectionKey;
+
+/**
+ * Abstract class for selection key.
+ * <p>
+ * The class takes charge of the validation and cancellation of key.
+ * </p>
+ * 
+ */
+public abstract class AbstractSelectionKey extends SelectionKey {
+
+    /*
+     * package private for deregister method in AbstractSelector.
+     */
+	boolean isValid = true;
+
+	/**
+	 * Constructor for this class.
+	 */
+	protected AbstractSelectionKey() {
+		super();
+	}
+
+	/**
+	 * @see java.nio.channels.SelectionKey#isValid()
+	 */
+	public final boolean isValid() {
+		return isValid;
+	}
+
+	/**
+	 * Cancels this key and adds it to the cancelled key set.
+	 * 
+	 * @see java.nio.channels.SelectionKey#cancel()
+	 */
+	public final void cancel() {
+		if (isValid) {
+			isValid = false;
+			((AbstractSelector) selector()).cancel(this);
+		}
+	}
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractSelectionKey.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractSelector.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractSelector.java?rev=411920&r1=411919&r2=411920&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractSelector.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/java/nio/channels/spi/AbstractSelector.java Mon Jun  5 14:17:56 2006
@@ -1,4 +1,4 @@
-/* Copyright 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 2005, 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -15,19 +15,125 @@
 
 package java.nio.channels.spi;
 
-
+import java.io.IOException;
 import java.nio.channels.SelectionKey;
 import java.nio.channels.Selector;
+import java.util.HashSet;
+import java.util.Set;
 
 /**
- * TODO Type description
+ * Abstract class for selectors.
+ * <p>
+ * This class realizes the interruption of selection by <code>begin</code> and
+ * <code>end</code>. It also holds the cancelled and the deletion of the key
+ * set.
+ * </p>
  * 
  */
 public abstract class AbstractSelector extends Selector {
+    private volatile boolean isOpen = true;
 
-	public SelectionKey register(AbstractSelectableChannel channel, int interestSet, Object attachment) {
-		//FIXME: waiting for patch for JIRA-41
-		return null;
-	}
+    private SelectorProvider provider = null;
 
+    /*
+     * Set of cancelled keys.
+     */
+    private Set<SelectionKey> cancelledKeysSet = new HashSet<SelectionKey>();
+
+    /**
+     * Constructor for this class.
+     * 
+     * @param selectorProvider
+     *            A instance of SelectorProvider
+     */
+    protected AbstractSelector(SelectorProvider selectorProvider) {
+        provider = selectorProvider;
+    }
+
+    /**
+     * Closes this channel.
+     * 
+     * @see java.nio.channels.Selector#close()
+     */
+    public synchronized final void close() throws IOException {
+        if (isOpen) {
+            isOpen = false;
+            implCloseSelector();
+        }
+    }
+
+    /**
+     * Implements the closing of this channel.
+     * 
+     * @throws IOException
+     *             If some I/O exception occurs.
+     */
+    protected abstract void implCloseSelector() throws IOException;
+
+    /**
+     * @see java.nio.channels.Selector#isOpen()
+     */
+    public final boolean isOpen() {
+        return isOpen;
+    }
+
+    /**
+     * Answers the SelectorProvider of this channel.
+     * 
+     * @see java.nio.channels.Selector#provider()
+     */
+    public final SelectorProvider provider() {
+        return provider;
+    }
+
+    /**
+     * Answers the cancelled key set of this channel.
+     * 
+     * @return The cancelled key set.
+     */
+    protected final Set cancelledKeys() {
+        return cancelledKeysSet;
+    }
+
+    /**
+     * Registers a channel to this selector.
+     * 
+     * @param channel
+     *            The channel to be registered.
+     * @param operations
+     *            The interest set.
+     * @param attachment
+     *            The attachment of the key.
+     * @return The key related with the channel and the selecotr.
+     */
+    protected abstract SelectionKey register(AbstractSelectableChannel channel,
+            int operations, Object attachment);
+
+    /**
+     * Deletes the key from channel's key set.
+     * 
+     * @param key
+     *            The key.
+     */
+    protected final void deregister(AbstractSelectionKey key) {
+        ((AbstractSelectableChannel) key.channel()).deregister(key);
+        key.isValid = false;
+    }
+
+    protected final void begin() {
+        // TODO wait for AbstractInterruptibleChannel
+    }
+
+    protected final void end() {
+        // TODO wait for AbstractInterruptibleChannel
+    }
+
+    /*
+     * package private method for AbstractSelectionKey.cancel()
+     */
+    void cancel(SelectionKey key) {
+        synchronized (cancelledKeysSet) {
+            cancelledKeysSet.add(key);
+        }
+    }
 }

Added: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AbstractInterruptibleChannelTest.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/AbstractInterruptibleChannelTest.java?rev=411920&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AbstractInterruptibleChannelTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AbstractInterruptibleChannelTest.java Mon Jun  5 14:17:56 2006
@@ -0,0 +1,135 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * 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.harmony.tests.java.nio.channels.spi;
+
+import java.io.IOException;
+import java.nio.channels.AsynchronousCloseException;
+import java.nio.channels.spi.AbstractInterruptibleChannel;
+
+import junit.framework.TestCase;
+
+public class AbstractInterruptibleChannelTest extends TestCase {
+    
+    /**
+     * @tests AbstractInterruptibleChannel#close()
+     */
+    public void test_close() throws IOException {
+        MockInterruptibleChannel testMiChannel = new MockInterruptibleChannel();
+        assertTrue(testMiChannel.isOpen());
+        testMiChannel.isImplCloseCalled = false;
+        testMiChannel.close();
+        assertTrue(testMiChannel.isImplCloseCalled);
+        assertFalse(testMiChannel.isOpen());
+    }
+
+    /**
+     * @tests AbstractInterruptibleChannel#begin/end()
+     */
+    public void test_begin_end() throws IOException {
+        boolean complete = false;
+        MockInterruptibleChannel testChannel = new MockInterruptibleChannel();
+        try {
+            testChannel.superBegin();
+            complete = true;
+        } finally {
+            testChannel.superEnd(complete);
+        }
+
+        try {
+            testChannel.superBegin();
+            complete = false;
+        } finally {
+            testChannel.superEnd(complete);
+        }
+        
+        try {
+            testChannel.superBegin();
+            complete = true;
+        } finally {
+            testChannel.superEnd(complete);
+        }
+        testChannel.superEnd(complete);
+        
+        testChannel.superBegin();
+        try {
+            testChannel.superBegin();
+            complete = true;
+        } finally {
+            testChannel.superEnd(complete);
+        }
+        assertTrue(testChannel.isOpen());
+        testChannel.close();
+    }
+
+    /**
+     * @tests AbstractInterruptibleChannel#close/begin/end()
+     */
+    public void test_close_begin_end() throws IOException {
+        boolean complete = false;
+        MockInterruptibleChannel testChannel = new MockInterruptibleChannel();
+        assertTrue(testChannel.isOpen());
+        try {
+            testChannel.superBegin();
+            complete = true;
+        } finally {
+            testChannel.superEnd(complete);
+        }
+        assertTrue(testChannel.isOpen());
+        testChannel.close();
+        try {
+            testChannel.superBegin();
+            complete = false;
+        } finally {
+            try {
+                testChannel.superEnd(complete);
+                fail("should throw AsynchronousCloseException");
+            } catch (AsynchronousCloseException e) {
+                // expected
+            }
+        }
+        assertFalse(testChannel.isOpen());
+        try {
+            testChannel.superBegin();
+            complete = true;
+        } finally {
+            testChannel.superEnd(complete);
+        }
+        assertFalse(testChannel.isOpen());
+    }
+    
+    private class MockInterruptibleChannel extends AbstractInterruptibleChannel {
+
+        private boolean isImplCloseCalled = false;
+        
+        public MockInterruptibleChannel() {
+            super();
+        }
+
+        protected void implCloseChannel() throws IOException {
+        	isImplCloseCalled = true; 
+        }
+
+        // call super.begin() for test
+        void superBegin() {
+            super.begin();
+        }
+        
+        // call super.end() for test
+        void superEnd(boolean completed) throws AsynchronousCloseException {
+            super.end(completed);
+        }
+    }
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AbstractInterruptibleChannelTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 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=411920&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AbstractSelectableChannelTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AbstractSelectableChannelTest.java Mon Jun  5 14:17:56 2006
@@ -0,0 +1,122 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * 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.harmony.tests.java.nio.channels.spi;
+
+import java.io.IOException;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.spi.AbstractSelectableChannel;
+import java.nio.channels.spi.SelectorProvider;
+
+import junit.framework.TestCase;
+
+public class AbstractSelectableChannelTest extends TestCase {
+
+    private MockSelectableChannel testChannel;
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        testChannel = new MockSelectableChannel(SelectorProvider.provider());
+    }
+
+    protected void tearDown() throws Exception {
+        if (testChannel.isOpen()) {
+            testChannel.close();
+        }
+    }
+
+    /**
+     * @tests AbstractSelectableChannel#implCloseChannel()
+     */
+    public void test_implClose() throws IOException {
+        testChannel.isImplCloseSelectableChannelCalled = false;
+        testChannel.implCloseSelectableChannelCount = 0;
+        testChannel.close();
+        assertFalse(testChannel.isOpen());
+        assertTrue(testChannel.isImplCloseSelectableChannelCalled);
+        assertEquals(1, testChannel.implCloseSelectableChannelCount);
+
+        testChannel = new MockSelectableChannel(SelectorProvider.provider());
+        testChannel.isImplCloseSelectableChannelCalled = false;
+        testChannel.implCloseSelectableChannelCount = 0;
+        // close twice.
+        // make sure implCloseSelectableChannelCount is called only once.
+        testChannel.close();
+        testChannel.close();
+        assertFalse(testChannel.isOpen());
+        assertTrue(testChannel.isImplCloseSelectableChannelCalled);
+        assertEquals(1, testChannel.implCloseSelectableChannelCount);
+    }
+
+    /**
+     * @tests AbstractSelectableChannel#provider()
+     */
+    public void test_provider() {
+        SelectorProvider provider = testChannel.provider();
+        assertSame(SelectorProvider.provider(), provider);
+        testChannel = new MockSelectableChannel(null);
+        provider = testChannel.provider();
+        assertNull(provider);
+    }
+
+    /**
+     * @tests AbstractSelectableChannel#isBlocking()
+     */
+    public void test_isBlocking() throws IOException {
+        assertTrue(testChannel.isBlocking());
+        testChannel.configureBlocking(false);
+        assertFalse(testChannel.isBlocking());
+        testChannel.configureBlocking(true);
+        assertTrue(testChannel.isBlocking());
+    }
+
+    /**
+     * 
+     * @tests AbstractSelectableChannel#blockingLock()
+     */
+    public void test_blockingLock() {
+        Object gotObj = testChannel.blockingLock();
+        assertNotNull(gotObj);
+    }
+
+    private class MockSelectableChannel extends AbstractSelectableChannel {
+
+        private boolean isImplCloseSelectableChannelCalled = false;
+
+        private int implCloseSelectableChannelCount = 0;
+        
+        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
+        }
+
+        public int validOps() {
+            return SelectionKey.OP_ACCEPT;
+        }
+
+    }
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AbstractSelectableChannelTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AbstractSelectionKeyTest.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/AbstractSelectionKeyTest.java?rev=411920&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AbstractSelectionKeyTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AbstractSelectionKeyTest.java Mon Jun  5 14:17:56 2006
@@ -0,0 +1,75 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * 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.harmony.tests.java.nio.channels.spi;
+
+import java.nio.channels.SelectableChannel;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.Selector;
+import java.nio.channels.spi.AbstractSelectionKey;
+
+import junit.framework.TestCase;
+
+public class AbstractSelectionKeyTest extends TestCase {
+
+    /**
+     * @tests AbstractSelectionKey#isValid() without selector
+     */
+    public void test_isValid() throws Exception {
+        MockSelectionKey testKey = new MockSelectionKey();
+        assertTrue(testKey.isValid());
+    }
+
+    /**
+     * @tests AbstractSelectionKey#cancel
+     */
+    public void test_cancel() throws Exception {
+        MockSelectionKey testKey = new MockSelectionKey();
+        try {
+            testKey.cancel();
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected: no selector available
+        }
+        assertFalse(testKey.isValid());
+    }
+
+    private class MockSelectionKey extends AbstractSelectionKey {
+
+        MockSelectionKey() {
+            super();
+        }
+
+        public SelectableChannel channel() {
+            return null;
+        }
+
+        public Selector selector() {
+            return null;
+        }
+
+        public int interestOps() {
+            return 0;
+        }
+
+        public SelectionKey interestOps(int arg0) {
+            return null;
+        }
+
+        public int readyOps() {
+            return 0;
+        }
+    }
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AbstractSelectionKeyTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AbstractSelectorTest.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/AbstractSelectorTest.java?rev=411920&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AbstractSelectorTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AbstractSelectorTest.java Mon Jun  5 14:17:56 2006
@@ -0,0 +1,92 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * 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.harmony.tests.java.nio.channels.spi;
+
+import java.io.IOException;
+import java.nio.channels.Selector;
+import java.nio.channels.spi.SelectorProvider;
+
+import junit.framework.TestCase;
+
+public class AbstractSelectorTest extends TestCase {
+
+    /**
+     * @tests AbstractSelector#provider()
+     */
+    public void test_provider() throws IOException {
+        Selector mockSelector = new MockAbstractSelector(SelectorProvider
+                .provider());
+        assertTrue(mockSelector.isOpen());
+        assertSame(SelectorProvider.provider(), mockSelector.provider());
+        mockSelector = new MockAbstractSelector(null);
+        assertNull(mockSelector.provider());
+    }
+
+    /**
+     * @tests AbstractSelector#close()
+     */
+    public void test_close() throws IOException {
+        MockAbstractSelector mockSelector = new MockAbstractSelector(
+                SelectorProvider.provider());
+        mockSelector.close();
+        assertTrue(mockSelector.isImplCloseSelectorCalled);
+    }
+
+    /**
+     * 
+     * @tests AbstractSelector#begin/end()
+     */
+    public void test_begin_end() throws IOException {
+        MockAbstractSelector mockSelector = new MockAbstractSelector(
+                SelectorProvider.provider());     
+        try {
+            mockSelector.superBegin();
+        } finally {
+            mockSelector.superEnd();
+        }
+        
+        mockSelector = new MockAbstractSelector(SelectorProvider.provider());
+        try {
+            mockSelector.superBegin();
+            mockSelector.close();
+        } finally {
+            mockSelector.superEnd();
+        }
+       
+        try {
+            // begin twice
+            mockSelector.superBegin();
+            mockSelector.superBegin();
+        } finally {
+            mockSelector.superEnd();
+        }
+        
+        try {
+            mockSelector.superBegin();
+        } finally {
+            // end twice
+            mockSelector.superEnd();
+            mockSelector.superEnd();
+        }
+
+        mockSelector.close();
+        try {
+            mockSelector.superBegin();
+        } finally {
+            mockSelector.superEnd();
+        }
+    }
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AbstractSelectorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AllTests.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/AllTests.java?rev=411920&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AllTests.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AllTests.java Mon Jun  5 14:17:56 2006
@@ -0,0 +1,36 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * 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.harmony.tests.java.nio.channels.spi;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+public class AllTests {
+
+    public static Test suite() {
+        TestSuite suite = new TestSuite(
+                "Test for tests.api.java.nio.channels.spi");
+        //$JUnit-BEGIN$
+        suite.addTestSuite(AbstractInterruptibleChannelTest.class);
+        suite.addTestSuite(AbstractSelectorTest.class);
+        suite.addTestSuite(AbstractSelectableChannelTest.class);
+        suite.addTestSuite(SelectorProviderTest.class);
+        suite.addTestSuite(AbstractSelectionKeyTest.class);
+        //$JUnit-END$
+        return suite;
+    }
+
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/AllTests.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/MockAbstractSelector.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/MockAbstractSelector.java?rev=411920&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/MockAbstractSelector.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/MockAbstractSelector.java Mon Jun  5 14:17:56 2006
@@ -0,0 +1,88 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * 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.harmony.tests.java.nio.channels.spi;
+
+import java.io.IOException;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.Selector;
+import java.nio.channels.spi.AbstractSelectableChannel;
+import java.nio.channels.spi.AbstractSelectionKey;
+import java.nio.channels.spi.AbstractSelector;
+import java.nio.channels.spi.SelectorProvider;
+import java.util.Set;
+
+public class MockAbstractSelector extends AbstractSelector {
+
+    public boolean isImplCloseSelectorCalled = false;
+    
+	public MockAbstractSelector(SelectorProvider arg0) {
+		super(arg0);
+	}
+
+	public static MockAbstractSelector openSelector() {
+		return new MockAbstractSelector(SelectorProvider.provider());
+	}
+
+	public Set getCancelledKeys() {
+		return super.cancelledKeys();
+	}
+
+	protected void implCloseSelector() throws IOException {
+        isImplCloseSelectorCalled = true;
+	}
+
+	protected SelectionKey register(AbstractSelectableChannel arg0, int arg1,
+			Object arg2) {
+		return null;
+	}
+
+	public void superBegin() {
+		super.begin();
+	}
+
+	public void superEnd() {
+		super.end();
+	}
+
+	protected void mockDeregister(AbstractSelectionKey key) {
+		super.deregister(key);
+	}
+
+	public Set<SelectionKey> keys() {
+		return null;
+	}
+
+	public Set<SelectionKey> selectedKeys() {
+		return null;
+	}
+
+	public int selectNow() throws IOException {
+		return 0;
+	}
+
+	public int select(long arg0) throws IOException {
+		return 0;
+	}
+
+	public int select() throws IOException {
+		return 0;
+	}
+
+	public Selector wakeup() {
+		return null;
+	}
+
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/MockAbstractSelector.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/SelectorProviderTest.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/SelectorProviderTest.java?rev=411920&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/SelectorProviderTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/SelectorProviderTest.java Mon Jun  5 14:17:56 2006
@@ -0,0 +1,114 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * 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.harmony.tests.java.nio.channels.spi;
+
+import java.io.IOException;
+import java.nio.channels.DatagramChannel;
+import java.nio.channels.Pipe;
+import java.nio.channels.ServerSocketChannel;
+import java.nio.channels.SocketChannel;
+import java.nio.channels.spi.AbstractSelector;
+import java.nio.channels.spi.SelectorProvider;
+import java.security.Permission;
+
+import junit.framework.TestCase;
+
+public class SelectorProviderTest extends TestCase {
+
+    /**
+     * @tests SelectorProvider#provider() using security manager
+     */
+    public void test_provider_security() {        
+        SecurityManager originalSecuirtyManager = System.getSecurityManager();
+        System.setSecurityManager(new MockSelectorProviderSecurityManager());
+        try {
+            new MockSelectorProvider();
+            fail("should throw SecurityException");
+        } catch (SecurityException e) {
+            // expected
+        } finally {
+            System.setSecurityManager(originalSecuirtyManager);
+        }
+    }
+
+    /**
+     * @tests SelectorProvider#provider() using security manager
+     */
+    public void test_provider_security_twice() {
+        SelectorProvider.provider();
+        SecurityManager originalSecuirtyManager = System.getSecurityManager();
+        System.setSecurityManager(new MockSelectorProviderSecurityManager());
+        try {
+            // should not throw SecurityException since it has been initialized
+            // in the begining of this method.
+            SelectorProvider testProvider = SelectorProvider.provider();
+            assertNotNull(testProvider);
+        } finally {
+            System.setSecurityManager(originalSecuirtyManager);
+        }
+    }
+
+    private static class MockSelectorProviderSecurityManager extends
+            SecurityManager {
+
+        public MockSelectorProviderSecurityManager() {
+            super();
+        }
+
+        public void checkPermission(Permission perm) {
+            if (perm instanceof RuntimePermission) {
+                if ("selectorProvider".equals(perm.getName())) {
+                    throw new SecurityException();
+                }
+            }
+        }
+
+        public void checkPermission(Permission perm, Object context) {
+            if (perm instanceof RuntimePermission) {
+                if ("selectorProvider".equals(perm.getName())) {
+                    throw new SecurityException();
+                }
+            }
+        }
+    }
+
+    private class MockSelectorProvider extends SelectorProvider {
+
+        public MockSelectorProvider() {
+            super();
+        }
+
+        public DatagramChannel openDatagramChannel() throws IOException {
+            return null;
+        }
+
+        public Pipe openPipe() throws IOException {
+            return null;
+        }
+
+        public AbstractSelector openSelector() throws IOException {
+            return MockAbstractSelector.openSelector();
+        }
+
+        public ServerSocketChannel openServerSocketChannel() throws IOException {
+            return null;
+        }
+
+        public SocketChannel openSocketChannel() throws IOException {
+            return null;
+        }
+    }
+}
\ No newline at end of file

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/channels/spi/SelectorProviderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native