/** * 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 FilesException if an I/O exception occurs. */ public static String contentOf(File file, Charset charset) { return Files.contentOf(file, charset); }
/** * 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; } for (File f : file.listFiles()) { delete(f); } file.delete(); }
/** * Creates a new directory in the system's temporary directory. The name of the directory will be the result of: * * <pre> * System.currentTimeMillis(); * </pre> * * @return the created file. */ public static File newTemporaryFolder() { String tempFileName = String.valueOf(System.currentTimeMillis()); return newFolder(concat(temporaryFolderPath(), tempFileName)); }
/** * Creates a new file in the system's temporary directory. The name of the file will be the result of: * * <pre> * concat(String.valueOf(System.currentTimeMillis()), ".txt"); * </pre> * * @return the created file. */ public static File newTemporaryFile() { String tempFileName = concat(valueOf(System.currentTimeMillis()), ".txt"); return newFile(concat(temporaryFolderPath(), tempFileName)); }
private static FilesException cannotCreateNewFile(String path, String reason) { throw cannotCreateNewFile(path, reason, null); }
/** * 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); if (!dir.isDirectory()) { throw new IllegalArgumentException(format("%s is not a directory", quote(dirName))); } return fileNamesIn(dir, recurse); }
/** * Loads the text content of a file into a character string. * * @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 FilesException if an I/O exception occurs. */ public static String contentOf(File file, Charset charset) { if (charset == null) { throw new NullPointerException("The charset should not be null"); } try { return loadContents(file, charset); } catch (IOException e) { throw new FilesException("Unable to read " + file.getAbsolutePath(), e); } }
Files.delete(file);
private static FilesException cannotCreateNewFile(String path, Exception cause) { throw cannotCreateNewFile(path, null, cause); }
/** * 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<String>(); 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, 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 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 FilesException if an I/O exception occurs. */ public static String contentOf(File file, String charsetName) { return Files.contentOf(file, charsetName); }
/** * Creates a new directory using the given path. * * @param path the path of the new directory. * @return the new created directory. * @throws FilesException if the path belongs to an existing non-empty directory. * @throws FilesException if the path belongs to an existing file. * @throws FilesException if any I/O error is thrown when creating the new directory. */ public static File newFolder(String path) { File file = new File(path); if (file.isDirectory() && !isNullOrEmpty(file.list())) { throw cannotCreateNewFile(path, "a non-empty directory was found with the same 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 with the default character set, 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. * @return the content of the file. * @throws FilesException if an I/O exception occurs. */ public static String contentOf(File file) { return Files.contentOf(file, Charset.defaultCharset()); }
/** * Creates a new file using the given path. * * @param path the path of the new file. * @return the new created file. * @throws FilesException if the path belongs to an existing non-empty directory. * @throws FilesException if the path belongs to an existing file. * @throws FilesException if any I/O error is thrown when creating the new file. */ public static File newFile(String path) { File file = new File(path); if (file.isDirectory() && !isNullOrEmpty(file.list())) { throw cannotCreateNewFile(path, "a non-empty directory was found with the same path"); } try { if (!file.createNewFile()) { throw cannotCreateNewFile(path, "a file was found with the same path"); } } catch (IOException 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 FilesException if an I/O exception occurs. */ public static String contentOf(File file, String charsetName) { if (!Charset.isSupported(charsetName)) { throw new IllegalArgumentException(String.format("Charset:<'%s'> is not supported on this system", charsetName)); } return contentOf(file, Charset.forName(charsetName)); }
private String readAndClearFileEntries() throws IOException { handler.flush(); String currentFileName = handler.getCurrentFileName(); String contents = Files.contentOf(new File(currentFileName), Charset.forName("UTF-8")); FileChannel outChan = new FileOutputStream(currentFileName, true).getChannel(); outChan.truncate(0); outChan.close(); return contents; }
@Test(retryAnalyzer = RetryAnalyzer.class) public void pasteClipboardOnOpen() throws IOException { final File file1 = File.createTempFile("otrosTest", ""); logEvents(file1, 10); final String clipboardContent = Files.contentOf(file1, "UTF-8"); setClipboard(clipboardContent); final MainFrame mainFrame = new MainFrame(robot()); final ParseClipboardDialog dialog = mainFrame.welcomeScreen().clickParseClipboard(); final String actual = dialog.clipboardTextAreaContent().text(); assertThat(actual).isEqualTo(clipboardContent); }
@Test(retryAnalyzer = RetryAnalyzer.class) public void importLogsFromClipboard() throws Exception { final File tempFile = File.createTempFile("olv", "logs"); logEvents(tempFile, 10, integer -> Level.INFO); final String logsInClipboard = Files.contentOf(tempFile, "UTF-8").trim(); final MainFrame mainFrame = new MainFrame(robot()); final ParseClipboardDialog dialog = mainFrame.welcomeScreen().clickParseClipboard(); setClipboard(logsInClipboard); dialog.refresh().click(); dialog.waitForProcessedContent(logsInClipboard); assertThat(dialog.processedContent().text()).isEqualTo(logsInClipboard); dialog.processingPattern().setText("sed s/Message/XXX/g"); dialog.waitForProcessedContent(logsInClipboard.replaceAll("Message", "XXX")); final LogViewPanel logViewPanel = dialog.importLogs(); await("waiting for 10 events in log table") .atMost(Duration.ONE_MINUTE) .until(() -> logViewPanel.logsTable().visibleLogsCount() == 10); IntStream.range(0, 9) .forEach(i -> logViewPanel.logsTable().hasValueInRow(i, "XXX " + i)); }