-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Language specification support - added API to descriptors.
- Loading branch information
Showing
9 changed files
with
236 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
jopa-api/src/test/java/cz/cvut/kbss/jopa/model/descriptors/EntityDescriptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package cz.cvut.kbss.jopa.model.descriptors; | ||
|
||
import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty; | ||
import cz.cvut.kbss.jopa.model.metamodel.Attribute; | ||
import org.junit.Test; | ||
|
||
import java.lang.reflect.Field; | ||
import java.net.URI; | ||
|
||
import static org.junit.Assert.*; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class EntityDescriptorTest { | ||
|
||
private static final URI CONTEXT_ONE = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/contextOne"); | ||
private static final URI CONTEXT_TWO = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/contextTwo"); | ||
private static final String LANG = "en"; | ||
|
||
@Test | ||
public void fieldDescriptorByDefaultInheritsEntityContext() throws Exception { | ||
final EntityDescriptor descriptor = new EntityDescriptor(CONTEXT_ONE); | ||
final Attribute<TestClass, String> att = mock(Attribute.class); | ||
when(att.getJavaField()).thenReturn(TestClass.stringAttField()); | ||
final Descriptor result = descriptor.getAttributeDescriptor(att); | ||
assertEquals(CONTEXT_ONE, result.getContext()); | ||
} | ||
|
||
@Test | ||
public void fieldDescriptorHasItsOwnContextWhenItIsSetForIt() throws Exception { | ||
final EntityDescriptor descriptor = new EntityDescriptor(CONTEXT_ONE); | ||
final Attribute<TestClass, String> att = mock(Attribute.class); | ||
when(att.getJavaField()).thenReturn(TestClass.stringAttField()); | ||
descriptor.addAttributeContext(att.getJavaField(), CONTEXT_TWO); | ||
|
||
final Descriptor result = descriptor.getAttributeDescriptor(att); | ||
assertEquals(CONTEXT_TWO, result.getContext()); | ||
} | ||
|
||
@Test | ||
public void fieldDescriptorByDefaultInheritsEntityLanguageTag() throws Exception { | ||
final EntityDescriptor descriptor = new EntityDescriptor(); | ||
final Attribute<TestClass, String> att = mock(Attribute.class); | ||
when(att.getJavaField()).thenReturn(TestClass.stringAttField()); | ||
descriptor.setLanguage(LANG); | ||
assertTrue(descriptor.getLanguage().isPresent()); | ||
|
||
final Descriptor result = descriptor.getAttributeDescriptor(att); | ||
assertTrue(result.getLanguage().isPresent()); | ||
assertEquals(LANG, result.getLanguage().get()); | ||
} | ||
|
||
@Test | ||
public void fieldDescriptorInheritsChangeOfLanguageTagFromEntityDescriptor() throws Exception { | ||
final EntityDescriptor descriptor = new EntityDescriptor(); | ||
final Attribute<TestClass, String> att = mock(Attribute.class); | ||
when(att.getJavaField()).thenReturn(TestClass.stringAttField()); | ||
descriptor.setLanguage(LANG); | ||
final String newLang = "cs"; | ||
descriptor.setLanguage(newLang); | ||
final Descriptor result = descriptor.getAttributeDescriptor(att); | ||
assertTrue(result.getLanguage().isPresent()); | ||
assertEquals(newLang, result.getLanguage().get()); | ||
} | ||
|
||
@Test | ||
public void fieldDescriptorHasLanguageSetToItThroughEntityDescriptor() throws Exception { | ||
final EntityDescriptor descriptor = new EntityDescriptor(); | ||
final Attribute<TestClass, String> att = mock(Attribute.class); | ||
when(att.getJavaField()).thenReturn(TestClass.stringAttField()); | ||
descriptor.setLanguage(LANG); | ||
final String newLang = "cs"; | ||
descriptor.setAttributeLanguage(att.getJavaField(), newLang); | ||
|
||
final Descriptor result = descriptor.getAttributeDescriptor(att); | ||
assertTrue(result.getLanguage().isPresent()); | ||
assertEquals(newLang, result.getLanguage().get()); | ||
} | ||
|
||
@Test | ||
public void twoEntityDescriptorsAreEqualWhenTheirFieldDescriptorsHaveTheSameContexts() throws Exception { | ||
final EntityDescriptor dOne = new EntityDescriptor(CONTEXT_ONE); | ||
final EntityDescriptor dTwo = new EntityDescriptor(CONTEXT_ONE); | ||
dOne.addAttributeContext(TestClass.stringAttField(), CONTEXT_TWO); | ||
dTwo.addAttributeContext(TestClass.stringAttField(), CONTEXT_TWO); | ||
dOne.addAttributeDescriptor(TestClass.intAttField(), new FieldDescriptor(CONTEXT_ONE, TestClass.intAttField())); | ||
dTwo.addAttributeDescriptor(TestClass.intAttField(), new FieldDescriptor(CONTEXT_ONE, TestClass.intAttField())); | ||
|
||
assertTrue(dOne.equals(dTwo)); | ||
assertTrue(dTwo.equals(dOne)); | ||
assertEquals(dOne.hashCode(), dTwo.hashCode()); | ||
} | ||
|
||
@Test | ||
public void twoEntityDescriptorsAreNotEqualWhenTheyDifferInFieldContext() throws Exception { | ||
final EntityDescriptor dOne = new EntityDescriptor(CONTEXT_ONE); | ||
final EntityDescriptor dTwo = new EntityDescriptor(CONTEXT_ONE); | ||
dOne.addAttributeContext(TestClass.stringAttField(), CONTEXT_TWO); | ||
dTwo.addAttributeContext(TestClass.stringAttField(), CONTEXT_ONE); | ||
|
||
assertFalse(dOne.equals(dTwo)); | ||
assertNotEquals(dOne.hashCode(), dTwo.hashCode()); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
private static class TestClass { | ||
|
||
@OWLDataProperty(iri = "http://krizik.felk.cvut.cz/ontologies/jopa/attributes/stringAtt") | ||
private String stringAtt; | ||
|
||
@OWLDataProperty(iri = "http://krizik.felk.cvut.cz/ontologies/jopa/attributes/intAtt") | ||
private Integer intAtt; | ||
|
||
private static Field stringAttField() throws NoSuchFieldException { | ||
return TestClass.class.getDeclaredField("stringAtt"); | ||
} | ||
|
||
private static Field intAttField() throws NoSuchFieldException { | ||
return TestClass.class.getDeclaredField("intAtt"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.