Search in sources :

Example 11 with ContentSource

use of org.apache.stanbol.enhancer.servicesapi.ContentSource in project stanbol by apache.

the class ContentSourceTest method checkStreamFromStreamSource.

/*
     * Tests checking correct handling of data
     */
@Test
public void checkStreamFromStreamSource() throws IOException {
    ContentSource source = new StreamSource(new ByteArrayInputStream(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) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamSource(org.apache.stanbol.enhancer.servicesapi.impl.StreamSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 12 with ContentSource

use of org.apache.stanbol.enhancer.servicesapi.ContentSource in project stanbol by apache.

the class ContentSourceTest method checkDataFromStreamSource.

@Test
public void checkDataFromStreamSource() throws IOException {
    ContentSource source = new StreamSource(new ByteArrayInputStream(DATA));
    Assert.assertTrue(Arrays.equals(DATA, source.getData()));
    //multiple calls must work
    source.getData();
}
Also used : ContentSource(org.apache.stanbol.enhancer.servicesapi.ContentSource) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamSource(org.apache.stanbol.enhancer.servicesapi.impl.StreamSource) Test(org.junit.Test)

Example 13 with ContentSource

use of org.apache.stanbol.enhancer.servicesapi.ContentSource in project stanbol by apache.

the class ContentSourceTest method checkFileName.

@Test
public void checkFileName() throws IOException {
    ContentSource source = new StreamSource(new ByteArrayInputStream(DATA), null, null, null);
    assertNull(source.getFileName());
    source = new StreamSource(new ByteArrayInputStream(DATA), null, FILE_NAME, null);
    assertEquals(FILE_NAME, source.getFileName());
    source = new ByteArraySource(DATA, null, FILE_NAME);
    assertEquals(FILE_NAME, source.getFileName());
    source = new ByteArraySource(DATA, null, FILE_NAME, null);
    assertEquals(FILE_NAME, source.getFileName());
}
Also used : ContentSource(org.apache.stanbol.enhancer.servicesapi.ContentSource) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamSource(org.apache.stanbol.enhancer.servicesapi.impl.StreamSource) ByteArraySource(org.apache.stanbol.enhancer.servicesapi.impl.ByteArraySource) Test(org.junit.Test)

Example 14 with ContentSource

use of org.apache.stanbol.enhancer.servicesapi.ContentSource in project stanbol by apache.

the class ContentSourceTest method checkStreamFromStringSource.

@Test
public void checkStreamFromStringSource() throws IOException {
    ContentSource source = new StringSource(TEST_STRING);
    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);
    }
    //test different encoding
    Charset ISO8859_4 = Charset.forName("ISO-8859-4");
    byte[] iso8859_4_data = TEST_STRING.getBytes(ISO8859_4);
    source = new StringSource(TEST_STRING, ISO8859_4, null);
    out = new ByteArrayOutputStream();
    IOUtils.copy(source.getStream(), out);
    Assert.assertTrue(Arrays.equals(iso8859_4_data, out.toByteArray()));
}
Also used : ContentSource(org.apache.stanbol.enhancer.servicesapi.ContentSource) Charset(java.nio.charset.Charset) StringSource(org.apache.stanbol.enhancer.servicesapi.impl.StringSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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