Search in sources :

Example 46 with XSDParticle

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

the class XSDUtilTest method testIsFirstLevelChild.

@Test
public void testIsFirstLevelChild() 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 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) {
                    assertTrue(XSDUtil.isFirstLevelChild(particle));
                    if (particle.getTerm() instanceof XSDElementDeclaration) {
                        XSDElementDeclaration xsdElementDecl = (XSDElementDeclaration) particle.getTerm();
                        XSDTypeDefinition typeDefinition = xsdElementDecl.getTypeDefinition();
                        if (typeDefinition instanceof XSDComplexTypeDefinition) {
                            XSDParticle childPart = (XSDParticle) ((XSDComplexTypeDefinition) typeDefinition).getContent();
                            XSDModelGroup childModelGroup = (XSDModelGroup) childPart.getContent();
                            for (XSDParticle childParticle : childModelGroup.getContents()) {
                                assertFalse(XSDUtil.isFirstLevelChild(childParticle));
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : XSDComplexTypeContent(org.eclipse.xsd.XSDComplexTypeContent) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDParticleContent(org.eclipse.xsd.XSDParticleContent) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDParticle(org.eclipse.xsd.XSDParticle) XSDSchema(org.eclipse.xsd.XSDSchema) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition) Test(org.junit.Test)

Example 47 with XSDParticle

use of org.eclipse.xsd.XSDParticle 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 48 with XSDParticle

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

Example 49 with XSDParticle

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

the class UtilMockTest method buildXSDParticleWithChildren.

private XSDParticle buildXSDParticleWithChildren(int childrenNumb, String[] childrenNames) {
    XSDParticle xsdParticle = XSDFactory.eINSTANCE.createXSDParticle();
    XSDModelGroup xsdModelGroup = XSDFactory.eINSTANCE.createXSDModelGroup();
    xsdParticle.setTerm(xsdModelGroup);
    for (int i = 0; i < childrenNumb && i < childrenNames.length; i++) {
        XSDParticle xsdParticle_child = XSDFactory.eINSTANCE.createXSDParticle();
        XSDElementDeclaration xsdElementDeclaration_child = XSDFactory.eINSTANCE.createXSDElementDeclaration();
        xsdElementDeclaration_child.setName(childrenNames[i]);
        xsdParticle_child.setTerm(xsdElementDeclaration_child);
        xsdModelGroup.getContents().add(xsdParticle_child);
    }
    return xsdParticle;
}
Also used : XSDModelGroup(org.eclipse.xsd.XSDModelGroup) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDParticle(org.eclipse.xsd.XSDParticle)

Example 50 with XSDParticle

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

the class UtilMockTest method testUpdateReferenceToXSDTypeDefinition.

@Test
public void testUpdateReferenceToXSDTypeDefinition() {
    // $NON-NLS-1$
    String method_getall = "getAllObject";
    // $NON-NLS-1$
    String method_updateref = "updateReferenceToXSDTypeDefinition";
    PowerMockito.mockStatic(Util.class);
    try {
        PowerMockito.when(Util.class, method_updateref, any(), any(XSDTypeDefinition.class), any(IStructuredContentProvider.class)).thenCallRealMethod();
        Object[] allNodes = {};
        XSDFactory factory = XSDFactory.eINSTANCE;
        XSDTypeDefinition[] newTypes = { factory.createXSDComplexTypeDefinition(), factory.createXSDSimpleTypeDefinition() };
        for (XSDTypeDefinition newType : newTypes) {
            // 
            // 
            XSDElementDeclaration mockElementDecl = mock(XSDElementDeclaration.class);
            when(mockElementDecl.getTypeDefinition()).thenReturn(newType);
            // 
            // 
            XSDParticle xsdParticle1 = mock(XSDParticle.class);
            XSDElementDeclaration mockElementDec2 = mock(XSDElementDeclaration.class);
            when(mockElementDec2.getTypeDefinition()).thenReturn(newType);
            when(xsdParticle1.getTerm()).thenReturn(mockElementDec2);
            // 
            // 
            XSDParticle xsdParticle2 = mock(XSDParticle.class);
            XSDParticle particle_c1 = mock(XSDParticle.class);
            XSDElementDeclaration mockElementDec_c1 = mock(XSDElementDeclaration.class);
            when(mockElementDec_c1.getTypeDefinition()).thenReturn(newType);
            when(particle_c1.getContent()).thenReturn(mockElementDec_c1);
            XSDParticle particle_c2 = mock(XSDParticle.class);
            XSDElementDeclaration mockElementDec_c2 = mock(XSDElementDeclaration.class);
            when(mockElementDec_c2.getTypeDefinition()).thenReturn(newType);
            when(particle_c2.getContent()).thenReturn(mockElementDec_c2);
            XSDParticle particle_c3 = mock(XSDParticle.class);
            XSDElementDeclaration mockElementDec_c3 = mock(XSDElementDeclaration.class);
            when(mockElementDec_c3.getTypeDefinition()).thenReturn(factory.createXSDComplexTypeDefinition());
            when(particle_c3.getContent()).thenReturn(mockElementDec_c3);
            XSDModelGroup xsdModelGroup = mock(XSDModelGroup.class);
            EList<XSDParticle> elist = new BasicEList<XSDParticle>();
            elist.add(particle_c1);
            elist.add(particle_c2);
            elist.add(particle_c3);
            when(xsdModelGroup.getContents()).thenReturn(elist);
            when(xsdParticle2.getTerm()).thenReturn(xsdModelGroup);
            allNodes = new Object[] { mockElementDecl, xsdParticle1, xsdParticle2 };
            // 
            PowerMockito.when(Util.class, method_getall, any(), anyList(), any(IStructuredContentProvider.class)).thenReturn(allNodes);
            Util.updateReferenceToXSDTypeDefinition(new Object(), newType, mock(IStructuredContentProvider.class));
            Mockito.verify(mockElementDecl).setTypeDefinition(newType);
            Mockito.verify(mockElementDec2).setTypeDefinition(newType);
            Mockito.verify(mockElementDec_c1).setTypeDefinition(newType);
            Mockito.verify(mockElementDec_c2).setTypeDefinition(newType);
            if (newType instanceof XSDComplexTypeDefinition) {
                PowerMockito.verifyStatic();
                Util.updateChildrenReferenceToComplexType((XSDComplexTypeDefinition) newType);
            }
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) XSDModelGroup(org.eclipse.xsd.XSDModelGroup) BasicEList(org.eclipse.emf.common.util.BasicEList) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) IStructuredContentProvider(org.eclipse.jface.viewers.IStructuredContentProvider) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDParticle(org.eclipse.xsd.XSDParticle) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

XSDParticle (org.eclipse.xsd.XSDParticle)146 XSDElementDeclaration (org.eclipse.xsd.XSDElementDeclaration)103 XSDModelGroup (org.eclipse.xsd.XSDModelGroup)93 XSDComplexTypeDefinition (org.eclipse.xsd.XSDComplexTypeDefinition)75 XSDTypeDefinition (org.eclipse.xsd.XSDTypeDefinition)36 ArrayList (java.util.ArrayList)34 XSDSimpleTypeDefinition (org.eclipse.xsd.XSDSimpleTypeDefinition)34 XSDTerm (org.eclipse.xsd.XSDTerm)31 XSDFactory (org.eclipse.xsd.XSDFactory)27 XSDComplexTypeContent (org.eclipse.xsd.XSDComplexTypeContent)24 XSDSchema (org.eclipse.xsd.XSDSchema)24 Test (org.junit.Test)23 Iterator (java.util.Iterator)21 XSDAnnotation (org.eclipse.xsd.XSDAnnotation)21 XSDIdentityConstraintDefinition (org.eclipse.xsd.XSDIdentityConstraintDefinition)19 EList (org.eclipse.emf.common.util.EList)17 XSDParticleContent (org.eclipse.xsd.XSDParticleContent)17 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)16 Element (org.w3c.dom.Element)16 List (java.util.List)15