MaxmindDbEnrichmentLoader { protected File downloadGeoFile(String urlStr, String tmpDir, int numRetries) { File localFile = null; int attempts = 0; boolean valid = false; while (!valid && attempts <= numRetries) { try { URL url = new URL(urlStr); localFile = new File(tmpDir + new File(url.getPath()).getName()); System.out.println("Downloading " + url.toString() + " to " + localFile.getAbsolutePath()); if (localFile.exists() && !localFile.delete()) { System.err.println( "File already exists locally and can't be deleted. Please delete before continuing"); System.exit(3); } FileUtils.copyURLToFile(url, localFile, 5000, 10000); if (!CompressionStrategies.GZIP.test(localFile)) { throw new IOException("Invalid Gzip file"); } else { valid = true; } } catch (MalformedURLException e) { System.err.println("Malformed URL - aborting: " + e); e.printStackTrace(); System.exit(4); } catch (IOException e) { System.err.println("Warning: Unable to copy remote GeoIP database to local file, attempt " + attempts + ": " + e); e.printStackTrace(); } attempts++; } if (!valid) { System.err.println("Unable to copy remote GeoIP database to local file after " + attempts + " attempts"); throw new IllegalStateException("Unable to download geo enrichment database."); } return localFile; }  static void main(String... argv);  }
@Test public void loader_throws_exception_on_bad_gzip_file() throws Exception { File dbFile = new File(remoteDir.getAbsolutePath() + "/MaxmindDbEnrichmentLoaderTest.mmdb"); dbFile.createNewFile(); String geoUrl = "file: int numRetries = 2; MaxmindDbEnrichmentLoader loader = new MockMaxmindDbEnrichmentLoader(); IllegalStateException e = assertThrows(IllegalStateException.class, () -> loader.downloadGeoFile(geoUrl, tmpDir.getAbsolutePath(), numRetries)); assertEquals("Unable to download geo enrichment database.", e.getMessage()); }