From 5f3869c451e46aadc943d00087d6847877dd1c50 Mon Sep 17 00:00:00 2001 From: lresende Date: Wed, 19 Nov 2008 05:27:58 +0000 Subject: Merging the 1.x delta on top of the equinox based modules git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@718858 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/assembly/xml/BaseAssemblyProcessor.java | 21 + .../sca/assembly/xml/ComponentTypeProcessor.java | 254 +++---- .../sca/assembly/xml/CompositeProcessor.java | 758 +++++++++++---------- .../assembly/xml/ConstrainingTypeProcessor.java | 184 ++--- .../assembly-xml-validation-messages.properties | 2 +- .../assembly/xml/AnyElementReadWriteTestCase.java | 83 ++- .../xml/MultiplicityReadWriteTestCase.java | 86 +++ .../sca/assembly/xml/ReadDocumentTestCase.java | 2 +- .../assembly/xml/ReadWriteAttributeTestCase.java | 40 +- .../xml/ReadWriteLocalCompositeTestCase.java | 94 +++ .../tuscany/sca/assembly/xml/Calculator.composite | 14 +- .../xml/CalculatorComponent.constrainingType | 4 +- .../sca/assembly/xml/CalculatorImpl.componentType | 6 +- .../sca/assembly/xml/Multiplicity.composite | 30 + .../sca/assembly/xml/UnknownElement.composite | 42 ++ .../tuscany/sca/assembly/xml/local.composite | 27 + 16 files changed, 1006 insertions(+), 641 deletions(-) create mode 100644 java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/MultiplicityReadWriteTestCase.java create mode 100644 java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteLocalCompositeTestCase.java create mode 100644 java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/Multiplicity.composite create mode 100644 java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/UnknownElement.composite create mode 100644 java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/local.composite (limited to 'java/sca/modules/assembly-xml') diff --git a/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/BaseAssemblyProcessor.java b/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/BaseAssemblyProcessor.java index 8ac9386e19..ebe85da4be 100644 --- a/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/BaseAssemblyProcessor.java +++ b/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/BaseAssemblyProcessor.java @@ -242,8 +242,29 @@ abstract class BaseAssemblyProcessor extends BaseStAXArtifactProcessor implement reference.setMultiplicity(Multiplicity.ONE_N); } else if (ZERO_N.equals(value)) { reference.setMultiplicity(Multiplicity.ZERO_N); + } else if (ONE_ONE.equals(value)) { + reference.setMultiplicity(Multiplicity.ONE_ONE); } } + + protected XAttr writeMultiplicity(AbstractReference reference) { + Multiplicity multiplicity = reference.getMultiplicity(); + if (multiplicity != null) { + String value = null; + if (Multiplicity.ZERO_ONE.equals(multiplicity)) { + value = ZERO_ONE; + } else if (Multiplicity.ONE_N.equals(multiplicity)) { + value = ONE_N; + } else if (Multiplicity.ZERO_N.equals(multiplicity)) { + value = ZERO_N; + } else if (Multiplicity.ONE_ONE.equals(multiplicity)) { + value = ONE_ONE; + return null; + } + return new XAttr(MULTIPLICITY, value); + } + return null; + } /** * Returns the value of a constrainingType attribute. diff --git a/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeProcessor.java b/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeProcessor.java index bdf2c98338..695d133940 100644 --- a/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeProcessor.java +++ b/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeProcessor.java @@ -84,7 +84,7 @@ public class ComponentTypeProcessor extends BaseAssemblyProcessor implements StA modelFactories.getFactory(PolicyFactory.class), extensionProcessor, monitor); } - public ComponentType read(XMLStreamReader reader) throws ContributionReadException, XMLStreamException { + public ComponentType read(XMLStreamReader reader) throws ContributionReadException { ComponentType componentType = null; Service service = null; Reference reference = null; @@ -93,136 +93,143 @@ public class ComponentTypeProcessor extends BaseAssemblyProcessor implements StA Callback callback = null; QName name = null; - // Read the componentType document - while (reader.hasNext()) { - int event = reader.getEventType(); - switch (event) { - case START_ELEMENT: - name = reader.getName(); - - if (Constants.COMPONENT_TYPE_QNAME.equals(name)) { - - // Read a - componentType = assemblyFactory.createComponentType(); - componentType.setConstrainingType(readConstrainingType(reader)); - - } else if (Constants.SERVICE_QNAME.equals(name)) { - - // Read a - service = assemblyFactory.createService(); - contract = service; - service.setName(getString(reader, Constants.NAME)); - componentType.getServices().add(service); - policyProcessor.readPolicies(service, reader); - - } else if (Constants.REFERENCE_QNAME.equals(name)) { - - // Read a - reference = assemblyFactory.createReference(); - contract = reference; - reference.setName(getString(reader, Constants.NAME)); - reference.setWiredByImpl(getBoolean(reader, Constants.WIRED_BY_IMPL)); - readMultiplicity(reference, reader); - readTargets(reference, reader); - componentType.getReferences().add(reference); - policyProcessor.readPolicies(reference, reader); - - } else if (Constants.PROPERTY_QNAME.equals(name)) { - - // Read a - property = assemblyFactory.createProperty(); - readAbstractProperty(property, reader); - policyProcessor.readPolicies(property, reader); - - // Read the property value - Document value = readPropertyValue(property.getXSDElement(), property.getXSDType(), reader); - property.setValue(value); - - componentType.getProperties().add(property); - - } else if (Constants.IMPLEMENTATION_QNAME.equals(name)) { - - // Read an element - policyProcessor.readPolicies(componentType, reader); - - } else if (Constants.CALLBACK_QNAME.equals(name)) { - - // Read a - callback = assemblyFactory.createCallback(); - contract.setCallback(callback); - policyProcessor.readPolicies(callback, reader); - - } else if (OPERATION_QNAME.equals(name)) { - - // Read an - Operation operation = new OperationImpl(); - operation.setName(getString(reader, NAME)); - operation.setUnresolved(true); - if (callback != null) { - policyProcessor.readPolicies(callback, operation, reader); - } else { - policyProcessor.readPolicies(contract, operation, reader); - } - } else { - - // Read an extension element - Object extension = extensionProcessor.read(reader); - if (extension != null) { - if (extension instanceof InterfaceContract) { - - // and - contract.setInterfaceContract((InterfaceContract)extension); - - } else if (extension instanceof Binding) { - - // and - if (callback != null) { - callback.getBindings().add((Binding)extension); - } else { - contract.getBindings().add((Binding)extension); - } + try { + // Read the componentType document + while (reader.hasNext()) { + int event = reader.getEventType(); + switch (event) { + case START_ELEMENT: + name = reader.getName(); + + if (Constants.COMPONENT_TYPE_QNAME.equals(name)) { + + // Read a + componentType = assemblyFactory.createComponentType(); + componentType.setConstrainingType(readConstrainingType(reader)); + + } else if (Constants.SERVICE_QNAME.equals(name)) { + + // Read a + service = assemblyFactory.createService(); + contract = service; + service.setName(getString(reader, Constants.NAME)); + componentType.getServices().add(service); + policyProcessor.readPolicies(service, reader); + + } else if (Constants.REFERENCE_QNAME.equals(name)) { + + // Read a + reference = assemblyFactory.createReference(); + contract = reference; + reference.setName(getString(reader, Constants.NAME)); + reference.setWiredByImpl(getBoolean(reader, Constants.WIRED_BY_IMPL)); + readMultiplicity(reference, reader); + readTargets(reference, reader); + componentType.getReferences().add(reference); + policyProcessor.readPolicies(reference, reader); + + } else if (Constants.PROPERTY_QNAME.equals(name)) { + + // Read a + property = assemblyFactory.createProperty(); + readAbstractProperty(property, reader); + policyProcessor.readPolicies(property, reader); + + // Read the property value + Document value = readPropertyValue(property.getXSDElement(), property.getXSDType(), reader); + property.setValue(value); + + componentType.getProperties().add(property); + + } else if (Constants.IMPLEMENTATION_QNAME.equals(name)) { + + // Read an element + policyProcessor.readPolicies(componentType, reader); + + } else if (Constants.CALLBACK_QNAME.equals(name)) { + + // Read a + callback = assemblyFactory.createCallback(); + contract.setCallback(callback); + policyProcessor.readPolicies(callback, reader); + + } else if (OPERATION_QNAME.equals(name)) { + + // Read an + Operation operation = new OperationImpl(); + operation.setName(getString(reader, NAME)); + operation.setUnresolved(true); + if (callback != null) { + policyProcessor.readPolicies(callback, operation, reader); } else { - - // Add the extension element to the current element - if (callback != null) { - callback.getExtensions().add(extension); - } else if (contract != null) { - contract.getExtensions().add(extension); - } else if (property != null) { - property.getExtensions().add(extension); + policyProcessor.readPolicies(contract, operation, reader); + } + } else { + + // Read an extension element + Object extension = extensionProcessor.read(reader); + if (extension != null) { + if (extension instanceof InterfaceContract) { + + // and + contract.setInterfaceContract((InterfaceContract)extension); + + } else if (extension instanceof Binding) { + + // and + if (callback != null) { + callback.getBindings().add((Binding)extension); + } else { + contract.getBindings().add((Binding)extension); + } } else { - if (componentType instanceof Extensible) { - ((Extensible)componentType).getExtensions().add(extension); + + // Add the extension element to the current element + if (callback != null) { + callback.getExtensions().add(extension); + } else if (contract != null) { + contract.getExtensions().add(extension); + } else if (property != null) { + property.getExtensions().add(extension); + } else { + if (componentType instanceof Extensible) { + ((Extensible)componentType).getExtensions().add(extension); + } } } } } - } - break; - - case END_ELEMENT: - name = reader.getName(); - - // Clear current state when reading reaching end element - if (SERVICE_QNAME.equals(name)) { - service = null; - contract = null; - } else if (REFERENCE_QNAME.equals(name)) { - reference = null; - contract = null; - } else if (PROPERTY_QNAME.equals(name)) { - property = null; - } else if (CALLBACK_QNAME.equals(name)) { - callback = null; - } - break; - } - - // Read the next element - if (reader.hasNext()) { - reader.next(); + break; + + case END_ELEMENT: + name = reader.getName(); + + // Clear current state when reading reaching end element + if (SERVICE_QNAME.equals(name)) { + service = null; + contract = null; + } else if (REFERENCE_QNAME.equals(name)) { + reference = null; + contract = null; + } else if (PROPERTY_QNAME.equals(name)) { + property = null; + } else if (CALLBACK_QNAME.equals(name)) { + callback = null; + } + break; + } + + // Read the next element + if (reader.hasNext()) { + reader.next(); + } } } + catch (XMLStreamException e) { + ContributionReadException ex = new ContributionReadException(e); + error("XMLStreamException", reader, ex); + } + return componentType; } @@ -271,6 +278,7 @@ public class ComponentTypeProcessor extends BaseAssemblyProcessor implements StA writeStart(writer, REFERENCE, new XAttr(NAME, reference.getName()), + writeMultiplicity(reference), writeTargets(reference), policyProcessor.writePolicies(reference)); diff --git a/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeProcessor.java b/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeProcessor.java index b755684517..d8bb24f1fa 100644 --- a/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeProcessor.java +++ b/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeProcessor.java @@ -136,7 +136,7 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt } - public Composite read(XMLStreamReader reader) throws ContributionReadException, XMLStreamException { + public Composite read(XMLStreamReader reader) throws ContributionReadException { Composite composite = null; Composite include = null; Component component = null; @@ -151,411 +151,418 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt Callback callback = null; QName name = null; - // Read the composite document - while (reader.hasNext()) { - int event = reader.getEventType(); - switch (event) { - case START_ELEMENT: - name = reader.getName(); - - if (COMPOSITE_QNAME.equals(name)) { - - // Read a - composite = assemblyFactory.createComposite(); - - composite.setName(new QName(getString(reader, TARGET_NAMESPACE), getString(reader, NAME))); - - if(!isSet(reader, TARGET_NAMESPACE)){ - // spec says that a composite must have a namespace - warning("NoCompositeNamespace", composite, composite.getName().toString()); - } - - if(isSet(reader, AUTOWIRE)) { - composite.setAutowire(getBoolean(reader, AUTOWIRE)); - } - - //handle extension attributes - this.readExtendedAttributes(reader, name, composite, extensionAttributeProcessor); - - composite.setLocal(getBoolean(reader, LOCAL)); - composite.setConstrainingType(readConstrainingType(reader)); - policyProcessor.readPolicies(composite, reader); - - } else if (INCLUDE_QNAME.equals(name)) { - - // Read an - include = assemblyFactory.createComposite(); - include.setName(getQName(reader, NAME)); - include.setURI(getString(reader, URI)); - include.setUnresolved(true); - composite.getIncludes().add(include); - - } else if (SERVICE_QNAME.equals(name)) { - if (component != null) { - - // Read a - componentService = assemblyFactory.createComponentService(); - contract = componentService; - componentService.setName(getString(reader, NAME)); - - //handle extension attributes - this.readExtendedAttributes(reader, name, componentService, extensionAttributeProcessor); - - component.getServices().add(componentService); - policyProcessor.readPolicies(contract, reader); - } else { - - // Read a - compositeService = assemblyFactory.createCompositeService(); - contract = compositeService; - compositeService.setName(getString(reader, NAME)); - - String promoted = getString(reader, PROMOTE); - if (promoted != null) { - String promotedComponentName; - String promotedServiceName; - int s = promoted.indexOf('/'); - if (s == -1) { - promotedComponentName = promoted; - promotedServiceName = null; - } else { - promotedComponentName = promoted.substring(0, s); - promotedServiceName = promoted.substring(s + 1); - } + try { + // Read the composite document + while (reader.hasNext()) { + int event = reader.getEventType(); + switch (event) { + case START_ELEMENT: + name = reader.getName(); + + if (COMPOSITE_QNAME.equals(name)) { - Component promotedComponent = assemblyFactory.createComponent(); - promotedComponent.setUnresolved(true); - promotedComponent.setName(promotedComponentName); - compositeService.setPromotedComponent(promotedComponent); + // Read a + composite = assemblyFactory.createComposite(); + + composite.setName(new QName(getString(reader, TARGET_NAMESPACE), getString(reader, NAME))); - ComponentService promotedService = assemblyFactory.createComponentService(); - promotedService.setUnresolved(true); - promotedService.setName(promotedServiceName); - compositeService.setPromotedService(promotedService); + if(!isSet(reader, TARGET_NAMESPACE)){ + // spec says that a composite must have a namespace + warning("NoCompositeNamespace", composite, composite.getName().toString()); } - - //handle extension attributes - this.readExtendedAttributes(reader, name, compositeService, extensionAttributeProcessor); - - composite.getServices().add(compositeService); - policyProcessor.readPolicies(contract, reader); - } - - } else if (REFERENCE_QNAME.equals(name)) { - if (component != null) { - // Read a - componentReference = assemblyFactory.createComponentReference(); - contract = componentReference; - componentReference.setName(getString(reader, NAME)); - readMultiplicity(componentReference, reader); - if (isSet(reader, AUTOWIRE)) { - componentReference.setAutowire(getBoolean(reader, AUTOWIRE)); + + if(isSet(reader, AUTOWIRE)) { + composite.setAutowire(getBoolean(reader, AUTOWIRE)); } - readTargets(componentReference, reader); - componentReference.setWiredByImpl(getBoolean(reader, WIRED_BY_IMPL)); //handle extension attributes - this.readExtendedAttributes(reader, name, componentReference, extensionAttributeProcessor); - - component.getReferences().add(componentReference); - policyProcessor.readPolicies(contract, reader); - } else { - // Read a - compositeReference = assemblyFactory.createCompositeReference(); - contract = compositeReference; - compositeReference.setName(getString(reader, NAME)); - readMultiplicity(compositeReference, reader); - readTargets(compositeReference, reader); - String promote = reader.getAttributeValue(null, Constants.PROMOTE); - if (promote != null) { - for (StringTokenizer tokens = new StringTokenizer(promote); tokens.hasMoreTokens();) { - ComponentReference promotedReference = - assemblyFactory.createComponentReference(); - promotedReference.setUnresolved(true); - promotedReference.setName(tokens.nextToken()); - compositeReference.getPromotedReferences().add(promotedReference); + this.readExtendedAttributes(reader, name, composite, extensionAttributeProcessor); + + composite.setLocal(getBoolean(reader, LOCAL)); + composite.setConstrainingType(readConstrainingType(reader)); + policyProcessor.readPolicies(composite, reader); + + } else if (INCLUDE_QNAME.equals(name)) { + + // Read an + include = assemblyFactory.createComposite(); + include.setName(getQName(reader, NAME)); + include.setURI(getString(reader, URI)); + include.setUnresolved(true); + composite.getIncludes().add(include); + + } else if (SERVICE_QNAME.equals(name)) { + if (component != null) { + + // Read a + componentService = assemblyFactory.createComponentService(); + contract = componentService; + componentService.setName(getString(reader, NAME)); + + //handle extension attributes + this.readExtendedAttributes(reader, name, componentService, extensionAttributeProcessor); + + component.getServices().add(componentService); + policyProcessor.readPolicies(contract, reader); + } else { + + // Read a + compositeService = assemblyFactory.createCompositeService(); + contract = compositeService; + compositeService.setName(getString(reader, NAME)); + + String promoted = getString(reader, PROMOTE); + if (promoted != null) { + String promotedComponentName; + String promotedServiceName; + int s = promoted.indexOf('/'); + if (s == -1) { + promotedComponentName = promoted; + promotedServiceName = null; + } else { + promotedComponentName = promoted.substring(0, s); + promotedServiceName = promoted.substring(s + 1); + } + + Component promotedComponent = assemblyFactory.createComponent(); + promotedComponent.setUnresolved(true); + promotedComponent.setName(promotedComponentName); + compositeService.setPromotedComponent(promotedComponent); + + ComponentService promotedService = assemblyFactory.createComponentService(); + promotedService.setUnresolved(true); + promotedService.setName(promotedServiceName); + compositeService.setPromotedService(promotedService); } + + //handle extension attributes + this.readExtendedAttributes(reader, name, compositeService, extensionAttributeProcessor); + + composite.getServices().add(compositeService); + policyProcessor.readPolicies(contract, reader); } - compositeReference.setWiredByImpl(getBoolean(reader, WIRED_BY_IMPL)); - - //handle extension attributes - this.readExtendedAttributes(reader, name, compositeReference, extensionAttributeProcessor); - - composite.getReferences().add(compositeReference); - policyProcessor.readPolicies(contract, reader); - } - - } else if (PROPERTY_QNAME.equals(name)) { - if (component != null) { - - // Read a - componentProperty = assemblyFactory.createComponentProperty(); - property = componentProperty; - String source = getString(reader, SOURCE); - if(source!=null) { - source = source.trim(); + + } else if (REFERENCE_QNAME.equals(name)) { + if (component != null) { + // Read a + componentReference = assemblyFactory.createComponentReference(); + contract = componentReference; + componentReference.setName(getString(reader, NAME)); + readMultiplicity(componentReference, reader); + if (isSet(reader, AUTOWIRE)) { + componentReference.setAutowire(getBoolean(reader, AUTOWIRE)); + } + readTargets(componentReference, reader); + componentReference.setWiredByImpl(getBoolean(reader, WIRED_BY_IMPL)); + + //handle extension attributes + this.readExtendedAttributes(reader, name, componentReference, extensionAttributeProcessor); + + component.getReferences().add(componentReference); + policyProcessor.readPolicies(contract, reader); + } else { + // Read a + compositeReference = assemblyFactory.createCompositeReference(); + contract = compositeReference; + compositeReference.setName(getString(reader, NAME)); + readMultiplicity(compositeReference, reader); + readTargets(compositeReference, reader); + String promote = reader.getAttributeValue(null, Constants.PROMOTE); + if (promote != null) { + for (StringTokenizer tokens = new StringTokenizer(promote); tokens.hasMoreTokens();) { + ComponentReference promotedReference = + assemblyFactory.createComponentReference(); + promotedReference.setUnresolved(true); + promotedReference.setName(tokens.nextToken()); + compositeReference.getPromotedReferences().add(promotedReference); + } + } + compositeReference.setWiredByImpl(getBoolean(reader, WIRED_BY_IMPL)); + + //handle extension attributes + this.readExtendedAttributes(reader, name, compositeReference, extensionAttributeProcessor); + + composite.getReferences().add(compositeReference); + policyProcessor.readPolicies(contract, reader); } - componentProperty.setSource(source); - if (source != null) { - // $/... - if (source.charAt(0) == '$') { - int index = source.indexOf('/'); - if (index == -1) { - // Tolerating $prop - source = source + "/"; - index = source.length() - 1; + + } else if (PROPERTY_QNAME.equals(name)) { + if (component != null) { + + // Read a + componentProperty = assemblyFactory.createComponentProperty(); + property = componentProperty; + String source = getString(reader, SOURCE); + if(source!=null) { + source = source.trim(); + } + componentProperty.setSource(source); + if (source != null) { + // $/... + if (source.charAt(0) == '$') { + int index = source.indexOf('/'); + if (index == -1) { + // Tolerating $prop + source = source + "/"; + index = source.length() - 1; + } + source = source.substring(index + 1); + if ("".equals(source)) { + source = "."; + } } - source = source.substring(index + 1); - if ("".equals(source)) { - source = "."; + XPath xpath = xPathFactory.newXPath(); + xpath.setNamespaceContext(reader.getNamespaceContext()); + try { + componentProperty.setSourceXPathExpression(xpath.compile(source)); + } catch (XPathExpressionException e) { + ContributionReadException ce = new ContributionReadException(e); + error("ContributionReadException", xpath, ce); + //throw ce; } } - XPath xpath = xPathFactory.newXPath(); - xpath.setNamespaceContext(reader.getNamespaceContext()); - try { - componentProperty.setSourceXPathExpression(xpath.compile(source)); - } catch (XPathExpressionException e) { - ContributionReadException ce = new ContributionReadException(e); - error("ContributionReadException", xpath, ce); - //throw ce; - } + componentProperty.setFile(getString(reader, FILE)); + + //handle extension attributes + this.readExtendedAttributes(reader, name, componentProperty, extensionAttributeProcessor); + + policyProcessor.readPolicies(property, reader); + readAbstractProperty(componentProperty, reader); + + // Read the property value + Document value = readPropertyValue(property.getXSDElement(), property.getXSDType(), reader); + property.setValue(value); + + component.getProperties().add(componentProperty); + } else { + + // Read a + property = assemblyFactory.createProperty(); + policyProcessor.readPolicies(property, reader); + readAbstractProperty(property, reader); + + // Read the property value + Document value = readPropertyValue(property.getXSDElement(), property.getXSDType(), reader); + property.setValue(value); + + composite.getProperties().add(property); + } + + // TUSCANY-1949 + // If the property doesn't have a value, the END_ELEMENT event is read by the readPropertyValue + if (reader.getEventType() == END_ELEMENT && PROPERTY_QNAME.equals(reader.getName())) { + property = null; + componentProperty = null; + } + + } else if (COMPONENT_QNAME.equals(name)) { + + // Read a + component = assemblyFactory.createComponent(); + component.setName(getString(reader, NAME)); + if (isSet(reader, AUTOWIRE)) { + component.setAutowire(getBoolean(reader, AUTOWIRE)); + } + if (isSet(reader, URI)) { + component.setURI(getString(reader, URI)); } - componentProperty.setFile(getString(reader, FILE)); //handle extension attributes - this.readExtendedAttributes(reader, name, componentProperty, extensionAttributeProcessor); - - policyProcessor.readPolicies(property, reader); - readAbstractProperty(componentProperty, reader); + this.readExtendedAttributes(reader, name, component, extensionAttributeProcessor); - // Read the property value - Document value = readPropertyValue(property.getXSDElement(), property.getXSDType(), reader); - property.setValue(value); + component.setConstrainingType(readConstrainingType(reader)); + composite.getComponents().add(component); + policyProcessor.readPolicies(component, reader); + + } else if (WIRE_QNAME.equals(name)) { + + // Read a + wire = assemblyFactory.createWire(); + ComponentReference source = assemblyFactory.createComponentReference(); + source.setUnresolved(true); + source.setName(getString(reader, SOURCE)); + wire.setSource(source); + + ComponentService target = assemblyFactory.createComponentService(); + target.setUnresolved(true); + target.setName(getString(reader, TARGET)); + wire.setTarget(target); + + //handle extension attributes + this.readExtendedAttributes(reader, name, wire, extensionAttributeProcessor); + + composite.getWires().add(wire); + policyProcessor.readPolicies(wire, reader); + + } else if (CALLBACK_QNAME.equals(name)) { + + // Read a + callback = assemblyFactory.createCallback(); + contract.setCallback(callback); - component.getProperties().add(componentProperty); - } else { - - // Read a - property = assemblyFactory.createProperty(); - policyProcessor.readPolicies(property, reader); - readAbstractProperty(property, reader); + //handle extension attributes + this.readExtendedAttributes(reader, name, callback, extensionAttributeProcessor); - // Read the property value - Document value = readPropertyValue(property.getXSDElement(), property.getXSDType(), reader); - property.setValue(value); + policyProcessor.readPolicies(callback, reader); + + } else if (OPERATION_QNAME.equals(name)) { + + // Read an + ConfiguredOperation operation = assemblyFactory.createConfiguredOperation(); + operation.setName(getString(reader, NAME)); + operation.setUnresolved(true); + if (callback != null) { + policyProcessor.readPolicies(operation, reader); + } else { + policyProcessor.readPolicies(operation, reader); + } - composite.getProperties().add(property); - } - - // TUSCANY-1949 - // If the property doesn't have a value, the END_ELEMENT event is read by the readPropertyValue - if (reader.getEventType() == END_ELEMENT && PROPERTY_QNAME.equals(reader.getName())) { - property = null; - componentProperty = null; - } - - } else if (COMPONENT_QNAME.equals(name)) { - - // Read a - component = assemblyFactory.createComponent(); - component.setName(getString(reader, NAME)); - if (isSet(reader, AUTOWIRE)) { - component.setAutowire(getBoolean(reader, AUTOWIRE)); - } - if (isSet(reader, URI)) { - component.setURI(getString(reader, URI)); - } - - //handle extension attributes - this.readExtendedAttributes(reader, name, component, extensionAttributeProcessor); - - component.setConstrainingType(readConstrainingType(reader)); - composite.getComponents().add(component); - policyProcessor.readPolicies(component, reader); - - } else if (WIRE_QNAME.equals(name)) { - - // Read a - wire = assemblyFactory.createWire(); - ComponentReference source = assemblyFactory.createComponentReference(); - source.setUnresolved(true); - source.setName(getString(reader, SOURCE)); - wire.setSource(source); - - ComponentService target = assemblyFactory.createComponentService(); - target.setUnresolved(true); - target.setName(getString(reader, TARGET)); - wire.setTarget(target); - - //handle extension attributes - this.readExtendedAttributes(reader, name, wire, extensionAttributeProcessor); - - composite.getWires().add(wire); - policyProcessor.readPolicies(wire, reader); - - } else if (CALLBACK_QNAME.equals(name)) { - - // Read a - callback = assemblyFactory.createCallback(); - contract.setCallback(callback); - - //handle extension attributes - this.readExtendedAttributes(reader, name, callback, extensionAttributeProcessor); - - policyProcessor.readPolicies(callback, reader); - - } else if (OPERATION_QNAME.equals(name)) { - - // Read an - ConfiguredOperation operation = assemblyFactory.createConfiguredOperation(); - operation.setName(getString(reader, NAME)); - operation.setUnresolved(true); - if (callback != null) { - policyProcessor.readPolicies(operation, reader); + OperationsConfigurator opConfigurator = null; + if ( compositeService != null ) { + opConfigurator = compositeService; + } else if ( componentService != null ) { + opConfigurator = componentService; + } else if ( compositeReference != null ) { + opConfigurator = compositeReference; + } else if ( componentReference != null ) { + opConfigurator = componentReference; + } + + opConfigurator.getConfiguredOperations().add(operation); + } else if (IMPLEMENTATION_COMPOSITE_QNAME.equals(name)) { + + // Read an implementation.composite + Composite implementation = assemblyFactory.createComposite(); + implementation.setName(getQName(reader, NAME)); + implementation.setUnresolved(true); + + //handle extension attributes + this.readExtendedAttributes(reader, name, implementation, extensionAttributeProcessor); + + component.setImplementation(implementation); + policyProcessor.readPolicies(implementation, reader); } else { - policyProcessor.readPolicies(operation, reader); - } - - OperationsConfigurator opConfigurator = null; - if ( compositeService != null ) { - opConfigurator = compositeService; - } else if ( componentService != null ) { - opConfigurator = componentService; - } else if ( compositeReference != null ) { - opConfigurator = compositeReference; - } else if ( componentReference != null ) { - opConfigurator = componentReference; - } - - opConfigurator.getConfiguredOperations().add(operation); - } else if (IMPLEMENTATION_COMPOSITE_QNAME.equals(name)) { - - // Read an implementation.composite - Composite implementation = assemblyFactory.createComposite(); - implementation.setName(getQName(reader, NAME)); - implementation.setUnresolved(true); - - //handle extension attributes - this.readExtendedAttributes(reader, name, implementation, extensionAttributeProcessor); - - component.setImplementation(implementation); - policyProcessor.readPolicies(implementation, reader); - } else { - - // Read an extension element - Object extension = extensionProcessor.read(reader); - if (extension != null) { - if (extension instanceof InterfaceContract) { - - // and - // - if (contract != null) { - contract.setInterfaceContract((InterfaceContract)extension); - } else { - if (name.getNamespaceURI().equals(SCA10_NS)) { - error("UnexpectedInterfaceElement", extension); - //throw new ContributionReadException("Unexpected element found. It should appear inside a or element"); + + // Read an extension element + Object extension = extensionProcessor.read(reader); + if (extension != null) { + if (extension instanceof InterfaceContract) { + + // and + // + if (contract != null) { + contract.setInterfaceContract((InterfaceContract)extension); } else { - composite.getExtensions().add(extension); + if (name.getNamespaceURI().equals(SCA10_NS)) { + error("UnexpectedInterfaceElement", extension); + //throw new ContributionReadException("Unexpected element found. It should appear inside a or element"); + } else { + composite.getExtensions().add(extension); + } } - } - } else if (extension instanceof Binding) { - if ( extension instanceof PolicySetAttachPoint ) { - IntentAttachPointType bindingType = intentAttachPointTypeFactory.createBindingType(); - bindingType.setName(name); - bindingType.setUnresolved(true); - ((PolicySetAttachPoint)extension).setType(bindingType); - } - // and - // - if (callback != null) { - callback.getBindings().add((Binding)extension); - } else { - if (contract != null) { - contract.getBindings().add((Binding)extension); + } else if (extension instanceof Binding) { + if ( extension instanceof PolicySetAttachPoint ) { + IntentAttachPointType bindingType = intentAttachPointTypeFactory.createBindingType(); + bindingType.setName(name); + bindingType.setUnresolved(true); + ((PolicySetAttachPoint)extension).setType(bindingType); + } + // and + // + if (callback != null) { + callback.getBindings().add((Binding)extension); + } else { + if (contract != null) { + contract.getBindings().add((Binding)extension); + } else { + if (name.getNamespaceURI().equals(SCA10_NS)) { + error("UnexpectedBindingElement", extension); + //throw new ContributionReadException("Unexpected element found. It should appear inside a or element"); + } else { + composite.getExtensions().add(extension); + } + } + } + + } else if (extension instanceof Implementation) { + if ( extension instanceof PolicySetAttachPoint ) { + IntentAttachPointType implType = intentAttachPointTypeFactory.createImplementationType(); + implType.setName(name); + implType.setUnresolved(true); + ((PolicySetAttachPoint)extension).setType(implType); + } + // + if (component != null) { + component.setImplementation((Implementation)extension); } else { if (name.getNamespaceURI().equals(SCA10_NS)) { - error("UnexpectedBindingElement", extension); - //throw new ContributionReadException("Unexpected element found. It should appear inside a or element"); + error("UnexpectedImplementationElement", extension); + //throw new ContributionReadException("Unexpected element found. It should appear inside a element"); } else { composite.getExtensions().add(extension); } } - } - - } else if (extension instanceof Implementation) { - if ( extension instanceof PolicySetAttachPoint ) { - IntentAttachPointType implType = intentAttachPointTypeFactory.createImplementationType(); - implType.setName(name); - implType.setUnresolved(true); - ((PolicySetAttachPoint)extension).setType(implType); - } - // - if (component != null) { - component.setImplementation((Implementation)extension); } else { - if (name.getNamespaceURI().equals(SCA10_NS)) { - error("UnexpectedImplementationElement", extension); - //throw new ContributionReadException("Unexpected element found. It should appear inside a element"); + + // Add the extension element to the current + // element + if (callback != null) { + callback.getExtensions().add(extension); + } else if (contract != null) { + contract.getExtensions().add(extension); + } else if (property != null) { + property.getExtensions().add(extension); + } else if (component != null) { + component.getExtensions().add(extension); } else { composite.getExtensions().add(extension); } } - } else { - - // Add the extension element to the current - // element - if (callback != null) { - callback.getExtensions().add(extension); - } else if (contract != null) { - contract.getExtensions().add(extension); - } else if (property != null) { - property.getExtensions().add(extension); - } else if (component != null) { - component.getExtensions().add(extension); - } else { - composite.getExtensions().add(extension); - } } } - } - break; - - case XMLStreamConstants.CHARACTERS: - break; - - case END_ELEMENT: - name = reader.getName(); - - // Clear current state when reading reaching end element - if (SERVICE_QNAME.equals(name)) { - componentService = null; - compositeService = null; - contract = null; - } else if (INCLUDE_QNAME.equals(name)) { - include = null; - } else if (REFERENCE_QNAME.equals(name)) { - componentReference = null; - compositeReference = null; - contract = null; - } else if (PROPERTY_QNAME.equals(name)) { - componentProperty = null; - property = null; - } else if (COMPONENT_QNAME.equals(name)) { - component = null; - } else if (WIRE_QNAME.equals(name)) { - wire = null; - } else if (CALLBACK_QNAME.equals(name)) { - callback = null; - } - break; - } - - // Read the next element - if (reader.hasNext()) { - reader.next(); + break; + + case XMLStreamConstants.CHARACTERS: + break; + + case END_ELEMENT: + name = reader.getName(); + + // Clear current state when reading reaching end element + if (SERVICE_QNAME.equals(name)) { + componentService = null; + compositeService = null; + contract = null; + } else if (INCLUDE_QNAME.equals(name)) { + include = null; + } else if (REFERENCE_QNAME.equals(name)) { + componentReference = null; + compositeReference = null; + contract = null; + } else if (PROPERTY_QNAME.equals(name)) { + componentProperty = null; + property = null; + } else if (COMPONENT_QNAME.equals(name)) { + component = null; + } else if (WIRE_QNAME.equals(name)) { + wire = null; + } else if (CALLBACK_QNAME.equals(name)) { + callback = null; + } + break; + } + + // Read the next element + if (reader.hasNext()) { + reader.next(); + } } } + catch (XMLStreamException e) { + ContributionReadException ex = new ContributionReadException(e); + error("XMLStreamException", reader, ex); + } + return composite; } @@ -567,6 +574,7 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt writeConstrainingType(composite), new XAttr(TARGET_NAMESPACE, composite.getName().getNamespaceURI()), new XAttr(NAME, composite.getName().getLocalPart()), + new XAttr(LOCAL, composite.isLocal() ? Boolean.TRUE : null), new XAttr(AUTOWIRE, composite.getAutowire()), policyProcessor.writePolicies(composite)); @@ -670,6 +678,10 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt extensionProcessor.write(component.getImplementation(), writer); } + for (Object extension : component.getExtensions()) { + extensionProcessor.write(extension, writer); + } + // Write elements for (ComponentService service : component.getServices()) { writeStart(writer, SERVICE, new XAttr(NAME, service.getName()), @@ -719,6 +731,7 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt for (ComponentReference reference : component.getReferences()) { writeStart(writer, REFERENCE, new XAttr(NAME, reference.getName()), new XAttr(AUTOWIRE, reference.getAutowire()), + writeMultiplicity(reference), writeTargets(reference), policyProcessor.writePolicies(reference)); @@ -788,7 +801,7 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt writeEnd(writer); } - + writeEnd(writer); } @@ -805,6 +818,7 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt // Write element writeStart(writer, REFERENCE, new XAttr(NAME, reference.getName()), new XAttr(PROMOTE, promote), + writeMultiplicity(reference), policyProcessor.writePolicies(reference)); //write extended attributes @@ -888,9 +902,9 @@ public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArt } for (Object extension : composite.getExtensions()) { - extensionProcessor.write(extension, writer); + extensionProcessor.write(extension, writer); } - + writeEndDocument(writer); } diff --git a/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConstrainingTypeProcessor.java b/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConstrainingTypeProcessor.java index 2bc6086913..1031791c32 100644 --- a/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConstrainingTypeProcessor.java +++ b/java/sca/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConstrainingTypeProcessor.java @@ -42,8 +42,8 @@ import org.apache.tuscany.sca.core.FactoryExtensionPoint; import org.apache.tuscany.sca.interfacedef.InterfaceContract; import org.apache.tuscany.sca.interfacedef.Operation; import org.apache.tuscany.sca.interfacedef.impl.OperationImpl; -import org.apache.tuscany.sca.policy.PolicyFactory; import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.policy.PolicyFactory; import org.w3c.dom.Document; /** @@ -77,7 +77,7 @@ public class ConstrainingTypeProcessor extends BaseAssemblyProcessor implements modelFactories.getFactory(PolicyFactory.class), extensionProcessor, monitor); } - public ConstrainingType read(XMLStreamReader reader) throws ContributionReadException, XMLStreamException { + public ConstrainingType read(XMLStreamReader reader) throws ContributionReadException { ConstrainingType constrainingType = null; AbstractService abstractService = null; AbstractReference abstractReference = null; @@ -85,100 +85,107 @@ public class ConstrainingTypeProcessor extends BaseAssemblyProcessor implements AbstractContract abstractContract = null; QName name = null; - // Read the constrainingType document - while (reader.hasNext()) { - int event = reader.getEventType(); - switch (event) { - - case START_ELEMENT: - name = reader.getName(); - - // Read a - if (Constants.CONSTRAINING_TYPE_QNAME.equals(name)) { - constrainingType = assemblyFactory.createConstrainingType(); - constrainingType.setName(new QName(getString(reader, TARGET_NAMESPACE), getString(reader, NAME))); - policyProcessor.readPolicies(constrainingType, reader); - - } else if (Constants.SERVICE_QNAME.equals(name)) { - - // Read a - abstractService = assemblyFactory.createAbstractService(); - abstractContract = abstractService; - abstractService.setName(getString(reader, Constants.NAME)); - constrainingType.getServices().add(abstractService); - policyProcessor.readPolicies(abstractService, reader); - - } else if (Constants.REFERENCE_QNAME.equals(name)) { - - // Read a - abstractReference = assemblyFactory.createAbstractReference(); - abstractContract = abstractReference; - abstractReference.setName(getString(reader, Constants.NAME)); - readMultiplicity(abstractReference, reader); - constrainingType.getReferences().add(abstractReference); - policyProcessor.readPolicies(abstractReference, reader); - - } else if (Constants.PROPERTY_QNAME.equals(name)) { - - // Read a - abstractProperty = assemblyFactory.createAbstractProperty(); - readAbstractProperty(abstractProperty, reader); - - // Read the property value - Document value = readPropertyValue(abstractProperty.getXSDElement(), abstractProperty.getXSDType(), reader); - abstractProperty.setValue(value); - - constrainingType.getProperties().add(abstractProperty); - policyProcessor.readPolicies(abstractProperty, reader); - - } else if (OPERATION_QNAME.equals(name)) { - - // Read an - Operation operation = new OperationImpl(); - operation.setName(getString(reader, NAME)); - operation.setUnresolved(true); - policyProcessor.readPolicies(abstractContract, operation, reader); + try { + // Read the constrainingType document + while (reader.hasNext()) { + int event = reader.getEventType(); + switch (event) { + + case START_ELEMENT: + name = reader.getName(); - } else { - - // Read an extension element - Object extension = extensionProcessor.read(reader); - if (extension instanceof InterfaceContract) { + // Read a + if (Constants.CONSTRAINING_TYPE_QNAME.equals(name)) { + constrainingType = assemblyFactory.createConstrainingType(); + constrainingType.setName(new QName(getString(reader, TARGET_NAMESPACE), getString(reader, NAME))); + policyProcessor.readPolicies(constrainingType, reader); + + } else if (Constants.SERVICE_QNAME.equals(name)) { + + // Read a + abstractService = assemblyFactory.createAbstractService(); + abstractContract = abstractService; + abstractService.setName(getString(reader, Constants.NAME)); + constrainingType.getServices().add(abstractService); + policyProcessor.readPolicies(abstractService, reader); + + } else if (Constants.REFERENCE_QNAME.equals(name)) { + + // Read a + abstractReference = assemblyFactory.createAbstractReference(); + abstractContract = abstractReference; + abstractReference.setName(getString(reader, Constants.NAME)); + readMultiplicity(abstractReference, reader); + constrainingType.getReferences().add(abstractReference); + policyProcessor.readPolicies(abstractReference, reader); + + } else if (Constants.PROPERTY_QNAME.equals(name)) { + + // Read a + abstractProperty = assemblyFactory.createAbstractProperty(); + readAbstractProperty(abstractProperty, reader); + + // Read the property value + Document value = readPropertyValue(abstractProperty.getXSDElement(), abstractProperty.getXSDType(), reader); + abstractProperty.setValue(value); + + constrainingType.getProperties().add(abstractProperty); + policyProcessor.readPolicies(abstractProperty, reader); + + } else if (OPERATION_QNAME.equals(name)) { + + // Read an + Operation operation = new OperationImpl(); + operation.setName(getString(reader, NAME)); + operation.setUnresolved(true); + policyProcessor.readPolicies(abstractContract, operation, reader); - // and - abstractContract.setInterfaceContract((InterfaceContract)extension); } else { - - // Add the extension element to the current element - if (abstractContract != null) { - abstractContract.getExtensions().add(extension); + + // Read an extension element + Object extension = extensionProcessor.read(reader); + if (extension instanceof InterfaceContract) { + + // and + abstractContract.setInterfaceContract((InterfaceContract)extension); } else { - constrainingType.getExtensions().add(extension); + + // Add the extension element to the current element + if (abstractContract != null) { + abstractContract.getExtensions().add(extension); + } else { + constrainingType.getExtensions().add(extension); + } + } - } - } - break; - - case END_ELEMENT: - name = reader.getName(); - - // Clear current state when reading reaching end element - if (SERVICE_QNAME.equals(name)) { - abstractService = null; - abstractContract = null; - } else if (REFERENCE_QNAME.equals(name)) { - abstractReference = null; - abstractContract = null; - } else if (PROPERTY_QNAME.equals(name)) { - abstractProperty = null; - } - break; - } - if (reader.hasNext()) { - reader.next(); + break; + + case END_ELEMENT: + name = reader.getName(); + + // Clear current state when reading reaching end element + if (SERVICE_QNAME.equals(name)) { + abstractService = null; + abstractContract = null; + } else if (REFERENCE_QNAME.equals(name)) { + abstractReference = null; + abstractContract = null; + } else if (PROPERTY_QNAME.equals(name)) { + abstractProperty = null; + } + break; + } + if (reader.hasNext()) { + reader.next(); + } } } + catch (XMLStreamException e) { + ContributionReadException ex = new ContributionReadException(e); + error("XMLStreamException", reader, ex); + } + return constrainingType; } @@ -207,6 +214,7 @@ public class ConstrainingTypeProcessor extends BaseAssemblyProcessor implements // Write elements for (AbstractReference reference : constrainingType.getReferences()) { writeStart(writer, REFERENCE, new XAttr(NAME, reference.getName()), + writeMultiplicity(reference), policyProcessor.writePolicies(reference)); extensionProcessor.write(reference.getInterfaceContract(), writer); diff --git a/java/sca/modules/assembly-xml/src/main/resources/assembly-xml-validation-messages.properties b/java/sca/modules/assembly-xml/src/main/resources/assembly-xml-validation-messages.properties index 95edf89e30..8cc5e5a5ec 100644 --- a/java/sca/modules/assembly-xml/src/main/resources/assembly-xml-validation-messages.properties +++ b/java/sca/modules/assembly-xml/src/main/resources/assembly-xml-validation-messages.properties @@ -26,4 +26,4 @@ PolicyImplValidationException = PolicyValidation exception when processing imple PolicyServiceValidationException = PolicyValidation exceptions when processing service/reference {0} in {1}. Error is {2} ContributionReadException = ContributionReadException occured due to : {0} ContributionWriteException = ContributionWriteException occured due to : {0} - +XMLStreamException = XMLStreamException occured due to : {0} diff --git a/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/AnyElementReadWriteTestCase.java b/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/AnyElementReadWriteTestCase.java index 9300731007..abf0269c0f 100644 --- a/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/AnyElementReadWriteTestCase.java +++ b/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/AnyElementReadWriteTestCase.java @@ -25,48 +25,71 @@ import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamReader; -import junit.framework.Assert; +import junit.framework.TestCase; import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint; import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.ValidatingXMLInputFactory; import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; import org.apache.tuscany.sca.core.ExtensionPointRegistry; -import org.junit.BeforeClass; import org.junit.Test; -public class AnyElementReadWriteTestCase { +public class AnyElementReadWriteTestCase extends TestCase { - private static XMLInputFactory inputFactory; - //private static String XML = ""; - private static String XML = ""; - private static ExtensibleStAXArtifactProcessor staxProcessor; + private XMLInputFactory inputFactory; + String XML = ""; + private ExtensibleStAXArtifactProcessor staxProcessor; - @BeforeClass - public static void setUp() throws Exception { - ExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); - inputFactory = XMLInputFactory.newInstance(); - StAXArtifactProcessorExtensionPoint staxProcessors = - extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); - staxProcessor = - new ExtensibleStAXArtifactProcessor(staxProcessors, XMLInputFactory.newInstance(), XMLOutputFactory - .newInstance(), null); - } + @Override + public void setUp() throws Exception { + ExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); + ModelFactoryExtensionPoint modelFactories = extensionPoints.getExtensionPoint(ModelFactoryExtensionPoint.class); + inputFactory = modelFactories.getFactory(ValidatingXMLInputFactory.class); + + StAXArtifactProcessorExtensionPoint staxProcessors = extensionPoints + .getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, + inputFactory, XMLOutputFactory.newInstance(), null); + } - @Test - public void testReadWriteComposite() throws Exception { - InputStream is = getClass().getResourceAsStream("Calculator.composite"); - XMLStreamReader reader = inputFactory.createXMLStreamReader(is); - Composite composite = (Composite)staxProcessor.read(reader); - Assert.assertNotNull(composite); + @Override + public void tearDown() throws Exception { + } - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - staxProcessor.write(composite, bos); - System.out.println(bos.toString()); - Assert.assertEquals(XML, bos.toString()); - bos.close(); + /* + @Test + public void testReadWriteComposite() throws Exception { + InputStream is = getClass().getResourceAsStream("Calculator.composite"); + XMLStreamReader reader = inputFactory.createXMLStreamReader(is); + Composite composite = (Composite) staxProcessor.read(reader); + assertNotNull(composite); - is.close(); - } + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + staxProcessor.write(composite, bos); + //System.out.println(bos.toString()); + assertEquals(XML, bos.toString()); + bos.close(); + + is.close(); + } + */ + + @Test + public void testReadWriteUnknownElementComposite() throws Exception { + InputStream is = getClass().getResourceAsStream("UnknownElement.composite"); + XMLStreamReader reader = inputFactory.createXMLStreamReader(is); + Composite composite = (Composite) staxProcessor.read(reader); + assertNotNull(composite); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + staxProcessor.write(composite, bos); + System.out.println(bos.toString()); + //assertEquals(XML, bos.toString()); + bos.close(); + + is.close(); + } } diff --git a/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/MultiplicityReadWriteTestCase.java b/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/MultiplicityReadWriteTestCase.java new file mode 100644 index 0000000000..f34ff1f8bc --- /dev/null +++ b/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/MultiplicityReadWriteTestCase.java @@ -0,0 +1,86 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.assembly.xml; + +import static junit.framework.Assert.assertEquals; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLOutputFactory; + +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.assembly.Multiplicity; +import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.junit.Before; +import org.junit.Test; + +/** + * Test writing SCA XML assemblies. + * + * TUSCANY-2662 + * + * @version $Rev$ $Date$ + */ +public class MultiplicityReadWriteTestCase { + private XMLInputFactory inputFactory; + private XMLOutputFactory outputFactory; + private ExtensibleStAXArtifactProcessor staxProcessor; + + + @Before + public void setUp() throws Exception { + DefaultExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); + inputFactory = XMLInputFactory.newInstance(); + outputFactory = XMLOutputFactory.newInstance(); + StAXArtifactProcessorExtensionPoint staxProcessors = extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, inputFactory, outputFactory, null); + } + + + @Test + public void testReadWriteComposite() throws Exception { + InputStream is = getClass().getResourceAsStream("Multiplicity.composite"); + Composite composite = staxProcessor.read(is, Composite.class); + + verifyComposite(composite); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + staxProcessor.write(composite, bos); + bos.close(); + + ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); + composite = staxProcessor.read(bis, Composite.class); + + verifyComposite(composite); + + } + + + private void verifyComposite(Composite composite) { + assertEquals(composite.getComponents().get(0).getReferences().get(0).getMultiplicity(), Multiplicity.ZERO_N); + assertEquals(composite.getReferences().get(0).getMultiplicity(), Multiplicity.ONE_N); + } + +} diff --git a/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadDocumentTestCase.java b/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadDocumentTestCase.java index 4918271b1f..9409a1bfee 100644 --- a/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadDocumentTestCase.java +++ b/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadDocumentTestCase.java @@ -193,7 +193,7 @@ public class ReadDocumentTestCase { documentProcessor.resolve(composite, resolver); - assertEquals(composite.getConstrainingType(), constrainingType); + assertEquals(composite.getConstrainingType(), constrainingType); assertEquals(composite.getComponents().get(0).getConstrainingType(), constrainingType); } diff --git a/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteAttributeTestCase.java b/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteAttributeTestCase.java index 10012282fc..6105b4a86a 100644 --- a/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteAttributeTestCase.java +++ b/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteAttributeTestCase.java @@ -51,20 +51,33 @@ public class ReadWriteAttributeTestCase { private static final QName ATTRIBUTE = new QName("http://test", "customAttribute"); + // implementation.java for CalculatorServiceComponent appears in a strange place as the + // java implementation extension is not loaded and hence they are loaded as any elements private static final String XML = ""+ - ""+ - ""+ - ""+ - ""+ - ""+ - ""+ - ""+ - ""+ - ""+ - ""+ - ""+ - ""+ - ""; + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""; @BeforeClass public static void setUp() throws Exception { @@ -75,7 +88,6 @@ public class ReadWriteAttributeTestCase { StAXAttributeProcessorExtensionPoint staxAttributeProcessors = extensionPoints.getExtensionPoint(StAXAttributeProcessorExtensionPoint.class); staxAttributeProcessors.addArtifactProcessor(new TestAttributeProcessor()); - staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, XMLInputFactory.newInstance(), XMLOutputFactory.newInstance(), null); } diff --git a/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteLocalCompositeTestCase.java b/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteLocalCompositeTestCase.java new file mode 100644 index 0000000000..312b5c8fab --- /dev/null +++ b/java/sca/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteLocalCompositeTestCase.java @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.assembly.xml; + +import java.io.ByteArrayOutputStream; +import java.io.InputStream; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamReader; + +import junit.framework.TestCase; + +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.StAXAttributeProcessorExtensionPoint; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; + +/** + * Test reading SCA XML assemblies. + * + * @version $Rev: 711584 $ $Date: 2008-11-05 15:07:03 +0000 (Wed, 05 Nov 2008) $ + */ +public class ReadWriteLocalCompositeTestCase extends TestCase { + + private XMLInputFactory inputFactory; + private ExtensibleStAXArtifactProcessor staxProcessor; + + private static final String LOCAL_COMPOSITE_XML = ""+ + ""+ + ""; + + @Override + public void setUp() throws Exception { + ExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); + inputFactory = XMLInputFactory.newInstance(); + StAXArtifactProcessorExtensionPoint staxProcessors = extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + + StAXAttributeProcessorExtensionPoint staxAttributeProcessors = extensionPoints.getExtensionPoint(StAXAttributeProcessorExtensionPoint.class); + staxAttributeProcessors.addArtifactProcessor(new TestAttributeProcessor()); + + staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, XMLInputFactory.newInstance(), XMLOutputFactory.newInstance(), null); + } + + @Override + public void tearDown() throws Exception { + + } + + public void testReadComposite() throws Exception { + InputStream is = getClass().getResourceAsStream("local.composite"); + XMLStreamReader reader = inputFactory.createXMLStreamReader(is); + Composite composite = (Composite) staxProcessor.read(reader); + assertNotNull(composite); + assertTrue(composite.isLocal()); + is.close(); + } + + public void testWriteComposite() throws Exception { + InputStream is = getClass().getResourceAsStream("local.composite"); + XMLStreamReader reader = inputFactory.createXMLStreamReader(is); + Composite composite = (Composite) staxProcessor.read(reader); + assertNotNull(composite); + assertTrue(composite.isLocal()); + is.close(); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + staxProcessor.write(composite, bos); + System.out.println(bos.toString()); + + assertEquals(LOCAL_COMPOSITE_XML, bos.toString()); + } +} diff --git a/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/Calculator.composite b/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/Calculator.composite index 85f6a39243..602d42a06a 100644 --- a/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/Calculator.composite +++ b/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/Calculator.composite @@ -22,13 +22,14 @@ xmlns:calc="http://calc" targetNamespace="http://calc" name="Calculator"> - - + + + - + @@ -51,9 +52,8 @@ - - + + - - + diff --git a/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/CalculatorComponent.constrainingType b/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/CalculatorComponent.constrainingType index 072fe8fde1..d6ec9b0ff0 100644 --- a/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/CalculatorComponent.constrainingType +++ b/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/CalculatorComponent.constrainingType @@ -26,9 +26,9 @@ - + - \ No newline at end of file + diff --git a/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/CalculatorImpl.componentType b/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/CalculatorImpl.componentType index d67ba3ec2b..299eb8c197 100644 --- a/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/CalculatorImpl.componentType +++ b/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/CalculatorImpl.componentType @@ -20,12 +20,12 @@ - + - + - \ No newline at end of file + diff --git a/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/Multiplicity.composite b/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/Multiplicity.composite new file mode 100644 index 0000000000..a221c23eec --- /dev/null +++ b/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/Multiplicity.composite @@ -0,0 +1,30 @@ + + + + + + + + + + + + + diff --git a/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/UnknownElement.composite b/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/UnknownElement.composite new file mode 100644 index 0000000000..f8c0c5ea36 --- /dev/null +++ b/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/UnknownElement.composite @@ -0,0 +1,42 @@ + + + + Test unknown + + + + + + + + + +And I got this: + + + + + + + + + + diff --git a/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/local.composite b/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/local.composite new file mode 100644 index 0000000000..3bda9ac31c --- /dev/null +++ b/java/sca/modules/assembly-xml/src/test/resources/org/apache/tuscany/sca/assembly/xml/local.composite @@ -0,0 +1,27 @@ + + + + + -- cgit v1.2.3