Esempio n. 1
0
 @Override
 public void executeWithTracker(Tracker tracker) throws IOException {
   List<URL> paths = tracker.getPaths(key, domain);
   if (!paths.isEmpty()) {
     HttpURLConnection httpConnection = null;
     IOException lastException = null;
     for (URL path : paths) {
       try {
         log.debug("HTTP HEAD -> {}", path);
         httpConnection = httpFactory.newConnection(path);
         httpConnection.setRequestMethod("HEAD");
         length = getContentLength(httpConnection);
         log.debug("Content-Length: {}", length);
         return;
       } catch (IOException e) {
         log.debug("Failed to open input -> {}", path);
         log.debug("Exception was: ", e);
         lastException = e;
       } finally {
         if (httpConnection != null) {
           httpConnection.disconnect();
         }
       }
     }
     throw lastException;
   } else {
     log.debug("No paths found for domain={},key={} - throwing", domain, key);
     throw new FileNotFoundException("domain=" + domain + ",key=" + key);
   }
 }
  @Before
  public void setUp() throws IOException {
    URL url = new URL("http://www.last.fm/");
    when(mockDestination.getPath()).thenReturn(url);
    when(mockHttpFactory.newConnection(url)).thenReturn(mockHttpConnection);
    when(mockHttpConnection.getInputStream()).thenReturn(mockInputStream);
    when(mockHttpConnection.getOutputStream()).thenReturn(mockOutputStream);
    when(mockHttpConnection.getResponseMessage()).thenReturn("message");
    when(mockHttpConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
    when(mockTrackerFactory.getTracker()).thenReturn(mockTracker);
    when(address.size()).thenReturn(1);
    when(mockTrackerFactory.getAddresses()).thenReturn(address);

    stream =
        new FileUploadOutputStream(
            mockTrackerFactory, mockHttpFactory, KEY, DOMAIN, mockDestination, mockWriteLock);
  }