Search in sources :

Example 6 with ContentSource

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);
    }
}
Also used : ContentSource(org.apache.stanbol.enhancer.servicesapi.ContentSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArraySource(org.apache.stanbol.enhancer.servicesapi.impl.ByteArraySource) Test(org.junit.Test)

Example 7 with ContentSource

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"));
}
Also used : ContentSource(org.apache.stanbol.enhancer.servicesapi.ContentSource) Charset(java.nio.charset.Charset) StringSource(org.apache.stanbol.enhancer.servicesapi.impl.StringSource) Test(org.junit.Test)

Example 8 with ContentSource

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());
}
Also used : ContentSource(org.apache.stanbol.enhancer.servicesapi.ContentSource) Blob(org.apache.stanbol.enhancer.servicesapi.Blob) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamSource(org.apache.stanbol.enhancer.servicesapi.impl.StreamSource) Charset(java.nio.charset.Charset) StringSource(org.apache.stanbol.enhancer.servicesapi.impl.StringSource) ByteArraySource(org.apache.stanbol.enhancer.servicesapi.impl.ByteArraySource) Test(org.junit.Test)

Example 9 with ContentSource

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);
}
Also used : ContentSource(org.apache.stanbol.enhancer.servicesapi.ContentSource) Blob(org.apache.stanbol.enhancer.servicesapi.Blob) StringSource(org.apache.stanbol.enhancer.servicesapi.impl.StringSource) Test(org.junit.Test)

Example 10 with ContentSource

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();
}
Also used : ContentSource(org.apache.stanbol.enhancer.servicesapi.ContentSource) ByteArraySource(org.apache.stanbol.enhancer.servicesapi.impl.ByteArraySource) Test(org.junit.Test)

Aggregations

ContentSource (org.apache.stanbol.enhancer.servicesapi.ContentSource)14 Test (org.junit.Test)14 ByteArraySource (org.apache.stanbol.enhancer.servicesapi.impl.ByteArraySource)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 StreamSource (org.apache.stanbol.enhancer.servicesapi.impl.StreamSource)6 StringSource (org.apache.stanbol.enhancer.servicesapi.impl.StringSource)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Charset (java.nio.charset.Charset)3 Blob (org.apache.stanbol.enhancer.servicesapi.Blob)2 ContentReference (org.apache.stanbol.enhancer.servicesapi.ContentReference)1 UrlReference (org.apache.stanbol.enhancer.servicesapi.impl.UrlReference)1