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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.util.io;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import javax.annotation.Nonnull;
  26. import org.junit.Before;
  27. import org.junit.Test;
  28. import org.junit.runner.RunWith;
  29. import org.mockito.Mock;
  30. import org.mockito.runners.MockitoJUnitRunner;
  31. import static junit.framework.TestCase.assertEquals;
  32. import static junit.framework.TestCase.assertTrue;
  33. import static org.mockito.Mockito.never;
  34. import static org.mockito.Mockito.verify;
  35. @RunWith(MockitoJUnitRunner.class)
  36. public class ListenerInputStreamTest {
  37. @Mock private DownloadListener listener;
  38. private ListenerInputStream instance;
  39. @Before
  40. public void setUp() throws Exception {
  41. instance = new ListenerInputStream(new MockedInputStream(), listener, 100);
  42. }
  43. @Test
  44. public void testInit_Indeterminate() throws Exception {
  45. instance = new ListenerInputStream(new MockedInputStream(), listener, -1);
  46. assertEquals(2, instance.read());
  47. verify(listener, never()).downloadProgress(2);
  48. verify(listener).setIndeterminate(true);
  49. }
  50. @Test
  51. public void testInit_Determinate() throws Exception {
  52. assertEquals(2, instance.read());
  53. verify(listener).downloadProgress(2);
  54. verify(listener).setIndeterminate(false);
  55. }
  56. @Test
  57. public void testRead() throws Exception {
  58. assertEquals(2, instance.read());
  59. verify(listener).downloadProgress(2);
  60. }
  61. @Test
  62. public void testRead1() throws Exception {
  63. assertEquals(4, instance.read(new byte[6]));
  64. verify(listener).downloadProgress(4);
  65. }
  66. @Test
  67. public void testRead2() throws Exception {
  68. assertEquals(6, instance.read(new byte[8], 0, 8));
  69. verify(listener).downloadProgress(6);
  70. }
  71. @Test
  72. public void testSkip() throws Exception {
  73. assertEquals(8, instance.skip(4));
  74. verify(listener).downloadProgress(8);
  75. }
  76. @Test
  77. public void testAvailable() throws Exception {
  78. assertEquals(10, instance.available());
  79. }
  80. @Test(expected = IOException.class)
  81. public void testClose() throws Exception {
  82. instance.close();
  83. }
  84. @Test(expected = IOException.class)
  85. public void testMarkAndReset_Unsupported() throws Exception {
  86. instance.mark(0);
  87. instance.reset();
  88. }
  89. @Test
  90. public void testMarkSupported() throws Exception {
  91. assertTrue(instance.markSupported());
  92. }
  93. private static class MockedInputStream extends InputStream {
  94. public MockedInputStream() {
  95. }
  96. @Override
  97. public int read() throws IOException {
  98. return 2;
  99. }
  100. @Override
  101. public int read(@Nonnull final byte[] b) throws IOException {
  102. return 4;
  103. }
  104. @Override
  105. public int read(@Nonnull final byte[] b, final int off, final int len) throws IOException {
  106. if (off == 0 && len == 6) {
  107. return 4;
  108. } else {
  109. return 6;
  110. }
  111. }
  112. @Override
  113. public long skip(final long n) throws IOException {
  114. return 8;
  115. }
  116. @Override
  117. public int available() throws IOException {
  118. return 10;
  119. }
  120. @Override
  121. public void close() throws IOException {
  122. throw new IOException("Test");
  123. }
  124. @Override
  125. public synchronized void mark(final int readlimit) {
  126. }
  127. @Override
  128. public synchronized void reset() throws IOException {
  129. throw new IOException("Test");
  130. }
  131. @Override
  132. public boolean markSupported() {
  133. return true;
  134. }
  135. }
  136. }