You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by rw...@apache.org on 2012/04/08 17:14:54 UTC
svn commit: r1311021 [2/2] - in /incubator/stanbol/trunk/enhancer/generic:
core/src/main/resources/OSGI-INF/metatype/
core/src/test/java/org/apache/stanbol/enhancer/contentitem/inmemory/
rdfentities/src/main/java/org/apache/stanbol/enhancer/rdfentities...
Added: incubator/stanbol/trunk/enhancer/generic/test/src/main/java/org/apache/stanbol/enhancer/test/BlobTest.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/generic/test/src/main/java/org/apache/stanbol/enhancer/test/BlobTest.java?rev=1311021&view=auto
==============================================================================
--- incubator/stanbol/trunk/enhancer/generic/test/src/main/java/org/apache/stanbol/enhancer/test/BlobTest.java (added)
+++ incubator/stanbol/trunk/enhancer/generic/test/src/main/java/org/apache/stanbol/enhancer/test/BlobTest.java Sun Apr 8 15:14:53 2012
@@ -0,0 +1,233 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.stanbol.enhancer.test;
+
+import static org.apache.stanbol.enhancer.servicesapi.impl.StringSource.UTF8;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.nio.charset.Charset;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.stanbol.enhancer.servicesapi.Blob;
+import org.apache.stanbol.enhancer.servicesapi.ContentSource;
+import org.apache.stanbol.enhancer.servicesapi.helper.ContentItemHelper;
+import org.apache.stanbol.enhancer.servicesapi.impl.ByteArraySource;
+import org.apache.stanbol.enhancer.servicesapi.impl.StreamSource;
+import org.apache.stanbol.enhancer.servicesapi.impl.StringSource;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test class intended to be extended by UnitTest classes for specific {@link Blob}
+ * implementations. This class tests if parsed mime-types are handled correctly.
+ * It does not test the actual handling of the data, because this is considered
+ * specific for each Blob implementation.<p>
+ * The {@link #createBlob(String)} MUST BE implemented to use
+ * the generic unit tests defined by this class.<p>
+ * <b>NOTE:</b>: {@link Blob} implementation can use the
+ * {@link ContentItemHelper#parseMimeType(String)} method for parsing
+ * mime-type string.
+ */
+public abstract class BlobTest {
+
+ private static byte[] content = "This is a test".getBytes(Charset.forName("UTF-8"));
+
+ /**
+ * Getter used to get the Blob to test mime-type handling. The content is
+ * not used for such tests and may be set to anything.
+ * @param mimeType the mimetype
+ * @return
+ */
+ protected abstract Blob createBlob(ContentSource cs) throws IOException;
+ /**
+ * Internally used to create {@link ContentSource} instanced for the
+ * MimeType parsing tests
+ */
+ private static ContentSource createContentSource(String mimeType){
+ return new ByteArraySource(content, mimeType);
+ }
+
+
+ @Test(expected=IllegalArgumentException.class)
+ public void testEmptyMimeType() throws IOException {
+ createBlob(createContentSource(""));
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void testWildcardType() throws IOException {
+ createBlob(createContentSource("*/*;charset=UTF-8"));
+ }
+ @Test(expected=IllegalArgumentException.class)
+ public void testWildcardSubType() throws IOException {
+ createBlob(createContentSource("text/*;charset=UTF-8"));
+ }
+ @Test(expected=IllegalArgumentException.class)
+ public void testEmptyMimetype() throws IOException {
+ createBlob(createContentSource(";charset=UTF-8"));
+ }
+ @Test(expected=IllegalArgumentException.class)
+ public void testMissingSubType() throws IOException {
+ createBlob(createContentSource("text;charset=UTF-8"));
+ }
+ @Test(expected=IllegalArgumentException.class)
+ public void testEmptyType() throws IOException {
+ createBlob(createContentSource("/plain;charset=UTF-8"));
+ }
+ @Test(expected=IllegalArgumentException.class)
+ public void testEmptySubType() throws IOException {
+ createBlob(createContentSource("text/;charset=UTF-8"));
+ }
+
+ @Test
+ public void testMimeType() throws IOException {
+ Blob blob = createBlob(createContentSource("text/plain;charset=UTF-8"));
+ Assert.assertEquals("text/plain", blob.getMimeType());
+ Assert.assertTrue(blob.getParameter().containsKey("charset"));
+ Assert.assertEquals("UTF-8", blob.getParameter().get("charset"));
+
+ blob = createBlob(createContentSource("text/plain;charset=UTF-8;other=test"));
+ Assert.assertEquals("text/plain", blob.getMimeType());
+ Assert.assertTrue(blob.getParameter().containsKey("charset"));
+ Assert.assertEquals("UTF-8", blob.getParameter().get("charset"));
+ Assert.assertTrue(blob.getParameter().containsKey("other"));
+ Assert.assertEquals("test", blob.getParameter().get("other"));
+ }
+ @Test
+ public void testMultipleSeparators() throws IOException {
+ Blob blob = createBlob(createContentSource("text/plain;;charset=UTF-8"));
+ Assert.assertEquals("text/plain", blob.getMimeType());
+ Assert.assertTrue(blob.getParameter().containsKey("charset"));
+ Assert.assertEquals("UTF-8", blob.getParameter().get("charset"));
+
+ blob = createBlob(createContentSource("text/plain;charset=UTF-8;;other=test"));
+ Assert.assertEquals("text/plain", blob.getMimeType());
+ Assert.assertTrue(blob.getParameter().containsKey("charset"));
+ Assert.assertEquals("UTF-8", blob.getParameter().get("charset"));
+ Assert.assertTrue(blob.getParameter().containsKey("other"));
+ Assert.assertEquals("test", blob.getParameter().get("other"));
+ }
+ @Test
+ public void testIllegalFormatedParameter() throws IOException {
+ Blob blob = createBlob(createContentSource("text/plain;=UTF-8"));
+ Assert.assertEquals("text/plain", blob.getMimeType());
+ Assert.assertTrue(blob.getParameter().isEmpty());
+
+ blob = createBlob(createContentSource("text/plain;charset=UTF-8;=illegal"));
+ Assert.assertEquals("text/plain", blob.getMimeType());
+ Assert.assertEquals(blob.getParameter().size(),1);
+ Assert.assertTrue(blob.getParameter().containsKey("charset"));
+ Assert.assertEquals("UTF-8", blob.getParameter().get("charset"));
+
+ blob = createBlob(createContentSource("text/plain;=illegal;charset=UTF-8"));
+ Assert.assertEquals("text/plain", blob.getMimeType());
+ Assert.assertEquals(blob.getParameter().size(),1);
+ Assert.assertTrue(blob.getParameter().containsKey("charset"));
+ Assert.assertEquals("UTF-8", blob.getParameter().get("charset"));
+
+ blob = createBlob(createContentSource("text/plain;charset="));
+ Assert.assertEquals("text/plain", blob.getMimeType());
+ Assert.assertTrue(blob.getParameter().isEmpty());
+ blob = createBlob(createContentSource("text/plain;charset"));
+ Assert.assertEquals("text/plain", blob.getMimeType());
+ Assert.assertTrue(blob.getParameter().isEmpty());
+
+ blob = createBlob(createContentSource("text/plain;charset=UTF-8;test="));
+ Assert.assertEquals("text/plain", blob.getMimeType());
+ Assert.assertEquals(blob.getParameter().size(),1);
+ Assert.assertTrue(blob.getParameter().containsKey("charset"));
+ Assert.assertEquals("UTF-8", blob.getParameter().get("charset"));
+
+ blob = createBlob(createContentSource("text/plain;charset=UTF-8;test"));
+ Assert.assertEquals("text/plain", blob.getMimeType());
+ Assert.assertEquals(blob.getParameter().size(),1);
+ Assert.assertTrue(blob.getParameter().containsKey("charset"));
+ Assert.assertEquals("UTF-8", blob.getParameter().get("charset"));
+
+ blob = createBlob(createContentSource("text/plain;test;charset=UTF-8;"));
+ Assert.assertEquals("text/plain", blob.getMimeType());
+ Assert.assertEquals(blob.getParameter().size(),1);
+ Assert.assertTrue(blob.getParameter().containsKey("charset"));
+ Assert.assertEquals("UTF-8", blob.getParameter().get("charset"));
+ }
+ @Test(expected=UnsupportedOperationException.class)
+ public void testReadOnlyParameter() throws IOException {
+ Blob blob = createBlob(createContentSource("text/plain;test;charset=UTF-8"));
+ blob.getParameter().put("test", "dummy");
+ }
+ /**
+ * 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);
+ }
+ /**
+ * 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());
+ }
+ /**
+ * Tests the default mimeType "application/octet-stream" for binary data.
+ * @throws IOException
+ */
+ @Test
+ public void testDefaultBinaryMimeType() throws IOException {
+ Blob blob = createBlob(new ByteArraySource("dummy".getBytes(UTF8)));
+ Assert.assertEquals("application/octet-stream", blob.getMimeType());
+ Assert.assertTrue(blob.getParameter().isEmpty());
+
+ blob = createBlob(new StreamSource(new ByteArrayInputStream("dummy".getBytes(UTF8))));
+ Assert.assertEquals("application/octet-stream", blob.getMimeType());
+ Assert.assertTrue(blob.getParameter().isEmpty());
+ }
+}
Propchange: incubator/stanbol/trunk/enhancer/generic/test/src/main/java/org/apache/stanbol/enhancer/test/BlobTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: incubator/stanbol/trunk/enhancer/generic/test/src/main/java/org/apache/stanbol/enhancer/test/ContentItemFactoryTest.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/generic/test/src/main/java/org/apache/stanbol/enhancer/test/ContentItemFactoryTest.java?rev=1311021&view=auto
==============================================================================
--- incubator/stanbol/trunk/enhancer/generic/test/src/main/java/org/apache/stanbol/enhancer/test/ContentItemFactoryTest.java (added)
+++ incubator/stanbol/trunk/enhancer/generic/test/src/main/java/org/apache/stanbol/enhancer/test/ContentItemFactoryTest.java Sun Apr 8 15:14:53 2012
@@ -0,0 +1,303 @@
+package org.apache.stanbol.enhancer.test;
+
+import static junit.framework.Assert.assertNull;
+import static junit.framework.Assert.assertSame;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.charset.Charset;
+
+import junit.framework.Assert;
+
+import org.apache.clerezza.rdf.core.MGraph;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.rdf.core.impl.PlainLiteralImpl;
+import org.apache.clerezza.rdf.core.impl.SimpleMGraph;
+import org.apache.clerezza.rdf.core.impl.TripleImpl;
+import org.apache.commons.io.IOUtils;
+import org.apache.stanbol.enhancer.servicesapi.Blob;
+import org.apache.stanbol.enhancer.servicesapi.ContentItem;
+import org.apache.stanbol.enhancer.servicesapi.ContentItemFactory;
+import org.apache.stanbol.enhancer.servicesapi.ContentReference;
+import org.apache.stanbol.enhancer.servicesapi.ContentSink;
+import org.apache.stanbol.enhancer.servicesapi.ContentSource;
+import org.apache.stanbol.enhancer.servicesapi.impl.StringSource;
+import org.apache.stanbol.enhancer.servicesapi.rdf.Enhancer;
+import org.apache.stanbol.enhancer.servicesapi.rdf.Properties;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * This UnitTest MUST BE passed by all {@link ContentItemFactory} implementations.
+ * It only checks the creation of {@link ContentItem}s and {@link Blob}s. It
+ * does not actually test the ContentItem and Blob implementations as those
+ * need to pass {@link ContentItemTest} and {@link BlobTest} respectively.
+ *
+ * @author Rupert Westenthaler
+ */
+public abstract class ContentItemFactoryTest {
+
+ /**
+ * Internally used to test {@link ContentReference} constructors
+ */
+ private static ContentReference TEST_CR = new ContentReference() {
+ ContentSource source = new StringSource("Dummy Reference Content");
+ @Override
+ public String getReference() {
+ return "urn:dummy.reference";
+ }
+
+ @Override
+ public ContentSource dereference() throws IOException {
+ return source;
+ }
+ };
+ /**
+ * Internally used to test {@link ContentSource} constructors
+ */
+ private static ContentSource TEST_CS = new StringSource("Dummy Content");
+ /**
+ * The prefix used for testing
+ */
+ private static String PREFIX = "http://www.example.com/prefix#";
+ /**
+ * The {@link ContentItem#getUri() ID} used for testing
+ */
+ private static UriRef ID = new UriRef("http://www.example.com/content-items#12345");
+ /**
+ * Graph used to test of parsed metadata are preserved
+ */
+ private static MGraph METADATA = new SimpleMGraph();
+ static {
+ METADATA.add(new TripleImpl(ID, Properties.RDF_TYPE, Enhancer.CONTENT_ITEM));
+ METADATA.add(new TripleImpl(ID, Properties.RDFS_LABEL, new PlainLiteralImpl("Test ContentItem")));
+ }
+ /**
+ * Member variable for the {@link ContentItemFactory} instance used for
+ * testing. This is initialise {@link Before} the first unit test is
+ * esecuted.
+ */
+ protected ContentItemFactory contentItemFactory;
+
+ /**
+ * This method is assumed to return an new ContentItemFactory instance on
+ * every call. Currently it is called only once in the {@link Before} state
+ * of the first test.<p>
+ * Subclasses can use the {@link #contentItemFactory} member
+ * variable in additional tests.
+ * @return the {@link ContentItemFactory} instance used for the tests.
+ */
+ protected abstract ContentItemFactory createContentItemFactory() throws IOException ;
+
+ /**
+ * Initialises the {@link #contentItemFactory} {@link Before} the first
+ * unit test is executed. Needs not to be manually called by test methods.
+ */
+ @Before
+ public void initFactory() throws IOException {
+ if(contentItemFactory == null){
+ contentItemFactory = createContentItemFactory();
+ assertNotNull("Unable to create ContentItemFactory instance",
+ contentItemFactory);
+ }
+ }
+
+ /*
+ * Set of tests to test that IllegalArgumentExceptions are
+ * thrown if null is parsed as ContentSource to the various
+ * createContentItem methods
+ */
+ @Test(expected=IllegalArgumentException.class)
+ public void missingCiContentSource() throws IOException{
+ contentItemFactory.createContentItem((ContentSource)null);
+ }
+ @Test(expected=IllegalArgumentException.class)
+ public void missingCiContentSource2() throws IOException{
+ contentItemFactory.createContentItem(PREFIX,null);
+ }
+ @Test(expected=IllegalArgumentException.class)
+ public void missingCiContentSource3() throws IOException{
+ contentItemFactory.createContentItem(ID,null);
+ }
+ @Test(expected=IllegalArgumentException.class)
+ public void missingCiContentSource4() throws IOException{
+ contentItemFactory.createContentItem(ID,null,new SimpleMGraph());
+ }
+ @Test(expected=IllegalArgumentException.class)
+ public void missingCiContentSource5() throws IOException{
+ contentItemFactory.createContentItem(PREFIX,null,new SimpleMGraph());
+ }
+ /*
+ * Set of tests to test that IllegalArgumentExceptions are
+ * thrown if null is parsed as ContentReference to the various
+ * createContentItem methods
+ */
+ @Test(expected=IllegalArgumentException.class)
+ public void missingCiContentReference() throws IOException{
+ contentItemFactory.createContentItem((ContentReference)null);
+ }
+ @Test(expected=IllegalArgumentException.class)
+ public void missingCiContentReference2() throws IOException{
+ contentItemFactory.createContentItem(null,new SimpleMGraph());
+ }
+ /*
+ * Set of tests to test that IllegalArgumentExceptions are
+ * thrown if null is parsed to the createBlob methods.
+ */
+ @Test(expected=IllegalArgumentException.class)
+ public void missingBlobContentReference() throws IOException{
+ contentItemFactory.createBlob((ContentReference)null);
+ }
+ @Test(expected=IllegalArgumentException.class)
+ public void missingBlobContentSource() throws IOException{
+ contentItemFactory.createBlob((ContentSource)null);
+ }
+ /*
+ * Set of tests to test that IllegalArgumentExceptions if
+ * null is parsed as prefix
+ */
+ @Test(expected=IllegalArgumentException.class)
+ public void missingCiPrefix() throws IOException{
+ contentItemFactory.createContentItem((String)null,TEST_CS);
+ }
+ @Test(expected=IllegalArgumentException.class)
+ public void missingCiPrefix2() throws IOException{
+ contentItemFactory.createContentItem((String)null,TEST_CS,new SimpleMGraph());
+ }
+ /**
+ * Test that the generated ID starts with the parsed prefix
+ */
+ @Test
+ public void testPrefix() throws IOException{
+ ContentItem ci = contentItemFactory.createContentItem(PREFIX, TEST_CS);
+ assertNotNull(ci);
+ assertNotNull(ci.getUri());
+ assertTrue("The ID of the created ContentItem MUST start with the parsed prefix",
+ ci.getUri().getUnicodeString().startsWith(PREFIX));
+
+ ci = contentItemFactory.createContentItem(PREFIX, TEST_CS,new SimpleMGraph());
+ assertNotNull(ci);
+ assertNotNull(ci.getUri());
+ assertTrue("The ID of the created ContentItem MUST start with the parsed prefix",
+ ci.getUri().getUnicodeString().startsWith(PREFIX));
+ }
+ /**
+ * Test that the parsed URI is used as ID of the ContentItem
+ */
+ @Test
+ public void testURI() throws IOException {
+ ContentItem ci = contentItemFactory.createContentItem(ID, TEST_CS);
+ assertNotNull(ci);
+ assertNotNull(ci.getUri());
+ assertTrue("The ID of the created ContentItem MUST be equals to the parsed ID",
+ ci.getUri().equals(ID));
+
+ ci = contentItemFactory.createContentItem(ID, TEST_CS,new SimpleMGraph());
+ assertNotNull(ci);
+ assertNotNull(ci.getUri());
+ assertTrue("The ID of the created ContentItem MUST be equals to the parsed ID",
+ ci.getUri().equals(ID));
+ }
+ /**
+ * Test the generation of valid IDs if no or <code>null</code> is parsed
+ * as id
+ */
+ @Test
+ public void testDefaultId() throws IOException {
+ ContentItem ci = contentItemFactory.createContentItem(TEST_CS);
+ assertNotNull(ci);
+ assertNotNull(ci.getUri());
+ ci = contentItemFactory.createContentItem((UriRef)null,TEST_CS);
+ assertNotNull(ci);
+ assertNotNull(ci.getUri());
+ ci = contentItemFactory.createContentItem((UriRef)null,TEST_CS, new SimpleMGraph());
+ assertNotNull(ci);
+ assertNotNull(ci.getUri());
+ }
+ /**
+ * Tests if the {@link ContentReference#getReference()} is used as ID for
+ * the contentItem
+ */
+ @Test
+ public void testContentReferenceId() throws IOException {
+ ContentItem ci = contentItemFactory.createContentItem(TEST_CR);
+ assertNotNull(ci);
+ assertNotNull(ci.getUri());
+ assertEquals(TEST_CR.getReference(),ci.getUri().getUnicodeString());
+
+ contentItemFactory.createContentItem(TEST_CR, new SimpleMGraph());
+ assertNotNull(ci);
+ assertNotNull(ci.getUri());
+ assertEquals(TEST_CR.getReference(),ci.getUri().getUnicodeString());
+ }
+ /**
+ * Tests if triples contained in parsed Metadata are also present within
+ * the {@link ContentItem#getMetadata()} graph
+ */
+ @Test
+ public void testParsedMetadata() throws IOException {
+ ContentItem ci = contentItemFactory.createContentItem(TEST_CR, METADATA);
+ assertNotNull(ci);
+ assertEquals("The created ContentItem MUST contain parsed metadata",
+ METADATA.size(), ci.getMetadata().size());
+
+ ci = contentItemFactory.createContentItem(ID,TEST_CS, METADATA);
+ assertNotNull(ci);
+ assertEquals("The created ContentItem MUST contain parsed metadata",
+ METADATA.size(), ci.getMetadata().size());
+
+ ci = contentItemFactory.createContentItem(PREFIX,TEST_CS, METADATA);
+ assertNotNull(ci);
+ assertEquals("The created ContentItem MUST contain parsed metadata",
+ METADATA.size(), ci.getMetadata().size());
+ }
+
+ @Test
+ public void testContentSink() throws IOException {
+ String mt = "text/plain";
+ Charset ISO8859_4 = Charset.forName("ISO-8859-4");
+ ContentSink cs = contentItemFactory.createContentSink(mt+"; charset="+ISO8859_4.name());
+ assertNotNull(cs);
+ assertNotNull(cs.getBlob());
+ OutputStream out = cs.getOutputStream();
+ assertNotNull(cs);
+ // multiple calls MUST return the same OutputStream!
+ assertSame(out, cs.getOutputStream());
+ //test mime type
+ assertNotNull(cs.getBlob().getMimeType());
+ //get MimeType MUST return the simple mime type
+ assertEquals(mt, cs.getBlob().getMimeType());
+ String charsetParam = cs.getBlob().getParameter().get("charset");
+ assertNotNull("expected charset parameter is missing!",charsetParam);
+ assertEquals(ISO8859_4.name(), charsetParam);
+
+ //now write some data to the sink
+ String TEST_CONTENT = "Thîs tésts wrîtîng to â ConténtSînk!";
+ //note this uses the same charset as parsed as charset in the
+ //constructor!
+ IOUtils.write(TEST_CONTENT, cs.getOutputStream(),ISO8859_4.name());
+ IOUtils.closeQuietly(cs.getOutputStream());
+ //now read the data from the blob
+ String content = IOUtils.toString(
+ cs.getBlob().getStream(),
+ charsetParam);
+ assertEquals(TEST_CONTENT, content);
+ }
+
+ @Test
+ public void testContentSinkDefaultMimeType() throws IOException {
+ String DEFAULT = "application/octet-stream";
+ ContentSink cs = contentItemFactory.createContentSink(null);
+ assertNotNull(cs);
+ assertNotNull(cs.getBlob());
+ assertNotNull(cs.getBlob().getMimeType());
+ //get MimeType MUST return the simple mime type
+ assertEquals(DEFAULT, cs.getBlob().getMimeType());
+ assertNull(cs.getBlob().getParameter().get("charset"));
+ }
+
+
+}
Propchange: incubator/stanbol/trunk/enhancer/generic/test/src/main/java/org/apache/stanbol/enhancer/test/ContentItemFactoryTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: incubator/stanbol/trunk/enhancer/generic/test/src/main/java/org/apache/stanbol/enhancer/test/ContentItemTest.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/generic/test/src/main/java/org/apache/stanbol/enhancer/test/ContentItemTest.java?rev=1311021&view=auto
==============================================================================
--- incubator/stanbol/trunk/enhancer/generic/test/src/main/java/org/apache/stanbol/enhancer/test/ContentItemTest.java (added)
+++ incubator/stanbol/trunk/enhancer/generic/test/src/main/java/org/apache/stanbol/enhancer/test/ContentItemTest.java Sun Apr 8 15:14:53 2012
@@ -0,0 +1,184 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.stanbol.enhancer.test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.util.Date;
+
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.rdf.core.impl.TripleImpl;
+import org.apache.stanbol.enhancer.servicesapi.Blob;
+import org.apache.stanbol.enhancer.servicesapi.ContentItem;
+import org.apache.stanbol.enhancer.servicesapi.ContentSource;
+import org.apache.stanbol.enhancer.servicesapi.NoSuchPartException;
+import org.apache.stanbol.enhancer.servicesapi.impl.StringSource;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public abstract class ContentItemTest {
+
+ private final Logger log = LoggerFactory.getLogger(ContentItemTest.class);
+ //private static final UriRef ciUri = new UriRef("http://example.org/");
+ private static final ContentSource contentSource = new StringSource("This is a Test!");
+ /**
+ * Used to create ContentItems used by this Test. Each call MUST return a
+ * newly initialised version.
+ * @param source The ContentSource
+ * @return the ContentItem used for a test
+ */
+ protected abstract ContentItem createContentItem(ContentSource source) throws IOException;
+ /**
+ * Used to create Blobs used by this Tests. Each call MUST return a new
+ * blob instance.
+ * @param source The {@link ContentSource} used to create the Blob
+ * @return the Blob
+ */
+ protected abstract Blob createBlob(ContentSource source) throws IOException;
+
+ @Test
+ public void addingAndRetrieving() throws IOException{
+ ContentItem ci = createContentItem(contentSource);
+ assertNotNull(ci);
+ assertNotNull(ci.getUri());
+ UriRef partUri = new UriRef("http://foo/");
+ Date someObject = new Date();
+ ci.addPart(partUri, someObject);
+ ci.getMetadata().add(new TripleImpl(ci.getUri(), new UriRef("http://example.org/ontology#hasPart"), partUri));
+ ci.getMetadata().add(new TripleImpl(partUri, new UriRef("http://example.org/ontology#isPartOf"),ci.getUri()));
+ assertEquals(someObject, ci.getPart(partUri, Date.class));
+ assertEquals(someObject, ci.getPart(1, Date.class));
+ assertEquals(partUri, ci.getPartUri(1));
+ assertEquals(new UriRef(ci.getUri().getUnicodeString()+"_main"), ci.getPartUri(0));
+ try {
+ ci.getPart(2, Object.class);
+ assertTrue("Requesting non existance part MUST throw an NoSuchPartException", false);
+ } catch (NoSuchPartException e) {/* expected*/}
+ try {
+ ci.getPart(new UriRef("http://foo/nonexisting"), Object.class);
+ assertTrue("Requesting non existance part MUST throw an NoSuchPartException", false);
+ } catch (NoSuchPartException e) {/* expected*/}
+ try {
+ ci.getPartUri(2);
+ assertTrue("Requesting non existance part MUST throw an NoSuchPartException", false);
+ } catch (NoSuchPartException e) {/* expected*/}
+ //finally log the toString
+ log.info("toString: {}",ci);
+ }
+ @Test(expected=IllegalArgumentException.class)
+ public void addPartWithoutUri() throws IOException{
+ ContentItem ci = createContentItem(contentSource);
+ ci.addPart(null, new Date());
+ }
+ @Test(expected=IllegalArgumentException.class)
+ public void addPartWithoutPartContent() throws IOException{
+ ContentItem ci = createContentItem(contentSource);
+ ci.addPart(new UriRef("http://foo/"), null);
+ }
+ /**
+ * The ContentItem MUST NOT allow to replace the main content part (the
+ * Blob stored at index 0)
+ */
+ @Test(expected=IllegalArgumentException.class)
+ public void replaceMainPart() throws IOException{
+ ContentItem ci = createContentItem(contentSource);
+ UriRef mainPart = ci.getPartUri(0);
+ ci.addPart(mainPart, new Date());
+ }
+ @Test(expected=IllegalArgumentException.class)
+ public void removeNullPart() throws IOException{
+ ContentItem ci = createContentItem(contentSource);
+ ci.removePart(null);
+ }
+ @Test(expected=IllegalArgumentException.class)
+ public void removeNegaitveIndexPart() throws IOException {
+ ContentItem ci = createContentItem(contentSource);
+ ci.removePart(-1);
+ }
+ @Test(expected=IllegalStateException.class)
+ public void removeMainContentPartByUri() throws IOException {
+ ContentItem ci = createContentItem(contentSource);
+ ci.removePart(ci.getPartUri(0));
+ }
+ @Test(expected=IllegalStateException.class)
+ public void removeMainContentPartByIndex() throws IOException {
+ ContentItem ci = createContentItem(contentSource);
+ ci.removePart(0);
+ }
+ @Test(expected=NoSuchPartException.class)
+ public void removeNonExistentPartByUri() throws IOException {
+ ContentItem ci = createContentItem(contentSource);
+ ci.removePart(new UriRef("urn:does.not.exist:and.can.not.be.removed"));
+ }
+ @Test(expected=NoSuchPartException.class)
+ public void removeNonExistentPartByIndex() throws IOException {
+ ContentItem ci = createContentItem(contentSource);
+ ci.removePart(12345);
+ }
+ @Test
+ public void removeRemoveByUri() throws IOException {
+ ContentItem ci = createContentItem(contentSource);
+ UriRef uri = new UriRef("urn:content.part:remove.test");
+ ci.addPart(uri, new Date());
+ try {
+ ci.getPart(uri, Date.class);
+ }catch (NoSuchPartException e) {
+ assertFalse("The part with the uri "+uri+" was not added correctly",
+ true);
+ }
+ ci.removePart(uri);
+ try {
+ ci.getPart(uri, Date.class);
+ assertFalse("The part with the uri "+uri+" was not removed correctly",
+ true);
+ }catch (NoSuchPartException e) {
+ // expected
+ }
+ }
+ @Test
+ public void removeRemoveByIndex() throws IOException {
+ ContentItem ci = createContentItem(contentSource);
+ UriRef uri = new UriRef("urn:content.part:remove.test");
+ ci.addPart(uri, new Date());
+ int index = -1;
+ try {
+ for(int i=0; index < 0; i++){
+ UriRef partUri = ci.getPartUri(i);
+ if(partUri.equals(uri)){
+ index = i;
+ }
+ }
+ }catch (NoSuchPartException e) {
+ assertFalse("The part with the uri "+uri+" was not added correctly",
+ true);
+ }
+ ci.removePart(index);
+ try {
+ ci.getPart(index, Date.class);
+ assertTrue("The part with the uri "+uri+" was not removed correctly",
+ false);
+ }catch (NoSuchPartException e) {
+ // expected
+ }
+ }
+
+}
Propchange: incubator/stanbol/trunk/enhancer/generic/test/src/main/java/org/apache/stanbol/enhancer/test/ContentItemTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain