Search in sources :

Example 16 with Text

use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.

the class RepresentationTest method testFieldRemoval.

@Test
public void testFieldRemoval() throws URISyntaxException {
    String field = "urn:the.field:used.for.this.Test";
    ValueFactory vf = getValueFactory();
    Representation rep = createRepresentation(null);
    // Test removal for References
    String strRef = "urn:testValue";
    rep.addReference(field, strRef);
    assertTrue(asCollection(rep.getFieldNames()).contains(field));
    rep.removeReference(field, strRef);
    assertFalse(asCollection(rep.getFieldNames()).contains(field));
    Reference ref = vf.createReference("urn:testValue2");
    rep.add(field, ref);
    assertTrue(asCollection(rep.getFieldNames()).contains(field));
    rep.remove(field, ref);
    assertFalse(asCollection(rep.getFieldNames()).contains(field));
    // test removal for texts (with and without language)
    String strText = "test text";
    String strTextLang = "en";
    rep.addNaturalText(field, strText, strTextLang);
    assertTrue(asCollection(rep.getFieldNames()).contains(field));
    rep.removeNaturalText(field, strText, strTextLang);
    assertFalse(asCollection(rep.getFieldNames()).contains(field));
    String strTextNoLang = "test text without lang";
    rep.addNaturalText(field, strTextNoLang);
    assertTrue(asCollection(rep.getFieldNames()).contains(field));
    rep.removeNaturalText(field, strTextNoLang);
    assertFalse(asCollection(rep.getFieldNames()).contains(field));
    // there is also the possibility to explicitly parse null as language
    // could internally case differences however externally this is the same
    rep.addNaturalText(field, strTextNoLang, (String) null);
    assertTrue(asCollection(rep.getFieldNames()).contains(field));
    rep.removeNaturalText(field, strTextNoLang, (String) null);
    assertFalse(asCollection(rep.getFieldNames()).contains(field));
    Text text = vf.createText("Das ist ein Text zum testen des Text Objektes", "de");
    rep.add(field, text);
    assertTrue(asCollection(rep.getFieldNames()).contains(field));
    rep.remove(field, text);
    assertFalse(asCollection(rep.getFieldNames()).contains(field));
    // Test a dataTypes values
    Integer intValue = 42;
    rep.add(field, intValue);
    assertTrue(asCollection(rep.getFieldNames()).contains(field));
    rep.remove(field, intValue);
    assertFalse(asCollection(rep.getFieldNames()).contains(field));
    // Some Values are converted by the add(String field,Object value) Method
    // to other data types. This MUST also be assured for removal
    // NOTE: testing the conversions is done in other test methods!
    URI testURI = new URI("http://www.test.org/test");
    rep.add(field, testURI);
    assertTrue(asCollection(rep.getFieldNames()).contains(field));
    rep.remove(field, testURI);
    assertFalse(asCollection(rep.getFieldNames()).contains(field));
}
Also used : Reference(org.apache.stanbol.entityhub.servicesapi.model.Reference) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory) URI(java.net.URI) Test(org.junit.Test)

Example 17 with Text

use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.

the class RepresentationTest method testRemoveStringValuesByRemovingNaturalLanguageTextsWithNoLanguage.

/**
     * String values are treated the same as natural language values with the default (<code>null</code>)
     * language. <br>
     * Removing a natural language value with no defined language MUST therefore also remove a string value
     * with the parse same text.
     */
@Test
public void testRemoveStringValuesByRemovingNaturalLanguageTextsWithNoLanguage() {
    String field = "urn:the.field:used.for.this.Test";
    Representation rep = initNaturalLanguageTest(field);
    Set<String> textSet = new HashSet<String>(NL_TEST_all);
    // remove a string value by parsing only the text
    rep.removeNaturalText(field, NL_TEST_string);
    textSet.addAll(Arrays.asList(NL_TEST_string, NL_TEST_noLang, NL_TEST_en, NL_TEST_en2, NL_TEST_de, NL_TEST_de_AT));
    for (Iterator<Text> texts = rep.getText(field); texts.hasNext(); textSet.remove(texts.next().getText())) ;
    // only one element should be removed
    assertTrue(textSet.size() == 1);
    // and this should be the stringTest
    assertTrue(textSet.remove(NL_TEST_string));
}
Also used : Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 18 with Text

use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.

the class RepresentationTest method testRemoveStringValuesByRemovingNaturalLanguageTextsWithNullLanguage.

/**
     * String values are treated the same as natural language values with the default (<code>null</code>)
     * language. <br>
     * Removing a natural language value with parsing null as language MUST therefore also remove a string
     * value with the parse same text.
     */
@Test
public void testRemoveStringValuesByRemovingNaturalLanguageTextsWithNullLanguage() {
    String field = "urn:the.field:used.for.this.Test";
    Representation rep = initNaturalLanguageTest(field);
    Set<String> textSet = new HashSet<String>(NL_TEST_all);
    // remove a string value by parsing the text and null as language
    rep.removeNaturalText(field, NL_TEST_string, (String) null);
    for (Iterator<Text> texts = rep.getText(field); texts.hasNext(); textSet.remove(texts.next().getText())) ;
    // only one element should be removed
    assertTrue(textSet.size() == 1);
    // and this should be the stringTest
    assertTrue(textSet.remove(NL_TEST_string));
}
Also used : Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 19 with Text

use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.

the class RepresentationTest method testRemoveStringValuesByRemovingNaturalLanguageTextsWithEmptyLanguageArray.

/**
     * String values are treated the same as natural language values with the default (<code>null</code>)
     * language. <br>
     * Removing a natural language value with an empty language array MUST be interpreted as default language
     * and therefore remove the String value.
     */
@Test
public void testRemoveStringValuesByRemovingNaturalLanguageTextsWithEmptyLanguageArray() {
    String field = "urn:the.field:used.for.this.Test";
    Representation rep = initNaturalLanguageTest(field);
    Set<String> textSet = new HashSet<String>(NL_TEST_all);
    // remove a string value by parsing an empty list of languages
    rep.removeNaturalText(field, NL_TEST_string);
    textSet.addAll(Arrays.asList(NL_TEST_string, NL_TEST_noLang, NL_TEST_en, NL_TEST_en2, NL_TEST_de, NL_TEST_de_AT));
    for (Iterator<Text> texts = rep.getText(field); texts.hasNext(); textSet.remove(texts.next().getText())) ;
    // only one element should be removed
    assertTrue(textSet.size() == 1);
    // and this should be the stringTest
    assertTrue(textSet.remove(NL_TEST_string));
    // re add the value for further tests
    rep.add(field, NL_TEST_string);
}
Also used : Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 20 with Text

use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.

the class RepresentationTest method testMultipleAddAndRemove.

@Test
public void testMultipleAddAndRemove() throws MalformedURLException, URISyntaxException {
    String field = "urn:the.field:used.for.this.Test";
    ValueFactory vf = getValueFactory();
    Representation rep = createRepresentation(null);
    Reference ref = vf.createReference("http://www.test.org/test");
    Text text = vf.createText("test", "en");
    Integer i = 42;
    Double d = Math.PI;
    URI uri = new URI("http://www.test.org/uriTest");
    URL url = new URL("http://www.test.org/urlTest");
    String[] textAsArray = new String[] { "Test text as Array", "en" };
    Collection<Object> values = Arrays.asList(ref, text, i, d);
    Collection<Object> convertedValues = Arrays.asList((Object) url, uri, textAsArray);
    Collection<Object> allValues = Arrays.asList(ref, text, i, d, uri, url, textAsArray);
    // test adding of collections
    rep.add(field, values);
    assertTrue(asCollection(rep.get(field)).size() == 4);
    rep.remove(field, values);
    assertFalse(rep.get(field).hasNext());
    // test adding of Iterators
    rep.add(field, values.iterator());
    assertTrue(asCollection(rep.get(field)).size() == 4);
    rep.remove(field, values.iterator());
    assertFalse(rep.get(field).hasNext());
    // test adding of Enumerations
    Vector<Object> v = new Vector<Object>(values);
    rep.add(field, v.elements());
    assertTrue(asCollection(rep.get(field)).size() == 4);
    rep.remove(field, v.elements());
    assertFalse(rep.get(field).hasNext());
    // test adding and removing elements that need to be converted
    // only for collections this time -> want to test only converting is
    // applied for both add and remove
    rep.add(field, convertedValues);
    assertTrue(asCollection(rep.get(field)).size() == 3);
    rep.remove(field, convertedValues);
    assertFalse(rep.get(field).hasNext());
    // a final test to ensure, that remove does not only delete all values
    rep.add(field, allValues);
    assertTrue(asCollection(rep.get(field)).size() == 7);
    rep.remove(field, convertedValues);
    assertTrue(asCollection(rep.get(field)).size() == 4);
}
Also used : Reference(org.apache.stanbol.entityhub.servicesapi.model.Reference) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory) URI(java.net.URI) URL(java.net.URL) Vector(java.util.Vector) Test(org.junit.Test)

Aggregations

Text (org.apache.stanbol.entityhub.servicesapi.model.Text)50 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)32 Test (org.junit.Test)24 HashSet (java.util.HashSet)14 Reference (org.apache.stanbol.entityhub.servicesapi.model.Reference)12 ArrayList (java.util.ArrayList)11 IRI (org.apache.clerezza.commons.rdf.IRI)6 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)4 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)4 ValueFactory (org.apache.stanbol.entityhub.servicesapi.model.ValueFactory)4 RepresentationTest (org.apache.stanbol.entityhub.test.model.RepresentationTest)4 Graph (org.apache.clerezza.commons.rdf.Graph)3 Language (org.apache.clerezza.commons.rdf.Language)3 Literal (org.apache.clerezza.commons.rdf.Literal)3 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)3 EntityhubException (org.apache.stanbol.entityhub.servicesapi.EntityhubException)3 FieldQuery (org.apache.stanbol.entityhub.servicesapi.query.FieldQuery)3 TextConstraint (org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)3 URI (java.net.URI)2 URL (java.net.URL)2