diff options
Diffstat (limited to 'branches/sca-java-1.x/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteAnyAttributeTestCase.java')
-rw-r--r-- | branches/sca-java-1.x/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteAnyAttributeTestCase.java | 134 |
1 files changed, 74 insertions, 60 deletions
diff --git a/branches/sca-java-1.x/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteAnyAttributeTestCase.java b/branches/sca-java-1.x/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteAnyAttributeTestCase.java index 32a3878db3..826e39baab 100644 --- a/branches/sca-java-1.x/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteAnyAttributeTestCase.java +++ b/branches/sca-java-1.x/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/assembly/xml/ReadWriteAnyAttributeTestCase.java @@ -23,7 +23,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import java.io.ByteArrayOutputStream; -import java.io.InputStream; +import java.io.StringReader; import javax.xml.namespace.QName; import javax.xml.stream.XMLInputFactory; @@ -33,11 +33,11 @@ import javax.xml.stream.XMLStreamReader; import org.apache.tuscany.sca.assembly.Composite; import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.StAXAttributeProcessor; import org.apache.tuscany.sca.contribution.processor.StAXAttributeProcessorExtensionPoint; import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; import org.apache.tuscany.sca.core.ExtensionPointRegistry; -import org.junit.After; -import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; /** @@ -47,77 +47,91 @@ import org.junit.Test; */ public class ReadWriteAnyAttributeTestCase { - private XMLInputFactory inputFactory; - private ExtensibleStAXArtifactProcessor staxProcessor; - 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 = "<?xml version='1.0' encoding='UTF-8'?>"+ - "<composite xmlns=\"http://www.osoa.org/xmlns/sca/1.0\" xmlns:ns1=\"http://www.osoa.org/xmlns/sca/1.0\" targetNamespace=\"http://calc\" name=\"Calculator\">"+ - "<service name=\"CalculatorService\" promote=\"CalculatorServiceComponent\">"+ - "<interface.java interface=\"calculator.CalculatorService\" />"+ - "</service>"+ - "<component name=\"CalculatorServiceComponent\" customAttribute=\"customValue\">"+ - "<implementation.java class=\"calculator.CalculatorServiceImpl\" />"+ - "<reference name=\"addService\" target=\"AddServiceComponent\" />"+ - "<reference name=\"subtractService\" target=\"SubtractServiceComponent\" />"+ - "<reference name=\"multiplyService\" target=\"MultiplyServiceComponent\" />"+ - "<reference name=\"divideService\" target=\"DivideServiceComponent\" />"+ - "</component>"+ - "<component name=\"AddServiceComponent\">"+ + "<composite xmlns=\"http://www.osoa.org/xmlns/sca/1.0\" " + + "xmlns:ns1=\"http://www.osoa.org/xmlns/sca/1.0\" " + + "targetNamespace=\"http://calc\" " + + "name=\"Calculator\">"+ + "<component name=\"AddServiceComponent\" xmlns:test=\"http://test\" test:customAttribute=\"customValue\">"+ "<implementation.java class=\"calculator.AddServiceImpl\" />"+ - "</component>"+ - "<component name=\"SubtractServiceComponent\">"+ - "<implementation.java class=\"calculator.SubtractServiceImpl\" />"+ - "</component>"+ - "<component name=\"MultiplyServiceComponent\">"+ - "<implementation.java class=\"calculator.MultiplyServiceImpl\" />"+ - "</component>"+ - "<component name=\"DivideServiceComponent\">"+ - "<implementation.java class=\"calculator.DivideServiceImpl\" />"+ - "</component>"+ + "</component>"+ "</composite>"; - @Before - 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); - } + private XMLInputFactory inputFactory; + private ExtensibleStAXArtifactProcessor staxProcessor; + - @After - public void tearDown() throws Exception { + /** + * Initialize the test environment + * This takes care to register attribute processors when provided + * + * @param attributeProcessor + * @throws Exception + */ + private void init(StAXAttributeProcessor<?> attributeProcessor) throws Exception { + ExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); + inputFactory = XMLInputFactory.newInstance(); + StAXArtifactProcessorExtensionPoint staxProcessors = extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + + if(attributeProcessor != null) { + StAXAttributeProcessorExtensionPoint staxAttributeProcessors = extensionPoints.getExtensionPoint(StAXAttributeProcessorExtensionPoint.class); + staxAttributeProcessors.addArtifactProcessor(attributeProcessor); + } + staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, XMLInputFactory.newInstance(), XMLOutputFactory.newInstance(), null); } + /** + * Read and Write a composite that has a extended attribute + * and a particular attribute processor + * @throws Exception + */ @Test - public void testReadComposite() throws Exception { - InputStream is = getClass().getResourceAsStream("CalculatorExtended.composite"); - XMLStreamReader reader = inputFactory.createXMLStreamReader(is); - Composite composite = (Composite) staxProcessor.read(reader); - assertNotNull(composite); - is.close(); + @Ignore + public void testReadWriteCompositeWithAttributeProcessor() throws Exception { + init(new TestAttributeProcessor()); + + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(XML)); + Composite composite = (Composite) staxProcessor.read(reader); + assertNotNull(composite); + reader.close(); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + staxProcessor.write(composite, bos); + + // used for debug comparison + System.out.println(XML); + System.out.println(bos.toString()); + + assertEquals(XML, bos.toString()); } - + + /** + * Read and Write a composite that has a extended attribute + * but no particular processor for it + * @throws Exception + */ @Test - public void testWriteComposite() throws Exception { - InputStream is = getClass().getResourceAsStream("CalculatorExtended.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()); + @Ignore + public void testDefaultReadWriteComposite() throws Exception { + init(null); + + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(XML)); + Composite composite = (Composite) staxProcessor.read(reader); + assertNotNull(composite); + reader.close(); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + staxProcessor.write(composite, bos); + + // used for debug comparison + System.out.println(XML); + System.out.println(bos.toString()); + + assertEquals(XML, bos.toString()); } } |