Search in sources :

Example 16 with XSDFactory

use of org.eclipse.xsd.XSDFactory in project tmdm-studio-se by Talend.

the class UtilTest method testGetTopParent.

@Test
public void testGetTopParent() {
    // 
    List<Object> topParent = Util.getTopParent(null);
    assertNull(topParent);
    XSDFactory factory = XSDFactory.eINSTANCE;
    // 
    XSDElementDeclaration concept = factory.createXSDElementDeclaration();
    topParent = Util.getTopParent(concept);
    assertNull(topParent);
    // concept with three children
    // $NON-NLS-1$
    String element1 = "Id";
    // $NON-NLS-1$
    String element2 = "code";
    // $NON-NLS-1$
    String element3 = "address";
    XSDSchema xsdSchema = factory.createXSDSchema();
    xsdSchema.getContents().add(concept);
    XSDComplexTypeDefinition xsdComplexTypeDef = factory.createXSDComplexTypeDefinition();
    xsdComplexTypeDef.setBaseTypeDefinition(// $NON-NLS-1$
    xsdSchema.resolveComplexTypeDefinition(xsdSchema.getSchemaForSchemaNamespace(), "anyType"));
    concept.setAnonymousTypeDefinition(xsdComplexTypeDef);
    XSDParticle xsdParticle = factory.createXSDParticle();
    xsdComplexTypeDef.setContent(xsdParticle);
    XSDModelGroup xsdModelGroup = factory.createXSDModelGroup();
    xsdParticle.setContent(xsdModelGroup);
    XSDParticle childParticle1 = factory.createXSDParticle();
    XSDElementDeclaration childElement1 = factory.createXSDElementDeclaration();
    childElement1.setName(element1);
    childParticle1.setContent(childElement1);
    XSDParticle childParticle2 = factory.createXSDParticle();
    XSDElementDeclaration childElement2 = factory.createXSDElementDeclaration();
    childElement2.setName(element2);
    childParticle2.setContent(childElement2);
    XSDParticle childParticle3 = factory.createXSDParticle();
    XSDElementDeclaration childElement3 = factory.createXSDElementDeclaration();
    childElement3.setName(element3);
    childParticle3.setContent(childElement3);
    xsdModelGroup.getContents().add(childParticle1);
    xsdModelGroup.getContents().add(childParticle2);
    xsdModelGroup.getContents().add(childParticle3);
    XSDIdentityConstraintDefinition xsdIdConsDef = factory.createXSDIdentityConstraintDefinition();
    concept.getIdentityConstraintDefinitions().add(xsdIdConsDef);
    XSDXPathDefinition xsdXPathDefinition1 = factory.createXSDXPathDefinition();
    xsdXPathDefinition1.setValue(element1);
    XSDXPathDefinition xsdXPathDefinition2 = factory.createXSDXPathDefinition();
    xsdXPathDefinition2.setValue(element2);
    xsdIdConsDef.getFields().add(xsdXPathDefinition1);
    xsdIdConsDef.getFields().add(xsdXPathDefinition2);
    // complex type with one child
    XSDComplexTypeDefinition xsdComplexTypeDef2 = factory.createXSDComplexTypeDefinition();
    xsdComplexTypeDef2.setBaseTypeDefinition(// $NON-NLS-1$
    xsdSchema.resolveComplexTypeDefinition(xsdSchema.getSchemaForSchemaNamespace(), "anyType"));
    XSDParticle xsdParticle2 = factory.createXSDParticle();
    xsdComplexTypeDef2.setContent(xsdParticle2);
    XSDModelGroup xsdModelGroup2 = factory.createXSDModelGroup();
    xsdParticle2.setContent(xsdModelGroup2);
    XSDParticle childParticle_2 = factory.createXSDParticle();
    XSDElementDeclaration childElement_2 = factory.createXSDElementDeclaration();
    childElement_2.setName(element1);
    childParticle_2.setContent(childElement_2);
    xsdModelGroup2.getContents().add(childParticle_2);
    xsdSchema.getContents().add(xsdComplexTypeDef2);
    // 
    topParent = Util.getTopParent(childElement1);
    assertNotNull(topParent);
    assertTrue(topParent.size() == 2);
    // 
    topParent = Util.getTopParent(childElement_2);
    assertNull(topParent);
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDIdentityConstraintDefinition(org.eclipse.xsd.XSDIdentityConstraintDefinition) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDXPathDefinition(org.eclipse.xsd.XSDXPathDefinition) XSDParticle(org.eclipse.xsd.XSDParticle) XSDSchema(org.eclipse.xsd.XSDSchema) Test(org.junit.Test)

Example 17 with XSDFactory

use of org.eclipse.xsd.XSDFactory in project tmdm-studio-se by Talend.

the class UtilTest method testIsSimpleTypedParticle.

@Test
public void testIsSimpleTypedParticle() {
    boolean isSimpleTypedParticle = Util.isSimpleTypedParticle(null);
    assertFalse(isSimpleTypedParticle);
    XSDFactory factory = XSDFactory.eINSTANCE;
    XSDParticle particle = factory.createXSDParticle();
    isSimpleTypedParticle = Util.isSimpleTypedParticle(particle);
    assertFalse(isSimpleTypedParticle);
    XSDElementDeclaration elementDeclaration = factory.createXSDElementDeclaration();
    elementDeclaration.setTypeDefinition(factory.createXSDSimpleTypeDefinition());
    particle.setContent(elementDeclaration);
    isSimpleTypedParticle = Util.isSimpleTypedParticle(particle);
    assertTrue(isSimpleTypedParticle);
    XSDModelGroup modelGroup = factory.createXSDModelGroup();
    particle.setContent(modelGroup);
    isSimpleTypedParticle = Util.isSimpleTypedParticle(particle);
    assertFalse(isSimpleTypedParticle);
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDParticle(org.eclipse.xsd.XSDParticle) Test(org.junit.Test)

Example 18 with XSDFactory

use of org.eclipse.xsd.XSDFactory in project tmdm-studio-se by Talend.

the class XSDUtilTest method testGetKeyFields.

@Test
public void testGetKeyFields() throws Exception {
    XSDFactory factory = XSDFactory.eINSTANCE;
    // $NON-NLS-1$
    String conceptName = "Product";
    // $NON-NLS-1$ //$NON-NLS-2$
    String[] fields = { "Id", "code" };
    XSDElementDeclaration concept = factory.createXSDElementDeclaration();
    concept.setName(conceptName);
    XSDIdentityConstraintDefinition ideCDef = factory.createXSDIdentityConstraintDefinition();
    XSDXPathDefinition xpathDefinition1 = factory.createXSDXPathDefinition();
    xpathDefinition1.setValue(fields[0]);
    ideCDef.getFields().add(xpathDefinition1);
    concept.getIdentityConstraintDefinitions().add(ideCDef);
    List<String> keyFields = XSDUtil.getKeyFields(concept);
    assertTrue(keyFields.isEmpty());
    // 
    ideCDef.setName(conceptName);
    XSDXPathDefinition xpathDefinition2 = factory.createXSDXPathDefinition();
    xpathDefinition2.setValue(fields[1]);
    ideCDef.getFields().add(xpathDefinition2);
    keyFields = XSDUtil.getKeyFields(concept);
    assertTrue(keyFields.size() == 2);
    assertTrue(keyFields.contains(fields[0]));
    assertTrue(keyFields.contains(fields[1]));
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDIdentityConstraintDefinition(org.eclipse.xsd.XSDIdentityConstraintDefinition) XSDXPathDefinition(org.eclipse.xsd.XSDXPathDefinition) Test(org.junit.Test)

Example 19 with XSDFactory

use of org.eclipse.xsd.XSDFactory 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);
    }
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) HashMap(java.util.HashMap) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDParticle(org.eclipse.xsd.XSDParticle) HashSet(java.util.HashSet) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 20 with XSDFactory

use of org.eclipse.xsd.XSDFactory 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);
    }
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) Element(org.w3c.dom.Element) XSDAnnotation(org.eclipse.xsd.XSDAnnotation) Document(org.w3c.dom.Document) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XSDSchema(org.eclipse.xsd.XSDSchema) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

XSDFactory (org.eclipse.xsd.XSDFactory)50 XSDElementDeclaration (org.eclipse.xsd.XSDElementDeclaration)37 XSDParticle (org.eclipse.xsd.XSDParticle)27 XSDModelGroup (org.eclipse.xsd.XSDModelGroup)26 XSDComplexTypeDefinition (org.eclipse.xsd.XSDComplexTypeDefinition)23 Test (org.junit.Test)19 XSDSimpleTypeDefinition (org.eclipse.xsd.XSDSimpleTypeDefinition)15 XSDIdentityConstraintDefinition (org.eclipse.xsd.XSDIdentityConstraintDefinition)13 ArrayList (java.util.ArrayList)12 XSDSchema (org.eclipse.xsd.XSDSchema)12 XSDTypeDefinition (org.eclipse.xsd.XSDTypeDefinition)12 XSDXPathDefinition (org.eclipse.xsd.XSDXPathDefinition)12 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)9 XSDAnnotation (org.eclipse.xsd.XSDAnnotation)8 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)6 HashSet (java.util.HashSet)5 Iterator (java.util.Iterator)5 List (java.util.List)5