Search in sources :

Example 1 with ContentSource

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

the class ContentReferenceTest method testUrlReference.

@Test
public void testUrlReference() throws IOException {
    ContentReference ref = new UrlReference(testURL);
    assertNotNull(ref);
    assertEquals(ref.getReference(), testURL.toString());
    ContentSource source = ref.dereference();
    assertNotNull(source);
    String content = IOUtils.toString(source.getStream(), "UTF-8");
    assertNotNull(content);
    assertEquals(TEST_RESOURCE_CONTENT, content);
    //same as above, but by using ContentSource.getData() instead of
    //ContentSource.getStream()
    ref = new UrlReference(testURL);
    assertNotNull(ref);
    assertEquals(ref.getReference(), testURL.toString());
    source = ref.dereference();
    assertNotNull(source);
    content = new String(source.getData(), "UTF-8");
    assertNotNull(content);
    assertEquals(TEST_RESOURCE_CONTENT, content);
    //test the constructor that takes a String
    ref = new UrlReference(testURL.toString());
    assertNotNull(ref);
    assertEquals(ref.getReference(), testURL.toString());
    source = ref.dereference();
    assertNotNull(source);
    content = IOUtils.toString(source.getStream(), "UTF-8");
    assertNotNull(content);
    assertEquals(TEST_RESOURCE_CONTENT, content);
}
Also used : UrlReference(org.apache.stanbol.enhancer.servicesapi.impl.UrlReference) ContentSource(org.apache.stanbol.enhancer.servicesapi.ContentSource) ContentReference(org.apache.stanbol.enhancer.servicesapi.ContentReference) Test(org.junit.Test)

Example 2 with ContentSource

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

the class ContentSourceTest method checkMediaTypeForByteArraySource.

@Test
public void checkMediaTypeForByteArraySource() throws IOException {
    ContentSource source = new ByteArraySource(DATA);
    assertEquals(DEFAULT_MT, source.getMediaType());
    source = new ByteArraySource(DATA, null);
    assertEquals(DEFAULT_MT, source.getMediaType());
    source = new ByteArraySource(DATA, null, FILE_NAME, HEADERS);
    assertEquals(DEFAULT_MT, source.getMediaType());
    source = new ByteArraySource(DATA, MT);
    assertEquals(MT, source.getMediaType());
    source = new ByteArraySource(DATA, MT, FILE_NAME, HEADERS);
    assertEquals(MT, source.getMediaType());
    //Parameters MUST BE preserved!
    source = new ByteArraySource(DATA, MT_WITH_PARAM);
    assertEquals(MT_WITH_PARAM, source.getMediaType());
    source = new ByteArraySource(DATA, MT_WITH_PARAM, FILE_NAME);
    assertEquals(MT_WITH_PARAM, source.getMediaType());
    source = new ByteArraySource(DATA, MT_WITH_PARAM, FILE_NAME, HEADERS);
    assertEquals(MT_WITH_PARAM, source.getMediaType());
}
Also used : ContentSource(org.apache.stanbol.enhancer.servicesapi.ContentSource) ByteArraySource(org.apache.stanbol.enhancer.servicesapi.impl.ByteArraySource) Test(org.junit.Test)

Example 3 with ContentSource

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

the class ContentSourceTest method checkHeaders.

@Test
public void checkHeaders() throws IOException {
    ContentSource source = new StreamSource(new ByteArrayInputStream(DATA), null, null, null);
    assertNotNull(source.getHeaders());
    assertTrue(source.getHeaders().isEmpty());
    source = new StreamSource(new ByteArrayInputStream(DATA), null, null, HEADERS);
    assertEquals(HEADERS, source.getHeaders());
    source = new ByteArraySource(DATA, null, null, null);
    assertNotNull(source.getHeaders());
    assertTrue(source.getHeaders().isEmpty());
    source = new ByteArraySource(DATA, null, null, HEADERS);
    assertEquals(HEADERS, source.getHeaders());
}
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 4 with ContentSource

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

the class ContentSourceTest method checkDataFromByteArraySource.

@Test
public void checkDataFromByteArraySource() throws IOException {
    ContentSource source = new ByteArraySource(DATA);
    assertTrue(Arrays.equals(DATA, source.getData()));
    //also check that the array is not copied
    //Also checks multiple calls to getData MUST work
    assertSame(DATA, source.getData());
}
Also used : ContentSource(org.apache.stanbol.enhancer.servicesapi.ContentSource) ByteArraySource(org.apache.stanbol.enhancer.servicesapi.impl.ByteArraySource) Test(org.junit.Test)

Example 5 with ContentSource

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

the class ContentSourceTest method checkMediaTypeForStreamSource.

/*
     * Tests checking correct handling of parameters and default values
     */
@Test
public void checkMediaTypeForStreamSource() throws IOException {
    ContentSource source = new StreamSource(new ByteArrayInputStream(DATA));
    assertEquals(DEFAULT_MT, source.getMediaType());
    source = new StreamSource(new ByteArrayInputStream(DATA), null);
    assertEquals(DEFAULT_MT, source.getMediaType());
    source = new StreamSource(new ByteArrayInputStream(DATA), null, HEADERS);
    assertEquals(DEFAULT_MT, source.getMediaType());
    source = new StreamSource(new ByteArrayInputStream(DATA), null, FILE_NAME, HEADERS);
    assertEquals(DEFAULT_MT, source.getMediaType());
    source = new StreamSource(new ByteArrayInputStream(DATA), MT);
    assertEquals(MT, source.getMediaType());
    source = new StreamSource(new ByteArrayInputStream(DATA), MT, HEADERS);
    assertEquals(MT, source.getMediaType());
    source = new StreamSource(new ByteArrayInputStream(DATA), MT, FILE_NAME, HEADERS);
    assertEquals(MT, source.getMediaType());
    //Parameters MUST BE preserved!
    source = new StreamSource(new ByteArrayInputStream(DATA), MT_WITH_PARAM);
    assertEquals(MT_WITH_PARAM, source.getMediaType());
    source = new StreamSource(new ByteArrayInputStream(DATA), MT_WITH_PARAM, HEADERS);
    assertEquals(MT_WITH_PARAM, source.getMediaType());
    source = new StreamSource(new ByteArrayInputStream(DATA), MT_WITH_PARAM, FILE_NAME, HEADERS);
    assertEquals(MT_WITH_PARAM, source.getMediaType());
}
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)

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