@Test public void testSuccess() throws Exception { doThrow(RuntimeIOException.class).when(callback1).run(); handler.add(callback1); handler.add(callback2); handler.add(callback3); for(int i=0; i < 11; i++) { handler.add(callback4); } handler.runCallbacks(); handler.remove(callback2); handler.runCallbacks(); verify(callback1, times(2)).run(); verify(callback2, times(1)).run(); verify(callback3, times(2)).run(); verify(callback4, times(2)).run(); }
public void assertHasContent(final AssertionInfo info, Path actual, String expected, Charset charset) { checkNotNull(expected, "The text to compare to should not be null"); assertIsReadable(info, actual); try { List<Delta<String>> diffs = diff.diff(actual, expected, charset); if (diffs.isEmpty()) return; throw failures.failure(info, shouldHaveContent(actual, charset, diffs)); } catch (IOException e) { throw new RuntimeIOException(format("Unable to verify text contents of path:<%s>", actual), e); } }
public void assertHasBinaryContent(AssertionInfo info, Path actual, byte[] expected) { checkNotNull(expected, "The binary content to compare to should not be null"); assertIsReadable(info, actual); try { BinaryDiffResult diffResult = binaryDiff.diff(actual, expected); if (diffResult.hasNoDiff()) return; throw failures.failure(info, shouldHaveBinaryContent(actual, diffResult)); } catch (IOException e) { throw new RuntimeIOException(format("Unable to verify binary contents of path:<%s>", actual), e); } }
public void assertHasSameContentAs(AssertionInfo info, Path actual, Charset actualCharset, Path expected, Charset expectedCharset) { checkNotNull(expected, "The given Path to compare actual content to should not be null"); checkArgument(nioFilesWrapper.isReadable(expected), "The given Path <%s> to compare actual content to should be readable", expected); assertIsReadable(info, actual); try { List<Delta<String>> diffs = diff.diff(actual, actualCharset, expected, expectedCharset); if (diffs.isEmpty()) return; throw failures.failure(info, shouldHaveSameContent(actual, expected, diffs)); } catch (IOException e) { throw new RuntimeIOException(format("Unable to compare contents of paths:<%s> and:<%s>", actual, expected), 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 RuntimeIOException 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) { checkNotNull(expected, "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 RuntimeIOException(msg, e); } }
/** * 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 RuntimeIOException 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) { checkNotNull(expected, "The text to compare to should not be null"); assertIsFile(info, actual); try { List<Delta<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 RuntimeIOException(msg, e); } }
/** * Asserts that the given {@code File} has the given parent. * * @param info contains information about the assertion. * @param actual the given file. * @param expected the expected parent {@code File}. * @throws NullPointerException if the expected parent {@code File} is {@code null}. * @throws RuntimeIOException if an I/O error occurs. * @throws AssertionError if the given {@code File} is {@code null}. * @throws AssertionError if the given {@code File} does not have a parent. * @throws AssertionError if the given {@code File} parent is not equal to the expected one. */ public void assertHasParent(AssertionInfo info, File actual, File expected) { checkNotNull(expected, "The expected parent file should not be null."); assertNotNull(info, actual); try { if (actual.getParentFile() != null && areEqual(expected.getCanonicalFile(), actual.getParentFile().getCanonicalFile())) return; } catch (IOException e) { throw new RuntimeIOException(String.format("Unable to get canonical form of [%s] or [%s].", actual, expected), e); } throw failures.failure(info, shouldHaveParent(actual, expected)); }
/** * Returns the system's temporary directory. * * @return the system's temporary directory. * @throws RuntimeIOException 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 RuntimeIOException("Unable to find temporary directory"); } return temp; }
private static RuntimeIOException 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 RuntimeIOException(message, cause); } throw new RuntimeIOException(message); }
/** * Returns the current directory. * * @return the current directory. * @throws RuntimeIOException if the current directory cannot be obtained. */ public static File currentFolder() { try { return new File(".").getCanonicalFile(); } catch (IOException e) { throw new RuntimeIOException("Unable to get current directory", e); } }
@Test public void should_throw_error_wrapping_catched_IOException() throws IOException { IOException cause = new IOException(); when(diff.diff(path, expected, charset)).thenThrow(cause); when(nioFilesWrapper.exists(path)).thenReturn(true); when(nioFilesWrapper.isReadable(path)).thenReturn(true); thrown.expectWithCause(RuntimeIOException.class, cause); paths.assertHasContent(someInfo(), path, expected, charset); }
@Test public void should_throw_error_wrapping_catched_IOException() throws IOException { IOException cause = new IOException(); when(binaryDiff.diff(path, expected)).thenThrow(cause); when(nioFilesWrapper.exists(path)).thenReturn(true); when(nioFilesWrapper.isReadable(path)).thenReturn(true); thrown.expectWithCause(RuntimeIOException.class, cause); paths.assertHasBinaryContent(someInfo(), path, expected); }
@Test public void should_throw_error_wrapping_catched_IOException() throws IOException { IOException cause = new IOException(); when(diff.diff(actual, defaultCharset(), other, defaultCharset())).thenThrow(cause); when(nioFilesWrapper.exists(actual)).thenReturn(true); when(nioFilesWrapper.isReadable(actual)).thenReturn(true); when(nioFilesWrapper.isReadable(other)).thenReturn(true); thrown.expectWithCause(RuntimeIOException.class, cause); paths.assertHasSameContentAs(someInfo(), actual, defaultCharset(), other, defaultCharset()); }
@Test public void should_throw_error_wrapping_catched_IOException() throws IOException { IOException cause = new IOException(); when(diff.diff(actual, defaultCharset(), expected, defaultCharset())).thenThrow(cause); thrown.expectWithCause(RuntimeIOException.class, cause); files.assertSameContentAs(someInfo(), actual, defaultCharset(), expected, defaultCharset()); }
@Test public void should_throw_an_error_if_files_cant_be_compared_with_the_given_charsets_even_if_binary_identical() throws IOException { thrown.expectWithMessageStartingWith(RuntimeIOException.class, "Unable to compare contents of files"); unMockedFiles.assertSameContentAs(someInfo(), createFileWithNonUTF8Character(), StandardCharsets.UTF_8, createFileWithNonUTF8Character(), StandardCharsets.UTF_8); }
@Test public void should_throw_exception_when_canonical_form_representation_fail() throws Exception { thrown.expect(RuntimeIOException.class); File actual = mock(File.class); File expectedParent = mock(File.class); when(actual.getParentFile()).thenReturn(expectedParent); when(expectedParent.getCanonicalFile()).thenThrow(new IOException()); files.assertHasParent(someInfo(), actual, expectedParent); }
@Test public void should_throw_exception_when_canonical_form_representation_fail_for_expected_parent() throws Exception { thrown.expect(RuntimeIOException.class); File expectedParent = mock(File.class); when(expectedParent.getCanonicalFile()).thenThrow(new IOException()); files.assertHasParent(someInfo(), actual, expectedParent); }
@Test public void should_throw_error_wrapping_catched_IOException() throws IOException { IOException cause = new IOException(); when(binaryDiff.diff(actual, expected)).thenThrow(cause); thrown.expectWithCause(RuntimeIOException.class, cause); files.assertHasBinaryContent(someInfo(), actual, expected); }
@Test public void should_throw_error_wrapping_catched_IOException() throws IOException { IOException cause = new IOException(); when(diff.diff(actual, expected, charset)).thenThrow(cause); thrown.expectWithCause(RuntimeIOException.class, cause); files.assertHasContent(someInfo(), actual, expected, charset); }
@Test public void should_throw_exception_if_file_not_found() { File missingFile = new File("missing.txt"); assertThat(missingFile.exists()).isFalse(); thrown.expect(RuntimeIOException.class); Files.contentOf(missingFile, Charset.defaultCharset()); }
@Test public void should_throw_exception_if_file_not_found() { File missingFile = new File("missing.txt"); assertThat(missingFile).doesNotExist(); thrown.expect(RuntimeIOException.class); linesOf(missingFile, Charset.defaultCharset()); }
@Test public void should_throw_exception_if_url_not_found() throws MalformedURLException { File missingFile = new File("missing.txt"); assertThat(missingFile.exists()).isFalse(); thrown.expect(RuntimeIOException.class); URLs.contentOf(missingFile.toURI().toURL(), Charset.defaultCharset()); }
@Test public void should_throw_exception_if_url_not_found() throws MalformedURLException { File missingFile = new File("missing.txt"); assertThat(missingFile).doesNotExist(); thrown.expect(RuntimeIOException.class); URLs.linesOf(missingFile.toURI().toURL(), Charset.defaultCharset()); }
private static RuntimeIOException cannotCreateNewFile(String path, String reason) { throw cannotCreateNewFile(path, reason, null); }
private static RuntimeIOException cannotCreateNewFile(String path, Exception cause) { throw cannotCreateNewFile(path, null, cause); }
/** * Test that the delegate method is called. */ @Test(expected = RuntimeIOException.class) public void withAssertions_contentOf_Test() { contentOf(new File("/non-existent file")).contains("a"); }
/** * Test that the delegate method is called. */ @Test(expected = RuntimeIOException.class) public void withAssertions_contentOf_with_charset_Test() { contentOf(new File("/non-existent file", "UTF-8")).contains("a"); }
/** * Test that the delegate method is called. */ @Test(expected = RuntimeIOException.class) public void withAssertions_linesOf_Test() { linesOf(new File("/non-existent file")).contains("a"); }
/** * Test that the delegate method is called. */ @Test(expected = RuntimeIOException.class) public void withAssertions_linesOf_with_charsetTest() { linesOf(new File("/non-existent file", "UTF-8")).contains("a"); }
@Test public void should_throw_error_if_file_path_belongs_to_directory_that_is_not_empty() { thrown.expect(RuntimeIOException.class); Files.newFile("root"); }
@Test public void should_throw_error_if_file_path_belongs_to_an_existing_file() { String path = join("root", "dir_1", "file_1_1").with(separator); thrown.expect(RuntimeIOException.class); Files.newFile(path); }
/** * Loads the text content of a URL into a character string. * * @param url the URL. * @param charset the character set to use. * @return the content of the URL. * @throws NullPointerException if the given charset is {@code null}. * @throws RuntimeIOException if an I/O exception occurs. */ public static String contentOf(URL url, Charset charset) { checkNotNull(charset, "The charset should not be null"); try { return loadContents(url.openStream(), charset); } catch (IOException e) { throw new RuntimeIOException("Unable to read " + url, e); } }
/** * Loads the text content of a URL into a list of strings, each string corresponding to a line. The line endings are * either \n, \r or \r\n. * * @param url the URL. * @param charset the character set to use. * @return the content of the URL. * @throws NullPointerException if the given charset is {@code null}. * @throws RuntimeIOException if an I/O exception occurs. */ public static List<String> linesOf(URL url, Charset charset) { checkNotNull(charset, "The charset should not be null"); try { return loadLines(url.openStream(), charset); } catch (IOException e) { throw new RuntimeIOException("Unable to read " + url, e); } }
/** * 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 RuntimeIOException if an I/O exception occurs. */ public static String contentOf(File file, Charset charset) { checkNotNull(charset, "The charset should not be null"); try { return loadContents(file, charset); } catch (IOException e) { throw new RuntimeIOException("Unable to read " + file.getAbsolutePath(), e); } }
/** * 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 charset the character set to use. * @return the content of the file. * @throws NullPointerException if the given charset is {@code null}. * @throws RuntimeIOException if an I/O exception occurs. */ public static List<String> linesOf(File file, Charset charset) { checkNotNull(charset, "The charset should not be null"); try { return loadLines(file, charset); } catch (IOException e) { throw new RuntimeIOException("Unable to read " + file.getAbsolutePath(), e); } }