/** * Read and return the entire contents of the supplied {@link InputStream}. This method always closes the stream when finished * reading. * * @param stream the streamed contents; may be null * @return the contents, or an empty string if the supplied stream is null * @throws IOException if there is an error reading the content */ public static String read(InputStream stream) throws IOException { return stream == null ? "" : read(new InputStreamReader(stream)); }
/** * Read the lines from the content of the resource file at the given path on the classpath. * * @param resourcePath the logical path to the classpath, file, or URL resource * @param classLoader the classloader that should be used to load the resource as a stream; may be null * @param clazz the class that should be used to load the resource as a stream; may be null * @param lineProcessor the function that this method calls for each line read from the supplied stream; may not be null * @throws IOException if an I/O error occurs */ public static void readLines(String resourcePath, ClassLoader classLoader, Class<?> clazz, Consumer<String> lineProcessor) throws IOException { try (InputStream stream = IoUtil.getResourceAsStream(resourcePath, classLoader, clazz, null, null)) { IoUtil.readLines(stream, lineProcessor); } }
/** * A method that will delete multiple file and/or folders. Folders are removed recursively. * * @param filesOrFolder the files and folders to be deleted * @throws IOException if there is a problem deleting the file at the given path */ public static void delete(File... filesOrFolder) throws IOException { for (File fileOrFolder : filesOrFolder) { delete(fileOrFolder); } }
/** * Reads the lines starting with a given line number from the specified file on the classpath. Any lines preceding the * given line number will be included as empty lines, meaning the line numbers will match the input file. * * @param startingLineNumber the 1-based number designating the first line to be included * @param classpathResource the path to the file on the classpath * @return the string containing the subset of the file contents; never null but possibly empty */ protected String readLines(int startingLineNumber, String classpathResource) { try (InputStream stream = getClass().getClassLoader().getResourceAsStream(classpathResource);) { assertThat(stream).isNotNull(); StringBuilder sb = new StringBuilder(); AtomicInteger counter = new AtomicInteger(); IoUtil.readLines(stream, line -> { if (counter.incrementAndGet() >= startingLineNumber) sb.append(line); sb.append(System.lineSeparator()); }); return sb.toString(); } catch (IOException e) { fail("Unable to read '" + classpathResource + "'"); } assert false : "should never get here"; return null; }
/** * Find a port that is available. This method starts a {@link ServerSocket} and obtains the port on which the socket is * listening, and then shuts down the socket so the port becomes available. * * @return the number of the now-available port */ public static int getAvailablePort() { return IoUtil.getAvailablePort(); }
/** * Create a directory within the test data directory at the given relative path. * * @param relativePath the path of the directory within the test data directory; may not be null * @return the reference to the existing readable and writable directory */ public static File createTestingDirectory(String relativePath) { Path dirPath = createTestingPath(relativePath); return IoUtil.createDirectory(dirPath); }
/** * Create a file within the test data directory at the given relative path. * * @param relativePath the path of the file within the test data directory; may not be null * @return the reference to the existing readable and writable file */ public static File createTestingFile(String relativePath) { Path path = createTestingPath(relativePath); return IoUtil.createFile(path); }
/** * Obtain a configuration instance by loading the Properties from a file on the file system or classpath given by the supplied * path. * * @param path the path to the file containing the configuration properties; may not be null * @param classLoader the class loader to use; may be null * @param logger the function that will be called with status updates; may be null * @return the configuration; never null but possibly empty * @throws IOException if there is an error reading the stream */ public static Configuration load(String path, ClassLoader classLoader, Consumer<String> logger) throws IOException { try (InputStream stream = IoUtil.getResourceAsStream(path, classLoader, null, null, logger)) { Properties props = new Properties(); if (stream != null) { props.load(stream); } return from(props); } }
/** * Read the contents of the supplied {@link InputStream}, replace all variables found in the content, and write the * result to a temporary file. * * @param stream the input stream containing zero or more {@link Strings#replaceVariables(String, java.util.function.Function) * variable expressions} * @param variables the variables * @return the temporary file that exists in the data directory * @throws IOException if there is a problem reading the input stream or writing to the temporary file */ protected static File replaceVariables(InputStream stream, AvailableVariables variables) throws IOException { File tmpFile = Testing.Files.createTestingFile(); try (OutputStream ostream = new FileOutputStream(tmpFile)) { IoUtil.readLines(stream, (line) -> { String newLine = Strings.replaceVariables(line, variables::variableForName); try { ostream.write(newLine.getBytes(StandardCharsets.UTF_8)); } catch (IOException e) { throw new RuntimeException("Error writing to file '" + tmpFile + "'", e); } }, StandardCharsets.UTF_8); } return tmpFile; } }
if (factory != null) throw new IllegalStateException("" + this + " is already running"); if (this.port == -1) this.port = IoUtil.getAvailablePort(); this.factory = ServerCnxnFactory.createFactory(new InetSocketAddress("localhost", port), 1024); if ( this.dataDir == null ) {
/** * Create a directory within the test data directory at the given relative path. * * @param relativePath the path of the directory within the test data directory; may not be null * @param removeExistingContent true if any existing content should be removed * @return the reference to the existing readable and writable directory * @throws IOException if there is a problem deleting the files at this path */ public static File createTestingDirectory(String relativePath, boolean removeExistingContent) throws IOException { Path dirPath = createTestingPath(relativePath); return IoUtil.createDirectory(dirPath, removeExistingContent); }
/** * Create a file within the test data directory at the given relative path. * * @param relativePath the path of the file within the test data directory; may not be null * @return the reference to the existing readable and writable file */ public static File createTestingFile(Path relativePath) { Path path = relativePath.toAbsolutePath(); if ( !inTestDataDir(path) ) { throw new IllegalStateException("Expecting '" + relativePath + "' to be within the testing directory"); } return IoUtil.createFile(path); }
/** * Obtain a configuration instance by loading the Properties from a file on the file system or classpath given by the supplied * path. * * @param path the path to the file containing the configuration properties; may not be null * @param classLoader the class loader to use; may be null * @param logger the function that will be called with status updates; may be null * @return the configuration; never null but possibly empty * @throws IOException if there is an error reading the stream */ public static Configuration load(String path, ClassLoader classLoader, Consumer<String> logger) throws IOException { try (InputStream stream = IoUtil.getResourceAsStream(path, classLoader, null, null, logger)) { Properties props = new Properties(); if (stream != null) { props.load(stream); } return from(props); } }
/** * Read and return the entire contents of the supplied {@link InputStream}. This method always closes the stream when finished * reading. * * @param stream the streamed contents; may be null * @param charset character set of the stream data; may not be null * @return the contents, or an empty string if the supplied stream is null * @throws IOException if there is an error reading the content */ public static String read(InputStream stream, String charset) throws IOException { return stream == null ? "" : read(new InputStreamReader(stream, charset)); }
protected List<Document> loadTestDocuments(String pathOnClasspath) { List<Document> results = new ArrayList<>(); try (InputStream stream = Testing.Files.readResourceAsStream(pathOnClasspath);) { assertThat(stream).isNotNull(); IoUtil.readLines(stream, line -> { Document doc = Document.parse(line); assertThat(doc.size()).isGreaterThan(0); results.add(doc); }); } catch (IOException e) { fail("Unable to find or read file '" + pathOnClasspath + "': " + e.getMessage()); } return results; }
/** * A method that will delete a file or folder only if it is within the 'target' directory (for safety). * Folders are removed recursively. * * @param path the path to the file or folder in the target directory * @throws IOException if there is a problem deleting the file at the given path */ public static void delete(String path) throws IOException { if (path != null) delete(Paths.get(path)); }
/** * Read the lines from the content of the resource file at the given path on the classpath. * * @param resourcePath the logical path to the classpath, file, or URL resource * @param classLoader the classloader that should be used to load the resource as a stream; may be null * @param clazz the class that should be used to load the resource as a stream; may be null * @param lineProcessor the function that this method calls for each line read from the supplied stream; may not be null * @throws IOException if an I/O error occurs */ public static void readLines(String resourcePath, ClassLoader classLoader, Class<?> clazz, Consumer<String> lineProcessor) throws IOException { try (InputStream stream = IoUtil.getResourceAsStream(resourcePath, classLoader, clazz, null, null)) { IoUtil.readLines(stream, lineProcessor); } }
port = desiredPort > 0 ? desiredPort : IoUtil.getAvailablePort(); config.setProperty(KafkaConfig.PortProp(), Integer.toString(port));
protected String readFile(String classpathResource) { try (InputStream stream = getClass().getClassLoader().getResourceAsStream(classpathResource);) { assertThat(stream).isNotNull(); return IoUtil.read(stream); } catch (IOException e) { fail("Unable to read '" + classpathResource + "'"); } assert false : "should never get here"; return null; }
/** * Read the contents of the supplied {@link InputStream}, replace all variables found in the content, and write the * result to a temporary file. * * @param stream the input stream containing zero or more {@link Strings#replaceVariables(String, java.util.function.Function) * variable expressions} * @param variables the variables * @return the temporary file that exists in the data directory * @throws IOException if there is a problem reading the input stream or writing to the temporary file */ protected static File replaceVariables(InputStream stream, AvailableVariables variables) throws IOException { File tmpFile = Testing.Files.createTestingFile(); try (OutputStream ostream = new FileOutputStream(tmpFile)) { IoUtil.readLines(stream, (line) -> { String newLine = Strings.replaceVariables(line, variables::variableForName); try { ostream.write(newLine.getBytes(StandardCharsets.UTF_8)); } catch (IOException e) { throw new RuntimeException("Error writing to file '" + tmpFile + "'", e); } }, StandardCharsets.UTF_8); } return tmpFile; } }