Search in sources :

Example 51 with XSDComplexTypeDefinition

use of org.eclipse.xsd.XSDComplexTypeDefinition 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 52 with XSDComplexTypeDefinition

use of org.eclipse.xsd.XSDComplexTypeDefinition 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)

Example 53 with XSDComplexTypeDefinition

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

the class UtilMockTest method testFindOutAllSonElements.

@Test
public void testFindOutAllSonElements() {
    XSDFactory factory = XSDFactory.eINSTANCE;
    Set<XSDConcreteComponent> complexTypes = new HashSet<XSDConcreteComponent>();
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    String[] targetNameSpaces = { "http://www.w3.org/2001/XMLSchema", "", "" };
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    String[] names = { "p1", "p2", "p3" };
    XSDTypeDefinition[] types = { factory.createXSDSimpleTypeDefinition(), factory.createXSDSimpleTypeDefinition(), factory.createXSDSimpleTypeDefinition() };
    XSDElementDeclaration xsdElementDeclaration = factory.createXSDElementDeclaration();
    xsdElementDeclaration.setName(names[0]);
    xsdElementDeclaration.setTargetNamespace(targetNameSpaces[0]);
    XSDComplexTypeDefinition xsdComplexTypeDef = factory.createXSDComplexTypeDefinition();
    xsdElementDeclaration.setAnonymousTypeDefinition(xsdComplexTypeDef);
    XSDParticle typeParticle = factory.createXSDParticle();
    xsdComplexTypeDef.setContent(typeParticle);
    XSDModelGroup xsdModelGroup = factory.createXSDModelGroup();
    for (int i = 0; i < names.length; i++) {
        XSDElementDeclaration xsdEleDec = factory.createXSDElementDeclaration();
        xsdEleDec.setName(names[i]);
        xsdEleDec.setTargetNamespace(targetNameSpaces[i]);
        if (i == 0) {
            xsdEleDec.setTypeDefinition(xsdComplexTypeDef);
        } else {
            xsdEleDec.setTypeDefinition(types[i]);
        }
        XSDParticle xsdParticle = factory.createXSDParticle();
        xsdParticle.setContent(xsdEleDec);
        xsdModelGroup.getContents().add(xsdParticle);
    }
    typeParticle.setContent(xsdModelGroup);
    PowerMockito.mockStatic(Util.class);
    try {
        // $NON-NLS-1$
        String methodToExecute = "findOutAllSonElements";
        PowerMockito.when(Util.class, methodToExecute, any(XSDElementDeclaration.class), anySet()).thenCallRealMethod();
        List<XSDElementDeclaration> allson = Whitebox.invokeMethod(Util.class, methodToExecute, xsdElementDeclaration, complexTypes);
        assertNotNull(allson);
        assertTrue(allson.size() == 2);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) XSDConcreteComponent(org.eclipse.xsd.XSDConcreteComponent) 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 54 with XSDComplexTypeDefinition

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

the class UtilMockTest method testGetComplexTypes.

@Test
public void testGetComplexTypes() {
    XSDFactory factory = XSDFactory.eINSTANCE;
    XSDSchema xSchema = factory.createXSDSchema();
    List<XSDComplexTypeDefinition> complexTypes = Util.getComplexTypes(xSchema);
    assertNotNull(complexTypes);
    assertTrue(complexTypes.isEmpty());
    XSDSimpleTypeDefinition simpleTypeDefinition = factory.createXSDSimpleTypeDefinition();
    simpleTypeDefinition.setBaseTypeDefinition(// $NON-NLS-1$ );
    xSchema.resolveSimpleTypeDefinition(xSchema.getSchemaForSchemaNamespace(), "string"));
    xSchema.getContents().add(simpleTypeDefinition);
    complexTypes = Util.getComplexTypes(xSchema);
    assertNotNull(complexTypes);
    assertTrue(complexTypes.isEmpty());
    XSDComplexTypeDefinition baseComplexTypeDefinition = xSchema.resolveComplexTypeDefinition(xSchema.getSchemaForSchemaNamespace(), // $NON-NLS-1$
    "anyType");
    XSDComplexTypeDefinition complexType1 = factory.createXSDComplexTypeDefinition();
    complexType1.setBaseTypeDefinition(baseComplexTypeDefinition);
    complexType1.setTargetNamespace(null);
    // $NON-NLS-1$
    complexType1.setName("ctype1");
    XSDComplexTypeDefinition complexType2 = factory.createXSDComplexTypeDefinition();
    // $NON-NLS-1$
    complexType2.setTargetNamespace("targetNameSpace");
    // $NON-NLS-1$
    complexType2.setName("ctype2");
    complexType2.setBaseTypeDefinition(baseComplexTypeDefinition);
    XSDComplexTypeDefinition complexType3 = factory.createXSDComplexTypeDefinition();
    complexType3.setTargetNamespace(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001);
    // $NON-NLS-1$
    complexType3.setName("topLevelComplexType");
    complexType3.setBaseTypeDefinition(baseComplexTypeDefinition);
    EList<XSDTypeDefinition> contents = new BasicEList<XSDTypeDefinition>();
    contents.add(complexType1);
    contents.add(complexType2);
    contents.add(complexType3);
    XSDSchema spySchema = spy(xSchema);
    when(spySchema.getTypeDefinitions()).thenReturn(contents);
    complexTypes = Util.getComplexTypes(spySchema);
    assertNotNull(complexTypes);
    assertTrue(complexTypes.size() == 2);
    assertTrue(complexTypes.contains(complexType1));
    assertTrue(complexTypes.contains(complexType2));
    assertFalse(complexTypes.contains(complexType3));
}
Also used : XSDFactory(org.eclipse.xsd.XSDFactory) XSDSimpleTypeDefinition(org.eclipse.xsd.XSDSimpleTypeDefinition) BasicEList(org.eclipse.emf.common.util.BasicEList) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDSchema(org.eclipse.xsd.XSDSchema) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 55 with XSDComplexTypeDefinition

use of org.eclipse.xsd.XSDComplexTypeDefinition in project webtools.sourceediting by eclipse.

the class XSDTypeReferenceEditManager method modifyComponentReference.

public void modifyComponentReference(Object referencingObject, ComponentSpecification component) {
    XSDConcreteComponent concreteComponent = null;
    if (referencingObject instanceof Adapter) {
        Adapter adpater = (Adapter) referencingObject;
        if (adpater.getTarget() instanceof XSDConcreteComponent) {
            concreteComponent = (XSDConcreteComponent) adpater.getTarget();
        }
    } else if (referencingObject instanceof XSDConcreteComponent) {
        concreteComponent = (XSDConcreteComponent) referencingObject;
    }
    if (concreteComponent != null) {
        if (component.isNew()) {
            XSDTypeDefinition td = null;
            if (// This means we set to anonymous type
            component.getName() == null && concreteComponent instanceof XSDElementDeclaration) {
                XSDFactory factory = XSDSchemaBuildingTools.getXSDFactory();
                XSDElementDeclaration elementDeclaration = (XSDElementDeclaration) concreteComponent;
                // with the proper undo descriptions
                if (component.getMetaName() == IXSDSearchConstants.COMPLEX_TYPE_META_NAME) {
                    XSDComplexTypeDefinition complexType = factory.createXSDComplexTypeDefinition();
                    elementDeclaration.setAnonymousTypeDefinition(complexType);
                } else {
                    XSDSimpleTypeDefinition simpleType = factory.createXSDSimpleTypeDefinition();
                    simpleType.setBaseTypeDefinition(// $NON-NLS-1$
                    elementDeclaration.getSchema().getSchemaForSchema().resolveSimpleTypeDefinition(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001, "string"));
                    elementDeclaration.setAnonymousTypeDefinition(simpleType);
                }
                // TODO use external literal string
                elementDeclaration.getElement().removeAttribute("type");
            } else if (component.getMetaName() == IXSDSearchConstants.COMPLEX_TYPE_META_NAME) {
                AddXSDComplexTypeDefinitionCommand command = new AddXSDComplexTypeDefinitionCommand(Messages._UI_ACTION_ADD_COMPLEX_TYPE, concreteComponent.getSchema());
                command.setNameToAdd(component.getName());
                command.execute();
                td = command.getCreatedComplexType();
            } else {
                AddXSDSimpleTypeDefinitionCommand command = new AddXSDSimpleTypeDefinitionCommand(Messages._UI_ACTION_ADD_SIMPLE_TYPE, concreteComponent.getSchema());
                command.setNameToAdd(component.getName());
                command.execute();
                td = command.getCreatedSimpleType();
            }
            if (td != null) {
                Command command = new UpdateTypeReferenceCommand(concreteComponent, td);
                command.setLabel(Messages._UI_ACTION_SET_TYPE);
                command.execute();
            }
            XSDDirectivesManager.removeUnusedXSDImports(concreteComponent.getSchema());
        } else {
            Command command = new UpdateTypeReferenceAndManageDirectivesCommand(concreteComponent, component.getName(), component.getQualifier(), component.getFile());
            command.setLabel(Messages._UI_ACTION_SET_TYPE);
            command.execute();
        }
    }
}
Also used : AddXSDComplexTypeDefinitionCommand(org.eclipse.wst.xsd.ui.internal.common.commands.AddXSDComplexTypeDefinitionCommand) UpdateTypeReferenceAndManageDirectivesCommand(org.eclipse.wst.xsd.ui.internal.common.commands.UpdateTypeReferenceAndManageDirectivesCommand) XSDFactory(org.eclipse.xsd.XSDFactory) XSDConcreteComponent(org.eclipse.xsd.XSDConcreteComponent) UpdateTypeReferenceCommand(org.eclipse.wst.xsd.ui.internal.common.commands.UpdateTypeReferenceCommand) AddXSDSimpleTypeDefinitionCommand(org.eclipse.wst.xsd.ui.internal.common.commands.AddXSDSimpleTypeDefinitionCommand) AddXSDComplexTypeDefinitionCommand(org.eclipse.wst.xsd.ui.internal.common.commands.AddXSDComplexTypeDefinitionCommand) UpdateTypeReferenceCommand(org.eclipse.wst.xsd.ui.internal.common.commands.UpdateTypeReferenceCommand) UpdateTypeReferenceAndManageDirectivesCommand(org.eclipse.wst.xsd.ui.internal.common.commands.UpdateTypeReferenceAndManageDirectivesCommand) Command(org.eclipse.gef.commands.Command) XSDElementDeclaration(org.eclipse.xsd.XSDElementDeclaration) XSDSimpleTypeDefinition(org.eclipse.xsd.XSDSimpleTypeDefinition) AddXSDSimpleTypeDefinitionCommand(org.eclipse.wst.xsd.ui.internal.common.commands.AddXSDSimpleTypeDefinitionCommand) Adapter(org.eclipse.emf.common.notify.Adapter) XSDComplexTypeDefinition(org.eclipse.xsd.XSDComplexTypeDefinition) XSDTypeDefinition(org.eclipse.xsd.XSDTypeDefinition)

Aggregations

XSDComplexTypeDefinition (org.eclipse.xsd.XSDComplexTypeDefinition)162 XSDElementDeclaration (org.eclipse.xsd.XSDElementDeclaration)93 XSDModelGroup (org.eclipse.xsd.XSDModelGroup)76 XSDParticle (org.eclipse.xsd.XSDParticle)75 XSDTypeDefinition (org.eclipse.xsd.XSDTypeDefinition)66 XSDSimpleTypeDefinition (org.eclipse.xsd.XSDSimpleTypeDefinition)53 ArrayList (java.util.ArrayList)43 XSDSchema (org.eclipse.xsd.XSDSchema)37 Test (org.junit.Test)37 EList (org.eclipse.emf.common.util.EList)25 XSDConcreteComponent (org.eclipse.xsd.XSDConcreteComponent)25 XSDFactory (org.eclipse.xsd.XSDFactory)23 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)22 XSDIdentityConstraintDefinition (org.eclipse.xsd.XSDIdentityConstraintDefinition)21 XSDTerm (org.eclipse.xsd.XSDTerm)21 XSDAnnotation (org.eclipse.xsd.XSDAnnotation)20 XSDComplexTypeContent (org.eclipse.xsd.XSDComplexTypeContent)20 Iterator (java.util.Iterator)19 List (java.util.List)19 XSDAttributeGroupDefinition (org.eclipse.xsd.XSDAttributeGroupDefinition)16