private void attemptToCacheFileLocally(final File cachedFile, final Function<URI, Optional<Resource>> defaultFetcher, final URI resourceURI) if (!cachedFile.exists()) this.getStrategyID(), resourceURI, cachedFile.toString()); final File temporaryLocalFile = File.temporary(); try "StrategyID {}: something went wrong copying {} to temporary local file {}", this.getStrategyID(), resourceFromDefaultFetcher.toString(), temporaryLocalFile.toString(), exception); return; if (!cachedFile.exists()) final Path temporaryLocalFilePath = Paths.get(temporaryLocalFile.getPath()); final Path cachedFilePath = Paths.get(cachedFile.getPath()); Files.move(temporaryLocalFilePath, cachedFilePath, StandardCopyOption.ATOMIC_MOVE); cachedFile.toString()); return; this.getStrategyID(), temporaryLocalFile.toString(), cachedFile.toString(), exception);
private static List<File> fetchAtlasFilesInDirectory(final Path directory) { return new File(directory.toFile()).listFilesRecursively().stream().filter(IS_ATLAS) .collect(Collectors.toList()); } }
@Override public void invalidate() { final Path storageDirectory = this.getStorageDirectory(); try { new File(storageDirectory.toString()).deleteRecursively(); } catch (final Exception exception) { logger.warn("StrategyID {}: invalidate failed due to {}", this.getStrategyID(), exception); } }
@Override protected void processAtlas(final String atlasName, final Atlas atlas, final String folder) { final File file = new File(folder).child(atlasName + "-statistics.csv"); final AtlasStatistics statistics = processAtlas(atlas); file.writeAndClose(statistics.toString()); }
@Test public void fileCreationFromFileTest() { final File temp = File.temporaryFolder(); // Run AtlasJoinerSubCommand final String[] args = { "log-diff", String.format("-source=%s", GEO_11), String.format("-target=%s", GEO_21), String.format("-output=%s", temp.getPath()) }; new AtlasChecksCommand(args).runWithoutQuitting(args); final List<File> outputFiles = temp.listFilesRecursively(); Assert.assertTrue(outputFiles.stream() .anyMatch(file -> file.getName().matches("additions-\\d+-1.log"))); Assert.assertTrue(outputFiles.stream() .anyMatch(file -> file.getName().matches("changes-\\d+-1.log"))); Assert.assertTrue(outputFiles.stream() .anyMatch(file -> file.getName().matches("subtractions-\\d+-1.log"))); temp.deleteRecursively(); }
@Test public void testNoWaysConversion() { final File temp = File.temporaryFolder(); try { // Run OsmPbfToAtlasSubCommand final String[] args = { "pbf-to-atlas", String.format("-pbf=%s", PBF), String.format("-output=%s/%s", temp, ATLAS_NAME), "-load-ways=false" }; new AtlasReader(args).runWithoutQuitting(args); // Load new atlas final Atlas atlas = new AtlasResourceLoader() .load(new File(String.format("%s/%s", temp, ATLAS_NAME))); // Test no load ways Assert.assertFalse(atlas.areas().iterator().hasNext()); Assert.assertFalse(atlas.edges().iterator().hasNext()); Assert.assertFalse(atlas.lines().iterator().hasNext()); } finally { temp.deleteRecursively(); } } }
/** * @return {@link File} object of the parent directory */ public File parent() { return new File(this.javaFile.getParent()); }
new TaskDeserializer(configuration.getProjectName())).create(); final Configuration instructions = this.loadConfiguration(commandMap); ((File) commandMap.get(INPUT_DIRECTORY)).listFilesRecursively().forEach(logFile -> if (FilenameUtils.getExtension(logFile.getName()).equals(LOG_EXTENSION)) try (BufferedReader reader = new BufferedReader(new FileReader(logFile.getPath()))) MessageFormat.format("Error while reading {0}: ", logFile.getName()), error);
@Test public void testJoinedOutput() { final File temp = File.temporary(); // Run AtlasFindByAtlasIdentifierSubCommand final String[] args = { "find-atlas-id", String.format("-input=%1$s", SHARD_PATH.getPath()), "-id=546649246000001,575954012000000", String.format("-joinedOutput=%1$s", temp.getPath()) }; new AtlasReader(args).runWithoutQuitting(args); Assert.assertTrue(temp.length() > 0); temp.delete(); } }
/** * @return An list of files in the directory and sub directories if this object a directory. The * list will be empty if the directory is empty. If this object is a file, the list will * contain only the file itself. */ public List<File> listFilesRecursively() { final List<File> result = new ArrayList<>(); if (this.getFile().isDirectory()) { for (final java.io.File file : this.getFile().listFiles()) { final File newFile = new File(file.getAbsolutePath()); if (file.isDirectory()) { result.addAll(newFile.listFilesRecursively()); } else { result.add(newFile); } } } else if (this.getFile().exists()) { result.add(this); } return result; }
private Set<File> getFilesOfType(final File file) { if (FilenameUtils.getExtension(file.getName()).equalsIgnoreCase(this.fileExtension)) { return Collections.singleton(file); } else if (file.isDirectory()) { return file .listFilesRecursively().stream().filter(subFile -> FilenameUtils .getExtension(subFile.getName()).equalsIgnoreCase(this.fileExtension)) .collect(Collectors.toSet()); } return new HashSet<>(); }
@Test public void multipleFiles() { final File parent = File.temporaryFolder(); try { final File atlas1 = parent.child("iAmAn.atlas"); atlas1.writeAndClose("1"); final File atlas2 = parent.child("iTooAmAn.atlas"); atlas2.writeAndClose("2"); final File other = parent.child("iAmNotAnAtlas.txt"); other.writeAndClose("3"); final List<Resource> selected = new AtlasFileSelector().select(parent); // This one does not filter on an Atlas. Assert.assertEquals(3, selected.size()); } finally { parent.deleteRecursively(); } }
@BeforeClass public static void createBinaryAtlases() { final List<String> shardList = Arrays.asList(TXT_PATH + "/DNK_1.txt", TXT_PATH + "/DNK_2.txt", TXT_PATH + "/DNK_3.txt"); shardList.forEach(shard -> new TextAtlasBuilder().read(new File(shard)) .save(new File(SHARD_PATH.getPath() + shard.replace("txt", "atlas")))); }
public List<Resource> select(final File file) { final List<Resource> result = new ArrayList<>(); if (file != null && file.exists()) { file.listFilesRecursively().forEach(child -> { if (child.isGzipped()) { child.setDecompressor(Decompressor.GZIP); } result.add(child); }); } else { logger.warn("File {} does not exist.", file); } return result; } }
private static ChangeSet serializeAndDeserialize(final ChangeSet changeSet) throws Exception { // Serialize final File tempFile = File.temporary(); try (BinaryChangeSetSerializer serializer = new BinaryChangeSetSerializer(tempFile)) { serializer.accept(changeSet); } // Deserialize Optional<ChangeSet> readChangeSet = Optional.empty(); try (BinaryChangeSetDeserializer deserializer = new BinaryChangeSetDeserializer(tempFile)) { readChangeSet = deserializer.get(); Assert.assertTrue(readChangeSet.isPresent()); } tempFile.delete(); return readChangeSet.get(); }
final Resource testData = new InputStreamResource( () -> ExtractorTest.class.getResourceAsStream("testExtractor.zip")); testData.copyTo(new org.openstreetmap.atlas.streaming.resource.File(temporaryFile)); final Extractor extractor = Extractor.extractZipArchive(outputDirectory); extractor.extract(temporaryFile); final String expectedOutput = "file1"; final String actualOutput = new org.openstreetmap.atlas.streaming.resource.File( new File(outputDirectory, "file1.txt")).all(); Assert.assertEquals(expectedOutput, actualOutput); new org.openstreetmap.atlas.streaming.resource.File(outputDirectory) .listFilesRecursively() .forEach(org.openstreetmap.atlas.streaming.resource.File::delete); outputDirectory.delete();
/** * Get or create a {@link CountryBoundaryMap}. If the country-boundary-map parameter is set, * this will attempt to load the text or shape file from that parameter. Else, this will load * using the entire world as the country UNK (unknown). * * @param map * {@link CommandMap} containing the {@code COUNTRY_MAP_PARAMETER} * @return {@link CountryBoundaryMap} loaded from a file or default */ private CountryBoundaryMap getCountryBoundaryMap(final CommandMap map) { final Optional<File> countryMapOption = (Optional<File>) map .getOption(COUNTRY_MAP_PARAMETER); CountryBoundaryMap countryMap = CountryBoundaryMap .fromBoundaryMap(Collections.singletonMap("UNK", MultiPolygon.MAXIMUM)); if (countryMapOption.isPresent()) { final File countryMapFile = countryMapOption.get(); if (FilenameUtils.isExtension(countryMapFile.getName(), "txt")) { countryMap = CountryBoundaryMap.fromPlainText(countryMapFile); } else if (FilenameUtils.isExtension(countryMapFile.getName(), "shp")) { countryMap = CountryBoundaryMap.fromShapeFile(countryMapFile.getFile()); } } return countryMap; }
@Test public void testHighCompressionLevel() throws IOException { final File source = File.temporary(); logger.info("testCompressionLevel using {}", source); try { final ZipFileWritableResource zipFile = new ZipFileWritableResource(source); zipFile.writeAndClose( new StringResource("HereIsSomeTextThatRepeatsHereIsSomeTextThatRepeats") .withName(NAME_1), new StringResource("HereIsSomeTextThatDoesn'tRepeat").withName(NAME_2)); final ZipFile file = new ZipFile(source.getFile()); final ZipEntry name1 = file.getEntry(NAME_1); Assert.assertNotEquals(-1, name1.getCompressedSize()); Assert.assertTrue(name1.getCompressedSize() < name1.getSize()); final ZipEntry name2 = file.getEntry(NAME_2); Assert.assertNotEquals(-1, name2.getCompressedSize()); Assert.assertTrue(name2.getCompressedSize() >= name2.getSize()); file.close(); } finally { source.delete(); logger.info("testZipFile deleted {}", source); } }
try temporaryFile = File.temporary(); temporaryFile.writeAndClose(string); temporaryFile.getAbsolutePath()); processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT); processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT); temporaryFile.delete();