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.7KB

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