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 package org.apache.harmony.tests.java.nio.channels; 17 18 import java.io.File; 19 import java.io.FileInputStream; 20 import java.io.FileNotFoundException; 21 import java.io.FileOutputStream; 22 import java.io.IOException; 23 import java.io.InputStream; 24 import java.io.OutputStream; 25 import java.io.Reader; 26 import java.io.Writer; 27 import java.nio.ByteBuffer; 28 import java.nio.CharBuffer; 29 import java.nio.channels.Channels; 30 import java.nio.channels.ClosedChannelException; 31 import java.nio.channels.IllegalBlockingModeException; 32 import java.nio.channels.ReadableByteChannel; 33 import java.nio.channels.ServerSocketChannel; 34 import java.nio.channels.SocketChannel; 35 import java.nio.channels.WritableByteChannel; 36 import java.nio.charset.Charset; 37 import java.nio.charset.UnsupportedCharsetException; 38 import junit.framework.TestCase; 39 40 /** 41 * Note: the test case uses a temp text file named "test" which contains 31 42 * characters : "P@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]" 43 * 44 */ 45 46 public class ChannelsTest extends TestCase { 47 private static final String CODE_SET = "GB2312"; //$NON-NLS-1$ 48 49 private static final String BAD_CODE_SET = "GB2313"; //$NON-NLS-1$ 50 51 private FileInputStream fins; 52 53 private FileOutputStream fouts; 54 55 private final int writebufSize = 60; 56 57 private final int testNum = 10; 58 59 private final int fileSize = 31;// the file size 60 61 private File tmpFile; 62 setUp()63 protected void setUp() throws Exception { 64 super.setUp(); 65 // Make the test file same in every test 66 tmpFile = File.createTempFile("test","tmp"); 67 tmpFile.deleteOnExit(); 68 this.writeFileSame(); 69 } 70 tearDown()71 protected void tearDown() throws Exception { 72 if (null != this.fins) { 73 this.fins.close(); 74 this.fins = null; 75 } 76 if (null != this.fouts) { 77 this.fouts.close(); 78 this.fouts = null; 79 } 80 81 tmpFile.delete(); 82 super.tearDown(); 83 84 } 85 writeFileSame()86 private void writeFileSame() throws IOException { 87 this.fouts = new FileOutputStream(tmpFile); 88 byte[] bit = new byte[1]; 89 bit[0] = 80; 90 this.fouts.write(bit); 91 this.fouts.flush(); 92 String writebuf = ""; //$NON-NLS-1$ 93 for (int val = 0; val < this.writebufSize / 2; val++) { 94 writebuf = writebuf + ((char) (val + 64)); 95 } 96 this.fouts.write(writebuf.getBytes()); 97 } 98 99 /* 100 * This private method is to assert if the file size is the same as the 101 * compare Number in the test 102 */ assertFileSizeSame(File fileToTest, int compareNumber)103 private void assertFileSizeSame(File fileToTest, int compareNumber) 104 throws IOException { 105 FileInputStream file = new FileInputStream(fileToTest); 106 assertEquals(file.available(), compareNumber); 107 file.close(); 108 } 109 110 // test if new Channel to input is null testNewChannelInputStream_InputNull()111 public void testNewChannelInputStream_InputNull() throws IOException { 112 ByteBuffer byteBuf = ByteBuffer.allocate(this.testNum); 113 this.fins = null; 114 int readres = this.testNum; 115 try { 116 ReadableByteChannel rbChannel = Channels.newChannel(this.fins); 117 assertNotNull(rbChannel); 118 readres = rbChannel.read(byteBuf); 119 fail(); 120 } catch (NullPointerException expected) { 121 } 122 assertEquals(this.testNum, readres); 123 } 124 125 // test if buffer to read is null testNewChannelInputStream_BufferNull()126 public void testNewChannelInputStream_BufferNull() throws IOException { 127 ByteBuffer byteBuf = ByteBuffer.allocate(this.testNum); 128 int readres = this.testNum; 129 this.fins = new FileInputStream(tmpFile); 130 ReadableByteChannel rbChannel = Channels.newChannel(this.fins); 131 assertNotNull(rbChannel); 132 try { 133 readres = rbChannel.read(null); 134 fail(); 135 } catch (NullPointerException e) { 136 // correct 137 } 138 assertEquals(this.testNum, readres); 139 readres = 0; 140 try { 141 readres = rbChannel.read(byteBuf); 142 } catch (NullPointerException e) { 143 fail(); 144 } 145 assertEquals(this.testNum, readres); 146 } 147 148 /* 149 * Test method for 'java.nio.channels.Channels.NewChannel' 150 */ testNewChannelInputStream()151 public void testNewChannelInputStream() throws IOException { 152 int bufSize = 10; 153 int readres = 0; 154 byte[] byteArray = new byte[bufSize]; 155 ByteBuffer byteBuf = ByteBuffer.allocate(bufSize); 156 this.fins = new FileInputStream(tmpFile); 157 readres = this.fins.read(byteArray); 158 159 assertEquals(bufSize, readres); 160 assertTrue(fins.available() > 0); 161 162 ReadableByteChannel rbChannel = Channels.newChannel(this.fins); 163 // fins still reads. 164 assertTrue(fins.available() > 0); 165 readres = this.fins.read(byteArray); 166 assertEquals(bufSize, readres); 167 168 // rbChannel also reads. 169 assertNotNull(rbChannel); 170 readres = rbChannel.read(byteBuf); 171 172 assertEquals(bufSize, readres); 173 InputStream ins = Channels.newInputStream(rbChannel); 174 assertNotNull(ins); 175 // Channels.newChannel has a special case for FileInputStream, which means 176 // they become SeekableByteChannels that can correctly predict the number of 177 // bytes they have left 178 // The file written is 31 bytes in size and we've read 30 bytes already. 179 assertEquals(1, ins.available()); 180 } 181 182 // test if fout to change is null testNewChannelOutputStream_inputNull()183 public void testNewChannelOutputStream_inputNull() throws IOException { 184 int writeres = this.testNum; 185 ByteBuffer writebuf = ByteBuffer.allocate(this.writebufSize); 186 for (int val = 0; val < this.writebufSize / 2; val++) { 187 writebuf.putChar((char) (val + 64)); 188 } 189 this.fouts = null; 190 try { 191 WritableByteChannel rbChannel = Channels.newChannel(this.fouts); 192 writeres = rbChannel.write(writebuf); 193 assertEquals(0, writeres); 194 195 writebuf.flip(); 196 writeres = rbChannel.write(writebuf); 197 fail("Should throw NPE."); 198 } catch (NullPointerException expected) { 199 } 200 } 201 202 // test if write buf is null testNewChannelOutputStream_BufNull()203 public void testNewChannelOutputStream_BufNull() throws IOException { 204 int writeres = this.testNum; 205 ByteBuffer writebuf = null; 206 try { 207 this.fouts = new FileOutputStream(tmpFile); 208 } catch (FileNotFoundException e) { 209 fail(); 210 } 211 212 WritableByteChannel rbChannel = Channels.newChannel(this.fouts); 213 try { 214 writeres = rbChannel.write(writebuf); 215 fail(); 216 } catch (NullPointerException e) { 217 // correct 218 } 219 assertEquals(this.testNum, writeres); 220 } 221 222 /* 223 * Test method for 'java.nio.channels.Channels.NewChannel(OutputStream)' 224 */ testNewChannelOutputStream()225 public void testNewChannelOutputStream() throws IOException { 226 int writeNum = 0; 227 ByteBuffer writebuf = ByteBuffer.allocateDirect(this.writebufSize); 228 for (int val = 0; val < this.writebufSize / 2; val++) { 229 writebuf.putChar((char) (val + 64)); 230 } 231 this.fouts = new FileOutputStream(tmpFile); 232 WritableByteChannel testChannel = this.fouts.getChannel(); 233 WritableByteChannel rbChannel = Channels.newChannel(this.fouts); 234 235 assertTrue(testChannel.isOpen()); 236 assertTrue(rbChannel.isOpen()); 237 238 byte[] bit = new byte[1]; 239 bit[0] = 80; 240 this.fouts.write(bit); 241 this.fouts.flush(); 242 this.fins = new FileInputStream(tmpFile); 243 assertEquals(this.fins.available(), 1); 244 this.fins.close(); 245 246 writeNum = rbChannel.write(writebuf); 247 // write success ,but output null 248 assertEquals(0, writeNum); 249 // close of fouts does not affect on channel 250 this.fouts.close(); 251 writeNum = rbChannel.write(writebuf); 252 assertEquals(0, writeNum); 253 try { 254 writeNum = testChannel.write(writebuf); 255 fail(); 256 } catch (ClosedChannelException e) { 257 // correct 258 } 259 assertEquals(0, writeNum); 260 // close of rbchannel does affect on testchannel(same channel) 261 rbChannel.close(); 262 try { 263 writeNum = testChannel.write(writebuf); 264 fail(); 265 } catch (ClosedChannelException e) { 266 // correct 267 } 268 } 269 testNewInputStreamReadableByteChannel_InputNull()270 public void testNewInputStreamReadableByteChannel_InputNull() 271 throws Exception { 272 byte[] readbuf = new byte[this.testNum]; 273 this.fins = new FileInputStream(tmpFile); 274 ReadableByteChannel readbc = this.fins.getChannel(); 275 assertEquals(this.fileSize, this.fins.available()); 276 assertTrue(readbc.isOpen()); 277 278 try { 279 InputStream testins = Channels.newInputStream((ReadableByteChannel) null); 280 assertNotNull(testins); 281 testins.read(readbuf); 282 fail(); 283 } catch (NullPointerException expected) { 284 } 285 } 286 testNewInputStreamReadableByteChannel()287 public void testNewInputStreamReadableByteChannel() throws Exception { 288 ByteBuffer readbcbuf = ByteBuffer.allocateDirect(this.testNum); 289 byte[] readbuf = new byte[this.testNum]; 290 this.fins = new FileInputStream(tmpFile); 291 ReadableByteChannel readbc = this.fins.getChannel(); 292 assertEquals(this.fileSize, this.fins.available()); 293 assertTrue(readbc.isOpen()); 294 InputStream testins = Channels.newInputStream(readbc); 295 // read in testins and fins use the same pointer 296 testins.read(readbuf); 297 assertEquals(this.fins.available(), this.fileSize - this.testNum); 298 int readNum = readbc.read(readbcbuf); 299 assertEquals(readNum, this.testNum); 300 assertEquals(this.fins.available(), this.fileSize - this.testNum * 2); 301 testins.read(readbuf); 302 assertEquals(this.fins.available(), this.fileSize - this.testNum * 3); 303 // readbc.close() affect testins 304 readbc.close(); 305 assertFalse(readbc.isOpen()); 306 try { 307 testins.read(readbuf); 308 fail(); 309 } catch (ClosedChannelException e) { 310 // correct 311 } 312 } 313 testNewOutputStreamWritableByteChannel_InputNull()314 public void testNewOutputStreamWritableByteChannel_InputNull() 315 throws Exception { 316 byte[] writebuf = new byte[this.testNum]; 317 try { 318 OutputStream testouts = Channels.newOutputStream((WritableByteChannel) null); 319 assertNotNull(testouts); 320 testouts.write(writebuf); 321 fail(); 322 } catch (NullPointerException expected) { 323 } 324 try { 325 WritableByteChannel writebc = Channels.newChannel((OutputStream) null); 326 assertTrue(writebc.isOpen()); 327 OutputStream testoutputS = Channels.newOutputStream(writebc); 328 testoutputS.write(writebuf); 329 fail(); 330 } catch (NullPointerException expected) { 331 } 332 } 333 testNewOutputStreamWritableByteChannel()334 public void testNewOutputStreamWritableByteChannel() throws Exception { 335 byte[] writebuf = new byte[this.testNum]; 336 ByteBuffer writebcbuf = ByteBuffer.allocateDirect(this.testNum); 337 this.fouts = new FileOutputStream(tmpFile); 338 WritableByteChannel writebc = this.fouts.getChannel(); 339 340 assertTrue(writebc.isOpen()); 341 OutputStream testouts = Channels.newOutputStream(writebc); 342 343 // read in testins and fins use the same pointer 344 testouts.write(writebuf); 345 this.assertFileSizeSame(tmpFile, this.testNum); 346 writebc.write(writebcbuf); 347 this.assertFileSizeSame(tmpFile, this.testNum * 2); 348 testouts.write(writebuf); 349 this.assertFileSizeSame(tmpFile, this.testNum * 3); 350 // readbc.close() affect testins 351 writebc.close(); 352 assertFalse(writebc.isOpen()); 353 try { 354 testouts.write(writebuf); 355 fail(); 356 } catch (ClosedChannelException e) { 357 // correct 358 } 359 } 360 testnewReaderCharsetError()361 public void testnewReaderCharsetError() throws Exception { 362 this.fins = new FileInputStream(tmpFile); 363 364 ReadableByteChannel rbChannel = Channels.newChannel(this.fins); 365 try { 366 Channels.newReader(rbChannel, Charset.forName(BAD_CODE_SET) 367 .newDecoder(), //$NON-NLS-1$ 368 -1); 369 fail(); 370 } catch (UnsupportedCharsetException e) { 371 // correct 372 } 373 } 374 testnewWriterCharsetError()375 public void testnewWriterCharsetError() throws Exception { 376 this.fouts = new FileOutputStream(tmpFile); 377 WritableByteChannel wbChannel = Channels.newChannel(this.fouts); 378 try { 379 Channels.newWriter(wbChannel, Charset.forName(BAD_CODE_SET) 380 .newEncoder(), -1); 381 fail(); 382 } catch (UnsupportedCharsetException e) { 383 // correct 384 } 385 } 386 387 /* 388 * Test method for 389 * 'java.nio.channels.Channels.newReader(ReadableByteChannel, String)' 390 */ testNewReaderReadableByteChannelString_InputNull()391 public void testNewReaderReadableByteChannelString_InputNull() 392 throws IOException { 393 int bufSize = this.testNum; 394 int readres = 0; 395 CharBuffer charBuf = CharBuffer.allocate(bufSize); 396 this.fins = new FileInputStream(tmpFile); 397 // channel null 398 Reader testReader; 399 try { 400 testReader = Channels.newReader(null, Charset.forName(CODE_SET).newDecoder(), -1); 401 assertNotNull(testReader); 402 assertFalse(testReader.ready()); 403 readres = testReader.read((CharBuffer) null); 404 fail(); 405 } catch (NullPointerException e) { 406 // correct 407 } 408 assertEquals(0, readres); 409 410 this.fins = null; 411 // channel with null inputs 412 try { 413 ReadableByteChannel rbChannel = Channels.newChannel(this.fins); 414 testReader = Channels.newReader(rbChannel, Charset.forName(CODE_SET).newDecoder(), -1); 415 assertNotNull(testReader); 416 assertFalse(testReader.ready()); 417 readres = testReader.read(charBuf); 418 fail(); 419 } catch (NullPointerException e) { 420 // correct 421 } 422 } 423 424 /* 425 * Test method for 426 * 'java.nio.channels.Channels.newReader(ReadableByteChannel, String)' 427 */ testNewReaderReadableByteChannelString_internalBufferZero()428 public void testNewReaderReadableByteChannelString_internalBufferZero() 429 throws IOException { 430 int bufSize = this.testNum; 431 int readres = 0; 432 CharBuffer charBuf = CharBuffer.allocate(bufSize); 433 this.fins = new FileInputStream(tmpFile); 434 // channel null 435 Reader testReader; 436 try { 437 testReader = Channels.newReader(null, Charset.forName(CODE_SET).newDecoder(), 0); 438 assertNotNull(testReader); 439 assertFalse(testReader.ready()); 440 readres = testReader.read((CharBuffer) null); 441 fail(); 442 } catch (NullPointerException expected) { 443 } 444 assertEquals(0, readres); 445 446 this.fins = null; 447 // channel with null inputs 448 try { 449 ReadableByteChannel rbChannel = Channels.newChannel(this.fins); 450 testReader = Channels.newReader(rbChannel, Charset.forName(CODE_SET).newDecoder(), -1); 451 assertNotNull(testReader); 452 assertFalse(testReader.ready()); 453 readres = testReader.read(charBuf); 454 fail(); 455 } catch (NullPointerException e) { 456 // correct 457 } 458 } 459 460 /* 461 * Test method for 462 * 'java.nio.channels.Channels.newReader(ReadableByteChannel, String)' 463 */ testNewReaderReadableByteChannelString()464 public void testNewReaderReadableByteChannelString() throws IOException { 465 int bufSize = this.testNum; 466 int readres = 0; 467 CharBuffer charBuf = CharBuffer.allocate(bufSize); 468 this.fins = new FileInputStream(tmpFile); 469 ReadableByteChannel rbChannel = Channels.newChannel(this.fins); 470 Reader testReader = Channels.newReader(rbChannel, Charset.forName( 471 CODE_SET).newDecoder(), //$NON-NLS-1$ 472 -1); 473 Reader testReader_s = Channels.newReader(rbChannel, CODE_SET); //$NON-NLS-1$ 474 475 assertEquals(this.fileSize, this.fins.available()); 476 // not ready... 477 assertTrue(testReader.ready()); 478 assertTrue(testReader_s.ready()); 479 // still reads 480 readres = testReader.read(charBuf); 481 assertEquals(bufSize, readres); 482 assertEquals(0, this.fins.available()); 483 484 try { 485 readres = testReader.read((CharBuffer) null); 486 fail(); 487 } catch (NullPointerException e) { 488 // correct 489 } 490 491 readres = testReader_s.read(charBuf); 492 assertEquals(0, readres); 493 assertTrue(testReader.ready()); 494 assertTrue(testReader_s.ready()); 495 } 496 497 /* 498 * Zero-Buffer 499 */ testNewWriterWritableByteChannelString_internalBufZero()500 public void testNewWriterWritableByteChannelString_internalBufZero() 501 throws IOException { 502 503 String writebuf = ""; //$NON-NLS-1$ 504 for (int val = 0; val < this.writebufSize / 2; val++) { 505 writebuf = writebuf + ((char) (val + 64)); 506 } 507 // null channel 508 try { 509 Writer testWriter = Channels.newWriter(null, Charset.forName(CODE_SET).newEncoder(), -1); 510 fail(); 511 } catch (NullPointerException expected) { 512 } 513 514 // channel with null input 515 this.fouts = null; 516 try { 517 WritableByteChannel wbChannel = Channels.newChannel(this.fouts); 518 fail(); 519 } catch (NullPointerException expected) { 520 } 521 } 522 523 /* 524 * this test cannot be passed when buffer set to 0! 525 */ testNewWriterWritableByteChannelString_InputNull()526 public void testNewWriterWritableByteChannelString_InputNull() 527 throws IOException { 528 this.fouts = new FileOutputStream(tmpFile); 529 WritableByteChannel wbChannel = Channels.newChannel(this.fouts); 530 Writer testWriter = Channels.newWriter(wbChannel, Charset.forName( 531 CODE_SET).newEncoder(), //$NON-NLS-1$ 532 1); 533 534 String writebuf = ""; //$NON-NLS-1$ 535 for (int val = 0; val < this.writebufSize / 2; val++) { 536 writebuf = writebuf + ((char) (val + 64)); 537 } 538 // can write to buffer 539 testWriter.write(writebuf); 540 testWriter.flush(); 541 testWriter.close(); 542 543 } 544 545 /* 546 * Test method for 547 * 'java.nio.channels.Channels.newWriter(WritableByteChannel, String)' 548 */ testNewWriterWritableByteChannelString()549 public void testNewWriterWritableByteChannelString() throws IOException { 550 this.fouts = new FileOutputStream(tmpFile); 551 WritableByteChannel wbChannel = Channels.newChannel(this.fouts); 552 Writer testWriter = Channels.newWriter(wbChannel, CODE_SET); //$NON-NLS-1$ 553 Writer testWriter_s = Channels.newWriter(wbChannel, Charset.forName( 554 CODE_SET).newEncoder(), //$NON-NLS-1$ 555 -1); 556 557 String writebuf = ""; //$NON-NLS-1$ 558 for (int val = 0; val < this.writebufSize / 2; val++) { 559 writebuf = writebuf + ((char) (val + 64)); 560 } 561 byte[] bit = new byte[1]; 562 bit[0] = 80; 563 this.fouts.write(bit); 564 this.assertFileSizeSame(tmpFile, 1); 565 566 // writer continues to write after '1',what the fouts write 567 testWriter.write(writebuf); 568 testWriter.flush(); 569 this.assertFileSizeSame(tmpFile, this.writebufSize / 2 + 1); 570 // testwriter_s does not know if testwrite writes 571 testWriter_s.write(writebuf); 572 testWriter.flush(); 573 this.assertFileSizeSame(tmpFile, this.writebufSize / 2 + 1); 574 // testwriter_s even does not know if himself writes? 575 testWriter_s.write(writebuf); 576 testWriter.flush(); 577 this.assertFileSizeSame(tmpFile, this.writebufSize / 2 + 1); 578 579 // close the fouts, no longer writable for testWriter 580 for (int val = 0; val < this.writebufSize; val++) { 581 writebuf = writebuf + ((char) (val + 64)); 582 } 583 this.fouts.close(); 584 testWriter_s.write(writebuf); 585 testWriter.flush(); 586 this.assertFileSizeSame(tmpFile, this.writebufSize / 2 + 1); 587 } 588 589 /** 590 * @tests java.nio.channels.Channels#newReader(ReadableByteChannel channel, 591 * String charsetName) 592 */ test_newReader_LReadableByteChannel_LString()593 public void test_newReader_LReadableByteChannel_LString() 594 throws IOException { 595 ServerSocketChannel ssc = ServerSocketChannel.open(); 596 ssc.socket().bind(null); 597 598 SocketChannel sc = SocketChannel.open(); 599 sc.connect(ssc.socket().getLocalSocketAddress()); 600 sc.configureBlocking(false); 601 assertFalse(sc.isBlocking()); 602 603 ssc.accept().close(); 604 ssc.close(); 605 assertFalse(sc.isBlocking()); 606 607 Reader reader = Channels.newReader(sc, "UTF16"); 608 try { 609 int i = reader.read(); 610 fail("should throw IllegalBlockingModeException"); 611 } catch (IllegalBlockingModeException expected) { 612 } 613 614 try { 615 Channels.newInputStream(sc).read(); 616 fail("should throw IllegalBlockingModeException"); 617 } catch (IllegalBlockingModeException expected) { 618 } 619 620 sc.close(); 621 } 622 } 623