Skip to content

Commit

Permalink
Added Junit tests for preferred language tags
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenR-York committed Dec 17, 2024
1 parent 6d71c86 commit f0a2804
Show file tree
Hide file tree
Showing 2 changed files with 284 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Sample Turtle document from the W3C: https://www.w3.org/TR/turtle/#sec-intro

@base <http://example.org/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix rel: <http://www.perceive.net/schemas/relationship/> .

<#green-goblin>
rel:enemyOf <#spiderman> ;
a foaf:Person ; # in the context of the Marvel universe
foaf:name "Green Goblin" .

<#spiderman>
rel:enemyOf <#green-goblin> ;
a foaf:Person ;
foaf:name "Spiderman", "Человек-паук"@ru ,"スパイダーマン"@ja .
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
/********************************************************************************
* Copyright (c) 2024 University of York
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Antonio Garcia-Dominguez - initial API and implementation
********************************************************************************/
package org.eclipse.epsilon.emc.rdf;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.epsilon.common.util.StringProperties;
import org.eclipse.epsilon.eol.exceptions.models.EolModelElementTypeNotFoundException;
import org.eclipse.epsilon.eol.exceptions.models.EolModelLoadingException;
import org.eclipse.epsilon.eol.execute.context.EolContext;
import org.eclipse.epsilon.eol.execute.introspection.IPropertyGetter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class RDFModelLanguageTagsTest {

private RDFModel model;
private IPropertyGetter pGetter;
private EolContext context;

private static final String SPIDERMAN_MULTILANG_TTL = "resources/spiderman-multiLang.ttl";
private static final String LANGUAGEPREFERENCE_STRING = "e n,en-us,123,ja,ru";

private static final String SPIDERMAN_URI = "http://example.org/#spiderman";
private static final String SPIDERMAN_NAME = "Spiderman";
private static final String SPIDERMAN_NAME_RU = "Человек-паук";
private static final String SPIDERMAN_NAME_JA = "スパイダーマン";

private static final Set<String> SPIDERMAN_NAMES = new HashSet<>(Arrays.asList(SPIDERMAN_NAME, SPIDERMAN_NAME_RU, SPIDERMAN_NAME_JA));

private static final String GREEN_GOBLIN_URI = "http://example.org/#green-goblin";

private static final Set<String> ALL_NAMES = new HashSet<>();
private static final Set<String> ALL_PERSON_URIS = new HashSet<>(Arrays.asList(SPIDERMAN_URI, GREEN_GOBLIN_URI));
static {
ALL_NAMES.add("Green Goblin");
ALL_NAMES.addAll(SPIDERMAN_NAMES);
}


@Before
public void setup () throws EolModelLoadingException {
this.model = new RDFModel();
StringProperties props = new StringProperties();
props.put(RDFModel.PROPERTY_URIS, SPIDERMAN_MULTILANG_TTL);
props.put(RDFModel.PROPERTY_LANGUAGEPREFERENCE, LANGUAGEPREFERENCE_STRING);
model.load(props);

this.pGetter = model.getPropertyGetter();
this.context = new EolContext();
}

@After
public void teardown() {
if (model != null) {
model.dispose();
}
}

@Test
public void modelDefaultLanguageTagProperty() throws Exception {
String answer = "[" + LANGUAGEPREFERENCE_STRING.replaceAll("\\s", "") + "]";
List<String> langList = model.getLanguagePreference();
String langListString = model.getLanguagePreference().toString().replaceAll("\\s", "");
assertEquals(answer, langListString);
}


@Test
public void modelLanguageTagValidator() throws Exception {
// Test for different tag patterns
assertTrue(model.isValidLanguageTag("en"));
assertFalse(model.isValidLanguageTag("e n"));

assertTrue(model.isValidLanguageTag("en-us"));
assertFalse(model.isValidLanguageTag("e n-u s"));

assertFalse(model.isValidLanguageTag("123"));
}

@Test
public void getNamesWithoutPrefixWithPreferredLanguageTag() throws Exception {
RDFResource res = (RDFResource) model.getElementById(SPIDERMAN_URI);
Set<String> names = new HashSet<>((Collection<String>) pGetter.invoke(res, "name@", context));
assertEquals(Collections.singleton(SPIDERMAN_NAME_JA), names);
}

@Test
public void getNamesWithPrefixAndPreferredLanguageTag() throws Exception {
RDFResource res = (RDFResource) model.getElementById(SPIDERMAN_URI);
Set<String> names = new HashSet<>((Collection<String>) pGetter.invoke(res, "foaf:name@", context));
assertEquals(Collections.singleton(SPIDERMAN_NAME_JA), names);
}

@Test
public void getNameLiteralWithPrefixAndPreferredLanguageTag() throws Exception {
RDFResource res = (RDFResource) model.getElementById(SPIDERMAN_URI);
Set<String> names = new HashSet<>();
for (RDFLiteral l : (Collection<RDFLiteral>) pGetter.invoke(res, "foaf:name_literal@", context)) {
names.add((String) l.getValue());
}
assertEquals(Collections.singleton(SPIDERMAN_NAME_JA), names);
}

@Test
public void getNameLiteralWithoutPrefixAndPreferredLanguageTag() throws Exception {
RDFResource res = (RDFResource) model.getElementById(SPIDERMAN_URI);
Set<String> names = new HashSet<>();
for (RDFLiteral l : (Collection<RDFLiteral>) pGetter.invoke(res, "name_literal@", context)) {
names.add((String) l.getValue());
}
assertEquals(Collections.singleton(SPIDERMAN_NAME_JA), names);
}


//
// Same test as performed in ModelTest are performed on the Spiderman multi-language ttl model

@Test
public void listAll() throws EolModelLoadingException {
assertEquals("allContents should produce one element per resource", 2, model.allContents().size());
}

@Test
public void getNamesWithoutPrefix() throws Exception {
Set<String> names = new HashSet<>();
for (RDFModelElement o : model.allContents()) {
names.addAll((Collection<String>) pGetter.invoke(o, "name", context));
}
assertEquals(ALL_NAMES, names);
}

@Test
public void getNamesWithPrefix() throws Exception {
RDFResource res = (RDFResource) model.getElementById(SPIDERMAN_URI);
Set<String> names = new HashSet<>((Collection<String>) pGetter.invoke(res, "foaf:name", context));
assertEquals(SPIDERMAN_NAMES, names);
}

@Test
public void getNamesWithDoubleColonPrefix() throws Exception {
RDFResource res = (RDFResource) model.getElementById(SPIDERMAN_URI);
Set<String> names = new HashSet<>((Collection<String>) pGetter.invoke(res, "foaf::name", context));
assertEquals(SPIDERMAN_NAMES, names);
}

@Test
public void getNameLiteralsWithPrefix() throws Exception {
RDFResource res = (RDFResource) model.getElementById(SPIDERMAN_URI);
Set<String> names = new HashSet<>();
for (RDFLiteral l : (Collection<RDFLiteral>) pGetter.invoke(res, "foaf:name_literal", context)) {
names.add((String) l.getValue());
}
assertEquals(SPIDERMAN_NAMES, names);
}

@Test
public void getNamesWithoutPrefixWithLanguageTag() throws Exception {
RDFResource res = (RDFResource) model.getElementById(SPIDERMAN_URI);
Set<String> names = new HashSet<>((Collection<String>) pGetter.invoke(res, "name@ru", context));
assertEquals(Collections.singleton(SPIDERMAN_NAME_RU), names);
}



@Test
public void getNamesWithPrefixAndLanguageTag() throws Exception {
RDFResource res = (RDFResource) model.getElementById(SPIDERMAN_URI);
Set<String> names = new HashSet<>((Collection<String>) pGetter.invoke(res, "foaf:name@ru", context));
assertEquals(Collections.singleton(SPIDERMAN_NAME_RU), names);
}

@Test
public void getEnemiesOfSpiderman() throws Exception {
RDFResource res = (RDFResource) model.getElementById(SPIDERMAN_URI);
assertEquals("The getElementById and getElementId methods should match each other",
SPIDERMAN_URI, model.getElementId(res));

Set<String> uris = new HashSet<>();
for (RDFResource r : (Collection<RDFResource>) pGetter.invoke(res, "rel:enemyOf", context)) {
Object uri = pGetter.invoke(r, "uri", context);
uris.add((String) uri);
}

assertEquals(Collections.singleton(GREEN_GOBLIN_URI), uris);
}

@Test
public void getAllPeopleWithoutPrefix() throws Exception {
Set<String> uris = new HashSet<>();
for (Object o : model.getAllOfType("Person")) {
uris.add((String) pGetter.invoke(o, "uri", context));
}
assertEquals(ALL_PERSON_URIS, uris);
}

@Test(expected=EolModelElementTypeNotFoundException.class)
public void getAllInstancesOfMissingType() throws Exception {
model.getAllOfType("NoSuchTypeExists");
}

@Test
public void getAllPeopleWithPrefix() throws Exception {
Set<String> uris = new HashSet<>();
for (Object o : model.getAllOfType("foaf:Person")) {
uris.add((String) pGetter.invoke(o, "uri", context));
}
assertEquals(ALL_PERSON_URIS, uris);
}

@Test
public void getAllPeopleWithPrefixDoubleColon() throws Exception {
Set<String> uris = new HashSet<>();
for (Object o : model.getAllOfType("foaf::Person")) {
uris.add((String) pGetter.invoke(o, "uri", context));
}
assertEquals(ALL_PERSON_URIS, uris);
}

@Test
public void knownTypes() {
assertTrue("The model should confirm that it knows the foaf:Person type", model.hasType("foaf:Person"));
assertFalse("The model should deny that it knows the foaf:SomethingElse type", model.hasType("foaf:SomethingElse"));
}

@Test
public void getPersonInformation() throws Exception {
RDFModelElement firstPerson = model.getAllOfType("foaf:Person").iterator().next();
assertTrue("The model should own the person", model.owns(firstPerson));
assertFalse("The model should not own an unrelated object", model.owns(1234));

assertEquals("The model should report the first type of the resource",
"foaf:Person",
model.getTypeNameOf(firstPerson));

// This may be revised later, if generic types are introduced
assertEquals("The model should only report the Person type for that person",
Collections.singletonList("foaf:Person"),
model.getAllTypeNamesOf(firstPerson));
}

@Test(expected=EolModelElementTypeNotFoundException.class)
public void jenaDoesNotFetchRelatedVocabulary() throws Exception {
// By itself, Jena will not fetch the related FOAF vocabulary referenced in the Turtles example
model.getAllOfType("Class");
}

}

0 comments on commit f0a2804

Please sign in to comment.