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.

DownloaderTest.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2006-2014 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 com.google.common.collect.Lists;
  24. import com.google.common.collect.Maps;
  25. import com.google.common.jimfs.Configuration;
  26. import com.google.common.jimfs.Jimfs;
  27. import java.io.ByteArrayInputStream;
  28. import java.io.IOException;
  29. import java.io.OutputStream;
  30. import java.net.URLConnection;
  31. import java.nio.charset.Charset;
  32. import java.nio.file.FileSystem;
  33. import java.nio.file.Files;
  34. import java.nio.file.Path;
  35. import java.util.Map;
  36. import org.junit.Before;
  37. import org.junit.Test;
  38. import org.junit.runner.RunWith;
  39. import org.mockito.InOrder;
  40. import org.mockito.Mock;
  41. import org.mockito.runners.MockitoJUnitRunner;
  42. import static org.junit.Assert.assertEquals;
  43. import static org.junit.Assert.assertFalse;
  44. import static org.junit.Assert.assertTrue;
  45. import static org.mockito.Matchers.anyBoolean;
  46. import static org.mockito.Matchers.anyByte;
  47. import static org.mockito.Matchers.anyInt;
  48. import static org.mockito.Mockito.atLeastOnce;
  49. import static org.mockito.Mockito.inOrder;
  50. import static org.mockito.Mockito.never;
  51. import static org.mockito.Mockito.verify;
  52. import static org.mockito.Mockito.when;
  53. @RunWith(MockitoJUnitRunner.class)
  54. public class DownloaderTest {
  55. @Mock private URLConnection mockedConnection;
  56. @Mock private OutputStream os;
  57. @Mock private DownloadListener listener;
  58. private FileSystem fakeFS;
  59. @Before
  60. public void setup() throws IOException {
  61. fakeFS = Jimfs.newFileSystem(Configuration.unix());
  62. ByteArrayInputStream is = new ByteArrayInputStream(
  63. "OMG IM A FAKE DOWNLOAD".getBytes("UTF-8"));
  64. when(mockedConnection.getInputStream()).thenReturn(is);
  65. when(mockedConnection.getOutputStream()).thenReturn(os);
  66. when(mockedConnection.getLastModified()).thenReturn((Long)10L, (Long)11L);
  67. }
  68. @Test
  69. public void testGetPage() throws IOException {
  70. final String postData = "sdfgsdfgsdfg";
  71. new TestableDownloader().getPage("rar");
  72. verify(mockedConnection, never()).setRequestProperty("Content-Type",
  73. "application/x-www-form-urlencoded");
  74. verify(os, never()).write(anyByte());
  75. }
  76. @Test
  77. public void testGetPageString() throws IOException {
  78. final String postData = "sdfgsdfgsdfg";
  79. new TestableDownloader().getPage("rar", postData);
  80. verify(mockedConnection).setRequestProperty("Content-Type",
  81. "application/x-www-form-urlencoded");
  82. final InOrder order = inOrder(os);
  83. for (int i=0; i < postData.length(); i++) {
  84. order.verify(os).write((byte) postData.charAt(i));
  85. }
  86. }
  87. @Test
  88. public void testGetPageMap() throws IOException {
  89. final Map<String, String> postData = Maps.newHashMap();
  90. postData.put("key1", "value1");
  91. postData.put("key2", "value2");
  92. final String postDataString = "key1=value1&key2=value2";
  93. new TestableDownloader().getPage("rar", postData);
  94. verify(mockedConnection).setRequestProperty("Content-Type",
  95. "application/x-www-form-urlencoded");
  96. final InOrder order = inOrder(os);
  97. for (int i=0; i < postDataString.length(); i++) {
  98. order.verify(os).write(postDataString.charAt(i));
  99. }
  100. }
  101. @Test
  102. public void testDownloadPage() throws IOException {
  103. final Path file = fakeFS.getPath("test.txt");
  104. assertFalse(Files.exists(file));
  105. new TestableDownloader().downloadPage("rar", file);
  106. assertTrue(Files.exists(file));
  107. assertEquals(Lists.newArrayList("OMG IM A FAKE DOWNLOAD"), Files.readAllLines(file,
  108. Charset.forName("UTF-8")));
  109. }
  110. @Test
  111. public void testListener() throws IOException {
  112. final Path file = fakeFS.getPath("test.txt");
  113. assertFalse(Files.exists(file));
  114. new TestableDownloader().downloadPage("rar", file, listener);
  115. assertTrue(Files.exists(file));
  116. assertEquals(Lists.newArrayList("OMG IM A FAKE DOWNLOAD"), Files.readAllLines(file,
  117. Charset.forName("UTF-8")));
  118. verify(listener).setIndeterminate(anyBoolean());
  119. verify(listener, atLeastOnce()).downloadProgress(anyInt());
  120. }
  121. private class TestableDownloader extends Downloader {
  122. protected URLConnection getURLConnection(final String url) throws IOException {
  123. return mockedConnection;
  124. }
  125. }
  126. }