diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2011-10-10 05:01:22 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2011-10-10 05:01:22 +0000 |
commit | 5edad53664c23e540bea5ed336bbfa3948bb1035 (patch) | |
tree | 9d7254c29826912db0c03e111958ae895c28c438 /sca-java-2.x/trunk/modules/binding-jsonrpc-runtime | |
parent | fc7cb9fac15928f69dc89adbadd4ef24c1cc3340 (diff) |
Fix test case and update license
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1180782 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/trunk/modules/binding-jsonrpc-runtime')
7 files changed, 88 insertions, 14 deletions
diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc10Request.java b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc10Request.java index e4ae943685..82eb6ea2f4 100644 --- a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc10Request.java +++ b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc10Request.java @@ -80,7 +80,7 @@ public class JsonRpc10Request { } public boolean isNotification() { - return id == null; + return id == null || id == JSONObject.NULL; } public String getMethod() { diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Request.java b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Request.java index 2ee18bcf5a..e73f049545 100644 --- a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Request.java +++ b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/protocol/JsonRpc20Request.java @@ -101,7 +101,7 @@ public class JsonRpc20Request { } public boolean isNotification() { - return id == null; + return id == null || id == JSONObject.NULL; } public String getMethod() { diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JsonRpcServlet.java b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JsonRpcServlet.java index 9e4ef8168b..a183d2211c 100644 --- a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JsonRpcServlet.java +++ b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/main/java/org/apache/tuscany/sca/binding/jsonrpc/provider/JsonRpcServlet.java @@ -165,11 +165,15 @@ public class JsonRpcServlet extends HttpServlet { if (input.has("jsonrpc")) { JsonRpc20Request jsonReq = new JsonRpc20Request(input); JsonRpc20Result jsonResult = invoke(jsonReq); - jsonResult.write(response.getWriter()); + if (jsonResult != null) { + jsonResult.write(response.getWriter()); + } } else { JsonRpc10Request jsonReq = new JsonRpc10Request(input); JsonRpc10Response jsonResult = invoke(jsonReq); - jsonResult.write(response.getWriter()); + if (jsonResult != null) { + jsonResult.write(response.getWriter()); + } } } } catch (Throwable e) { @@ -178,7 +182,9 @@ public class JsonRpcServlet extends HttpServlet { } private JsonRpc20Result invoke(JsonRpc20Request request) throws Exception { - + if (request.isNotification()) { + return null; + } // invoke the request String method = request.getMethod(); Object[] params = request.getParams(); @@ -251,7 +257,9 @@ public class JsonRpcServlet extends HttpServlet { } private JsonRpc10Response invoke(JsonRpc10Request request) throws Exception { - + if (request.isNotification()) { + return null; + } // invoke the request String method = request.getMethod(); Object[] params = request.getParams(); diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCReferenceTestCase.java b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCReferenceTestCase.java index 3d817354bc..a83adb4a1b 100644 --- a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCReferenceTestCase.java +++ b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCReferenceTestCase.java @@ -31,9 +31,6 @@ import org.junit.Test; import echo.Echo; public class JSONRPCReferenceTestCase { - private static final String SERVICE_PATH = "/EchoService"; - private static final String SERVICE_URL = "http://localhost:8085/SCADomain" + SERVICE_PATH; - private static Node nodeServer; private static Node node; @@ -82,5 +79,29 @@ public class JSONRPCReferenceTestCase { throw e; } } + + @Test + public void testInvokeReference20() throws Exception { + Echo echoComponent = node.getService(Echo.class,"EchoComponentWithReference20"); + String result = echoComponent.echo("ABC"); + Assert.assertEquals("echo: ABC", result); + } + + @Test + public void testInvokeReferenceVoidOperation20() throws Exception { + Echo echoComponent = node.getService(Echo.class,"EchoComponentWithReference20"); + echoComponent.echoVoid(); + } + + @Test(expected = Exception.class) + public void testInvokeReferenceException20() throws Exception { + Echo echoComponent = node.getService(Echo.class, "EchoComponentWithReference20"); + try { + echoComponent.echoBusinessException(); + } catch (Exception e) { + System.err.println(e); + throw e; + } + } } diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCServiceTestCase.java b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCServiceTestCase.java index a0e7af8172..bbf8d4cee7 100644 --- a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCServiceTestCase.java +++ b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/java/org/apache/tuscany/sca/binding/jsonrpc/JSONRPCServiceTestCase.java @@ -28,6 +28,7 @@ import org.apache.tuscany.sca.node.Contribution; import org.apache.tuscany.sca.node.ContributionLocationHelper; import org.apache.tuscany.sca.node.Node; import org.apache.tuscany.sca.node.NodeFactory; +import org.json.JSONArray; import org.json.JSONObject; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -44,9 +45,8 @@ import com.meterware.httpunit.WebResponse; */ public class JSONRPCServiceTestCase { - private static final String SERVICE_PATH = "/EchoService"; - - private static final String SERVICE_URL = "http://localhost:8085/SCADomain" + SERVICE_PATH; + private static String SERVICE_URL; + private static String SERVICE20_URL; private static Node node; @@ -56,6 +56,8 @@ public class JSONRPCServiceTestCase { String contribution = ContributionLocationHelper.getContributionLocation(JSONRPCServiceTestCase.class); node = NodeFactory.newInstance().createNode("JSONRPCBinding.composite", new Contribution("test", contribution)); node.start(); + SERVICE_URL = node.getEndpointAddress("EchoComponent/Echo/Echo"); + SERVICE20_URL = node.getEndpointAddress("EchoComponent/Echo/jsonrpc20"); } catch (Exception e) { e.printStackTrace(); } @@ -79,6 +81,40 @@ public class JSONRPCServiceTestCase { JSONObject jsonResp = new JSONObject(response.getText()); Assert.assertEquals("echo: Hello JSON-RPC", jsonResp.getString("result")); } + + @Test + public void testEchoWithJSONRPC20Binding() throws Exception { + JSONObject jsonRequest = new JSONObject("{ \"jsonrpc\": \"2.0\", \"method\": \"echo\", \"params\": [\"Hello JSON-RPC\"], \"id\": 1}"); + + WebConversation wc = new WebConversation(); + WebRequest request = new PostMethodWebRequest( SERVICE20_URL, new ByteArrayInputStream(jsonRequest.toString().getBytes("UTF-8")),"application/json"); + WebResponse response = wc.getResource(request); + + Assert.assertEquals(200, response.getResponseCode()); + + JSONObject jsonResp = new JSONObject(response.getText()); + Assert.assertEquals("echo: Hello JSON-RPC", jsonResp.getString("result")); + } + + + @Test + public void testEchoWithJSONRPC20BindingBatch() throws Exception { + JSONObject jsonRequest1 = new JSONObject("{ \"jsonrpc\": \"2.0\", \"method\": \"echo\", \"params\": [\"Hello JSON-RPC\"], \"id\": 1}"); + JSONObject jsonRequest2 = new JSONObject("{ \"jsonrpc\": \"2.0\", \"method\": \"echo\", \"params\": [\"Hello JSON-RPC 2.0\"], \"id\": 2}"); + JSONArray batchReq = new JSONArray(); + batchReq.put(jsonRequest1); + batchReq.put(jsonRequest2); + + WebConversation wc = new WebConversation(); + WebRequest request = new PostMethodWebRequest( SERVICE20_URL, new ByteArrayInputStream(batchReq.toString().getBytes("UTF-8")),"application/json"); + WebResponse response = wc.getResource(request); + + Assert.assertEquals(200, response.getResponseCode()); + + JSONArray jsonResp = new JSONArray(response.getText()); + Assert.assertEquals("echo: Hello JSON-RPC", ((JSONObject) jsonResp.get(0)).getString("result")); + Assert.assertEquals("echo: Hello JSON-RPC 2.0", ((JSONObject) jsonResp.get(1)).getString("result")); + } @Test public void testJSONRPCBindingGET() throws Exception { diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/resources/JSONRPCBinding.composite b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/resources/JSONRPCBinding.composite index 9c97574db7..0c588cf137 100644 --- a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/resources/JSONRPCBinding.composite +++ b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/resources/JSONRPCBinding.composite @@ -32,6 +32,7 @@ <implementation.java class="echo.EchoComponentImpl"/>
<service name="Echo">
<tuscany:binding.jsonrpc uri="http://localhost:8085/SCADomain/EchoService"/>
+ <tuscany:binding.jsonrpc version="2.0" name="jsonrpc20" uri="http://localhost:8085/SCADomain/EchoService20"/>
</service>
</component>
diff --git a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/resources/JSONRPCReference.composite b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/resources/JSONRPCReference.composite index 16dcbce45f..5391416510 100644 --- a/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/resources/JSONRPCReference.composite +++ b/sca-java-2.x/trunk/modules/binding-jsonrpc-runtime/src/test/resources/JSONRPCReference.composite @@ -24,9 +24,17 @@ name="JSONRPCReference"> <component name="EchoComponentWithReference"> - <implementation.java class="echo.EchoClientImpl"/> + <implementation.java class="echo.EchoClientImpl" /> <reference name="echoReference"> - <tuscany:binding.jsonrpc uri="http://localhost:8085/SCADomain/EchoService"/> + <tuscany:binding.jsonrpc uri="http://localhost:8085/SCADomain/EchoService" /> + </reference> + </component> + + <component name="EchoComponentWithReference20"> + <implementation.java class="echo.EchoClientImpl" /> + + <reference name="echoReference"> + <tuscany:binding.jsonrpc version="2.0" uri="http://localhost:8085/SCADomain/EchoService20" /> </reference> </component> |