use of org.eclipse.xsd.XSDIdentityConstraintDefinition in project tmdm-studio-se by Talend.
the class UtilTest method testGetComponentName.
@Test
public void testGetComponentName() {
// $NON-NLS-1$ //$NON-NLS-2$
String prefix = "name=\"", suffix = "\"";
String[] objNames = { // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
"product_elementdeclaration", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
"product_particle", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
"p_complextype", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
"p_simpletype", "product_identityconstraintdef", // $NON-NLS-1$ //$NON-NLS-2$
"p_xpathdef" };
String[] expectedObjNames = new String[objNames.length];
for (int i = 0; i < objNames.length - 1; i++) {
expectedObjNames[i] = prefix + objNames[i] + suffix;
}
// $NON-NLS-1$
expectedObjNames[objNames.length - 1] = "xpath=\"" + objNames[objNames.length - 1] + suffix;
XSDFactory factory = XSDFactory.eINSTANCE;
try {
XSDElementDeclaration xsdElementDeclaration = factory.createXSDElementDeclaration();
xsdElementDeclaration.setName(objNames[0]);
XSDParticle xsdParticle = factory.createXSDParticle();
XSDElementDeclaration xsdParticleDeclaration = factory.createXSDElementDeclaration();
xsdParticleDeclaration.setName(objNames[1]);
xsdParticle.setTerm(xsdParticleDeclaration);
XSDComplexTypeDefinition xsdComplexTypeDefinition = factory.createXSDComplexTypeDefinition();
xsdComplexTypeDefinition.setName(objNames[2]);
XSDSimpleTypeDefinition xsdSimpleTypeDefinition = factory.createXSDSimpleTypeDefinition();
xsdSimpleTypeDefinition.setName(objNames[3]);
XSDIdentityConstraintDefinition xsdIdConsDef = factory.createXSDIdentityConstraintDefinition();
xsdIdConsDef.setName(objNames[4]);
XSDXPathDefinition xsdPathDefinition = factory.createXSDXPathDefinition();
xsdPathDefinition.setValue(objNames[5]);
String name = Util.getComponentName(xsdElementDeclaration);
assertEquals(expectedObjNames[0], name);
name = Util.getComponentName(xsdParticle);
assertEquals(expectedObjNames[1], name);
name = Util.getComponentName(xsdComplexTypeDefinition);
assertEquals(expectedObjNames[2], name);
name = Util.getComponentName(xsdSimpleTypeDefinition);
assertEquals(expectedObjNames[3], name);
name = Util.getComponentName(xsdIdConsDef);
assertEquals(expectedObjNames[4], name);
name = Util.getComponentName(xsdPathDefinition);
assertEquals(expectedObjNames[5], name);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
use of org.eclipse.xsd.XSDIdentityConstraintDefinition in project tmdm-studio-se by Talend.
the class UtilTest method testRetrieveXSDComponentPath.
@Test
public void testRetrieveXSDComponentPath() {
// $NON-NLS-1$
String conceptName = "Product";
// $NON-NLS-1$
String complextypeName = "ctype";
// $NON-NLS-1$
String simpletypeName = "stype";
// $NON-NLS-1$
String identityName = "identityName";
// $NON-NLS-1$
String xsdxpathValue = "Id";
// $NON-NLS-1$
String childelementName = "childelementName";
List<String> xsdComponentPath = null;
XSDFactory factory = XSDFactory.eINSTANCE;
XSDSchema xschema = factory.createXSDSchema();
XSDElementDeclaration concept = factory.createXSDElementDeclaration();
concept.setName(conceptName);
xschema.getContents().add(concept);
XSDComplexTypeDefinition complexTypeDefinition = factory.createXSDComplexTypeDefinition();
complexTypeDefinition.setBaseTypeDefinition(// $NON-NLS-1$ );
xschema.resolveComplexTypeDefinition(xschema.getSchemaForSchemaNamespace(), "anyType"));
xschema.getContents().add(complexTypeDefinition);
XSDSimpleTypeDefinition simpleTypeDefinition = factory.createXSDSimpleTypeDefinition();
simpleTypeDefinition.setName(simpletypeName);
XSDModelGroup modelGroup = factory.createXSDModelGroup();
modelGroup.setCompositor(XSDCompositor.SEQUENCE_LITERAL);
XSDParticle typeparticle = factory.createXSDParticle();
complexTypeDefinition.setContent(typeparticle);
typeparticle.setContent(modelGroup);
XSDParticle childParticle = factory.createXSDParticle();
XSDElementDeclaration childelement = factory.createXSDElementDeclaration();
childelement.setName(childelementName);
childParticle.setContent(childelement);
childParticle.setTerm(childelement);
modelGroup.getContents().add(childParticle);
XSDIdentityConstraintDefinition IdConsDef = factory.createXSDIdentityConstraintDefinition();
IdConsDef.setName(identityName);
concept.getIdentityConstraintDefinitions().add(IdConsDef);
XSDXPathDefinition xsdXPathDefinition = factory.createXSDXPathDefinition();
xsdXPathDefinition.setValue(xsdxpathValue);
IdConsDef.getFields().add(xsdXPathDefinition);
XSDAnnotation conceptAnnotation = factory.createXSDAnnotation();
concept.setAnnotation(conceptAnnotation);
XSDElementDeclaration anotherConcept = factory.createXSDElementDeclaration();
// $NON-NLS-1$
String concept2name = "anotherConcept";
anotherConcept.setName(concept2name);
xschema.getContents().add(anotherConcept);
//
List<String> buffer = new ArrayList<String>();
xsdComponentPath = Util.retrieveXSDComponentPath(concept, xschema, buffer);
assertEquals(1, xsdComponentPath.size());
// $NON-NLS-1$ //$NON-NLS-2$
assertEquals("//xsd:element[@name='" + conceptName + "']", xsdComponentPath.get(0));
//
buffer.clear();
xsdComponentPath = Util.retrieveXSDComponentPath(complexTypeDefinition, xschema, buffer);
assertEquals(1, xsdComponentPath.size());
// $NON-NLS-1$
assertEquals("//xsd:complexType", xsdComponentPath.get(0));
//
buffer.clear();
complexTypeDefinition.setName(complextypeName);
xsdComponentPath = Util.retrieveXSDComponentPath(complexTypeDefinition, xschema, buffer);
assertEquals(1, xsdComponentPath.size());
// $NON-NLS-1$ //$NON-NLS-2$
assertEquals("//xsd:complexType[@name='" + complextypeName + "']", xsdComponentPath.get(0));
//
buffer.clear();
xsdComponentPath = Util.retrieveXSDComponentPath(simpleTypeDefinition, xschema, buffer);
assertEquals(1, xsdComponentPath.size());
// $NON-NLS-1$ //$NON-NLS-2$
assertEquals("//xsd:simpleType[@name='" + simpletypeName + "']", xsdComponentPath.get(0));
//
buffer.clear();
xsdComponentPath = Util.retrieveXSDComponentPath(modelGroup, xschema, buffer);
assertEquals(2, xsdComponentPath.size());
// $NON-NLS-1$
assertTrue(xsdComponentPath.contains("//xsd:" + XSDCompositor.SEQUENCE_LITERAL.getLiteral()));
// $NON-NLS-1$ //$NON-NLS-2$
assertTrue(xsdComponentPath.contains("//xsd:complexType[@name='" + complextypeName + "']"));
//
buffer.clear();
xsdComponentPath = Util.retrieveXSDComponentPath(IdConsDef, xschema, buffer);
assertEquals(2, xsdComponentPath.size());
// $NON-NLS-1$ //$NON-NLS-2$
assertTrue(xsdComponentPath.contains("//xsd:element[@name='" + conceptName + "']"));
// $NON-NLS-1$ //$NON-NLS-2$
assertTrue(xsdComponentPath.contains("//xsd:unique[@name='" + identityName + "']"));
//
buffer.clear();
xsdComponentPath = Util.retrieveXSDComponentPath(xsdXPathDefinition, xschema, buffer);
assertEquals(3, xsdComponentPath.size());
// $NON-NLS-1$ //$NON-NLS-2$
assertTrue(xsdComponentPath.contains("//xsd:element[@name='" + conceptName + "']"));
// $NON-NLS-1$ //$NON-NLS-2$
assertTrue(xsdComponentPath.contains("//xsd:unique[@name='" + identityName + "']"));
// $NON-NLS-1$ //$NON-NLS-2$
assertTrue(xsdComponentPath.contains("//xsd:field[@xpath='" + xsdxpathValue + "']"));
//
buffer.clear();
xsdComponentPath = Util.retrieveXSDComponentPath(conceptAnnotation, xschema, buffer);
assertEquals(2, xsdComponentPath.size());
// $NON-NLS-1$ //$NON-NLS-2$
assertTrue(xsdComponentPath.contains("//xsd:element[@name='" + conceptName + "']"));
// $NON-NLS-1$
assertTrue(xsdComponentPath.contains("//xsd:annotation"));
//
buffer.clear();
xsdComponentPath = Util.retrieveXSDComponentPath(childParticle, xschema, buffer);
assertEquals(2, xsdComponentPath.size());
// $NON-NLS-1$
assertTrue(xsdComponentPath.contains("//xsd:" + XSDCompositor.SEQUENCE_LITERAL.getLiteral()));
// $NON-NLS-1$ //$NON-NLS-2$
assertTrue(xsdComponentPath.contains("//xsd:complexType[@name='" + complextypeName + "']"));
//
buffer.clear();
xsdComponentPath = Util.retrieveXSDComponentPath(childelement, xschema, buffer);
assertEquals(3, xsdComponentPath.size());
// $NON-NLS-1$
assertTrue(xsdComponentPath.contains("//xsd:" + XSDCompositor.SEQUENCE_LITERAL.getLiteral()));
// $NON-NLS-1$ //$NON-NLS-2$
assertTrue(xsdComponentPath.contains("//xsd:complexType[@name='" + complextypeName + "']"));
// $NON-NLS-1$ //$NON-NLS-2$
assertTrue(xsdComponentPath.contains("//xsd:element[@name='" + childelementName + "']"));
//
buffer.clear();
childelement.setResolvedElementDeclaration(anotherConcept);
xsdComponentPath = Util.retrieveXSDComponentPath(childParticle, xschema, buffer);
assertEquals(3, xsdComponentPath.size());
// $NON-NLS-1$
assertTrue(xsdComponentPath.contains("//xsd:" + XSDCompositor.SEQUENCE_LITERAL.getLiteral()));
// $NON-NLS-1$ //$NON-NLS-2$
assertTrue(xsdComponentPath.contains("//xsd:complexType[@name='" + complextypeName + "']"));
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertTrue(xsdComponentPath.contains("//xsd:element[@name='" + concept2name + "' or @ref='" + concept2name + "']"));
//
buffer.clear();
// $NON-NLS-1$
String targetNamespace = "targetnamespace";
// $NON-NLS-1$
String key = "prefix";
anotherConcept.setTargetNamespace(targetNamespace);
xschema.getQNamePrefixToNamespaceMap().put(key, targetNamespace);
xsdComponentPath = Util.retrieveXSDComponentPath(childParticle, xschema, buffer);
assertEquals(3, xsdComponentPath.size());
// $NON-NLS-1$
assertTrue(xsdComponentPath.contains("//xsd:" + XSDCompositor.SEQUENCE_LITERAL.getLiteral()));
// $NON-NLS-1$ //$NON-NLS-2$
assertTrue(xsdComponentPath.contains("//xsd:complexType[@name='" + complextypeName + "']"));
assertTrue(xsdComponentPath.contains(// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
"//xsd:element[@name='" + concept2name + "' or @ref='" + key + ":" + concept2name + "']"));
}
use of org.eclipse.xsd.XSDIdentityConstraintDefinition in project tmdm-studio-se by Talend.
the class UtilTest method testFilterOutDuplicatedElems.
@Test
public void testFilterOutDuplicatedElems() {
// $NON-NLS-1$
String simpletypeName = "simpletype";
// $NON-NLS-1$
String complextypeName = "complextype";
// $NON-NLS-1$
String concepName = "concept";
// $NON-NLS-1$
String idconstraintName = "identityconstraint";
// $NON-NLS-1$
String modelgroupName = "modelgroup";
XSDFactory factory = XSDFactory.eINSTANCE;
XSDSimpleTypeDefinition simpleTypeDefinition = factory.createXSDSimpleTypeDefinition();
XSDComplexTypeDefinition complexTypeDefinition = factory.createXSDComplexTypeDefinition();
XSDElementDeclaration elementDeclaration = factory.createXSDElementDeclaration();
XSDIdentityConstraintDefinition identityConstraintDefinition = factory.createXSDIdentityConstraintDefinition();
XSDModelGroupDefinition modelGroupDefinition = factory.createXSDModelGroupDefinition();
simpleTypeDefinition.setName(simpletypeName);
complexTypeDefinition.setName(complextypeName);
elementDeclaration.setName(concepName);
identityConstraintDefinition.setName(idconstraintName);
modelGroupDefinition.setName(modelgroupName);
XSDNamedComponent[] checkedElements = { simpleTypeDefinition, complexTypeDefinition, elementDeclaration, identityConstraintDefinition, modelGroupDefinition };
Object[] duplicatedElems = Util.filterOutDuplicatedElems(checkedElements);
List<XSDNamedComponent> allElements = Arrays.asList(checkedElements);
assertNotNull(duplicatedElems);
assertTrue(checkedElements.length == duplicatedElems.length);
for (int i = 0; i < duplicatedElems.length; i++) {
assertTrue(allElements.contains(duplicatedElems[i]));
}
//
XSDSimpleTypeDefinition simpleTypeDefinition2 = factory.createXSDSimpleTypeDefinition();
XSDComplexTypeDefinition complexTypeDefinition2 = factory.createXSDComplexTypeDefinition();
XSDElementDeclaration elementDeclaration2 = factory.createXSDElementDeclaration();
XSDIdentityConstraintDefinition identityConstraintDefinition2 = factory.createXSDIdentityConstraintDefinition();
XSDModelGroupDefinition modelGroupDefinition2 = factory.createXSDModelGroupDefinition();
simpleTypeDefinition2.setName(simpletypeName);
complexTypeDefinition2.setName(complextypeName);
elementDeclaration2.setName(concepName);
identityConstraintDefinition2.setName(idconstraintName);
modelGroupDefinition2.setName(modelgroupName);
XSDNamedComponent[] checkedElements2 = { simpleTypeDefinition, complexTypeDefinition, elementDeclaration, identityConstraintDefinition, modelGroupDefinition, simpleTypeDefinition2, complexTypeDefinition2, elementDeclaration2, identityConstraintDefinition2, modelGroupDefinition2 };
duplicatedElems = Util.filterOutDuplicatedElems(checkedElements2);
assertNotNull(duplicatedElems);
assertTrue(checkedElements.length == duplicatedElems.length);
for (int i = 0; i < duplicatedElems.length; i++) {
assertTrue(allElements.contains(duplicatedElems[i]));
}
}
use of org.eclipse.xsd.XSDIdentityConstraintDefinition in project tmdm-studio-se by Talend.
the class UtilTest method testCheckConcept.
@Test
public void testCheckConcept() {
XSDElementDeclaration concept = XSDFactory.eINSTANCE.createXSDElementDeclaration();
XSDIdentityConstraintDefinition xsdIdConsDef1 = XSDFactory.eINSTANCE.createXSDIdentityConstraintDefinition();
XSDIdentityConstraintDefinition xsdIdConsDef2 = XSDFactory.eINSTANCE.createXSDIdentityConstraintDefinition();
concept.getIdentityConstraintDefinitions().add(xsdIdConsDef1);
concept.getIdentityConstraintDefinitions().add(xsdIdConsDef2);
boolean isConcept = Util.checkConcept(concept);
assertFalse(isConcept);
xsdIdConsDef1.setIdentityConstraintCategory(XSDIdentityConstraintCategory.UNIQUE_LITERAL);
xsdIdConsDef2.setIdentityConstraintCategory(XSDIdentityConstraintCategory.KEY_LITERAL);
isConcept = Util.checkConcept(concept);
assertTrue(isConcept);
}
use of org.eclipse.xsd.XSDIdentityConstraintDefinition in project tmdm-studio-se by Talend.
the class EditXSDIdentityConstraintNameValidatorTest method testIsValid.
@Test
public void testIsValid() {
// $NON-NLS-1$
String entityName = "Product";
XSDElementDeclaration mockEntity = mock(XSDElementDeclaration.class);
when(mockEntity.getName()).thenReturn(entityName);
XSDSchema mockSchema = mock(XSDSchema.class);
XSDIdentityConstraintDefinition mockConstraintDef0 = mock(XSDIdentityConstraintDefinition.class);
when(mockConstraintDef0.getName()).thenReturn(entityName);
when(mockConstraintDef0.getSchema()).thenReturn(mockSchema);
when(mockConstraintDef0.getIdentityConstraintCategory()).thenReturn(XSDIdentityConstraintCategory.UNIQUE_LITERAL);
when(mockConstraintDef0.getContainer()).thenReturn(mockEntity);
EList<XSDIdentityConstraintDefinition> constraintDefs = new BasicEList<XSDIdentityConstraintDefinition>();
XSDIdentityConstraintDefinition mockConstraintDef1 = mock(XSDIdentityConstraintDefinition.class);
// $NON-NLS-1$
when(mockConstraintDef1.getName()).thenReturn("Family");
constraintDefs.add(mockConstraintDef0);
constraintDefs.add(mockConstraintDef1);
when(mockSchema.getIdentityConstraintDefinitions()).thenReturn(constraintDefs);
EditXSDIdentityConstraintNameValidator validator = new EditXSDIdentityConstraintNameValidator(mockConstraintDef0);
// /////////
String constraintName = null;
String msg = validator.isValid(constraintName);
assertEquals(Messages.EditXSDIdXXValidator_UniqueKeyNameCannotbeEmpty, msg);
// $NON-NLS-1$
constraintName = "";
msg = validator.isValid(constraintName);
assertEquals(Messages.EditXSDIdXXValidator_UniqueKeyNameCannotbeEmpty, msg);
// ///
// $NON-NLS-1$ blank string on the head
constraintName = " Prod_897";
msg = validator.isValid(constraintName);
assertEquals(Messages.EditXSDIdXXValidator_UniqueKeyNameCannotContainEmpty, msg);
// $NON-NLS-1$ blank string among it
constraintName = "Prod 897";
msg = validator.isValid(constraintName);
assertEquals(Messages.EditXSDIdXXValidator_UniqueKeyNameCannotContainEmpty, msg);
// ///
// $NON-NLS-1$ not equal to entity name
constraintName = entityName + "_tail";
msg = validator.isValid(constraintName);
assertEquals(Messages.EditXSDIdXXValidator_UniqueKeyNameMustbeEqualXX, msg);
//
constraintName = entityName;
msg = validator.isValid(constraintName);
assertEquals(Messages.EditXSDIdXXValidator_KeyAlreadyExist, msg);
}
Aggregations