@Test public void testFailed503_noresume_result() throws Exception { DownloadHandler downloadHandler = m_agentContext.getHandler(DownloadHandler.class); DownloadHandle handle = downloadHandler.getHandle(m_503url); assertRetryException(handle.start(null)); assertRetryException(handle.start(null)); }
private void assertSuccessful(Future<DownloadResult> future, String digest) throws Exception { DownloadResult result = future.get(5, TimeUnit.SECONDS); assertTrue(result.isComplete(), "Expected state SUCCESSFUL after succesful completion"); assertNotNull(result.getInputStream(), "Expected non null file after successful completion"); assertEquals(getDigest(result.getInputStream()), digest, "Expected same digest after successful completion"); }
@AfterClass public void tearDownOnceAgain() throws Exception { m_agentContextImpl.stop(); m_webServer.stop(); verifyTestMocks(); }
@BeforeClass public void setUpOnceAgain() throws Exception { File dataLocation = new File("generated"); m_200file = File.createTempFile("download", ".bin", dataLocation); m_200file.deleteOnExit(); int port = 8883; m_200url = new URL("http://localhost:" + port + "/" + m_200file.getName()); m_404url = new URL("http://localhost:" + port + "/error?status=404"); m_503url = new URL("http://localhost:" + port + "/error?status=503&retry=500"); DigestOutputStream dos = new DigestOutputStream(new FileOutputStream(m_200file), MessageDigest.getInstance("MD5")); for (int i = 0; i < 10000; i++) { dos.write(String.valueOf(System.currentTimeMillis()).getBytes()); dos.write(" Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum\n".getBytes()); } dos.close(); m_200digest = new String(dos.getMessageDigest().digest()); m_webServer = new TestWebServer(port, "/", dataLocation.getName()); m_webServer.addServlet(new TestErrorServlet(), "/error"); m_webServer.start(); m_agentContextImpl = mockAgentContext(); m_agentContext = m_agentContextImpl; BundleContext bc = mockBundleContext(); m_agentContextImpl.setHandler(EventsHandler.class, new EventsHandlerImpl(bc)); m_agentContextImpl.setHandler(ConnectionHandler.class, new ConnectionHandlerImpl()); m_agentContextImpl.setHandler(LoggingHandler.class, new LoggingHandlerImpl(bc, Levels.DEBUG)); m_agentContextImpl.setHandler(DownloadHandler.class, new DownloadHandlerImpl(dataLocation)); m_agentContextImpl.start(); replayTestMocks(); }
@Test public void testSuccessful_resume_result() throws Exception { DownloadHandler downloadHandler = m_agentContext.getHandler(DownloadHandler.class); final DownloadHandle handle = downloadHandler.getHandle(m_200url); handle.discard(); Future<DownloadResult> future = handle.start(new DownloadProgressListener() { @Override public void progress(long read) { Thread.currentThread().interrupt(); } }); assertStopped(future); assertSuccessful(handle.start(null), m_200digest); }
@Test public void testFailed404_noresume_result() throws Exception { DownloadHandler downloadHandler = m_agentContext.getHandler(DownloadHandler.class); DownloadHandle handle = downloadHandler.getHandle(m_404url); Future<DownloadResult> future = handle.start(null); assertIOException(future); }
@Test public void testSuccessful_noresume_result() throws Exception { DownloadHandler downloadHandler = m_agentContext.getHandler(DownloadHandler.class); DownloadHandle handle = downloadHandler.getHandle(m_200url); handle.discard(); Future<DownloadResult> result = handle.start(null); assertSuccessful(result, m_200digest); }
@Test public void testSuccessfulResumeAfterCompleteDownload() throws Exception { DownloadHandler downloadHandler = m_agentContext.getHandler(DownloadHandler.class); final DownloadHandle handle = downloadHandler.getHandle(m_200url); handle.discard(); Future<DownloadResult> future = handle.start(null); DownloadResult result = future.get(); assertNotNull(result); assertTrue(result.isComplete()); assertEquals(m_200digest, getDigest(result.getInputStream())); Future<DownloadResult> future2 = handle.start(null); DownloadResult result2 = future2.get(); assertNotNull(result2); assertTrue(result2.isComplete()); assertEquals(m_200digest, getDigest(result2.getInputStream())); }