You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StreamUtilsTest.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.util.io;
  18. import java.io.Closeable;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import org.junit.Rule;
  22. import org.junit.Test;
  23. import org.junit.rules.ExpectedException;
  24. import org.junit.runner.RunWith;
  25. import org.mockito.Mock;
  26. import org.mockito.runners.MockitoJUnitRunner;
  27. import static org.junit.Assert.assertEquals;
  28. import static org.mockito.Mockito.doThrow;
  29. import static org.mockito.Mockito.verify;
  30. @SuppressWarnings("resource")
  31. @RunWith(MockitoJUnitRunner.class)
  32. public class StreamUtilsTest {
  33. @Rule public ExpectedException thrown = ExpectedException.none();
  34. @Mock private Closeable closeable;
  35. @Test
  36. public void testCloseWithNullStream() {
  37. // Shouldn't throw an exception
  38. StreamUtils.close(null);
  39. }
  40. @Test
  41. public void testReadStream() throws Exception {
  42. final InputStream inputStream = getClass().getResource("test5.txt").openStream();
  43. StreamUtils.readStream(inputStream);
  44. inputStream.close();
  45. thrown.expect(IOException.class);
  46. thrown.expectMessage("Stream closed");
  47. final int result = inputStream.read();
  48. assertEquals(-1, result);
  49. }
  50. @Test
  51. public void testCloseWithIoException() throws IOException {
  52. doThrow(new IOException("Oops")).when(closeable).close();
  53. StreamUtils.close(closeable);
  54. verify(closeable).close();
  55. }
  56. @Test
  57. public void testCloseNormally() throws IOException {
  58. StreamUtils.close(closeable);
  59. verify(closeable).close();
  60. }
  61. }