diff options
author | nash <nash@13f79535-47bb-0310-9956-ffa450edef68> | 2011-11-20 21:07:24 +0000 |
---|---|---|
committer | nash <nash@13f79535-47bb-0310-9956-ffa450edef68> | 2011-11-20 21:07:24 +0000 |
commit | f86212d3e102a4f383fc85fc1c77563b84665bb3 (patch) | |
tree | d383a289f04ac8be78f6e019f6934b083919212a | |
parent | 628701e626a0fbf4b345396f1bfbf22aa640ae13 (diff) |
Add test for serializing a service reference to XML
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1204264 13f79535-47bb-0310-9956-ffa450edef68
4 files changed, 91 insertions, 0 deletions
diff --git a/sca-java-1.x/trunk/itest/serialization/src/main/java/org/apache/tuscany/sca/itest/servicereference/SCAManagedClient.java b/sca-java-1.x/trunk/itest/serialization/src/main/java/org/apache/tuscany/sca/itest/servicereference/SCAManagedClient.java index c268f9bda7..67113639b6 100644 --- a/sca-java-1.x/trunk/itest/serialization/src/main/java/org/apache/tuscany/sca/itest/servicereference/SCAManagedClient.java +++ b/sca-java-1.x/trunk/itest/serialization/src/main/java/org/apache/tuscany/sca/itest/servicereference/SCAManagedClient.java @@ -35,6 +35,13 @@ public interface SCAManagedClient { void testSerializeStatelessServiceReference() throws Exception; /** + * Tests Serializing a Stateless ServiceReference to XML + * + * @throws Exception Test failed + */ + void testSerializeStatelessServiceReferenceXML() throws Exception; + + /** * Tests Serializing a Nested Stateless ServiceReference. * * @throws Exception Test failed diff --git a/sca-java-1.x/trunk/itest/serialization/src/main/java/org/apache/tuscany/sca/itest/servicereference/SCAManagedClientImpl.java b/sca-java-1.x/trunk/itest/serialization/src/main/java/org/apache/tuscany/sca/itest/servicereference/SCAManagedClientImpl.java index cf0307eb8e..715e8452c4 100644 --- a/sca-java-1.x/trunk/itest/serialization/src/main/java/org/apache/tuscany/sca/itest/servicereference/SCAManagedClientImpl.java +++ b/sca-java-1.x/trunk/itest/serialization/src/main/java/org/apache/tuscany/sca/itest/servicereference/SCAManagedClientImpl.java @@ -53,6 +53,15 @@ public class SCAManagedClientImpl implements SCAManagedClient, StatelessServiceC public void testSerializeStatelessServiceReference() throws Exception { doTestSerializeStatelessServiceReference(statelessServiceRef); } + + /** + * Tests Serializing a Stateless ServiceReference to XML + * + * @throws Exception Test failed + */ + public void testSerializeStatelessServiceReferenceXML() throws Exception { + doTestSerializeStatelessServiceReferenceXML(statelessServiceRef); + } /** * Tests Serializing a Nested Stateless ServiceReference. @@ -91,6 +100,33 @@ public class SCAManagedClientImpl implements SCAManagedClient, StatelessServiceC } /** + * Tests Serializing a Stateless ServiceReference to XML. + * + * @throws Exception Test failed + */ + private void doTestSerializeStatelessServiceReferenceXML( + ServiceReference<StatelessService> aServiceRef) throws Exception { + Assert.assertNotNull(aServiceRef); + + StatelessService service = aServiceRef.getService(); + service.getCurrentTime(); + + // Serialize the ServiceReference + String serializedSR = ServiceReferenceUtils.serializeServiceReferenceXML(aServiceRef); + Assert.assertNotNull(serializedSR); + + // Deserialize the ServiceReference + ServiceReference<?> deserializedSR = ServiceReferenceUtils.deserializeServiceReferenceXML(serializedSR); + Assert.assertNotNull(deserializedSR); + ServiceReference<StatelessService> regotServiceRef = (ServiceReference<StatelessService>) deserializedSR; + Assert.assertNotNull(regotServiceRef); + + // Use the ServiceReference to access the Service. + StatelessService regotService = regotServiceRef.getService(); + Assert.assertNotNull(regotService); + } + + /** * Simple callback method * * @param msg The call back message diff --git a/sca-java-1.x/trunk/itest/serialization/src/main/java/org/apache/tuscany/sca/itest/servicereference/utils/ServiceReferenceUtils.java b/sca-java-1.x/trunk/itest/serialization/src/main/java/org/apache/tuscany/sca/itest/servicereference/utils/ServiceReferenceUtils.java index 2749891bd4..c46e97d40b 100644 --- a/sca-java-1.x/trunk/itest/serialization/src/main/java/org/apache/tuscany/sca/itest/servicereference/utils/ServiceReferenceUtils.java +++ b/sca-java-1.x/trunk/itest/serialization/src/main/java/org/apache/tuscany/sca/itest/servicereference/utils/ServiceReferenceUtils.java @@ -23,7 +23,12 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import java.io.StringReader; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import org.apache.tuscany.sca.core.context.ServiceReferenceImpl; import org.osoa.sca.CallableReference; import org.osoa.sca.ServiceReference; @@ -33,6 +38,7 @@ import org.osoa.sca.ServiceReference; * @version $Date $Revision$ */ public final class ServiceReferenceUtils { + private static final XMLInputFactory INPUT_FACTORY = XMLInputFactory.newInstance(); /** * Constructor @@ -71,6 +77,18 @@ public final class ServiceReferenceUtils { } /** + * Serializes the specified ServiceReference to an XML String + * + * @param obj The Object to Serialize + * @return The Serialized Object as an XML String + * @throws IOException Failed to Serialize the Object + */ + public static String serializeServiceReferenceXML(ServiceReference<?> sr) throws IOException { + ServiceReferenceImpl<?> sri = (ServiceReferenceImpl<?>) sr; + return sri.toXMLString(); + } + + /** * Deserializes the specified byte[] into a ServiceReference * * @param serializedSR The Serialized ServiceReference to deserialize @@ -124,4 +142,20 @@ public final class ServiceReferenceUtils { } } } + + /** + * Deserializes the specified XML String into a ServiceReference + * + * @param serializedSR The Serialized ServiceReference to deserialize + * @return The deserialized ServiceReference + * @throws IOException Failed to deserialize the ServiceReference + * @throws ClassNotFoundException Failed to deserialize the ServiceReference + */ + public static ServiceReference<?> deserializeServiceReferenceXML(String serializedSR) + throws Exception { + StringReader reader = new StringReader(serializedSR); + XMLStreamReader xmlReader = INPUT_FACTORY.createXMLStreamReader(reader); + ServiceReferenceImpl<?> sri = new ServiceReferenceImpl(xmlReader); + return (ServiceReference<?>) sri; + } } diff --git a/sca-java-1.x/trunk/itest/serialization/src/test/java/org/apache/tuscany/sca/itest/servicereference/SerializeServiceReferenceTestCase.java b/sca-java-1.x/trunk/itest/serialization/src/test/java/org/apache/tuscany/sca/itest/servicereference/SerializeServiceReferenceTestCase.java index 76e77284fb..5bde6900b4 100644 --- a/sca-java-1.x/trunk/itest/serialization/src/test/java/org/apache/tuscany/sca/itest/servicereference/SerializeServiceReferenceTestCase.java +++ b/sca-java-1.x/trunk/itest/serialization/src/test/java/org/apache/tuscany/sca/itest/servicereference/SerializeServiceReferenceTestCase.java @@ -74,6 +74,20 @@ public class SerializeServiceReferenceTestCase { } /** + * Tests Serializing a Stateless ServiceReference to XML as managed + * SCA code + * + * @throws Exception Test failed + */ + @Test + public void testSerializeStatelessServiceReferenceInsideSCAtoXML() throws Exception { + SCAManagedClient client = domain.getService( + SCAManagedClient.class, "SCAManagedClientComponent"); + + client.testSerializeStatelessServiceReferenceXML(); + } + + /** * Tests Serializing a Nested Stateless ServiceReference as managed * SCA code. * |