Search in sources :

Example 1 with Delta

use of org.assertj.core.util.diff.Delta in project assertj-core by joel-costigliola.

the class InputStreams_assertSameContentAs_Test method should_fail_if_inputstreams_do_not_have_equal_content.

@Test
public void should_fail_if_inputstreams_do_not_have_equal_content() throws IOException {
    @SuppressWarnings("unchecked") List<Delta<String>> diffs = newArrayList((Delta<String>) mock(Delta.class));
    when(diff.diff(actual, expected)).thenReturn(diffs);
    AssertionInfo info = someInfo();
    try {
        inputStreams.assertSameContentAs(info, actual, expected);
    } catch (AssertionError e) {
        verify(failures).failure(info, shouldHaveSameContent(actual, expected, diffs));
        return;
    }
    failBecauseExpectedAssertionErrorWasNotThrown();
}
Also used : AssertionInfo(org.assertj.core.api.AssertionInfo) Delta(org.assertj.core.util.diff.Delta) InputStreamsBaseTest(org.assertj.core.internal.InputStreamsBaseTest) Test(org.junit.Test)

Example 2 with Delta

use of org.assertj.core.util.diff.Delta in project assertj-core by joel-costigliola.

the class Files method assertSameContentAs.

/**
 * Asserts that the given files have same 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 actualCharset {@link Charset} of the "actual" file.
 * @param expected the "expected" file.
 * @param expectedCharset {@link Charset} of the "actual" 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 UncheckedIOException if an I/O error occurs.
 * @throws AssertionError if the given files do not have same content.
 */
public void assertSameContentAs(AssertionInfo info, File actual, Charset actualCharset, File expected, Charset expectedCharset) {
    verifyIsFile(expected);
    assertIsFile(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 (MalformedInputException e) {
        try {
            // MalformedInputException is thrown by readLine() called in diff
            // compute a binary diff, if there is a binary diff, it it shows the offset of the malformed input
            BinaryDiffResult binaryDiffResult = binaryDiff.diff(actual, readAllBytes(expected.toPath()));
            if (binaryDiffResult.hasNoDiff()) {
                // fall back to the UncheckedIOException : not throwing an error is wrong as there was one in the first place.
                throw e;
            }
            throw failures.failure(info, shouldHaveBinaryContent(actual, binaryDiffResult));
        } catch (IOException ioe) {
            throw new UncheckedIOException(format(UNABLE_TO_COMPARE_FILE_CONTENTS, actual, expected), ioe);
        }
    } catch (IOException e) {
        throw new UncheckedIOException(format(UNABLE_TO_COMPARE_FILE_CONTENTS, actual, expected), e);
    }
}
Also used : Delta(org.assertj.core.util.diff.Delta) MalformedInputException(java.nio.charset.MalformedInputException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Example 3 with Delta

use of org.assertj.core.util.diff.Delta in project assertj-core by joel-costigliola.

the class Files method assertHasContent.

/**
 * 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 UncheckedIOException 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 UncheckedIOException(msg, e);
    }
}
Also used : Delta(org.assertj.core.util.diff.Delta) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Example 4 with Delta

use of org.assertj.core.util.diff.Delta in project assertj-core by joel-costigliola.

the class ShouldHaveContent_create_Test method should_create_error_message.

@Test
public void should_create_error_message() {
    final FakeFile file = new FakeFile("xyz");
    @SuppressWarnings("unchecked") Delta<String> delta = mock(Delta.class);
    when(delta.toString()).thenReturn(DIFF);
    List<Delta<String>> diffs = Lists.newArrayList(delta);
    ErrorMessageFactory factory = shouldHaveContent(file, defaultCharset(), diffs);
    String message = factory.create(new TextDescription("Test"), CONFIGURATION_PROVIDER.representation());
    assertThat(message).isEqualTo(format("[Test] %n" + "File:%n" + "  <xyz>%n" + "read with charset <%s> does not have the expected content:%n%n" + DIFF, defaultCharset().name()));
}
Also used : Delta(org.assertj.core.util.diff.Delta) TextDescription(org.assertj.core.description.TextDescription) Test(org.junit.Test)

Example 5 with Delta

use of org.assertj.core.util.diff.Delta in project assertj-core by joel-costigliola.

the class Files_assertSameContentAs_Test method should_fail_if_files_do_not_have_equal_content.

@Test
public void should_fail_if_files_do_not_have_equal_content() throws IOException {
    List<Delta<String>> diffs = Lists.newArrayList(delta);
    when(diff.diff(actual, defaultCharset(), expected, defaultCharset())).thenReturn(diffs);
    when(binaryDiff.diff(actual, readAllBytes(expected.toPath()))).thenReturn(new BinaryDiffResult(1, -1, -1));
    AssertionInfo info = someInfo();
    try {
        files.assertSameContentAs(info, actual, defaultCharset(), expected, defaultCharset());
    } catch (AssertionError e) {
        verify(failures).failure(info, shouldHaveSameContent(actual, expected, diffs));
        return;
    }
    failBecauseExpectedAssertionErrorWasNotThrown();
}
Also used : AssertionInfo(org.assertj.core.api.AssertionInfo) Delta(org.assertj.core.util.diff.Delta) BinaryDiffResult(org.assertj.core.internal.BinaryDiffResult) FilesBaseTest(org.assertj.core.internal.FilesBaseTest) Test(org.junit.Test)

Aggregations

Delta (org.assertj.core.util.diff.Delta)11 Test (org.junit.Test)6 IOException (java.io.IOException)5 AssertionInfo (org.assertj.core.api.AssertionInfo)5 UncheckedIOException (java.io.UncheckedIOException)4 FilesBaseTest (org.assertj.core.internal.FilesBaseTest)2 Charset (java.nio.charset.Charset)1 MalformedInputException (java.nio.charset.MalformedInputException)1 ArrayList (java.util.ArrayList)1 RuntimeIOException (org.assertj.core.api.exception.RuntimeIOException)1 TextDescription (org.assertj.core.description.TextDescription)1 BinaryDiffResult (org.assertj.core.internal.BinaryDiffResult)1 InputStreamsBaseTest (org.assertj.core.internal.InputStreamsBaseTest)1 PathsBaseTest (org.assertj.core.internal.PathsBaseTest)1