Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

DownloaderTest.java 5.1KB

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