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.

ListenerInputStreamTest.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.IOException;
  19. import java.io.InputStream;
  20. import javax.annotation.Nonnull;
  21. import org.junit.Before;
  22. import org.junit.Test;
  23. import org.junit.runner.RunWith;
  24. import org.mockito.Mock;
  25. import org.mockito.runners.MockitoJUnitRunner;
  26. import static junit.framework.TestCase.assertEquals;
  27. import static junit.framework.TestCase.assertTrue;
  28. import static org.mockito.Mockito.never;
  29. import static org.mockito.Mockito.verify;
  30. @RunWith(MockitoJUnitRunner.class)
  31. public class ListenerInputStreamTest {
  32. @Mock private DownloadListener listener;
  33. private ListenerInputStream instance;
  34. @Before
  35. public void setUp() throws Exception {
  36. instance = new ListenerInputStream(new MockedInputStream(), listener, 100);
  37. }
  38. @Test
  39. public void testInit_Indeterminate() throws Exception {
  40. instance = new ListenerInputStream(new MockedInputStream(), listener, -1);
  41. assertEquals(2, instance.read());
  42. verify(listener, never()).downloadProgress(2);
  43. verify(listener).setIndeterminate(true);
  44. }
  45. @Test
  46. public void testInit_Determinate() throws Exception {
  47. assertEquals(2, instance.read());
  48. verify(listener).downloadProgress(2);
  49. verify(listener).setIndeterminate(false);
  50. }
  51. @Test
  52. public void testRead() throws Exception {
  53. assertEquals(2, instance.read());
  54. verify(listener).downloadProgress(2);
  55. }
  56. @Test
  57. public void testRead1() throws Exception {
  58. assertEquals(4, instance.read(new byte[6]));
  59. verify(listener).downloadProgress(4);
  60. }
  61. @Test
  62. public void testRead2() throws Exception {
  63. assertEquals(6, instance.read(new byte[8], 0, 8));
  64. verify(listener).downloadProgress(6);
  65. }
  66. @Test
  67. public void testSkip() throws Exception {
  68. assertEquals(8, instance.skip(4));
  69. verify(listener).downloadProgress(8);
  70. }
  71. @Test
  72. public void testAvailable() throws Exception {
  73. assertEquals(10, instance.available());
  74. }
  75. @Test(expected = IOException.class)
  76. public void testClose() throws Exception {
  77. instance.close();
  78. }
  79. @Test(expected = IOException.class)
  80. public void testMarkAndReset_Unsupported() throws Exception {
  81. instance.mark(0);
  82. instance.reset();
  83. }
  84. @Test
  85. public void testMarkSupported() throws Exception {
  86. assertTrue(instance.markSupported());
  87. }
  88. private static class MockedInputStream extends InputStream {
  89. public MockedInputStream() {
  90. }
  91. @Override
  92. public int read() throws IOException {
  93. return 2;
  94. }
  95. @Override
  96. public int read(@Nonnull final byte[] b) throws IOException {
  97. return 4;
  98. }
  99. @Override
  100. public int read(@Nonnull final byte[] b, final int off, final int len) throws IOException {
  101. if (off == 0 && len == 6) {
  102. return 4;
  103. } else {
  104. return 6;
  105. }
  106. }
  107. @Override
  108. public long skip(final long n) throws IOException {
  109. return 8;
  110. }
  111. @Override
  112. public int available() throws IOException {
  113. return 10;
  114. }
  115. @Override
  116. public void close() throws IOException {
  117. throw new IOException("Test");
  118. }
  119. @Override
  120. public synchronized void mark(final int readlimit) {
  121. }
  122. @Override
  123. public synchronized void reset() throws IOException {
  124. throw new IOException("Test");
  125. }
  126. @Override
  127. public boolean markSupported() {
  128. return true;
  129. }
  130. }
  131. }