/** * Loads the text content of a file, so that it can be passed to {@link #assertThat(String)}. * <p> * Note that this will load the entire file in memory; for larger files, there might be a more efficient alternative * with {@link #assertThat(File)}. * </p> * * @param file the file. * @param charset the character set to use. * @return the content of the file. * @throws NullPointerException if the given charset is {@code null}. * @throws UncheckedIOException if an I/O exception occurs. */ public static String contentOf(File file, Charset charset) { return Files.contentOf(file, charset); }
/** * Creates a new file in the system's temporary directory. The name of the file will be the result of: * <pre><code class='java'> concat(UUID.randomUUID().toString(), ".txt");</code></pre> * * @return the created file. */ public static File newTemporaryFile() { String tempFileName = concat(UUID.randomUUID().toString(), ".txt"); return newFile(concat(temporaryFolderPath(), tempFileName)); }
/** * Creates a new directory using the given path. * * @param path the path of the new directory. * @return the new created directory. * @throws RuntimeException if the path belongs to an existing non-empty directory. * @throws RuntimeException if the path belongs to an existing file. * @throws RuntimeException if any I/O error is thrown when creating the new directory. */ public static File newFolder(String path) { File file = createFileIfPathIsNotANonEmptyDirectory(path); try { if (!file.mkdir()) { throw cannotCreateNewFile(path, "a file was found with the same path"); } } catch (Exception e) { throw cannotCreateNewFile(path, e); } return file; }
/** * Loads the text content of a file into a character string. * * @param file the file. * @param charsetName the name of the character set to use. * @return the content of the file. * @throws IllegalArgumentException if the given character set is not supported on this platform. * @throws UncheckedIOException if an I/O exception occurs. */ public static String contentOf(File file, String charsetName) { checkArgumentCharsetIsSupported(charsetName); return contentOf(file, Charset.forName(charsetName)); }
/** * Loads the text content of a file into a list of strings, each string corresponding to a line. The line endings are * either \n, \r or \r\n. * * @param file the file. * @param charsetName the name of the character set to use. * @return the content of the file. * @throws NullPointerException if the given charset is {@code null}. * @throws UncheckedIOException if an I/O exception occurs. */ public static List<String> linesOf(File file, String charsetName) { checkArgumentCharsetIsSupported(charsetName); return linesOf(file, Charset.forName(charsetName)); }
/** * Deletes the given file or directory. * * @param file the file or directory to delete. */ public static void delete(File file) { if (file.isFile()) { file.delete(); return; } if (!file.isDirectory()) { return; } File[] existingFiles = file.listFiles(); if (!isNullOrEmpty(existingFiles)) { for (File f : existingFiles) { delete(f); } } file.delete(); }
/** * Loads the text content of a file into a list of strings, each string corresponding to a line. The line endings are * either \n, \r or \r\n. * * @param file the file. * @param charsetName the name of the character set to use. * @return the content of the file. * @throws NullPointerException if the given charset is {@code null}. * @throws UncheckedIOException if an I/O exception occurs. */ public static List<String> linesOf(File file, String charsetName) { return Files.linesOf(file, charsetName); }
@Before public void setUp() throws Exception { File baseFolder = Files.newTemporaryFolder(); this.inputJarFile = new File(baseFolder, NAME); this.outputJarFile = new File(baseFolder, EXPECTED_OUTPUT_JAR); this.inputFolder = new File(baseFolder, INPUT); this.outputFolder = new File(baseFolder, OUTPUT); this.provider = new JarsTransformOutputProvider(outputFolder, inputFolder); }
private static UncheckedIOException cannotCreateNewFile(String path, Exception cause) { throw cannotCreateNewFile(path, null, cause); }
/** * Returns the names of the files inside the specified directory. * * @param dirName the name of the directory to start the search from. * @param recurse if {@code true}, we will look in subdirectories. * @return the names of the files inside the specified directory. * @throws IllegalArgumentException if the given directory name does not point to an existing directory. */ public static List<String> fileNamesIn(String dirName, boolean recurse) { File dir = new File(dirName); checkArgument(dir.isDirectory(), "%s is not a directory", quote(dirName)); return fileNamesIn(dir, recurse); }
/** * Writes an image as a PNG file to the file system. If there is already a {@code File} present, its contents are * discarded. * * @param image a {@code BufferedImage} to be written. * @param filePath the path of the image file to create. * @return {@code false} if the image could not be saved. * @exception IOException if an error occurs during writing. */ public boolean writeAsPng(@Nonnull BufferedImage image, @Nonnull String filePath) throws IOException { return ImageIO.write(image, PNG, newFile(filePath)); } }
/** * Deletes the given file or directory. * * @param file the file or directory to delete. */ public static void delete(File file) { if (file.isFile()) { file.delete(); return; } if (!file.isDirectory()) { return; } File[] existingFiles = file.listFiles(); if (!isNullOrEmpty(existingFiles)) { for (File f : existingFiles) { delete(f); } } file.delete(); }
/** * Loads the text content of a file into a list of strings, each string corresponding to a line. The line endings are * either \n, \r or \r\n. * * @param file the file. * @param charsetName the name of the character set to use. * @return the content of the file. * @throws NullPointerException if the given charset is {@code null}. * @throws UncheckedIOException if an I/O exception occurs. */ public static List<String> linesOf(File file, String charsetName) { return Files.linesOf(file, charsetName); }
/** * Loads the text content of a file into a character string. * * @param file the file. * @param charsetName the name of the character set to use. * @return the content of the file. * @throws IllegalArgumentException if the given character set is not supported on this platform. * @throws UncheckedIOException if an I/O exception occurs. */ public static String contentOf(File file, String charsetName) { checkArgumentCharsetIsSupported(charsetName); return contentOf(file, Charset.forName(charsetName)); }
@SafeVarargs public EmbeddedBrokerRule( final Supplier<InputStream> configSupplier, int timeout, final Consumer<BrokerCfg>... configurators) { this.configSupplier = configSupplier; this.configurators = configurators; this.timeout = timeout; newTemporaryFolder = Files.newTemporaryFolder(); try (InputStream configStream = configSupplier.get()) { if (configStream == null) { brokerCfg = new BrokerCfg(); } else { brokerCfg = TomlConfigurationReader.read(configStream, BrokerCfg.class); } configureBroker(brokerCfg); } catch (final IOException e) { throw new RuntimeException("Unable to open configuration", e); } }
private static UncheckedIOException cannotCreateNewFile(String path, String reason) { throw cannotCreateNewFile(path, reason, null); }
/** * Returns the names of the files inside the specified directory. * * @param dir the name of the directory to start the search from. * @param recurse if {@code true}, we will look in subdirectories. * @return the names of the files inside the specified directory. */ private static List<String> fileNamesIn(File dir, boolean recurse) { List<String> scriptNames = new ArrayList<>(); File[] existingFiles = dir.listFiles(); if (isNullOrEmpty(existingFiles)) { return scriptNames; } for (File existingFile : existingFiles) { if (existingFile.isDirectory()) { if (recurse) { scriptNames.addAll(fileNamesIn(existingFile, recurse)); } continue; } String filename = existingFile.getAbsolutePath(); if (!scriptNames.contains(filename)) { scriptNames.add(filename); } } return scriptNames; }
/** * Loads the text content of a file into a list of strings, each string corresponding to a line. The line endings are * either \n, \r or \r\n. * * @param file the file. * @param charsetName the name of the character set to use. * @return the content of the file. * @throws NullPointerException if the given charset is {@code null}. * @throws UncheckedIOException if an I/O exception occurs. */ public static List<String> linesOf(File file, String charsetName) { checkArgumentCharsetIsSupported(charsetName); return linesOf(file, Charset.forName(charsetName)); }
/** * Loads the text content of a file, so that it can be passed to {@link #assertThat(String)}. * <p> * Note that this will load the entire file in memory; for larger files, there might be a more efficient alternative * with {@link #assertThat(File)}. * </p> * * @param file the file. * @param charset the character set to use. * @return the content of the file. * @throws NullPointerException if the given charset is {@code null}. * @throws UncheckedIOException if an I/O exception occurs. */ public static String contentOf(File file, Charset charset) { return Files.contentOf(file, charset); }
@Test public void unzipping_creates_target_directory_if_it_does_not_exist() throws IOException { File zip = FileUtils.toFile(urlToZip()); File tempDir = temp.newFolder(); Files.delete(tempDir); File subDir = new File(tempDir, "subDir"); ZipUtils.unzip(zip, subDir); assertThat(subDir.list()).hasSize(3); }