diff options
author | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2010-11-30 18:46:45 +0000 |
---|---|---|
committer | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2010-11-30 18:46:45 +0000 |
commit | ae9b105c6718dcb91b415e257acc8b8c8bf0c6c7 (patch) | |
tree | 0d02a931e37abbfd87d4a69b534dd4b7715b6de4 /sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org | |
parent | 66ac4e82a503af71fb664f0b2c3d5099e4337066 (diff) |
Create branch for 2.0 beta2 release
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1040693 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org')
10 files changed, 1249 insertions, 0 deletions
diff --git a/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/java/impl/ContributionClassLoaderTestCase.java b/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/java/impl/ContributionClassLoaderTestCase.java new file mode 100644 index 0000000000..8409dd9491 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/java/impl/ContributionClassLoaderTestCase.java @@ -0,0 +1,262 @@ +/* + * 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.contribution.java.impl; + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.ArrayList; + +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.ContributionFactory; +import org.apache.tuscany.sca.contribution.java.JavaExport; +import org.apache.tuscany.sca.contribution.java.JavaImport; +import org.apache.tuscany.sca.contribution.java.JavaImportExportFactory; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + + +/** + * Test ContributionClassLoader. + * + */ +public class ContributionClassLoaderTestCase { + + private static ContributionFactory contributionFactory; + private static JavaImportExportFactory javaImportExportFactory; + + @BeforeClass + public static void setUp() throws Exception { + ExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); + FactoryExtensionPoint modelFactories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class); + contributionFactory = modelFactories.getFactory(ContributionFactory.class); + javaImportExportFactory = modelFactories.getFactory(JavaImportExportFactory.class); + } + + private Contribution createContribution(String fileName) throws MalformedURLException { + Contribution contrib = contributionFactory.createContribution(); + File contribDir = new File(fileName); + contrib.setLocation(contribDir.toURI().toURL().toString()); + ClassLoader contextClassLoader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() { + public ClassLoader run() { + return Thread.currentThread().getContextClassLoader(); + } + }); + contrib.setClassLoader(new ContributionClassLoader(contrib, contextClassLoader)); + return contrib; + } + + + @Test + public void testClassLoadingFromContribution() throws ClassNotFoundException, MalformedURLException { + + Contribution contribA = createContribution("target/test-classes"); + Contribution contribB = createContribution("target"); + Contribution contribC = createContribution("target/test-classes/deployables/sample-calculator.jar"); + + // Class present in contribution, also in parent. Class is loaded from parent + Class<?> testClassA = contribA.getClassLoader().loadClass(this.getClass().getName()); + Assert.assertNotNull(testClassA); + Assert.assertSame(this.getClass(), testClassA); + + // Class not present in contribution, but present in parent ClassLoader + Class<?> testClassB = contribB.getClassLoader().loadClass(this.getClass().getName()); + Assert.assertNotNull(testClassB); + Assert.assertSame(this.getClass(), testClassB); + + // Class present in contribution, but not in parent + Class<?> testClassC = contribC.getClassLoader().loadClass("calculator.AddService"); + Assert.assertNotNull(testClassC); + + // Class not present in contribution or in parent + try { + contribA.getClassLoader().loadClass("NonExistent"); + + Assert.assertTrue("ClassNotFoundException not thrown as expected", false); + + } catch (ClassNotFoundException e) { + } + + + + } + + @Test + public void testResourceLoadingFromContribution() throws ClassNotFoundException, MalformedURLException { + + Contribution contribA = createContribution("target/test-classes"); + Contribution contribB = createContribution("target"); + Contribution contribC = createContribution("target/test-classes/deployables/sample-calculator.jar"); + + // Resource present in contribution, and in parent + URL resA = contribA.getClassLoader().getResource("deployables/sample-calculator.jar"); + Assert.assertNotNull(resA); + + // Resource not present in contribution, but present in parent ClassLoader + URL resB = contribB.getClassLoader().getResource("deployables/sample-calculator.jar"); + Assert.assertNotNull(resB); + + // Resource present in contribution, but not in parent + URL resC = contribC.getClassLoader().getResource("calculator/AddService.class"); + Assert.assertNotNull(resC); + + // Load Java class as resource from parent + String classResName = this.getClass().getName().replaceAll("\\.", "/") + ".class"; + URL classResA = contribA.getClassLoader().getResource(classResName); + Assert.assertNotNull(classResA); + + // Non-existent resource + URL res = contribA.getClassLoader().getResource("deployables/NonExistent"); + Assert.assertNull(res); + + } + + private static String getPackageName(Class<?> cls) { + String name = cls.getName(); + int index = name.lastIndexOf('.'); + return index == -1 ? "" : name.substring(0, index); + } + + @Test + public void testClassLoadingFromImportedContribution() throws ClassNotFoundException, MalformedURLException { + + Contribution contribA = createContribution("target/test-classes"); + Contribution contribB = createContribution("target"); + Contribution contribC = createContribution("target/test-classes/deployables/sample-calculator.jar"); + ArrayList<Contribution> exportContribList = new ArrayList<Contribution>(); + exportContribList.add(contribA); + exportContribList.add(contribC); + + JavaImport import_ = javaImportExportFactory.createJavaImport(); + import_.setPackage(getPackageName(getClass())); + import_.setModelResolver(new JavaImportModelResolver(exportContribList, null)); + contribB.getImports().add(import_); + import_ = javaImportExportFactory.createJavaImport(); + import_.setPackage("calculator"); + import_.setModelResolver(new JavaImportModelResolver(exportContribList, null)); + contribB.getImports().add(import_); + + JavaExport export = javaImportExportFactory.createJavaExport(); + export.setPackage(getPackageName(getClass())); + contribA.getExports().add(export); + export = javaImportExportFactory.createJavaExport(); + export.setPackage("calculator"); + contribC.getExports().add(export); + + // Load class from parent, class is also present in imported contribution. Class should + // be loaded from parent + Class<?> testClassB = contribB.getClassLoader().loadClass(this.getClass().getName()); + Assert.assertNotNull(testClassB); + Assert.assertSame(this.getClass(), testClassB); + + // Load class from parent, class is also present in parent. Class should be loaded + // from parent. + Class<?> testClassA = contribA.getClassLoader().loadClass(this.getClass().getName()); + Assert.assertNotNull(testClassA); + Assert.assertSame(this.getClass(), testClassA); + + // Imported class should be the same as the one loaded by the exporting contribution + Assert.assertSame(testClassA, testClassB); + + // Load class from imported contribution, class is not present in parent + Class<?> testClassB1 = contribB.getClassLoader().loadClass("calculator.AddService"); + Assert.assertNotNull(testClassB1); + + // Imported class should be the same as the one loaded by the exporting contribution + Class<?> testClassC = contribC.getClassLoader().loadClass("calculator.AddService"); + Assert.assertNotNull(testClassC); + Assert.assertSame(testClassC, testClassB1); + + + // Try to load class from package which is not explicitly imported - should throw ClassNotFoundException + try { + contribA.getClassLoader().loadClass("calculator.AddService"); + + Assert.assertTrue("ClassNotFoundException not thrown as expected", false); + + } catch (ClassNotFoundException e) { + } + + // Try to load non-existent class from imported package - should throw ClassNotFoundException + try { + contribB.getClassLoader().loadClass(getPackageName(getClass()) + ".NonExistentClass"); + + Assert.assertTrue("ClassNotFoundException not thrown as expected", false); + + } catch (ClassNotFoundException e) { + } + + } + + @Test + public void testResourceLoadingFromImportedContribution() throws ClassNotFoundException, MalformedURLException { + + Contribution contribA = createContribution("target/test-classes"); + Contribution contribB = createContribution("target"); + Contribution contribC = createContribution("target/test-classes/deployables/sample-calculator.jar"); + + ArrayList<Contribution> exportContribList = new ArrayList<Contribution>(); + exportContribList.add(contribA); + exportContribList.add(contribC); + + JavaImport import_ = javaImportExportFactory.createJavaImport(); + import_.setPackage(getPackageName(getClass())); + import_.setModelResolver(new JavaImportModelResolver(exportContribList, null)); + contribB.getImports().add(import_); + JavaImport import1_ = javaImportExportFactory.createJavaImport(); + import1_.setPackage("calculator"); + import1_.setModelResolver(new JavaImportModelResolver(exportContribList, null)); + contribB.getImports().add(import1_); + + JavaExport export = javaImportExportFactory.createJavaExport(); + export.setPackage(getPackageName(getClass())); + contribA.getExports().add(export); + JavaExport export1 = javaImportExportFactory.createJavaExport(); + export1.setPackage("calculator"); + contribC.getExports().add(export1); + + + // Load resource from parent + URL resB = contribB.getClassLoader().getResource("deployables/sample-calculator.jar"); + Assert.assertNotNull(resB); + + // Load Java class as resource from imported contribution with JavaImport + String classResName = this.getClass().getName().replaceAll("\\.", "/") + ".class"; + URL classResB = contribB.getClassLoader().getResource(classResName); + Assert.assertNotNull(classResB); + + // Load Java class as resource from imported contribution with JavaImport + URL classResB1 = contribB.getClassLoader().getResource("calculator/AddService.class"); + Assert.assertNotNull(classResB1); + + // Try to load resource not explicitly imported by contribution + URL classResA1 = contribA.getClassLoader().getResource("calculator/AddService.class"); + Assert.assertNull(classResA1); + + + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/java/impl/JavaExportProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/java/impl/JavaExportProcessorTestCase.java new file mode 100644 index 0000000000..25c4b91eaa --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/java/impl/JavaExportProcessorTestCase.java @@ -0,0 +1,103 @@ +/* + * 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.contribution.java.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.StringReader; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sca.contribution.java.JavaExport; +import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.Problem; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Test JavaExportProcessorTestCase + * + * @version $Rev$ $Date$ + */ +public class JavaExportProcessorTestCase { + + private static final String VALID_XML = + "<?xml version=\"1.0\" encoding=\"ASCII\"?>" + + "<export.java xmlns=\"http://docs.oasis-open.org/ns/opencsa/sca/200912\" xmlns:ns=\"http://ns\" package=\"org.apache.tuscany.sca.contribution.java\"/>"; + + private static final String INVALID_XML = + "<?xml version=\"1.0\" encoding=\"ASCII\"?>" + + "<export.java xmlns=\"http://docs.oasis-open.org/ns/opencsa/sca/200912\"/>"; + + private static XMLInputFactory inputFactory; + private static StAXArtifactProcessor<Object> staxProcessor; + private static Monitor monitor; + private static ProcessorContext context; + + + @BeforeClass + public static void setUp() throws Exception { + ExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); + context = new ProcessorContext(extensionPoints); + monitor = context.getMonitor(); + inputFactory = XMLInputFactory.newInstance(); + StAXArtifactProcessorExtensionPoint staxProcessors = extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, inputFactory, null); + } + + /** + * Test loading a valid export element from a contribution metadata stream + * @throws Exception + */ + @Test + public void testLoad() throws Exception { + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(VALID_XML)); + JavaExport javaExport = (JavaExport)staxProcessor.read(reader, context); + Assert.assertEquals("org.apache.tuscany.sca.contribution.java", javaExport.getPackage()); + } + + /** + * Test loading an INVALID export element from a contribution metadata stream + * @throws Exception + */ + @Test + public void testLoadInvalid() throws Exception { + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(INVALID_XML)); + /*try { + staxProcessor.read(reader); + fail("readerException should have been thrown"); + } catch (ContributionReadException e) { + assertTrue(true); + }*/ + staxProcessor.read(reader, context); + Problem problem = monitor.getLastProblem(); + assertNotNull(problem); + assertEquals("AttributePackageMissing", problem.getMessageId()); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportProcessorTestCase.java new file mode 100644 index 0000000000..e9bfa4b5f3 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/java/impl/JavaImportProcessorTestCase.java @@ -0,0 +1,106 @@ +/* + * 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.contribution.java.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.StringReader; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sca.contribution.java.JavaImport; +import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.Problem; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Test JavaImportProcessorTestCase + * + * @version $Rev$ $Date$ + */ +public class JavaImportProcessorTestCase { + + private static final String VALID_XML = + "<?xml version=\"1.0\" encoding=\"ASCII\"?>" + + "<import.java xmlns=\"http://docs.oasis-open.org/ns/opencsa/sca/200912\" xmlns:ns=\"http://ns\" package=\"org.apache.tuscany.sca.contribution.java\" location=\"sca://contributions/001\"/>"; + + private static final String INVALID_XML = + "<?xml version=\"1.0\" encoding=\"ASCII\"?>" + + "<import.java xmlns=\"http://docs.oasis-open.org/ns/opencsa/sca/200912\" xmlns:ns=\"http://ns\" location=\"sca://contributions/001\"/>"; + + private static XMLInputFactory inputFactory; + private static StAXArtifactProcessor<Object> staxProcessor; + private static Monitor monitor; + + private static ProcessorContext context; + + + @BeforeClass + public static void setUp() throws Exception { + ExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); + context = new ProcessorContext(extensionPoints); + monitor = context.getMonitor(); + inputFactory = XMLInputFactory.newInstance(); + + StAXArtifactProcessorExtensionPoint staxProcessors = extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, inputFactory, null); + } + + /** + * Test loading a valid import element from a contribution metadata stream + * @throws Exception + */ + @Test + public void testLoad() throws Exception { + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(VALID_XML)); + JavaImport javaImport = (JavaImport)staxProcessor.read(reader, context); + + assertEquals("org.apache.tuscany.sca.contribution.java", javaImport.getPackage()); + assertEquals("sca://contributions/001", javaImport.getLocation()); + } + + /** + * Test loading a INVALID import element from a contribution metadata stream + * @throws Exception + */ + @Test + public void testLoadInvalid() throws Exception { + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(INVALID_XML)); + /*try { + staxProcessor.read(reader); + fail("readerException should have been thrown"); + } catch (ContributionReadException e) { + assertTrue(true); + }*/ + staxProcessor.read(reader, context); + Problem problem = monitor.getLastProblem(); + assertNotNull(problem); + assertEquals("AttributePackageMissing", problem.getMessageId()); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/namespace/impl/NamespaceExportProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/namespace/impl/NamespaceExportProcessorTestCase.java new file mode 100644 index 0000000000..384892ce62 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/namespace/impl/NamespaceExportProcessorTestCase.java @@ -0,0 +1,106 @@ +/* + * 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.contribution.namespace.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.StringReader; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sca.contribution.namespace.NamespaceExport; +import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.Problem; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Test NamespaceExportProcessorTestCase + * + * @version $Rev$ $Date$ + */ +public class NamespaceExportProcessorTestCase { + + private static final String VALID_XML = + "<?xml version=\"1.0\" encoding=\"ASCII\"?>" + + "<export xmlns=\"http://docs.oasis-open.org/ns/opencsa/sca/200912\" xmlns:ns=\"http://ns\"" + + " ns:ext=\"extended\" namespace=\"http://foo\">" + + "<ns:foo/></export>"; + + private static final String INVALID_XML = + "<?xml version=\"1.0\" encoding=\"ASCII\"?>" + "<export xmlns=\"http://docs.oasis-open.org/ns/opencsa/sca/200912\" xmlns:ns=\"http://ns\"/>"; + + private static XMLInputFactory inputFactory; + private static StAXArtifactProcessor<Object> staxProcessor; + private static Monitor monitor; + private static ProcessorContext context; + + @BeforeClass + public static void setUp() throws Exception { + ExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); + context = new ProcessorContext(extensionPoints); + monitor = context.getMonitor(); + inputFactory = XMLInputFactory.newInstance(); + + StAXArtifactProcessorExtensionPoint staxProcessors = + extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, inputFactory, null); + } + + /** + * Test loading a valid export element from a contribution metadata stream + * @throws Exception + */ + @Test + public void testLoad() throws Exception { + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(VALID_XML)); + NamespaceExport namespaceExport = (NamespaceExport)staxProcessor.read(reader, context); + assertEquals("http://foo", namespaceExport.getNamespace()); + assertEquals(1, namespaceExport.getAttributeExtensions().size()); + assertEquals(1, namespaceExport.getExtensions().size()); + } + + /** + * Test loading an INVALID export element from a contribution metadata stream + * @throws Exception + */ + @Test + public void testLoadInvalid() throws Exception { + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(INVALID_XML)); + /*try { + staxProcessor.read(reader); + fail("readerException should have been thrown"); + } catch (ContributionReadException e) { + assertTrue(true); + }*/ + staxProcessor.read(reader, context); + Problem problem = monitor.getLastProblem(); + assertNotNull(problem); + assertEquals("AttributeNameSpaceMissing", problem.getMessageId()); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/namespace/impl/NamespaceImportProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/namespace/impl/NamespaceImportProcessorTestCase.java new file mode 100644 index 0000000000..32f95e86ab --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/namespace/impl/NamespaceImportProcessorTestCase.java @@ -0,0 +1,110 @@ +/* + * 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.contribution.namespace.impl; + + + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.StringReader; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sca.contribution.namespace.NamespaceImport; +import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.Problem; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Test NamespaceImportProcessorTestCase + * + * @version $Rev$ $Date$ + */ +public class NamespaceImportProcessorTestCase { + + private static final String VALID_XML = + "<?xml version=\"1.0\" encoding=\"ASCII\"?>" + + "<import xmlns=\"http://docs.oasis-open.org/ns/opencsa/sca/200912\" xmlns:ns=\"http://ns\"" + + " namespace=\"http://foo\" location=\"sca://contributions/001\" ns:ext=\"extended\">" + + "<ns:foo/></import>"; + + private static final String INVALID_XML = + "<?xml version=\"1.0\" encoding=\"ASCII\"?>" + + "<import xmlns=\"http://docs.oasis-open.org/ns/opencsa/sca/200912\" xmlns:ns=\"http://ns\" location=\"sca://contributions/001\"/>"; + + private static XMLInputFactory inputFactory; + private static StAXArtifactProcessor<Object> staxProcessor; + private static Monitor monitor; + private static ProcessorContext context; + + @BeforeClass + public static void setUp() throws Exception { + ExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); + context = new ProcessorContext(extensionPoints); + monitor = context.getMonitor(); + inputFactory = XMLInputFactory.newInstance(); + + StAXArtifactProcessorExtensionPoint staxProcessors = extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, inputFactory, null); + } + + /** + * Test loading a valid import element from a contribution metadata stream + * @throws Exception + */ + @Test + public void testLoad() throws Exception { + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(VALID_XML)); + NamespaceImport namespaceImport = (NamespaceImport)staxProcessor.read(reader, context); + + assertEquals("http://foo", namespaceImport.getNamespace()); + assertEquals("sca://contributions/001", namespaceImport.getLocation()); + assertEquals(1, namespaceImport.getAttributeExtensions().size()); + assertEquals(1, namespaceImport.getExtensions().size()); + } + + /** + * Test loading a INVALID import element from a contribution metadata stream + * @throws Exception + */ + @Test + public void testLoadInvalid() throws Exception { + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(INVALID_XML)); + /*try { + staxProcessor.read(reader); + fail("readerException should have been thrown"); + } catch (ContributionReadException e) { + assertTrue(true); + }*/ + staxProcessor.read(reader, context); + Problem problem = monitor.getLastProblem(); + assertNotNull(problem); + assertEquals("AttributeNameSpaceMissing", problem.getMessageId()); + } +} diff --git a/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/processor/URLartifactProcessorExtensionPointTestCase.java b/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/processor/URLartifactProcessorExtensionPointTestCase.java new file mode 100644 index 0000000000..9587d3c024 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/processor/URLartifactProcessorExtensionPointTestCase.java @@ -0,0 +1,116 @@ +/* + * 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.contribution.processor; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.net.URI; +import java.net.URL; + +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * URL Artifact Processor Extension Point test case + * Verifies the right registration and lookup for processors that handle filename and file types + * + * @version $Rev$ $Date$ + */ +public class URLartifactProcessorExtensionPointTestCase { + + private static URLArtifactProcessorExtensionPoint artifactProcessors; + + @BeforeClass + public static void setUp() throws Exception { + ExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); + artifactProcessors = new DefaultURLArtifactProcessorExtensionPoint(extensionPoints); + artifactProcessors.addArtifactProcessor(new FileTypeArtifactProcessor()); + artifactProcessors.addArtifactProcessor(new FileNameArtifactProcessor()); + } + + @Test + public final void testFileTypeProcessor() { + assertNotNull(artifactProcessors.getProcessor("dir1/file1.m1")); + assertNotNull(artifactProcessors.getProcessor("file1.m1")); + } + + @Test + public final void testFileNameProcessor() { + assertNotNull(artifactProcessors.getProcessor("file.m2")); + assertNotNull(artifactProcessors.getProcessor("dir1/file.m2")); + assertNull(artifactProcessors.getProcessor("onefile.m2")); + } + + /** + * Internal mock classes + * + */ + + private class M1 { + } + + private class M2 { + } + + private static class FileTypeArtifactProcessor implements URLArtifactProcessor<M1> { + public FileTypeArtifactProcessor() { + } + + public M1 read(URL contributionURL, URI uri, URL url, ProcessorContext context) throws ContributionReadException { + return null; + } + + public void resolve(M1 m1, ModelResolver resolver, ProcessorContext context) throws ContributionResolveException { + } + + public String getArtifactType() { + return ".m1"; + } + + public Class<M1> getModelType() { + return M1.class; + } + } + + private static class FileNameArtifactProcessor implements URLArtifactProcessor<M2> { + public FileNameArtifactProcessor() { + } + + public M2 read(URL contributionURL, URI uri, URL url, ProcessorContext context) throws ContributionReadException { + return null; + } + + public void resolve(M2 m2, ModelResolver resolver, ProcessorContext context) throws ContributionResolveException { + } + + public String getArtifactType() { + return "file.m2"; + } + + public Class<M2> getModelType() { + return M2.class; + } + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/resolver/DefaultModelResolverTestCase.java b/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/resolver/DefaultModelResolverTestCase.java new file mode 100644 index 0000000000..58d1616aa2 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/resolver/DefaultModelResolverTestCase.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.contribution.resolver; + +import static org.junit.Assert.assertTrue; + +import org.apache.tuscany.sca.contribution.Artifact; +import org.apache.tuscany.sca.contribution.ContributionFactory; +import org.apache.tuscany.sca.contribution.DefaultContributionFactory; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.junit.Before; +import org.junit.Test; + +/** + * Test the default model resolver implementation. + * + * @version $Rev$ $Date$ + */ +public class DefaultModelResolverTestCase { + + private ModelResolver resolver; + private ContributionFactory factory; + + @Before + public void setUp() throws Exception { + resolver = new DefaultModelResolver(); + factory = new DefaultContributionFactory(); + } + + @Test + public void testResolved() { + ProcessorContext context = new ProcessorContext(); + Model a = new Model("a"); + resolver.addModel(a, context); + Model x = new Model("a"); + x = resolver.resolveModel(Model.class, x, context); + assertTrue(x == a); + } + + @Test + public void testUnresolved() { + Model x = new Model("a"); + Model y = resolver.resolveModel(Model.class, x, new ProcessorContext()); + assertTrue(x == y); + } + + @Test + public void testResolvedArtifact() { + ProcessorContext context = new ProcessorContext(); + Artifact artifact = factory.createArtifact(); + artifact.setURI("foo/bar"); + resolver.addModel(artifact, context); + Artifact x = factory.createArtifact(); + x.setURI("foo/bar"); + x = resolver.resolveModel(Artifact.class, x, context); + assertTrue(x == artifact); + } + + class Model { + private String name; + + Model(String name) { + this.name = name; + } + + @Override + public int hashCode() { + return name.hashCode(); + } + + @Override + public boolean equals(Object obj) { + return name.equals(((Model)obj).name); + } + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/resolver/ExtensibleModelResolverTestCase.java b/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/resolver/ExtensibleModelResolverTestCase.java new file mode 100644 index 0000000000..7692c8ecdf --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/resolver/ExtensibleModelResolverTestCase.java @@ -0,0 +1,137 @@ +/* +* 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.contribution.resolver; + +import static org.junit.Assert.assertTrue; + +import org.apache.tuscany.sca.contribution.Artifact; +import org.apache.tuscany.sca.contribution.ContributionFactory; +import org.apache.tuscany.sca.contribution.DefaultContributionFactory; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.DefaultFactoryExtensionPoint; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.junit.Before; +import org.junit.Test; + +/** + * Test DefaultArtifactResolver. + * + * @version $Rev$ $Date$ + */ +public class ExtensibleModelResolverTestCase { + private ExtensibleModelResolver resolver; + + private ContributionFactory factory; + private ProcessorContext context; + + @Before + public void setUp() throws Exception { + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); + ModelResolverExtensionPoint resolvers = new DefaultModelResolverExtensionPoint(registry); + resolvers.addResolver(Model.class, TestModelResolver.class); + context = new ProcessorContext(); + FactoryExtensionPoint factories = new DefaultFactoryExtensionPoint(registry); + + resolver = new ExtensibleModelResolver(null, resolvers, factories); + + factory = new DefaultContributionFactory(); + } + + @Test + public void testResolvedDefault() { + OtherModel a = new OtherModel("a"); + resolver.addModel(a, context); + OtherModel x = new OtherModel("a"); + x = resolver.resolveModel(OtherModel.class, x, context); + assertTrue(x == a); + } + + @Test + public void testResolvedRegisteredClass() { + Model a = new Model("a"); + resolver.addModel(a, context); + Model x = new Model("a"); + x = resolver.resolveModel(Model.class, x, context); + assertTrue(x == a); + } + + @Test + public void testUnresolvedDefault() { + OtherModel x = new OtherModel("a"); + OtherModel y = resolver.resolveModel(OtherModel.class, x, context); + assertTrue(x == y); + } + + @Test + public void testUnresolved() { + Model x = new Model("a"); + Model y = resolver.resolveModel(Model.class, x, context); + assertTrue(x == y); + } + + @Test + public void testResolvedArtifact() { + Artifact artifact = factory.createArtifact(); + artifact.setURI("foo/bar"); + resolver.addModel(artifact, context); + Artifact x = factory.createArtifact(); + x.setURI("foo/bar"); + x = resolver.resolveModel(Artifact.class, x, context); + assertTrue(x == artifact); + } + + private class Model { + private String name; + + Model(String name) { + this.name = name; + } + + @Override + public int hashCode() { + return name.hashCode(); + } + + @Override + public boolean equals(Object obj) { + return name.equals(((Model)obj).name); + } + } + + private class OtherModel { + private String name; + + OtherModel(String name) { + this.name = name; + } + + @Override + public int hashCode() { + return name.hashCode(); + } + + @Override + public boolean equals(Object obj) { + return name.equals(((OtherModel)obj).name); + } + } +} diff --git a/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/resolver/TestModelResolver.java b/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/resolver/TestModelResolver.java new file mode 100644 index 0000000000..c04d56cafe --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/resolver/TestModelResolver.java @@ -0,0 +1,59 @@ +/* + * 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.contribution.resolver; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; + +/** + * A test implementation of a model resolver, based on a map. + * + * @version $Rev$ $Date$ + */ +public class TestModelResolver implements ModelResolver { + + private Map<Object, Object> map = new HashMap<Object, Object>(); + + public TestModelResolver(Contribution contribution, FactoryExtensionPoint modelFactories) { + } + + public <T> T resolveModel(Class<T> modelClass, T unresolved, ProcessorContext context) { + Object resolved = map.get(unresolved); + if (resolved != null) { + // Return the resolved object + return modelClass.cast(resolved); + } + // Return the unresolved object + return unresolved; + } + + public void addModel(Object resolved, ProcessorContext context) { + map.put(resolved, resolved); + } + + public Object removeModel(Object resolved, ProcessorContext context) { + return map.remove(resolved); + } + +} diff --git a/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/xml/ContributionMetadataProcessorTestCase.java b/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/xml/ContributionMetadataProcessorTestCase.java new file mode 100644 index 0000000000..1e4dcd8023 --- /dev/null +++ b/sca-java-2.x/branches/2.0-Beta2/modules/contribution/src/test/java/org/apache/tuscany/sca/contribution/xml/ContributionMetadataProcessorTestCase.java @@ -0,0 +1,156 @@ +/* + * 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.contribution.xml; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.StringReader; +import java.io.StringWriter; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.sca.contribution.ContributionMetadata; +import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.Problem; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Test the contribution metadata processor. + * + * @version $Rev$ $Date$ + */ + +public class ContributionMetadataProcessorTestCase { + private static final String SCA11_NS = "http://docs.oasis-open.org/ns/opencsa/sca/200912"; + + private static final String VALID_XML = + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<contribution xmlns=\"http://docs.oasis-open.org/ns/opencsa/sca/200912\"" + + " xmlns:ns=\"http://ns\" ns:foo=\"extended\">" + + "<deployable composite=\"ns:Composite1\"/>" + + "<deployable xmlns:ns2=\"http://ns2\" composite=\"ns2:Composite2\"/>" + + "<ns:bar x=\"1\"/>" + + "</contribution>"; + + private static final String INVALID_XML = + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<contribution xmlns=\"http://docs.oasis-open.org/ns/opencsa/sca/200912\" xmlns:ns=\"http://ns\">" + + "<deployable composite=\"ns:Composite1\"/>" + + "<deployable/>" + + "</contribution>"; + + private static XMLInputFactory inputFactory; + private static XMLOutputFactory outputFactory; + private static StAXArtifactProcessor<Object> staxProcessor; + private static Monitor monitor; + private static ProcessorContext context; + + @BeforeClass + public static void setUp() throws Exception { + ExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); + context = new ProcessorContext(extensionPoints); + monitor = context.getMonitor(); + inputFactory = XMLInputFactory.newInstance(); + outputFactory = XMLOutputFactory.newInstance(); + outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); + StAXArtifactProcessorExtensionPoint staxProcessors = + extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, inputFactory, outputFactory); + } + + @Test + public void testRead() throws Exception { + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(VALID_XML)); + ContributionMetadata contribution = (ContributionMetadata)staxProcessor.read(reader, context); + assertNotNull(contribution); + assertEquals(2, contribution.getDeployables().size()); + assertEquals(1, contribution.getAttributeExtensions().size()); + assertEquals(1, contribution.getExtensions().size()); + reader.close(); + } + + @Test + public void testReadInvalid() throws Exception { + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(INVALID_XML)); + /*try { + staxProcessor.read(reader); + fail("InvalidException should have been thrown"); + } catch (ContributionReadException e) { + assertTrue(true); + }*/ + staxProcessor.read(reader, context); + Problem problem = monitor.getLastProblem(); + assertNotNull(problem); + assertEquals("AttributeCompositeMissing", problem.getMessageId()); + reader.close(); + } + + @Test + public void testReadSpecVersion() throws Exception { + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(VALID_XML)); + ContributionMetadata contribution = (ContributionMetadata)staxProcessor.read(reader, context); + assertNotNull(contribution); + assertEquals(SCA11_NS, contribution.getSpecVersion()); + reader.close(); + } + + @Test + public void testWrite() throws Exception { + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(VALID_XML)); + ContributionMetadata contribution = (ContributionMetadata)staxProcessor.read(reader, context); + + validateContribution(contribution); + + //write the contribution metadata contents + StringWriter stringWriter = new StringWriter(); + XMLStreamWriter writer = outputFactory.createXMLStreamWriter(stringWriter); + staxProcessor.write(contribution, writer, context); + stringWriter.close(); + + reader = inputFactory.createXMLStreamReader(new StringReader(stringWriter.toString())); + contribution = (ContributionMetadata)staxProcessor.read(reader, context); + + validateContribution(contribution); + + reader.close(); + } + + private void validateContribution(ContributionMetadata contribution) { + QName deployable; + + assertNotNull(contribution); + assertEquals(2, contribution.getDeployables().size()); + deployable = new QName("http://ns", "Composite1"); + assertEquals(deployable, contribution.getDeployables().get(0).getName()); + deployable = new QName("http://ns2", "Composite2"); + assertEquals(deployable, contribution.getDeployables().get(1).getName()); + } + +} |