use of org.eclipse.xsd.XSDElementDeclaration in project tmdm-studio-se by Talend.
the class XSDUtilTest method testBuildEntityUsedComplexTypeMap.
@Test
public void testBuildEntityUsedComplexTypeMap() throws Exception {
// $NON-NLS-1$
String fileName = "Product_0.1.xsd";
String xsdString = TestUtil.readTestResource(XSDUtilTest.this.getClass(), fileName);
assertNotNull(xsdString);
XSDSchema xsdSchema = Util.getXSDSchema(xsdString);
Map<XSDElementDeclaration, List<XSDComplexTypeDefinition>> entityMapComplexType = XSDUtil.buildEntityUsedComplexTypeMap(xsdSchema);
assertNotNull(entityMapComplexType);
assertTrue(entityMapComplexType.size() == 3);
for (List<XSDComplexTypeDefinition> ctypes : entityMapComplexType.values()) {
assertTrue(ctypes.size() == 1);
}
}
use of org.eclipse.xsd.XSDElementDeclaration in project tmdm-studio-se by Talend.
the class AddBrowseItemsWizardRTest method testGetKeysForViewElements.
/**
* Test method for
* {@link org.talend.mdm.repository.ui.wizards.view.AddBrowseItemsWizardR#getKeysForViewElements(org.eclipse.xsd.XSDElementDeclaration)}
* .
*
* @throws Exception
*/
@Test
public void testGetKeysForViewElements() throws Exception {
String xsd = null;
XSDSchema schema = null;
xsd = // $NON-NLS-1$
"<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" + // $NON-NLS-1$
"<xsd:import namespace=\"http://www.w3.org/2001/XMLSchema\"/>" + // $NON-NLS-1$
"<xsd:element name=\"Entity\">" + // $NON-NLS-1$
"<xsd:complexType>" + // $NON-NLS-1$
"<xsd:all>" + // $NON-NLS-1$
"<xsd:element name=\"id\" type=\"xsd:string\"/>" + // $NON-NLS-1$
"<xsd:element maxOccurs=\"1\" minOccurs=\"0\" name=\"fff\" type=\"xsd:string\"/>" + // $NON-NLS-1$
"<xsd:element maxOccurs=\"1\" minOccurs=\"0\" name=\"eee\" type=\"xsd:string\"/>" + // $NON-NLS-1$
"<xsd:element maxOccurs=\"1\" minOccurs=\"0\" name=\"ddd\" type=\"xsd:string\"/>" + // $NON-NLS-1$
"<xsd:element maxOccurs=\"1\" minOccurs=\"0\" name=\"ggg\" type=\"xsd:string\"/>" + // $NON-NLS-1$
"<xsd:element maxOccurs=\"1\" minOccurs=\"0\" name=\"hhh\" type=\"xsd:string\"/>" + // $NON-NLS-1$
"<xsd:element maxOccurs=\"1\" minOccurs=\"0\" name=\"ccc\" type=\"xsd:string\"/>" + // $NON-NLS-1$
"</xsd:all>" + // $NON-NLS-1$
"</xsd:complexType>" + "<xsd:unique name=\"Entity\">" + // $NON-NLS-1$ //$NON-NLS-2$
"<xsd:selector xpath=\".\"/>" + // $NON-NLS-1$
"<xsd:field xpath=\"id\"/>" + // $NON-NLS-1$
"</xsd:unique>" + // $NON-NLS-1$
"</xsd:element>" + // $NON-NLS-1$
"</xsd:schema>";
schema = Util.getXSDSchema(xsd);
schema.getElementDeclarations();
XSDElementDeclaration decl = schema.getElementDeclarations().get(0);
List<String> idList = new ArrayList<String>();
idList.add("Entity/id");
List<String> keys = AddBrowseItemsWizardR.getFieldsForViewElements(decl, idList);
keys.addAll(0, idList);
assertNotNull(keys);
// Only the top 5 attributes restriction is test here,
// the original elements count is 7
assertEquals(keys.size(), 5);
for (int i = 0; i < keys.size(); i++) {
switch(i) {
case 0:
// the element name is #eee ,so it now has been replaced to empty string
// $NON-NLS-1$
assertEquals(keys.get(i), "Entity/id");
continue;
case 1:
// $NON-NLS-1$
assertEquals(keys.get(i), "Entity/fff");
continue;
case 2:
// $NON-NLS-1$
assertEquals(keys.get(i), "Entity/eee");
continue;
case 3:
// $NON-NLS-1$
assertEquals(keys.get(i), "Entity/ddd");
continue;
case 4:
// $NON-NLS-1$
assertEquals(keys.get(i), "Entity/ggg");
continue;
}
}
}
use of org.eclipse.xsd.XSDElementDeclaration in project tmdm-studio-se by Talend.
the class UtilMockTest method testGetComplexChilds.
@Test
public void testGetComplexChilds() {
// $NON-NLS-1$
String methodToExecute = "getComplexChilds";
// $NON-NLS-1$
String method_getChildElements = "getChildElements";
// $NON-NLS-1$
String parentxpath = "";
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
String[] names = { "simpleA", "complexB", "simpleC", "complexD" };
// $NON-NLS-1$ //$NON-NLS-2$
String[] childpath = { "child/aa", "child/bb" };
boolean onlyTopLevel = false;
Set<Object> visited = new HashSet<Object>();
XSDFactory factory = XSDFactory.eINSTANCE;
XSDComplexTypeDefinition complexTypeDef = factory.createXSDComplexTypeDefinition();
PowerMockito.mockStatic(Util.class);
try {
PowerMockito.when(Util.class, methodToExecute, anyString(), any(XSDComplexTypeDefinition.class), anyBoolean(), anySet()).thenCallRealMethod();
Map<String, XSDParticle> complexChilds = Whitebox.invokeMethod(Util.class, methodToExecute, parentxpath, complexTypeDef, true, null);
assertNotNull(complexChilds);
assertTrue(complexChilds.isEmpty());
//
Map<String, XSDParticle> childElements1 = new HashMap<String, XSDParticle>();
XSDParticle childParticle1 = factory.createXSDParticle();
XSDParticle childParticle2 = factory.createXSDParticle();
childElements1.put(childpath[0], childParticle1);
childElements1.put(childpath[1], childParticle2);
PowerMockito.when(Util.class, method_getChildElements, anyString(), any(XSDComplexTypeDefinition.class), anyBoolean(), anySet()).thenReturn(childElements1);
XSDModelGroup group = factory.createXSDModelGroup();
for (int i = 0; i < names.length; i++) {
XSDParticle particle = factory.createXSDParticle();
XSDElementDeclaration elementDecl = factory.createXSDElementDeclaration();
XSDTypeDefinition xsdType = factory.createXSDSimpleTypeDefinition();
if (i % 2 != 0) {
xsdType = factory.createXSDComplexTypeDefinition();
}
elementDecl.setTypeDefinition(xsdType);
elementDecl.setName(names[i]);
particle.setTerm(elementDecl);
group.getParticles().add(particle);
}
XSDParticle typeParticle = factory.createXSDParticle();
typeParticle.setTerm(group);
complexTypeDef.setContent(typeParticle);
complexChilds = Whitebox.invokeMethod(Util.class, methodToExecute, parentxpath, complexTypeDef, onlyTopLevel, visited);
// $NON-NLS-1$
assertTrue(complexChilds.keySet().contains("simpleA"));
// $NON-NLS-1$
assertTrue(complexChilds.keySet().contains("simpleC"));
// $NON-NLS-1$
assertTrue(complexChilds.keySet().contains("//complexB"));
// $NON-NLS-1$
assertTrue(complexChilds.keySet().contains("//complexD"));
if (complexChilds.size() == 6) {
assertTrue(complexChilds.keySet().contains(childpath[0]));
assertTrue(complexChilds.keySet().contains(childpath[1]));
}
onlyTopLevel = true;
complexChilds = Whitebox.invokeMethod(Util.class, methodToExecute, parentxpath, complexTypeDef, onlyTopLevel, visited);
// $NON-NLS-1$
assertTrue(complexChilds.keySet().contains("simpleA"));
// $NON-NLS-1$
assertTrue(complexChilds.keySet().contains("simpleC"));
// $NON-NLS-1$
assertTrue(complexChilds.keySet().contains("//complexB"));
// $NON-NLS-1$
assertTrue(complexChilds.keySet().contains("//complexD"));
//
// $NON-NLS-1$
parentxpath = "parentXPath";
onlyTopLevel = false;
visited.clear();
complexChilds = Whitebox.invokeMethod(Util.class, methodToExecute, parentxpath, complexTypeDef, onlyTopLevel, visited);
// $NON-NLS-1$
assertTrue(complexChilds.keySet().contains("parentXPath/simpleA"));
// $NON-NLS-1$
assertTrue(complexChilds.keySet().contains("parentXPath/simpleC"));
// $NON-NLS-1$
assertTrue(complexChilds.keySet().contains("parentXPath//complexB"));
// $NON-NLS-1$
assertTrue(complexChilds.keySet().contains("parentXPath//complexD"));
if (complexChilds.size() == 6) {
assertTrue(complexChilds.keySet().contains(childpath[0]));
assertTrue(complexChilds.keySet().contains(childpath[1]));
}
//
onlyTopLevel = true;
visited.clear();
complexChilds = Whitebox.invokeMethod(Util.class, methodToExecute, parentxpath, complexTypeDef, onlyTopLevel, visited);
// $NON-NLS-1$
assertTrue(complexChilds.keySet().contains("parentXPath/simpleA"));
// $NON-NLS-1$
assertTrue(complexChilds.keySet().contains("parentXPath/simpleC"));
// $NON-NLS-1$
assertTrue(complexChilds.keySet().contains("parentXPath//complexB"));
// $NON-NLS-1$
assertTrue(complexChilds.keySet().contains("parentXPath//complexD"));
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
use of org.eclipse.xsd.XSDElementDeclaration in project tmdm-studio-se by Talend.
the class UtilMockTest method testIsReferrenced.
@Test
public void testIsReferrenced() {
PowerMockito.mockStatic(Util.class);
try {
// $NON-NLS-1$
String method_isReferenced = "isReferrenced";
PowerMockito.when(Util.class, method_isReferenced, Mockito.any(XSDElementDeclaration.class), Mockito.any(XSDElementDeclaration.class)).thenCallRealMethod();
XSDElementDeclaration xsdElementDeclaration1 = XSDFactory.eINSTANCE.createXSDElementDeclaration();
XSDElementDeclaration xsdElementDeclaration2 = XSDFactory.eINSTANCE.createXSDElementDeclaration();
//
boolean isReferenced = Whitebox.invokeMethod(Util.class, method_isReferenced, xsdElementDeclaration1, xsdElementDeclaration1);
assertTrue(isReferenced);
//
isReferenced = Whitebox.invokeMethod(Util.class, method_isReferenced, xsdElementDeclaration1, xsdElementDeclaration2);
assertFalse(isReferenced);
//
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
XSDParticle xsdParticle = buildXSDParticleWithChildren(3, new String[] { "a", "b", "c" });
XSDComplexTypeDefinition xsdComplexTypeDefinition = XSDFactory.eINSTANCE.createXSDComplexTypeDefinition();
xsdComplexTypeDefinition.setContent(xsdParticle);
xsdElementDeclaration2.setTypeDefinition(xsdComplexTypeDefinition);
isReferenced = Whitebox.invokeMethod(Util.class, method_isReferenced, xsdElementDeclaration1, xsdElementDeclaration2);
assertFalse(isReferenced);
//
XSDParticle xsdParticle1 = XSDFactory.eINSTANCE.createXSDParticle();
xsdParticle1.setTerm(xsdElementDeclaration1);
XSDModelGroup modelGroup = ((XSDModelGroup) xsdParticle.getTerm());
modelGroup.getContents().add(xsdParticle1);
isReferenced = Whitebox.invokeMethod(Util.class, method_isReferenced, xsdElementDeclaration1, xsdElementDeclaration2);
assertTrue(isReferenced);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
use of org.eclipse.xsd.XSDElementDeclaration in project tmdm-studio-se by Talend.
the class UtilMockTest method testUpdatePrimaryKeyInfo.
@Test
public void testUpdatePrimaryKeyInfo() {
// $NON-NLS-1$
String method_private = "updatePrimaryKeyInfo";
// $NON-NLS-1$
String app_primaryKey = "X_PrimaryKeyInfo";
XSDFactory factory = XSDFactory.eINSTANCE;
PowerMockito.mockStatic(Util.class);
try {
PowerMockito.when(Util.class, method_private, any(XSDElementDeclaration.class), anyString(), anyString()).thenCallRealMethod();
XSDElementDeclaration concept = factory.createXSDElementDeclaration();
XSDAnnotation conceptAnnotation = factory.createXSDAnnotation();
concept.setAnnotation(conceptAnnotation);
XSDSchema xsdSchema = factory.createXSDSchema();
xsdSchema.getContents().add(concept);
// $NON-NLS-1$
String pk1 = "Product/Id";
// $NON-NLS-1$
String pk2 = "Product/code";
// $NON-NLS-1$
String attr_key = "source";
// $NON-NLS-1$
String namespaceURI = "http://www.w3.org/XML/1998/namespace";
Document doc = getEmptyDocument();
xsdSchema.setDocument(doc);
// $NON-NLS-1$
Element appinfoElement1 = doc.createElementNS(namespaceURI, "appinfo");
appinfoElement1.setAttribute(attr_key, app_primaryKey);
appinfoElement1.appendChild(doc.createTextNode(pk1));
// $NON-NLS-1$
Element appinfoElement2 = doc.createElementNS(namespaceURI, "appinfosssss");
appinfoElement2.setAttribute(attr_key, app_primaryKey);
appinfoElement2.appendChild(doc.createTextNode(pk2));
// $NON-NLS-1$
Element annoElement = doc.createElement("s");
annoElement.appendChild(appinfoElement1);
annoElement.appendChild(appinfoElement2);
conceptAnnotation.setElement(annoElement);
EList<Element> applicationInformations = conceptAnnotation.getApplicationInformation();
applicationInformations.add(appinfoElement1);
applicationInformations.add(appinfoElement2);
// $NON-NLS-1$
String newValue = "Product_sufix/Id";
Whitebox.invokeMethod(Util.class, method_private, concept, pk1, newValue);
assertEquals(newValue, annoElement.getChildNodes().item(1).getTextContent());
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
Aggregations