use of java.nio.charset.CharsetEncoder in project robovm by robovm.
the class CharsetEncoderTest method testCharsetEncoderCharsetfloatfloatbyteArray.
/*
* Class under test for constructor CharsetEncoder(Charset, float, float,
* byte[])
*/
public void testCharsetEncoderCharsetfloatfloatbyteArray() {
byte[] ba = getLegalByteArray();
// normal case
CharsetEncoder ec = new MockCharsetEncoder(cs, 1, MAX_BYTES, ba);
assertSame(ec.charset(), cs);
assertEquals(1.0, ec.averageBytesPerChar(), 0.0);
assertTrue(ec.maxBytesPerChar() == MAX_BYTES);
assertSame(ba, ec.replacement());
// NullPointerException: null charset
try {
ec = new MockCharsetEncoder(null, 1, MAX_BYTES, ba);
fail("should throw null pointer exception");
} catch (NullPointerException e) {
}
// Illegal Argument: null byte array
try {
ec = new MockCharsetEncoder(cs, 1, MAX_BYTES, null);
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
// Illegal Argument: empty byte array
try {
ec = new MockCharsetEncoder(cs, 1, MAX_BYTES, new byte[0]);
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
// Illegal Argument: byte array is longer than max length
try {
ec = new MockCharsetEncoder(cs, 1, MAX_BYTES, new byte[] { 1, 2, MAX_BYTES, 4 });
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
// Illegal Argument: zero length
try {
ec = new MockCharsetEncoder(cs, 0, MAX_BYTES, ba);
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
try {
ec = new MockCharsetEncoder(cs, 1, 0, ba);
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
// Illegal Argument: negative length
try {
ec = new MockCharsetEncoder(cs, -1, MAX_BYTES, ba);
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
try {
ec = new MockCharsetEncoder(cs, 1, -1, ba);
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
}
use of java.nio.charset.CharsetEncoder in project robovm by robovm.
the class CharsetEncoderTest method testCharsetEncoderCharsetfloatfloat.
/*
* Class under test for constructor CharsetEncoder(Charset, float, float)
*/
public void testCharsetEncoderCharsetfloatfloat() {
// default value
encoder = new MockCharsetEncoder(cs, (float) AVER_BYTES, MAX_BYTES);
assertSame(encoder.charset(), cs);
assertTrue(encoder.averageBytesPerChar() == AVER_BYTES);
assertTrue(encoder.maxBytesPerChar() == MAX_BYTES);
assertEquals(CodingErrorAction.REPORT, encoder.malformedInputAction());
assertEquals(CodingErrorAction.REPORT, encoder.unmappableCharacterAction());
assertEquals(new String(encoder.replacement()), new String(defaultReplacement));
assertSame(encoder, encoder.onMalformedInput(CodingErrorAction.IGNORE));
assertSame(encoder, encoder.onUnmappableCharacter(CodingErrorAction.IGNORE));
// normal case
CharsetEncoder ec = new MockCharsetEncoder(cs, 1, MAX_BYTES);
assertSame(ec.charset(), cs);
assertEquals(1.0, ec.averageBytesPerChar(), 0);
assertTrue(ec.maxBytesPerChar() == MAX_BYTES);
// NullPointerException: null charset
try {
ec = new MockCharsetEncoder(null, 1, MAX_BYTES);
fail("should throw null pointer exception");
} catch (NullPointerException e) {
}
ec = new MockCharsetEncoder(new MockCharset("mock", new String[0]), 1, MAX_BYTES);
// Illegal Argument: zero length
try {
ec = new MockCharsetEncoder(cs, 0, MAX_BYTES);
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
try {
ec = new MockCharsetEncoder(cs, 1, 0);
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
// Illegal Argument: negative length
try {
ec = new MockCharsetEncoder(cs, -1, MAX_BYTES);
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
try {
ec = new MockCharsetEncoder(cs, 1, -1);
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
}
use of java.nio.charset.CharsetEncoder in project robovm by robovm.
the class CharsetEncoderTest method testEncodeCharBuffer.
/*
* Class under test for ByteBuffer encode(CharBuffer)
*/
public void testEncodeCharBuffer() throws CharacterCodingException {
// Null pointer
try {
encoder.encode(null);
fail("should throw null pointer exception");
} catch (NullPointerException e) {
}
// empty input buffer
ByteBuffer out = encoder.encode(CharBuffer.wrap(""));
assertEquals(out.position(), 0);
assertByteArray(out, new byte[0]);
// assertByteArray(out, surrogate);
// normal case
out = encoder.encode(CharBuffer.wrap(unistr));
assertEquals(out.position(), 0);
assertByteArray(out, addSurrogate(unibytes));
// Regression test for harmony-3378
Charset cs = Charset.forName("UTF-8");
CharsetEncoder encoder = cs.newEncoder();
encoder.onMalformedInput(CodingErrorAction.REPLACE);
encoder = encoder.replaceWith(new byte[] { (byte) 0xef, (byte) 0xbf, (byte) 0xbd });
CharBuffer in = CharBuffer.wrap("�");
out = encoder.encode(in);
assertNotNull(out);
}
use of java.nio.charset.CharsetEncoder in project robovm by robovm.
the class CharsetTest method test_allAvailableCharsets.
public void test_allAvailableCharsets() throws Exception {
// Check that we can instantiate every Charset, CharsetDecoder, and CharsetEncoder.
for (String charsetName : Charset.availableCharsets().keySet()) {
if (charsetName.equals("UTF-32")) {
// TODO: remove this hack when UTF-32 is fixed.
continue;
}
Charset cs = Charset.forName(charsetName);
assertNotNull(cs.newDecoder());
if (cs.canEncode()) {
CharsetEncoder enc = cs.newEncoder();
assertNotNull(enc);
assertNotNull(enc.replacement());
}
}
}
use of java.nio.charset.CharsetEncoder in project robovm by robovm.
the class OldOutputStreamWriterTest method test_ConstructorLjava_io_OutputStreamLjava_nio_charset_CharsetEncoder.
public void test_ConstructorLjava_io_OutputStreamLjava_nio_charset_CharsetEncoder() throws IOException {
OutputStreamWriter writer;
Support_OutputStream out = new Support_OutputStream();
Charset cs = Charset.forName("ascii");
CharsetEncoder enc = cs.newEncoder();
try {
writer = new OutputStreamWriter(null, enc);
fail("Test 1: NullPointerException expected.");
} catch (NullPointerException e) {
// Expected
}
try {
writer = new OutputStreamWriter(out, (CharsetEncoder) null);
fail("Test 2: NullPointerException expected.");
} catch (NullPointerException e) {
// Expected
}
writer = new OutputStreamWriter(out, cs);
assertEquals("Test 3: CharacterEncoder not set correctly. ", cs, Charset.forName(writer.getEncoding()));
writer.close();
}
Aggregations