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));
}
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));
}
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));
}
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);
}
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);
}
Aggregations