DownloadSignatureUpdateCommand implements DroidCommand { @Override public void execute() throws CommandExecutionException { try { Map<SignatureType, SignatureFileInfo> signatureFileInfos = signatureManager.getLatestSignatureFiles(); if (signatureFileInfos.isEmpty()) { printWriter.println(I18N.getResource(I18N.CHECK_SIGNATURE_UPDATE_UNAVAILABLE)); } else { for (SignatureFileInfo sigFileInfo : signatureFileInfos.values()) { signatureManager.downloadLatest(sigFileInfo.getType()); printWriter.println(I18N.getResource(I18N.DOWNLOAD_SIGNATURE_UPDATE_SUCCESS, sigFileInfo.getVersion())); } } } catch (SignatureManagerException e) { throw new CommandExecutionException( I18N.getResource(I18N.DOWNLOAD_SIGNATURE_UPDATE_ERROR, e.getCause().getMessage(), e.getCauseType(), e.getCauseMessage()), e.getCause()); } }  @Override void execute(); void setPrintWriter(PrintWriter printWriter); void setSignatureManager(SignatureManager signatureManager);  }
@Test public void testDownloadSignatureUpdate() throws Exception { SignatureFileInfo sigFileInfo = new SignatureFileInfo(69, false, SignatureType.BINARY); when(signatureManager.downloadLatest(SignatureType.BINARY)).thenReturn(sigFileInfo); when(signatureManager.downloadLatest(SignatureType.CONTAINER)).thenReturn(sigFileInfo); command.execute(); verify(printWriter, times(2)).println("Signature update version 69 has been downloaded"); }
@Test public void testDownloadSignatureUpdateWhenConnectionFails() throws SignatureManagerException { Throwable cause = new UnknownHostException("proxy"); SignatureManagerException updateException = new SignatureManagerException( new RuntimeException("Failed", cause)); when(signatureManager.downloadLatest(SignatureType.BINARY)).thenThrow(updateException); when(signatureManager.downloadLatest(SignatureType.CONTAINER)).thenThrow(updateException); try { command.execute(); fail("Expected CommandExecutionException"); } catch (CommandExecutionException e) { assertEquals("Error downloading signature updates: Failed\n" + "Caused by java.net.UnknownHostException: proxy", e.getMessage()); } }