@Test public void textToTextConversion() { String source = "All those moments will be lost in time, like tears in rain."; byte[] sourceAs8859 = source.getBytes(ISO_8859_1); byte[] sourceAsASCII = source.getBytes(US_ASCII); Object result = StandardConversions.convertTextToText(sourceAs8859, TEXT_PLAIN.withCharset(ISO_8859_1), TEXT_PLAIN.withCharset(US_ASCII)); assertArrayEquals(sourceAsASCII, (byte[]) result); }
@Test public void testOctetStreamToTextConversion() { String text = "Have you ever retired a human by mistake?"; byte[] bytes1 = text.getBytes(); byte[] bytes2 = new byte[]{1, 2, 3}; byte[] result1 = StandardConversions.convertOctetStreamToText(bytes1, TEXT_PLAIN.withCharset(US_ASCII)); byte[] result2 = StandardConversions.convertOctetStreamToText(bytes2, TEXT_PLAIN); assertArrayEquals(text.getBytes(US_ASCII), result1); assertArrayEquals(new String(bytes2).getBytes(UTF_8), result2); }
@Test public void testTextToURLEncodedConversion() throws UnsupportedEncodingException { String source = "They're either a benefit or a hazard. If they're a benefit, it's not my problem."; String result = StandardConversions.convertTextToUrlEncoded(source, TEXT_PLAIN.withCharset(UTF_16)); assertEquals(URLEncoder.encode(source, "UTF-16"), result); }
@Test public void testJavaToTextConversion() { String string = "I've seen things you people wouldn't believe."; Double number = 12.1d; Calendar complex = Calendar.getInstance(); MediaType stringType = APPLICATION_OBJECT.withParameter("type", "java.lang.String"); byte[] result1 = StandardConversions.convertJavaToText(string, stringType, TEXT_PLAIN.withCharset(UTF_16BE)); assertArrayEquals(string.getBytes(UTF_16BE), result1); MediaType doubleType = APPLICATION_OBJECT.withParameter("type", "java.lang.Double"); byte[] result2 = StandardConversions.convertJavaToText(number, doubleType, TEXT_PLAIN.withCharset(US_ASCII)); assertArrayEquals("12.1".getBytes(US_ASCII), result2); MediaType customType = APPLICATION_OBJECT.withParameter("type", complex.getClass().getName()); byte[] bytes = StandardConversions.convertJavaToText(complex, customType, TEXT_PLAIN.withCharset(US_ASCII)); assertEquals(complex.toString(), new String(bytes)); }
@Test public void testTextToObjectConversion() { String source = "Can the maker repair what he makes?"; String source2 = "I had your job once. I was good at it."; byte[] sourceBytes = source2.getBytes(US_ASCII); Object result = StandardConversions.convertTextToObject(source, APPLICATION_OBJECT); Object result2 = StandardConversions.convertTextToObject(sourceBytes, TEXT_PLAIN.withCharset(US_ASCII)); assertEquals(source, result); assertEquals(source2, result2); }
@Test public void testEmptyContent() { byte[] empty = Util.EMPTY_BYTE_ARRAY; assertArrayEquals(empty, (byte[]) transcoder.transcode(empty, APPLICATION_JSON, APPLICATION_JSON.withCharset(US_ASCII))); assertArrayEquals(empty, (byte[]) transcoder.transcode(empty, APPLICATION_UNKNOWN, APPLICATION_JSON)); assertArrayEquals(empty, (byte[]) transcoder.transcode(empty, APPLICATION_OCTET_STREAM, APPLICATION_JSON)); assertArrayEquals(empty, (byte[]) transcoder.transcode(empty, TEXT_PLAIN, APPLICATION_JSON)); assertArrayEquals(empty, (byte[]) transcoder.transcode(empty, APPLICATION_OBJECT, APPLICATION_JSON)); }
@Test public void testCharset() { Charset korean = Charset.forName("EUC-KR"); MediaType textPlainKorean = TEXT_PLAIN.withCharset(korean); MediaType jsonKorean = APPLICATION_JSON.withCharset(korean); MediaType textPlainAsString = TEXT_PLAIN.withClassType(String.class); MediaType jsonAsString = APPLICATION_JSON.withClassType(String.class);