Package org.apache.stanbol.enhancer.servicesapi

Examples of org.apache.stanbol.enhancer.servicesapi.ContentSource


    @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"));
View Full Code Here


    @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
View Full Code Here

        source = new StreamSource(new ByteArrayInputStream(DATA),MT_WITH_PARAM,FILE_NAME,HEADERS);
        assertEquals(MT_WITH_PARAM, source.getMediaType());
    }
    @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());
    }
View Full Code Here

        source = new ByteArraySource(DATA,MT_WITH_PARAM,FILE_NAME,HEADERS);
        assertEquals(MT_WITH_PARAM, source.getMediaType());
    }
    @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"));
    }
View Full Code Here

        assertEquals(ISO8859_4.name(), mt.get("charset"));
    }

    @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());
       
    }
View Full Code Here

       
    }

    @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());
    }
View Full Code Here

    @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);
    }
View Full Code Here

    /*
     * 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);
        }
View Full Code Here

            Assert.assertTrue(e instanceof IllegalStateException);
        }
    }
    @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();
    }
View Full Code Here

        source.getData();
    }

    @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);
        }
View Full Code Here

TOP

Related Classes of org.apache.stanbol.enhancer.servicesapi.ContentSource

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.