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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.Matchers.anyBoolean;
  45. import static org.mockito.Matchers.anyInt;
  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. when(mockedConnection.getLastModified()).thenReturn(10L, (Long)11L);
  65. }
  66. @Test
  67. public void testGetPage() throws IOException {
  68. new TestableDownloader().getPage("rar");
  69. verify(mockedConnection, never()).setRequestProperty("Content-Type",
  70. "application/x-www-form-urlencoded");
  71. assertEquals(0, os.size());
  72. }
  73. @Test
  74. public void testGetPageString() throws IOException {
  75. final String postData = "sdfgsdfgsdfg";
  76. new TestableDownloader().getPage("rar", postData);
  77. verify(mockedConnection).setRequestProperty("Content-Type",
  78. "application/x-www-form-urlencoded");
  79. assertEquals(postData, os.toString());
  80. }
  81. @Test
  82. public void testGetPageMap() throws IOException {
  83. final Map<String, String> postData = Maps.newHashMap();
  84. postData.put("key1", "value1");
  85. postData.put("key2", "value2");
  86. new TestableDownloader().getPage("rar", postData);
  87. verify(mockedConnection).setRequestProperty("Content-Type",
  88. "application/x-www-form-urlencoded");
  89. final String postDataString1 = "key1=value1&key2=value2";
  90. final String postDataString2 = "key2=value2&key1=value1";
  91. assertTrue(postDataString1.equals(os.toString()) || postDataString2.equals(os.toString()));
  92. }
  93. @Test
  94. public void testGetPageMapEncoding() throws IOException {
  95. final Map<String, String> postData = Maps.newHashMap();
  96. postData.put("ke&y1", "value1");
  97. postData.put("key2", "val&ue2");
  98. new TestableDownloader().getPage("rar", postData);
  99. verify(mockedConnection).setRequestProperty("Content-Type",
  100. "application/x-www-form-urlencoded");
  101. final String postDataString1 = "ke%26y1=value1&key2=val%26ue2";
  102. final String postDataString2 = "key2=val%26ue2&ke%26y1=value1";
  103. assertTrue(postDataString1.equals(os.toString()) || postDataString2.equals(os.toString()));
  104. }
  105. @Test
  106. public void testDownloadPage() throws IOException {
  107. final Path file = fakeFS.getPath("test.txt");
  108. assertFalse(Files.exists(file));
  109. new TestableDownloader().downloadPage("rar", file);
  110. assertTrue(Files.exists(file));
  111. assertEquals(Lists.newArrayList("OMG IM A FAKE DOWNLOAD"), Files.readAllLines(file,
  112. Charset.forName("UTF-8")));
  113. }
  114. @Test
  115. public void testListener() throws IOException {
  116. final Path file = fakeFS.getPath("test.txt");
  117. assertFalse(Files.exists(file));
  118. new TestableDownloader().downloadPage("rar", file, listener);
  119. assertTrue(Files.exists(file));
  120. assertEquals(Lists.newArrayList("OMG IM A FAKE DOWNLOAD"), Files.readAllLines(file,
  121. Charset.forName("UTF-8")));
  122. verify(listener).setIndeterminate(anyBoolean());
  123. verify(listener, atLeastOnce()).downloadProgress(anyInt());
  124. }
  125. private class TestableDownloader extends Downloader {
  126. @Override
  127. protected URLConnection getURLConnection(final String url) throws IOException {
  128. return mockedConnection;
  129. }
  130. }
  131. }