use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.
the class RepresentationTest method testGetNaturalTextWithNoLanguage.
@Test
public void testGetNaturalTextWithNoLanguage() {
String field = "urn:the.field:used.for.this.Test";
Representation rep = initNaturalLanguageTest(field);
// Note that also String values need to be converted to texts with no language
Set<String> textSet = new HashSet<String>(NL_TEST_all);
Text text;
Iterator<Text> noLangTexts = rep.get(field, (String) null);
assertNotNull(noLangTexts);
while (noLangTexts.hasNext()) {
text = noLangTexts.next();
assertNull(text.getLanguage());
assertTrue(textSet.remove(text.getText()));
}
// check that both text where found
assertTrue(textSet.size() == 4);
}
use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.
the class RepresentationTest method testStringArrayToTextConversion.
/**
* Checks if {@link Text} instances are correctly generated for String[]. This test depends also on the
* correct implementation of the {@link Text#equals(Object)} method
*/
@Test
public void testStringArrayToTextConversion() {
String field = "urn:the.field:used.for.this.Test";
ValueFactory vf = getValueFactory();
Representation rep = createRepresentation(null);
// test conversion of String[] with language as second element
String[] textWithLang = new String[] { "Test text with language", "en" };
rep.add(field, textWithLang);
Iterator<Text> refs = rep.get(field, (String[]) null);
assertTrue(refs.hasNext());
Text test = refs.next();
assertEquals(textWithLang[1], test.getLanguage());
assertEquals(textWithLang[0], test.getText());
assertFalse(refs.hasNext());
// test multiple adds do not generate duplicate References
rep.add(field, textWithLang);
assertTrue(asCollection(rep.get(field)).size() == 1);
// test adding a equivalent reference
rep.add(field, vf.createText(textWithLang[0], textWithLang[1]));
assertTrue(asCollection(rep.get(field)).size() == 1);
// test removing
rep.remove(field, textWithLang);
assertFalse(rep.get(field).hasNext());
// test conversion of String[] with only one element (default language)
String[] textWithoutLang = new String[] { "Test text without language" };
rep.add(field, textWithoutLang);
refs = rep.get(field, (String[]) null);
assertTrue(refs.hasNext());
test = refs.next();
assertNull(test.getLanguage());
assertEquals(textWithoutLang[0], test.getText());
assertFalse(refs.hasNext());
// test multiple adds do not generate duplicate References
rep.add(field, textWithoutLang);
assertTrue(asCollection(rep.get(field)).size() == 1);
// test adding a equivalent reference
rep.add(field, vf.createText(textWithoutLang[0]));
assertTrue(asCollection(rep.get(field)).size() == 1);
// test removing
rep.remove(field, textWithoutLang);
assertFalse(rep.get(field).hasNext());
// test conversion of String[] with null as second element (default language)
String[] textWithDefaultLang = new String[] { "Test text with default language", null };
rep.add(field, textWithDefaultLang);
refs = rep.get(field, (String[]) null);
assertTrue(refs.hasNext());
test = refs.next();
assertNull(test.getLanguage());
assertEquals(textWithDefaultLang[0], test.getText());
assertFalse(refs.hasNext());
// test multiple adds do not generate duplicate References
rep.add(field, textWithDefaultLang);
assertTrue(asCollection(rep.get(field)).size() == 1);
// test adding a equivalent reference
rep.add(field, vf.createText(textWithDefaultLang[0], null));
assertTrue(asCollection(rep.get(field)).size() == 1);
// test removing
rep.remove(field, textWithDefaultLang);
assertFalse(rep.get(field).hasNext());
// finally test if additional Elements are correctly ignored
String[] ignoreAdditionalElements = new String[] { "Test if additional elements are ignored", "en", "ignored1", "ignored2", null, "ignored4" };
String[] sameText = new String[] { "Test if additional elements are ignored", "en" };
rep.add(field, ignoreAdditionalElements);
refs = rep.get(field, (String[]) null);
assertTrue(refs.hasNext());
test = refs.next();
assertEquals(ignoreAdditionalElements[1], test.getLanguage());
assertEquals(ignoreAdditionalElements[0], test.getText());
assertFalse(refs.hasNext());
// test multiple adds do not generate duplicate References
rep.add(field, ignoreAdditionalElements);
assertTrue(asCollection(rep.get(field)).size() == 1);
// test if an Array with only the first two elements generate the same Text
rep.add(field, sameText);
assertTrue(asCollection(rep.get(field)).size() == 1);
// test removing
rep.remove(field, ignoreAdditionalElements);
assertFalse(rep.get(field).hasNext());
}
use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.
the class RepresentationTest method testRemoveAllTextsWithNullLanguage.
@Test
public void testRemoveAllTextsWithNullLanguage() {
String field = "urn:the.field:used.for.this.Test";
Representation rep = initNaturalLanguageTest(field);
Set<String> textSet = new HashSet<String>(NL_TEST_all);
// test removal of null language
rep.removeAllNaturalText(field, (String) null);
for (Iterator<Text> texts = rep.getText(field); texts.hasNext(); textSet.remove(texts.next().getText())) ;
assertTrue(textSet.size() == 2);
assertTrue(textSet.remove(NL_TEST_noLang));
// 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 testAddNaturalLanguageTextForMultipleLanguages.
/**
* Tests the feature to add one and the same natural language text for multiple languages
*/
public void testAddNaturalLanguageTextForMultipleLanguages() {
String field = "urn:the.field:used.for.this.Test";
Representation rep = createRepresentation(null);
// add the same label for multiple language
String[] languageArray = new String[] { "en", "de", null };
rep.addNaturalText(field, NL_TEST_noLang, languageArray);
Set<String> languages = new HashSet<String>(Arrays.asList(languageArray));
Iterator<Text> texts = rep.get(field, (String[]) null);
while (texts.hasNext()) {
Text text = texts.next();
assertTrue(languages.remove(text.getLanguage()));
assertEquals(NL_TEST_noLang, text.getText());
}
assertTrue(languages.isEmpty());
}
use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.
the class RepresentationTest method testGetNaturalTextWithLanguage.
@Test
public void testGetNaturalTextWithLanguage() {
String field = "urn:the.field:used.for.this.Test";
Representation rep = initNaturalLanguageTest(field);
// test de texts
Iterator<Text> deTexts = rep.get(field, "de");
assertNotNull(deTexts);
// there is one German text in the test set
assertTrue(deTexts.hasNext());
Text text = deTexts.next();
// "de" lang
assertEquals(text.getLanguage(), "de");
// the de lang text
assertEquals(text.getText(), NL_TEST_de);
// only one Result
assertFalse(deTexts.hasNext());
// test en labels (2 results)
Iterator<Text> enTexts = rep.get(field, "en");
assertNotNull(enTexts);
Set<String> textSet = new HashSet<String>(Arrays.asList(NL_TEST_en, NL_TEST_en2));
while (enTexts.hasNext()) {
text = enTexts.next();
assertEquals("en", text.getLanguage());
// remove the found
assertTrue(textSet.remove(text.getText()));
}
// all texts found
assertTrue(textSet.isEmpty());
}
Aggregations