From f86212d3e102a4f383fc85fc1c77563b84665bb3 Mon Sep 17 00:00:00 2001 From: nash Date: Sun, 20 Nov 2011 21:07:24 +0000 Subject: 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 --- .../itest/servicereference/SCAManagedClient.java | 7 +++++ .../servicereference/SCAManagedClientImpl.java | 36 ++++++++++++++++++++++ .../utils/ServiceReferenceUtils.java | 34 ++++++++++++++++++++ .../SerializeServiceReferenceTestCase.java | 14 +++++++++ 4 files changed, 91 insertions(+) (limited to 'sca-java-1.x/trunk/itest') 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 @@ -34,6 +34,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. * 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. @@ -90,6 +99,33 @@ public class SCAManagedClientImpl implements SCAManagedClient, StatelessServiceC Assert.assertNotNull(regotService); } + /** + * Tests Serializing a Stateless ServiceReference to XML. + * + * @throws Exception Test failed + */ + private void doTestSerializeStatelessServiceReferenceXML( + ServiceReference 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 regotServiceRef = (ServiceReference) deserializedSR; + Assert.assertNotNull(regotServiceRef); + + // Use the ServiceReference to access the Service. + StatelessService regotService = regotServiceRef.getService(); + Assert.assertNotNull(regotService); + } + /** * Simple callback method * 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 @@ -70,6 +76,18 @@ public final class ServiceReferenceUtils { return bos.toByteArray(); } + /** + * 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 * @@ -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 @@ -73,6 +73,20 @@ public class SerializeServiceReferenceTestCase { client.testSerializeStatelessServiceReference(); } + /** + * 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. -- cgit v1.2.3