/** * Asserts that the given file has the given text content. * @param info contains information about the assertion. * @param actual the "actual" file. * @param expected the "expected" text content. * @param charset the charset to use to read the file. * @throws NullPointerException if {@code expected} is {@code null}. * @throws AssertionError if {@code actual} is {@code null}. * @throws AssertionError if {@code actual} is not an existing file. * @throws FilesException if an I/O error occurs. * @throws AssertionError if the file does not have the text content. */ public void assertHasContent(AssertionInfo info, File actual, String expected, Charset charset) { if (expected == null) throw new NullPointerException("The text to compare to should not be null"); assertIsFile(info, actual); try { List<String> diffs = diff.diff(actual, expected, charset); if (diffs.isEmpty()) return; throw failures.failure(info, shouldHaveContent(actual, charset, diffs)); } catch (IOException e) { String msg = String.format("Unable to verify text contents of file:<%s>", actual); throw new FilesException(msg, e); } }
/** * Asserts that the given file has the given binary content. * @param info contains information about the assertion. * @param actual the "actual" file. * @param expected the "expected" binary content. * @throws NullPointerException if {@code expected} is {@code null}. * @throws AssertionError if {@code actual} is {@code null}. * @throws AssertionError if {@code actual} is not an existing file. * @throws FilesException if an I/O error occurs. * @throws AssertionError if the file does not have the binary content. */ public void assertHasBinaryContent(AssertionInfo info, File actual, byte[] expected) { if (expected == null) throw new NullPointerException("The binary content to compare to should not be null"); assertIsFile(info, actual); try { BinaryDiffResult result = binaryDiff.diff(actual, expected); if (result.hasNoDiff()) return; throw failures.failure(info, shouldHaveBinaryContent(actual, result)); } catch (IOException e) { String msg = String.format("Unable to verify binary contents of file:<%s>", actual); throw new FilesException(msg, e); } }
/** * Asserts that the given files have equal content. Adapted from <a * href="http://junit-addons.sourceforge.net/junitx/framework/FileAssert.html" target="_blank">FileAssert</a> (from <a * href="http://sourceforge.net/projects/junit-addons">JUnit-addons</a>.) * @param info contains information about the assertion. * @param actual the "actual" file. * @param expected the "expected" file. * @throws NullPointerException if {@code expected} is {@code null}. * @throws IllegalArgumentException if {@code expected} is not an existing file. * @throws AssertionError if {@code actual} is {@code null}. * @throws AssertionError if {@code actual} is not an existing file. * @throws FilesException if an I/O error occurs. * @throws AssertionError if the given files do not have equal content. */ public void assertEqualContent(AssertionInfo info, File actual, File expected) { verifyIsFile(expected); assertIsFile(info, actual); try { List<String> diffs = diff.diff(actual, expected); if (diffs.isEmpty()) return; throw failures.failure(info, shouldHaveEqualContent(actual, expected, diffs)); } catch (IOException e) { String msg = String.format("Unable to compare contents of files:<%s> and:<%s>", actual, expected); throw new FilesException(msg, e); } }
/** * Returns the current directory. * * @return the current directory. * @throws FilesException if the current directory cannot be obtained. */ public static File currentFolder() { try { return new File(".").getCanonicalFile(); } catch (IOException e) { throw new FilesException("Unable to get current directory", e); } }
void createIfNecessary() { File f = new File(path); if (f.isDirectory()) return; if (f.mkdirs()) return; throw new FilesException(concat("Unable to create output directory ", path)); } }
/** * Returns the system's temporary directory. * * @return the system's temporary directory. * @throws FilesException if this method cannot find or create the system's temporary directory. */ public static File temporaryFolder() { File temp = new File(temporaryFolderPath()); if (!temp.isDirectory()) { throw new FilesException("Unable to find temporary directory"); } return temp; }
/** * 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); } }
private static FilesException cannotCreateNewFile(String path, String reason, Exception cause) { String message = String.format("Unable to create the new file %s", quote(path)); if (!Strings.isNullOrEmpty(reason)) { message = concat(message, ": ", reason); } if (cause != null) { throw new FilesException(message, cause); } throw new FilesException(message); }