use of org.eclipse.xsd.XSDModelGroup in project tmdm-studio-se by Talend.
the class XSDUtilTest method testGetAnnotationValue.
@Test
public void testGetAnnotationValue() throws Exception {
// $NON-NLS-1$
String fileName = "Product_0.1.xsd";
// $NON-NLS-1$
String elementName = "Family";
// $NON-NLS-1$
String fk = "Store/Id";
String xsdString = TestUtil.readTestResource(XSDUtilTest.this.getClass(), fileName);
assertNotNull(xsdString);
XSDSchema xsdSchema = Util.getXSDSchema(xsdString);
for (XSDElementDeclaration element : xsdSchema.getElementDeclarations()) {
XSDTypeDefinition typeDefinition = element.getTypeDefinition();
if (typeDefinition instanceof XSDComplexTypeDefinition) {
XSDComplexTypeContent xsdComplexTypeContent = ((XSDComplexTypeDefinition) typeDefinition).getContent();
if (xsdComplexTypeContent instanceof XSDParticle) {
XSDParticleContent content = ((XSDParticle) xsdComplexTypeContent).getContent();
if (content instanceof XSDModelGroup) {
for (XSDParticle particle : ((XSDModelGroup) content).getParticles()) {
XSDTerm term = particle.getTerm();
if (term instanceof XSDElementDeclaration) {
XSDElementDeclaration elementDeclaration = (XSDElementDeclaration) term;
if (elementDeclaration.getName().equals(elementName)) {
String value = XSDUtil.getAnnotationValue(elementDeclaration, X_FOREIGN_KEY);
assertEquals(fk, value);
return;
}
}
}
}
}
}
}
fail();
}
use of org.eclipse.xsd.XSDModelGroup in project tmdm-studio-se by Talend.
the class XSDUtilTest method testIsEntity.
@Test
public void testIsEntity() throws Exception {
// $NON-NLS-1$
String fileName = "Product_0.1.xsd";
// $NON-NLS-1$
String elementName = "Product";
String xsdString = TestUtil.readTestResource(XSDUtilTest.this.getClass(), fileName);
assertNotNull(xsdString);
XSDSchema xsdSchema = Util.getXSDSchema(xsdString);
XSDElementDeclaration decl = null;
for (XSDElementDeclaration element : xsdSchema.getElementDeclarations()) {
if (element.getName().equals(elementName)) {
decl = element;
}
assertTrue(XSDUtil.isEntity(element));
}
if (decl != null) {
XSDComplexTypeDefinition ctypeDefinition = (XSDComplexTypeDefinition) decl.getTypeDefinition();
XSDComplexTypeContent content = ctypeDefinition.getContent();
if (content instanceof XSDParticle) {
XSDParticle xsdParticle = (XSDParticle) content;
XSDParticleContent particleContent = xsdParticle.getContent();
if (particleContent instanceof XSDModelGroup) {
XSDModelGroup modelGroup = (XSDModelGroup) particleContent;
EList<XSDParticle> contents = modelGroup.getContents();
for (XSDParticle particle : contents) {
if (particle.getContent() instanceof XSDElementDeclaration) {
assertFalse(XSDUtil.isEntity(particle.getContent()));
}
}
}
}
}
}
use of org.eclipse.xsd.XSDModelGroup in project tmdm-studio-se by Talend.
the class XSDUtilTest method testIsPrimaryKeyElement.
@Test
public void testIsPrimaryKeyElement() throws Exception {
// $NON-NLS-1$
String fileName = "Product_0.1.xsd";
// $NON-NLS-1$
String elementName = "Product";
String xsdString = TestUtil.readTestResource(XSDUtilTest.this.getClass(), fileName);
assertNotNull(xsdString);
XSDSchema xsdSchema = Util.getXSDSchema(xsdString);
XSDElementDeclaration decl = null;
for (XSDElementDeclaration element : xsdSchema.getElementDeclarations()) {
if (element.getName().equals(elementName)) {
decl = element;
}
}
if (decl != null) {
XSDComplexTypeDefinition ctypeDefinition = (XSDComplexTypeDefinition) decl.getTypeDefinition();
XSDComplexTypeContent ctypeContent = ctypeDefinition.getContent();
if (ctypeContent instanceof XSDParticle) {
XSDParticle typeParticle = (XSDParticle) ctypeContent;
XSDParticleContent particleContent = typeParticle.getContent();
if (particleContent instanceof XSDModelGroup) {
XSDModelGroup particleGroup = (XSDModelGroup) particleContent;
for (XSDParticle particle : particleGroup.getContents()) {
if (particle.getContent() instanceof XSDElementDeclaration && ((XSDElementDeclaration) particle.getContent()).getName().equals("Id")) {
// $NON-NLS-1$
assertTrue(XSDUtil.isPrimaryKeyElement(particle));
} else {
assertFalse(XSDUtil.isPrimaryKeyElement(particle));
}
}
}
}
}
}
use of org.eclipse.xsd.XSDModelGroup in project tmdm-studio-se by Talend.
the class UtilMockTest method testGetParent.
@Test
public void testGetParent() {
Object parent = Util.getParent(null);
assertNull(parent);
XSDElementDeclaration concept = XSDFactory.eINSTANCE.createXSDElementDeclaration();
XSDParticle xsdParticle = XSDFactory.eINSTANCE.createXSDParticle();
parent = Util.getParent(concept);
assertNull(parent);
parent = Util.getParent(xsdParticle);
assertNull(parent);
XSDSchema xsdSchema = XSDFactory.eINSTANCE.createXSDSchema();
xsdSchema.getContents().add(concept);
parent = Util.getParent(concept);
// concept's parent is itself
assertSame(concept, parent);
//
PowerMockito.mockStatic(Util.class);
// $NON-NLS-1$
String method_private = "findOutSpecialSonElement";
try {
PowerMockito.when(Util.getParent(Mockito.any(Object.class))).thenCallRealMethod();
XSDComplexTypeDefinition xsdComplexTypeDef = XSDFactory.eINSTANCE.createXSDComplexTypeDefinition();
xsdComplexTypeDef.setBaseTypeDefinition(// $NON-NLS-1$
xsdSchema.resolveComplexTypeDefinition(xsdSchema.getSchemaForSchemaNamespace(), "anyType"));
XSDParticle particle = XSDFactory.eINSTANCE.createXSDParticle();
xsdComplexTypeDef.setContent(particle);
concept.setAnonymousTypeDefinition(xsdComplexTypeDef);
XSDModelGroup modelGroup = XSDFactory.eINSTANCE.createXSDModelGroup();
particle.setContent(modelGroup);
XSDParticle particle1 = XSDFactory.eINSTANCE.createXSDParticle();
XSDElementDeclaration elementDeclaration1 = XSDFactory.eINSTANCE.createXSDElementDeclaration();
particle1.setContent(elementDeclaration1);
XSDParticle particle2 = XSDFactory.eINSTANCE.createXSDParticle();
XSDElementDeclaration elementDeclaration2 = XSDFactory.eINSTANCE.createXSDElementDeclaration();
particle2.setContent(elementDeclaration2);
// add children to elementDeclaration2
XSDComplexTypeDefinition complexType2 = XSDFactory.eINSTANCE.createXSDComplexTypeDefinition();
complexType2.setBaseTypeDefinition(// $NON-NLS-1$
xsdSchema.resolveComplexTypeDefinition(xsdSchema.getSchemaForSchemaNamespace(), "anyType"));
XSDModelGroup modelGroup_type = XSDFactory.eINSTANCE.createXSDModelGroup();
XSDParticle particle_child = XSDFactory.eINSTANCE.createXSDParticle();
XSDElementDeclaration elementDeclaration_child = XSDFactory.eINSTANCE.createXSDElementDeclaration();
particle_child.setContent(elementDeclaration_child);
modelGroup_type.getContents().add(particle_child);
XSDParticle particle_type = XSDFactory.eINSTANCE.createXSDParticle();
particle_type.setContent(modelGroup_type);
complexType2.setContent(particle_type);
elementDeclaration2.setAnonymousTypeDefinition(complexType2);
modelGroup.getContents().add(particle1);
modelGroup.getContents().add(particle2);
parent = Util.getParent(elementDeclaration1);
assertSame(concept, parent);
parent = Util.getParent(elementDeclaration2);
assertSame(concept, parent);
XSDElementDeclaration son = XSDFactory.eINSTANCE.createXSDElementDeclaration();
parent = Util.getParent(son);
assertNull(parent);
//
xsdSchema.getContents().clear();
xsdSchema.getContents().add(xsdComplexTypeDef);
parent = Util.getParent(elementDeclaration1);
assertSame(xsdComplexTypeDef, parent);
parent = Util.getParent(elementDeclaration2);
assertSame(xsdComplexTypeDef, parent);
//
XSDElementDeclaration element = XSDFactory.eINSTANCE.createXSDElementDeclaration();
PowerMockito.when(Util.class, method_private, Mockito.any(XSDElementDeclaration.class), Mockito.any(XSDElementDeclaration.class), Mockito.anySet()).thenReturn(element);
parent = Util.getParent(elementDeclaration_child);
assertSame(element, parent);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
use of org.eclipse.xsd.XSDModelGroup in project tmdm-studio-se by Talend.
the class UtilMockTest method testIsAImporedElement.
@Test
public void testIsAImporedElement() {
XSDFactory factory = XSDFactory.eINSTANCE;
XSDSchema xsdSchema = factory.createXSDSchema();
XSDElementDeclaration elementDeclaration = factory.createXSDElementDeclaration();
XSDComplexTypeDefinition complexTypeDefinition = factory.createXSDComplexTypeDefinition();
complexTypeDefinition.setBaseTypeDefinition(// $NON-NLS-1$
xsdSchema.resolveComplexTypeDefinition(xsdSchema.getSchemaForSchemaNamespace(), "anyType"));
XSDParticle typeParticle = factory.createXSDParticle();
XSDModelGroup modelGroup = factory.createXSDModelGroup();
XSDParticle particle = factory.createXSDParticle();
XSDElementDeclaration pelement = factory.createXSDElementDeclaration();
elementDeclaration.setAnonymousTypeDefinition(complexTypeDefinition);
complexTypeDefinition.setContent(typeParticle);
typeParticle.setContent(modelGroup);
modelGroup.getContents().add(particle);
particle.setContent(pelement);
boolean isAImporedElement = Util.IsAImporedElement(null, xsdSchema);
assertFalse(isAImporedElement);
isAImporedElement = Util.IsAImporedElement(pelement, xsdSchema);
assertFalse(isAImporedElement);
xsdSchema.getContents().add(elementDeclaration);
isAImporedElement = Util.IsAImporedElement(pelement, xsdSchema);
assertFalse(isAImporedElement);
XSDSchema anotherSchema = factory.createXSDSchema();
isAImporedElement = Util.IsAImporedElement(pelement, anotherSchema);
assertFalse(isAImporedElement);
xsdSchema.getReferencingDirectives().add(factory.createXSDImport());
xsdSchema.getReferencingDirectives().add(factory.createXSDInclude());
isAImporedElement = Util.IsAImporedElement(pelement, anotherSchema);
assertTrue(isAImporedElement);
}
Aggregations