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

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