use of org.apache.stanbol.enhancer.servicesapi.ContentSource in project stanbol by apache.
the class ContentSourceTest method checkStreamFromByteArraySource.
@Test
public void checkStreamFromByteArraySource() throws IOException {
ContentSource source = new ByteArraySource(DATA);
ByteArrayOutputStream out = new ByteArrayOutputStream();
IOUtils.copy(source.getStream(), out);
Assert.assertTrue(Arrays.equals(DATA, out.toByteArray()));
try {
source.getStream();
//multiple calls are supported -> is OK
} catch (RuntimeException e) {
//multiple calls are not supported -> illegal state
Assert.assertTrue(e instanceof IllegalStateException);
}
}
use of org.apache.stanbol.enhancer.servicesapi.ContentSource in project stanbol by apache.
the class ContentSourceTest method checkMediaTypeForStringSource.
@Test
public void checkMediaTypeForStringSource() throws IOException {
ContentSource source = new StringSource(TEST_STRING);
assertEquals(STRING_DEFAULT_MT, source.getMediaType());
source = new StringSource(TEST_STRING, null);
assertEquals(STRING_DEFAULT_MT, source.getMediaType());
source = new StringSource(TEST_STRING, UTF8, null);
assertEquals(STRING_DEFAULT_MT, source.getMediaType());
source = new StringSource(TEST_STRING, null, null);
assertEquals(STRING_DEFAULT_MT, source.getMediaType());
//this can be used to force the system default
source = new StringSource(TEST_STRING, Charset.defaultCharset(), null);
Map<String, String> mt = ContentItemHelper.parseMimeType(source.getMediaType());
assertEquals("text/plain", mt.get(null));
assertEquals(Charset.defaultCharset().name(), mt.get("charset"));
String OTHER_MT = "text/rtf";
source = new StringSource(TEST_STRING, OTHER_MT);
mt = ContentItemHelper.parseMimeType(source.getMediaType());
assertEquals(OTHER_MT, mt.get(null));
assertEquals(UTF8.name(), mt.get("charset"));
source = new StringSource(TEST_STRING, null, OTHER_MT);
mt = ContentItemHelper.parseMimeType(source.getMediaType());
assertEquals(OTHER_MT, mt.get(null));
assertEquals(UTF8.name(), mt.get("charset"));
Charset ISO8859_4 = Charset.forName("ISO-8859-4");
source = new StringSource(TEST_STRING, ISO8859_4, OTHER_MT);
mt = ContentItemHelper.parseMimeType(source.getMediaType());
assertEquals(OTHER_MT, mt.get(null));
assertEquals(ISO8859_4.name(), mt.get("charset"));
}
use of org.apache.stanbol.enhancer.servicesapi.ContentSource in project stanbol by apache.
the class BlobTest method testStringWithCustomCharset.
/**
* This tests that texts with custom charsets are converted to UTF-8.
* @throws IOException
*/
@Test
public void testStringWithCustomCharset() throws IOException {
String test = "Exámplê";
Charset ISO8859_4 = Charset.forName("ISO-8859-4");
//first via a StringSource
ContentSource cs = new StringSource(test, ISO8859_4, "text/plain");
Blob blob = createBlob(cs);
Assert.assertEquals("text/plain", blob.getMimeType());
Assert.assertTrue(blob.getParameter().containsKey("charset"));
Assert.assertEquals(ISO8859_4.name(), blob.getParameter().get("charset"));
//2nd via a ByteArray
byte[] data = test.getBytes(ISO8859_4);
cs = new ByteArraySource(data, "text/plain; charset=" + ISO8859_4.name());
blob = createBlob(cs);
Assert.assertEquals("text/plain", blob.getMimeType());
Assert.assertTrue(blob.getParameter().containsKey("charset"));
Assert.assertEquals(ISO8859_4.name(), blob.getParameter().get("charset"));
//3rd as Stream
cs = new StreamSource(new ByteArrayInputStream(data), "text/plain; charset=" + ISO8859_4.name());
blob = createBlob(cs);
Assert.assertEquals("text/plain", blob.getMimeType());
Assert.assertTrue(blob.getParameter().containsKey("charset"));
Assert.assertEquals(ISO8859_4.name(), blob.getParameter().get("charset"));
cs = new StreamSource(new ByteArrayInputStream(data), "text/plain; " + ISO8859_4.name());
}
use of org.apache.stanbol.enhancer.servicesapi.ContentSource in project stanbol by apache.
the class BlobTest method testString.
/**
* Tests correct handling of UTF-8 as default charset
* @throws IOException
*/
@Test
public void testString() throws IOException {
String test = "Exámplê";
//first via a StringSource
ContentSource cs = new StringSource(test);
Blob blob = createBlob(cs);
Assert.assertEquals("text/plain", blob.getMimeType());
Assert.assertTrue(blob.getParameter().containsKey("charset"));
Assert.assertEquals(UTF8.name(), blob.getParameter().get("charset"));
String value = new String(IOUtils.toByteArray(blob.getStream()), UTF8);
Assert.assertEquals(test, value);
}
use of org.apache.stanbol.enhancer.servicesapi.ContentSource in project stanbol by apache.
the class ContentSourceTest method checkDataFromStringSource.
@Test
public void checkDataFromStringSource() throws IOException {
ContentSource source = new ByteArraySource(DATA);
Assert.assertTrue(Arrays.equals(DATA, source.getData()));
//multiple calls must work
source.getData();
}
Aggregations