use of java.nio.charset.MalformedInputException 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);
}
}
use of java.nio.charset.MalformedInputException in project robovm by robovm.
the class ASCIICharsetEncoderTest method testEncodeMapping.
public void testEncodeMapping() throws CharacterCodingException {
encoder.reset();
for (int i = 0; i <= MAXCODEPOINT; i++) {
char[] chars = Character.toChars(i);
CharBuffer cb = CharBuffer.wrap(chars);
ByteBuffer bb = encoder.encode(cb);
assertEquals(i, bb.get(0));
}
CharBuffer cb = CharBuffer.wrap("");
try {
encoder.encode(cb);
} catch (UnmappableCharacterException e) {
//expected
}
cb = CharBuffer.wrap("�");
try {
encoder.encode(cb);
} catch (MalformedInputException e) {
//expected
}
ByteBuffer bb = ByteBuffer.allocate(0x10);
cb = CharBuffer.wrap("A");
encoder.reset();
encoder.encode(cb, bb, false);
try {
encoder.encode(cb);
} catch (IllegalStateException e) {
//expected
}
}
use of java.nio.charset.MalformedInputException in project robovm by robovm.
the class CharsetDecoderTest method test_decode.
/**
* @tests java.nio.charset.CharsetDecoder#decode(java.nio.ByteBuffer)
*/
public void test_decode() throws CharacterCodingException {
// Regression for HARMONY-33
// ByteBuffer bb = ByteBuffer.allocate(1);
// bb.put(0, (byte) 77);
// CharsetDecoder decoder = Charset.forName("UTF-16").newDecoder();
// decoder.onMalformedInput(CodingErrorAction.REPLACE);
// decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
// decoder.decode(bb);
// Regression for HARMONY-67
// byte[] b = new byte[] { (byte) 1 };
// ByteBuffer buf = ByteBuffer.wrap(b);
// CharBuffer charbuf = Charset.forName("UTF-16").decode(buf);
// assertEquals("Assert 0: charset UTF-16", 1, charbuf.length());
//
// charbuf = Charset.forName("UTF-16BE").decode(buf);
// assertEquals("Assert 1: charset UTF-16BE", 0, charbuf.length());
//
// charbuf = Charset.forName("UTF-16LE").decode(buf);
// assertEquals("Assert 2: charset UTF16LE", 0, charbuf.length());
// Regression for HARMONY-99
CharsetDecoder decoder2 = Charset.forName("UTF-16").newDecoder();
decoder2.onMalformedInput(CodingErrorAction.REPORT);
decoder2.onUnmappableCharacter(CodingErrorAction.REPORT);
ByteBuffer in = ByteBuffer.wrap(new byte[] { 109, 97, 109 });
try {
decoder2.decode(in);
fail("Assert 3: MalformedInputException should have thrown");
} catch (MalformedInputException e) {
//expected
}
}
use of java.nio.charset.MalformedInputException in project robovm by robovm.
the class MalformedInputExceptionTest method testConstructor.
public void testConstructor() {
MalformedInputException ex = new MalformedInputException(3);
assertTrue(ex instanceof CharacterCodingException);
assertNull(ex.getCause());
assertEquals(ex.getInputLength(), 3);
assertTrue(ex.getMessage().indexOf("3") != -1);
ex = new MalformedInputException(-3);
assertNull(ex.getCause());
assertEquals(ex.getInputLength(), -3);
assertTrue(ex.getMessage().indexOf("-3") != -1);
ex = new MalformedInputException(0);
assertNull(ex.getCause());
assertEquals(ex.getInputLength(), 0);
assertTrue(ex.getMessage().indexOf("0") != -1);
}
use of java.nio.charset.MalformedInputException in project robovm by robovm.
the class CharsetEncoderTest method testEncodeCharBufferException.
public void testEncodeCharBufferException() throws CharacterCodingException {
ByteBuffer out;
CharBuffer in;
// MalformedException:
in = getMalformedCharBuffer();
encoder.onMalformedInput(CodingErrorAction.REPORT);
encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
if (in != null) {
try {
// regression test for Harmony-1379
encoder.encode(in);
fail("should throw MalformedInputException");
} catch (MalformedInputException e) {
}
encoder.reset();
in.rewind();
encoder.onMalformedInput(CodingErrorAction.IGNORE);
out = encoder.encode(in);
assertByteArray(out, addSurrogate(unibytes));
encoder.reset();
in.rewind();
encoder.onMalformedInput(CodingErrorAction.REPLACE);
out = encoder.encode(in);
assertByteArray(out, addSurrogate(unibytesWithRep));
}
// Unmapped Exception:
in = getUnmapCharBuffer();
encoder.onMalformedInput(CodingErrorAction.REPORT);
encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
if (in != null) {
encoder.reset();
try {
encoder.encode(in);
fail("should throw UnmappableCharacterException");
} catch (UnmappableCharacterException e) {
}
encoder.reset();
in.rewind();
encoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
out = encoder.encode(in);
assertByteArray(out, unibytes);
encoder.reset();
in.rewind();
encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
out = encoder.encode(in);
assertByteArray(out, unibytesWithRep);
}
// RuntimeException
try {
encoder.encode(getExceptionCharBuffer());
fail("should throw runtime exception");
} catch (RuntimeException e) {
}
}
Aggregations