1/* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package org.apache.harmony.tests.java.nio.channels.spi; 18 19import java.io.IOException; 20import java.nio.channels.AsynchronousCloseException; 21import java.nio.channels.spi.AbstractInterruptibleChannel; 22 23import junit.framework.TestCase; 24 25public class AbstractInterruptibleChannelTest extends TestCase { 26 27 /** 28 * @tests AbstractInterruptibleChannel#close() 29 */ 30 public void test_close() throws IOException { 31 MockInterruptibleChannel testMiChannel = new MockInterruptibleChannel(); 32 assertTrue(testMiChannel.isOpen()); 33 testMiChannel.isImplCloseCalled = false; 34 testMiChannel.close(); 35 assertTrue(testMiChannel.isImplCloseCalled); 36 assertFalse(testMiChannel.isOpen()); 37 } 38 39 /** 40 * @tests AbstractInterruptibleChannel#begin/end() 41 */ 42 public void test_begin_end() throws IOException { 43 boolean complete = false; 44 MockInterruptibleChannel testChannel = new MockInterruptibleChannel(); 45 try { 46 testChannel.superBegin(); 47 complete = true; 48 } finally { 49 testChannel.superEnd(complete); 50 } 51 52 try { 53 testChannel.superBegin(); 54 complete = false; 55 } finally { 56 testChannel.superEnd(complete); 57 } 58 59 try { 60 testChannel.superBegin(); 61 complete = true; 62 } finally { 63 testChannel.superEnd(complete); 64 } 65 66 testChannel.superBegin(); 67 try { 68 testChannel.superBegin(); 69 complete = true; 70 } finally { 71 testChannel.superEnd(complete); 72 } 73 assertTrue(testChannel.isOpen()); 74 testChannel.close(); 75 } 76 77 /** 78 * @tests AbstractInterruptibleChannel#close/begin/end() 79 */ 80 public void test_close_begin_end() throws IOException { 81 boolean complete = false; 82 MockInterruptibleChannel testChannel = new MockInterruptibleChannel(); 83 assertTrue(testChannel.isOpen()); 84 try { 85 testChannel.superBegin(); 86 complete = true; 87 } finally { 88 testChannel.superEnd(complete); 89 } 90 assertTrue(testChannel.isOpen()); 91 testChannel.close(); 92 try { 93 testChannel.superBegin(); 94 complete = false; 95 } finally { 96 try { 97 testChannel.superEnd(complete); 98 fail("should throw AsynchronousCloseException"); 99 } catch (AsynchronousCloseException e) { 100 // expected 101 } 102 } 103 assertFalse(testChannel.isOpen()); 104 try { 105 testChannel.superBegin(); 106 complete = true; 107 } finally { 108 testChannel.superEnd(complete); 109 } 110 assertFalse(testChannel.isOpen()); 111 } 112 113 private class MockInterruptibleChannel extends AbstractInterruptibleChannel { 114 115 private boolean isImplCloseCalled = false; 116 117 public MockInterruptibleChannel() { 118 super(); 119 } 120 121 protected void implCloseChannel() throws IOException { 122 isImplCloseCalled = true; 123 } 124 125 // call super.begin() for test 126 void superBegin() { 127 super.begin(); 128 } 129 130 // call super.end() for test 131 void superEnd(boolean completed) throws AsynchronousCloseException { 132 super.end(completed); 133 } 134 } 135} 136