PageRenderTime 48ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/core/src/test/php/net/xp_framework/unittest/io/streams/StreamTransferTest.class.php

https://github.com/ghiata/xp-framework
PHP | 177 lines | 93 code | 23 blank | 61 comment | 0 complexity | 8ac9e20f9ad0210a56b4b003da94539b MD5 | raw file
  1. <?php namespace net\xp_framework\unittest\io\streams;
  2. use unittest\TestCase;
  3. use io\streams\StreamTransfer;
  4. use io\streams\MemoryInputStream;
  5. use io\streams\MemoryOutputStream;
  6. /**
  7. * TestCase
  8. *
  9. * @see xp://io.streams.StreamTransfer
  10. */
  11. class StreamTransferTest extends TestCase {
  12. /**
  13. * Returns an uncloseable input stream
  14. *
  15. * @return io.streams.InputStream
  16. */
  17. protected function uncloseableInputStream() {
  18. return newinstance('io.streams.InputStream', array(), '{
  19. public function read($length= 8192) { }
  20. public function available() { }
  21. public function close() { throw new IOException("Close error"); }
  22. }');
  23. }
  24. /**
  25. * Returns a closeable input stream
  26. *
  27. * @return io.streams.InputStream
  28. */
  29. protected function closeableInputStream() {
  30. return newinstance('io.streams.InputStream', array(), '{
  31. public $closed= FALSE;
  32. public function read($length= 8192) { }
  33. public function available() { }
  34. public function close() { $this->closed= TRUE; }
  35. }');
  36. }
  37. /**
  38. * Returns an uncloseable output stream
  39. *
  40. * @return io.streams.OutputStream
  41. */
  42. protected function uncloseableOutputStream() {
  43. return newinstance('io.streams.OutputStream', array(), '{
  44. public function write($data) { }
  45. public function flush() { }
  46. public function close() { throw new IOException("Close error"); }
  47. }');
  48. }
  49. /**
  50. * Returns a closeable output stream
  51. *
  52. * @return io.streams.OutputStream
  53. */
  54. protected function closeableOutputStream() {
  55. return newinstance('io.streams.OutputStream', array(), '{
  56. public $closed= FALSE;
  57. public function write($data) { }
  58. public function flush() { }
  59. public function close() { $this->closed= TRUE; }
  60. }');
  61. }
  62. /**
  63. * Test
  64. *
  65. */
  66. #[@test]
  67. public function dataTransferred() {
  68. $out= new MemoryOutputStream();
  69. $s= new StreamTransfer(new MemoryInputStream('Hello'), $out);
  70. $s->transferAll();
  71. $this->assertEquals('Hello', $out->getBytes());
  72. }
  73. /**
  74. * Test
  75. *
  76. */
  77. #[@test]
  78. public function nothingAvailableAfterTransfer() {
  79. $in= new MemoryInputStream('Hello');
  80. $s= new StreamTransfer($in, new MemoryOutputStream());
  81. $s->transferAll();
  82. $this->assertEquals(0, $in->available());
  83. }
  84. /**
  85. * Test closing a stream twice has no effect.
  86. *
  87. * @see xp://lang.Closeable#close
  88. */
  89. #[@test]
  90. public function closingTwice() {
  91. $s= new StreamTransfer(new MemoryInputStream('Hello'), new MemoryOutputStream());
  92. $s->close();
  93. $s->close();
  94. }
  95. /**
  96. * Test close() method
  97. *
  98. */
  99. #[@test]
  100. public function close() {
  101. $in= $this->closeableInputStream();
  102. $out= $this->closeableOutputStream();
  103. create(new StreamTransfer($in, $out))->close();
  104. $this->assertTrue($in->closed, 'input closed');
  105. $this->assertTrue($out->closed, 'output closed');
  106. }
  107. /**
  108. * Test close() and exceptions
  109. *
  110. */
  111. #[@test]
  112. public function closingOutputFails() {
  113. $in= $this->closeableInputStream();
  114. $out= $this->uncloseableOutputStream();
  115. try {
  116. create(new StreamTransfer($in, $out))->close();
  117. $this->fail('Expected exception not caught', null, 'io.IOException');
  118. } catch (\io\IOException $expected) {
  119. $this->assertEquals('Could not close output stream: Close error', $expected->getMessage());
  120. }
  121. $this->assertTrue($in->closed, 'input closed');
  122. }
  123. /**
  124. * Test close() and exceptions
  125. *
  126. */
  127. #[@test]
  128. public function closingInputFails() {
  129. $in= $this->uncloseableInputStream();
  130. $out= $this->closeableOutputStream();
  131. try {
  132. create(new StreamTransfer($in, $out))->close();
  133. $this->fail('Expected exception not caught', null, 'io.IOException');
  134. } catch (\io\IOException $expected) {
  135. $this->assertEquals('Could not close input stream: Close error', $expected->getMessage());
  136. }
  137. $this->assertTrue($out->closed, 'output closed');
  138. }
  139. /**
  140. * Test close() and exceptions
  141. *
  142. */
  143. #[@test]
  144. public function closingInputAndOutputFails() {
  145. $in= $this->uncloseableInputStream();
  146. $out= $this->uncloseableOutputStream();
  147. try {
  148. create(new StreamTransfer($in, $out))->close();
  149. $this->fail('Expected exception not caught', null, 'io.IOException');
  150. } catch (\io\IOException $expected) {
  151. $this->assertEquals('Could not close input stream: Close error, Could not close output stream: Close error', $expected->getMessage());
  152. }
  153. }
  154. }